Skip to content

Commit

Permalink
More optimizations, error/bug fixes and clean up.
Browse files Browse the repository at this point in the history
- Fixed addon not loading on some servers. Fixed error spam.
- A lot of minor optimizations and some code clean up
- Reduced networking footprint of lists and GUI skins.
- Better error handling. If the addon won't load, it will tell you why in a graceful way. This fixes error spams.
- The addon loads on servers with version 230628 or later now. This also fixes error spams.
- Some hardening against abuse. All network caches are limited now, so they no longer fill up until out of memory. Caches are cleared on overflow and on map cleanup event.
  • Loading branch information
Grocel committed Jul 25, 2023
1 parent 24fa680 commit 921a58f
Show file tree
Hide file tree
Showing 91 changed files with 3,114 additions and 1,980 deletions.
291 changes: 93 additions & 198 deletions lua/autorun/streamradio_loader.lua
Original file line number Diff line number Diff line change
@@ -1,249 +1,144 @@
-- Loader of the 3D Stream Radio. Made By Grocel.
-- 3D Stream Radio. Made By Grocel.
AddCSLuaFile()

local function getVersion()
local versiondata = file.Read("materials/3dstreamradio/_data/version.vmt", "GAME") or ""
versiondata = string.Explode("[\r\n|\r|\n]", versiondata, true) or {}
local g_addonBrokenError = nil

local Version = string.Trim(tostring(versiondata[1] or ""))
local VersionTime = tonumber(string.Trim(versiondata[2] or "")) or -1

if Version == "" then
Version = "UNKNOWN"
end

return Version, VersionTime
if SERVER then
g_addonBrokenError = "Addon loadup is broken on SERVER! To many addons?"
else
g_addonBrokenError = "Addon loadup is broken on CLIENT! To many addons?"
end

local g_version, g_versionTime = getVersion()
local function initStreamRadioLibGlobal()
_G.StreamRadioLib = _G.StreamRadioLib or {}
local lib = _G.StreamRadioLib

local AddonTitle = ( "3D Stream Radio (ver. " .. g_version .. ")" )
local AddonPrefix = ( AddonTitle .. ":\n" )
table.Empty(lib)

StreamRadioLib = StreamRadioLib or {}
table.Empty(StreamRadioLib)

StreamRadioLib.AddonTitle = AddonTitle
StreamRadioLib.AddonPrefix = AddonPrefix
StreamRadioLib.Loaded = nil
StreamRadioLib.ErrorString = nil

function StreamRadioLib.GetVersion()
return g_version
end
lib.Loaded = nil
lib.Errors = {g_addonBrokenError}

function StreamRadioLib.GetVersionTime()
return g_versionTime
end
-- this is the failback content for tools and menus
lib.Loader_CreateErrorPanel = function(CPanel, message)
if not IsValid(CPanel) then
return
end

local g_loader_ok = true
local lib = _G.StreamRadioLib or {}
if lib.Loaded then
return
end

local g_loaded_cs = {}
local g_loaded_lua = {}
local addonPrefix = tostring(lib.AddonPrefix or "")
addonPrefix = string.Trim(addonPrefix)

local function appendError(err)
local lib = StreamRadioLib or {}
if addonPrefix ~= "" then
local prefixlabel = vgui.Create("DLabel")

err = tostring(err or "")
if err == "" then
return
end
prefixlabel:SetDark(true)
prefixlabel:SetHighlight(false)
prefixlabel:SetText(addonPrefix)
prefixlabel:SizeToContents()

lib.ErrorString = lib.ErrorString or ""
lib.ErrorString = string.Trim(lib.ErrorString .. "\n\n" .. err)
end
CPanel:AddPanel(prefixlabel)
end

local function throwError(err)
local lib = StreamRadioLib or {}
local errors = lib.Errors or {}

err = tostring(err or "")
for i, thiserr in ipairs(errors) do
thiserr = tostring(thiserr or "")
thiserr = string.Trim(thiserr)

if err == "" then
err = "Unknown error"
end
if thiserr == "" then
continue
end

appendError(err)
local errorlabel = vgui.Create("DLabel")

g_loader_ok = false
lib.Loaded = nil
errorlabel:SetDark(false)
errorlabel:SetHighlight(true)
errorlabel:SetText(i .. ". " .. thiserr)
errorlabel:SetTooltip(thiserr)
errorlabel:SizeToContents()

ErrorNoHaltWithStack((lib.AddonPrefix or "") .. err .. "\n")
return false, err
end
CPanel:AddPanel(errorlabel)
end

local function saveCSLuaFile(lua, force)
lua = tostring(lua or "")
lua = string.lower(lua or "")
message = tostring(message or "")
message = string.Trim(message)

if lua == "" then
return false
end
if message ~= "" then
local messagelabel = vgui.Create("DLabel")

if force then
g_loaded_cs[lua] = nil
end
messagelabel:SetDark(false)
messagelabel:SetHighlight(true)
messagelabel:SetText(message)
messagelabel:SetTooltip(message)
messagelabel:SizeToContents()

if g_loaded_cs[lua] ~= nil then
return g_loaded_cs[lua] or false
CPanel:AddPanel(messagelabel)
end
end

g_loaded_cs[lua] = false

