Skip to content

Commit

Permalink
Improved error handling and fixed broken loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Grocel committed Jan 4, 2024
1 parent 11dd2c7 commit 8ffd79a
Show file tree
Hide file tree
Showing 92 changed files with 312 additions and 124 deletions.
4 changes: 2 additions & 2 deletions lua/autorun/streamradio_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ do
initStreamRadioLibGlobal()

local status, loaded = xpcall(function()
AddCSLuaFile("streamradio_core/load.lua")
return include("streamradio_core/load.lua")
AddCSLuaFile("streamradio_core/_load.lua")
return include("streamradio_core/_load.lua")
end, ErrorNoHaltWithStack)

if not _G.StreamRadioLib then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,22 @@
if not StreamRadioLib then return end
local LIB = StreamRadioLib

local LIBLoadSH = LIB.LoadSH
local LIBLoadCL = LIB.LoadCL
local LIBLoadSV = LIB.LoadSV
local loadSH = LIB.LoadSH
local loadCL = LIB.LoadCL
local loadSV = LIB.LoadSV

if not LIBLoadSH then return end
if not LIBLoadCL then return end
if not LIBLoadSV then return end

local g_ok = true

local function loadSH(lua, ...)
local status, loaded = LIBLoadSH(lua, ...)

if not status then
g_ok = false
return false
end

if not loaded then
g_ok = false
return false
end

return true
end

local function loadCL(lua, ...)
local status, loaded = LIBLoadCL(lua, ...)

if not status then
g_ok = false
return false
end

if CLIENT and not loaded then
g_ok = false
return false
end

return true
end

local function loadSV(lua, ...)
local status, loaded = LIBLoadSV(lua, ...)

if not status then
g_ok = false
return false
end

if SERVER and not loaded then
g_ok = false
return false
end

return true
end
if not loadSH then return end
if not loadCL then return end
if not loadSV then return end

LIB.DataDirectory = "streamradio"

do
local status, lib = LIBLoadSH("streamradio_core/external/neturl.lua")
local status, lib = loadSH("streamradio_core/external/neturl.lua")

LIB.NetURL = nil

if not status then
g_ok = false
else
if status then
LIB.NetURL = lib
end
end
Expand Down Expand Up @@ -140,9 +88,5 @@ end
StreamRadioLib.Cfchttp.Load()
StreamRadioLib.Cache.Load()

if not g_ok then
return
end

return true

54 changes: 32 additions & 22 deletions lua/streamradio_core/load.lua → lua/streamradio_core/_load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ local function registerErrorFeedbackHook()
end
end

local g_lua_path = SERVER and "lsv" or "lcl"

local function luaExists(lua)
lua = tostring(lua or "")
lua = string.lower(lua or "")
Expand All @@ -140,22 +138,16 @@ local function luaExists(lua)
end

if g_exists_lua[lua] ~= nil then
-- Cache the results for an even faster performance
return g_exists_lua[lua] or false
end

-- We use a manual implementation, because file.Exists() on "LUA" paths can be VERY slow on some systems.
-- See https://github.com/Facepunch/garrysmod-issues/issues/5674

local f = file.Open(lua, "r", g_lua_path)
local exists = file.Exists(lua, "LUA")

if not f then
if not exists then
g_exists_lua[lua] = false
return false
end

f:Close()

g_exists_lua[lua] = true
return true
end
Expand Down Expand Up @@ -215,11 +207,24 @@ local function saveInclude(lua, force)
end

local status, result = xpcall(function()
if not luaExists(lua) then
error("Couldn't include file '" .. lua .. "' (File not found)")
if SERVER then
-- Too slow on clientside on some servers
-- See: https://github.com/Facepunch/garrysmod-issues/issues/5674

if not luaExists(lua) then
error("Couldn't include file '" .. lua .. "' (File not found)")
return nil
end
end

return include(lua)
local r = include(lua)

if not r then
error("Couldn't include file '" .. lua .. "' (Error during execution or file not found)")
return nil
end

return r
end, throwError)

if not status then
Expand Down Expand Up @@ -253,11 +258,11 @@ function LIB.LoadSV(lua, force)
return saveInclude(lua, force)
end

function LIB.LuaExists(lua)
return luaExists(lua)
end
local g_loadTime = 0

local function loadAddon()
local loadStartTime = SysTime()

local VERSION = VERSION or 0
local versionError = nil

Expand Down Expand Up @@ -285,7 +290,7 @@ local function loadAddon()
LIB.Loaded = true
LIB.Loading = true

local status, loaded = LIB.LoadSH("streamradio_core/init.lua")
local status, loaded = LIB.LoadSH("streamradio_core/_include.lua")