local status, err = pcall(function()
if CLIENT then
-- this is the failback error message for radio entity spawn
lib.Loader_ShowSpawnError = function(message)
local lib = _G.StreamRadioLib or {}
if lib.Loaded then
return
end

if not file.Exists(lua, "LUA") then
error("Couldn't AddCSLuaFile file '" .. lua .. "' (File not found)", 0)
end

AddCSLuaFile(lua)
end)

if not status then
throwError(err)
return false
end

g_loaded_cs[lua] = true
return true
end

local function saveInclude(lua, force)
lua = tostring(lua or "")
lua = string.lower(lua or "")

if lua == "" then
return nil
end

if force then
g_loaded_lua[lua] = nil
end

if g_loaded_lua[lua] then
-- Prevent loading twice
return true, g_loaded_lua[lua]
end
local addonPrefix = tostring(lib.AddonPrefix or "")
addonPrefix = string.Trim(addonPrefix)

local status, errOrResult = pcall(function()
if not file.Exists(lua, "LUA") then
error("Couldn't include file '" .. lua .. "' (File not found)", 0)
end
local errors = lib.Errors or {}

return include(lua)
end)
local errorString = table.concat(errors, "\n\n")
errorString = string.Trim(errorString)

if not status then
throwError(errOrResult)
message = tostring(message or "")
message = string.Trim(message)

g_loaded_lua[lua] = nil
local err = string.format("%s\n\n%s\n\n%s", addonPrefix, errorString, message)

return nil
ErrorNoHaltWithStack(err)
end

g_loaded_lua[lua] = errOrResult
return status, errOrResult
end

function StreamRadioLib.SaveCSLuaFile(lua, force)
return saveCSLuaFile(lua, force)
end
do
initStreamRadioLibGlobal()

function StreamRadioLib.LoadSH(lua, force)
if not saveCSLuaFile(lua) then return end
return saveInclude(lua, force)
end
local status, loaded = xpcall(function()
AddCSLuaFile("streamradio_core/load.lua")
return include("streamradio_core/load.lua")
end, ErrorNoHaltWithStack)

function StreamRadioLib.LoadCL(lua, force)
if SERVER then
return saveCSLuaFile(lua)
if not _G.StreamRadioLib then
initStreamRadioLibGlobal()
end

return saveInclude(lua, force)
end

function StreamRadioLib.LoadSV(lua, force)
if CLIENT then return true end
return saveInclude(lua, force)
end

do
local printLoaded = StreamRadioLib.LoadSH("streamradio_core/print.lua")

if not printLoaded or not StreamRadioLib.Print then
throwError(AddonTitle .. "Fatal error: Print and reporting system not loaded!")
return
if not status then
_G.StreamRadioLib.Loaded = nil
end
end

local outdated = false

if CLIENT then
if VERSION < 230714 and VERSION > 5 then
throwError("Your GMod-Client (Version: " .. VERSION .. ") is too old!\nPlease update the GMod-Client!")
outdated = true
end
else
if VERSION < 230714 and VERSION > 5 then
throwError("The GMod-Server (Version: " .. VERSION .. ") is too old!\nPlease update the GMod-Server. Tell an Admin!")
outdated = true
if not loaded then
_G.StreamRadioLib.Loaded = nil
end
end

if not outdated then
local status, loaded = StreamRadioLib.LoadSH("streamradio_core/load.lua")
StreamRadioLib.Loaded = status and loaded and g_loader_ok
end
local errors = _G.StreamRadioLib.Errors or {}

local realmname = "clientside"
if SERVER then
realmname = "serverside"
end
local errorString = tostring(errors[1] or "")
errorString = string.Trim(errorString)

if not StreamRadioLib.Loaded then
local err = StreamRadioLib.ErrorString or ""
if err == "" then
StreamRadioLib.ErrorString = "Unknown error"
if errorString ~= "" then
_G.StreamRadioLib.Loaded = nil
end

local errcol = "[color:255,128,128]"
local err = errcol .. StreamRadioLib.Print.IndentText(StreamRadioLib.ErrorString)
err = string.Replace(err, "\n", "\n" .. errcol)

StreamRadioLib.Print.Wrapped(AddonTitle .. "[color:255,128,128] could not be loaded " .. realmname .. ".", "Error:\n" .. err)
else
StreamRadioLib.Print.Wrapped(AddonTitle .. "[color:100,200,100] is loaded " .. realmname .. ".")
end

if SERVER then
util.AddNetworkString("3DStreamRadio/LoadError")

hook.Add("PlayerInitialSpawn", "3DStreamRadio/LoadError", function(ply)
if not IsValid(ply) then
return
end

if not StreamRadioLib then
return
end

if StreamRadioLib.Loaded then
return
end
collectgarbage( "collect" )

net.Start("3DStreamRadio/LoadError")
net.WriteString(StreamRadioLib.ErrorString or "")
net.Send(ply)
end)
else
net.Receive("3DStreamRadio/LoadError", function()
local err = net.ReadString()
if err == "" then return end
if errorString == g_addonBrokenError then
-- something went horribly wrong, so tell the user about it.

throwError(err)
end)
error(g_addonBrokenError)
return
end
end

collectgarbage( "collect" )
Loading

0 comments on commit 921a58f

Please sign in to comment.