if not status then
g_loader_ok = false
Expand All @@ -301,6 +306,8 @@ local function loadAddon()
end

LIB.Loading = nil

g_loadTime = SysTime() - loadStartTime
end

local g_colDefault = Color(255, 255, 255)
Expand All @@ -322,9 +329,12 @@ local function printAddon()
realmcol = g_colSV
end

local loadTimeString = string.format("Took %0.3f sec.", g_loadTime)
local border = "##########################################################################################"

MsgN()
MsgN()
MsgC(realmcol, "###########################################################################")
MsgC(realmcol, border)
MsgN()
MsgN()

Expand All @@ -335,11 +345,11 @@ local function printAddon()
appendError(string.format("Error loading addon on the %s!", realmname))
end

MsgC(g_colError, "could not be loaded on the " .. realmname .. ".")
MsgC(g_colError, "could not be loaded on the " .. realmname .. ". " .. loadTimeString)
MsgN()
MsgN()

MsgC(realmcol, "###########################################################################")
MsgC(realmcol, border)
MsgN()
MsgN()

Expand All @@ -358,12 +368,12 @@ local function printAddon()
MsgN()
end
else
MsgC(g_colOk, "is loaded on the " .. realmname .. ".")
MsgC(g_colOk, "is loaded on the " .. realmname .. ". " .. loadTimeString)
MsgN()
end

MsgN()
MsgC(realmcol, "###########################################################################")
MsgC(realmcol, border)
MsgN()
MsgN()
end
Expand Down
15 changes: 9 additions & 6 deletions lua/streamradio_core/classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,28 @@ local function AddClass(name, parentname)
name = normalize_classname(name)
parentname = normalize_classname(parentname)

if ( name == "" ) then return false end
if ( parentname == "" ) then
if name == "" then return false end
if parentname == "" then
parentname = "base"
end

if ( StreamRadioLib.Classes[name] ) then return true end
if StreamRadioLib.Classes[name] then return true end

local scriptfile = LuaClassDirectory .. "/" .. name .. ".lua"

if ( not StreamRadioLib.LuaExists( scriptfile ) ) then return false end

if name ~= parentname and not AddClass( parentname ) then
return false
end

local parent = StreamRadioLib.Classes[parentname]
CLASS = CreateClass(name, parent)

StreamRadioLib.LoadSH(scriptfile, true)
local loaded = StreamRadioLib.LoadSH(scriptfile, true)

if not loaded then
CLASS = nil
return false
end

if not CLASS then
CLASS = nil
Expand Down
3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,6 @@ function CLASS:__eg(other)
if not other then return false end
return self:GetID() ~= other:GetID()
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/base_listener.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1079,3 +1079,6 @@ end

function CLASS:PostDupe(data)
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/clientconvar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,6 @@ function CLASS:Remove()
self._convar = nil
BASE.Remove(self)
end

return true

5 changes: 4 additions & 1 deletion lua/streamradio_core/classes/gui_controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -806,4 +806,7 @@ end

function CLASS:IsReady()
return self.isReady or false
end
end

return true

4 changes: 3 additions & 1 deletion lua/streamradio_core/classes/rendertarget.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if SERVER then
CLASS = nil
return
return true
end

local StreamRadioLib = StreamRadioLib
Expand Down Expand Up @@ -515,3 +515,5 @@ function CLASS:__eg(other)
return true
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/skin_controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,6 @@ end
function CLASS:PostDupe(data)
self:SetSkin(data.skin)
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2899,3 +2899,6 @@ end
function CLASS:OnMute(muted)
-- override
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/button.lua
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,6 @@ function CLASS:OnModelSetup(setup)
self:SetFont(setup.font)
end
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,6 @@ end

function CLASS:ActivateNetworkedMode()
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/highlighter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,6 @@ end

function CLASS:ActivateNetworkedMode()
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,6 @@ function CLASS:IsVisible()

return BASE.IsVisible(self)
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/label.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,6 @@ function CLASS:OnModelSetup(setup)
self:SetFont(setup.font)
end
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/label_fade.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,6 @@ function CLASS:Render()

self:DrawText(self.InternalText, x, y, w, h)
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,6 @@ function CLASS:OnModelSetup(setup)
self:SetGridSize(w, h)
end
end

return true

2 changes: 2 additions & 0 deletions lua/streamradio_core/classes/ui/list_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,5 @@ function CLASS:PostDupe(data)
self:SetPath(data.Path)
end

return true

3 changes: 3 additions & 0 deletions lua/streamradio_core/classes/ui/panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1638,3 +1638,6 @@ function CLASS:OnModelSetup(setup)
self:SetPosY(x)
end
end

return true

Loading

0 comments on commit 8ffd79a

Please sign in to comment.