Skip to content

Optimisation pass #1365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions lua/autorun/netstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
net.Stream.MaxWriteStreams = 1024 --The maximum number of write data items to store
net.Stream.MaxReadStreams = 128 --The maximum number of queued read data items to store
net.Stream.MaxChunks = 3200 --Maximum number of pieces the stream can send to the server. 64 MB
net.Stream.MaxSize = net.Stream.SendSize*net.Stream.MaxChunks

Check warning on line 11 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace before the operator
net.Stream.MaxTries = 3 --Maximum times the client may retry downloading the whole data

local WriteStreamQueue = {
Expand All @@ -26,8 +26,8 @@
end
self.curidentifier = identifier % net.Stream.MaxWriteStreams + 1

if next(self.queue)==nil then

Check warning on line 29 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
self.activitytimeout = CurTime()+net.Stream.Timeout

Check warning on line 30 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
timer.Create("netstream_queueclean", 5, 0, function() self:Clean() end)
end
self.queue[identifier] = stream
Expand All @@ -42,8 +42,8 @@
--print("Got request", identifier, chunkidx, stream)
if stream then
if stream:Write(ply, chunkidx) then
self.activitytimeout = CurTime()+net.Stream.Timeout

Check warning on line 45 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
stream.timeout = CurTime()+net.Stream.Timeout

Check warning on line 46 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
end
else
-- Tell them the stream doesn't exist
Expand All @@ -57,19 +57,19 @@
Clean = function(self)
local t = CurTime()
for k, stream in pairs(self.queue) do
if (next(stream.clients)~=nil and t >= stream.timeout) or t >= self.activitytimeout then

Check warning on line 60 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
stream:Remove()
self.queue[k] = nil
end
end
if next(self.queue)==nil then

Check warning on line 65 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
timer.Remove("netstream_queueclean")
end
end,
},
__call = function(t)
return setmetatable({
activitytimeout = CurTime()+net.Stream.Timeout,

Check warning on line 72 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace after ')'
curidentifier = 1,
queue = {}
}, t)
Expand All @@ -87,7 +87,7 @@
ErrorNoHalt("Receiving too many ReadStream requests!")
return
end

for _, v in ipairs(queue) do
if v.identifier == stream.identifier then
ErrorNoHalt("Tried to start a new ReadStream for an already existing stream!")
Expand All @@ -95,7 +95,7 @@
end
end

queue[#queue+1] = stream

Check warning on line 98 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Whitespace style"

Style: Please put some whitespace before the operator
if #queue == 1 then
stream:Request()
end
Expand Down Expand Up @@ -134,7 +134,7 @@
},
__call = function(t)
return setmetatable({
queues = setmetatable({}, {__index = function(t,k) local r={} t[k]=r return r end})

Check warning on line 137 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lua-lint / lint

"Space after comma"

Style: Please add a space after the comma
}, t)
end
}
Expand Down Expand Up @@ -296,7 +296,6 @@
}
setmetatable(ReadingDataItem, ReadingDataItem)


function net.WriteStream(data, callback, dontcompress)
if not isstring(data) then
error("bad argument #1 to 'WriteStream' (string expected, got " .. type(data) .. ")", 2)
Expand All @@ -310,20 +309,18 @@
data = util.Compress(data) or ""
end

if #data == 0 then
if not data[1] then
net.WriteUInt(0, 32)
return
end

if #data > net.Stream.MaxSize then
ErrorNoHalt("net.WriteStream request is too large! ", #data/1048576, "MiB")
elseif #data > net.Stream.MaxSize then
ErrorNoHalt("net.WriteStream request is too large! ", #data / 1048576, "MiB")
net.WriteUInt(0, 32)
return
end

local stream = net.Stream.WriteStreams:Add(WritingDataItem(data, callback, compressed))
if not stream then return end

--print("WriteStream", #stream.chunks, stream.identifier, compressed)
net.WriteUInt(#stream.chunks, 32)
net.WriteUInt(stream.identifier, 32)
Expand All @@ -344,10 +341,11 @@
error("bad argument #1 to 'ReadStream' (Tried to use a NULL entity!)", 2)
end
end

if not isfunction(callback) then
error("bad argument #2 to 'ReadStream' (function expected, got " .. type(callback) .. ")", 2)
end

local numchunks = net.ReadUInt(32)
if numchunks == nil then
return
Expand Down
11 changes: 5 additions & 6 deletions lua/autorun/pac_core_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ if SERVER then
local function add_files(dir)
local files, folders = file.Find(dir .. "*", "LUA")

for key, file_name in pairs(files) do
AddCSLuaFile(dir .. file_name)
for i = 1, #files do
AddCSLuaFile(dir .. files[i])
end

for key, folder_name in pairs(folders) do
add_files(dir .. folder_name .. "/")
for i = 1, #folders do
add_files(dir .. folders[i] .. "/")
end
end

Expand All @@ -27,5 +27,4 @@ if CLIENT then
end

include("pac3/core/client/init.lua")
end

end
8 changes: 4 additions & 4 deletions lua/autorun/pac_editor_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ if SERVER then
local function add_files(dir)
local files, folders = file.Find(dir .. "*", "LUA")

for key, file_name in pairs(files) do
AddCSLuaFile(dir .. file_name)
for i = 1, #files do
AddCSLuaFile(dir .. files[i])
end

for key, folder_name in pairs(folders) do
add_files(dir .. folder_name .. "/")
for i = 1, #folders do
add_files(dir .. folders[i] .. "/")
end
end

Expand Down
8 changes: 4 additions & 4 deletions lua/autorun/pac_extra_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ if SERVER then
local function add_files(dir)
local files, folders = file.Find(dir .. "*", "LUA")

for key, file_name in pairs(files) do
AddCSLuaFile(dir .. file_name)
for i = 1, #files do
AddCSLuaFile(dir .. files[i])
end

for key, folder_name in pairs(folders) do
add_files(dir .. folder_name .. "/")
for i = 1, #folders do
add_files(dir .. folders[i] .. "/")
end
end

Expand Down
69 changes: 48 additions & 21 deletions lua/autorun/pac_restart.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
AddCSLuaFile()

if SERVER then
return
end
if SERVER then return end

local file_Find = file.Find
local file_Exists = file.Exists
local file_Read = file.Read

local sv_allowcslua = GetConVar("sv_allowcslua")
local prefer_local_version = CreateClientConVar("pac_restart_prefer_local_version", "0")

function _G.pac_ReloadParts()
local pacLocal = _G.pac

local _, dirs = file.Find("addons/*", "MOD")
for _, dir in ipairs(dirs) do
if file.Exists("addons/" .. dir .. "/lua/autorun/pac_editor_init.lua", "MOD") then
local _, dirs = file_Find("addons/*", "MOD")

for i = 1, #dirs do
local dir = dirs[i]

if file_Exists("addons/" .. dir .. "/lua/autorun/pac_editor_init.lua", "MOD") then
pacLocal.Message("found PAC3 in garrysmod/addons/" .. dir)

local old_include = _G.include

local function include(path, ...)
local new_path = path
if not file.Exists("addons/" .. dir .. "/lua/" .. path, "MOD") then

if not file_Exists("addons/" .. dir .. "/lua/" .. path, "MOD") then
local src = debug.getinfo(2).source
local lua_dir = src:sub(2):match("(.+/)")

if lua_dir:StartWith("addons/" .. dir) then
lua_dir = lua_dir:match("addons/.-/lua/(.+)")
end

new_path = lua_dir .. path
end

if file.Exists("addons/" .. dir .. "/lua/" .. new_path, "MOD") then
local str = file.Read("addons/" .. dir .. "/lua/" .. new_path, "MOD")
if file_Exists("addons/" .. dir .. "/lua/" .. new_path, "MOD") then
local str = file_Read("addons/" .. dir .. "/lua/" .. new_path, "MOD")
if str then
local func = CompileString(str, "addons/" .. dir .. "/lua/" .. new_path)
if isfunction(func) then
Expand All @@ -53,6 +62,7 @@ function _G.pac_ReloadParts()
pac.LoadParts()
end)
_G.include = old_include

break
end
end
Expand Down Expand Up @@ -100,7 +110,7 @@ function _G.pac_Restart()
pace.Panic()
end

for _, ent in pairs(ents.GetAll()) do
for _, ent in ents.Iterator() do
for k in pairs(ent:GetTable()) do
if k:sub(0, 4) == "pac_" then
ent[k] = nil
Expand Down Expand Up @@ -133,12 +143,15 @@ function _G.pac_Restart()
if not prefer_local_version:GetBool() then
pacLocal.Message("pac_restart: not reloading from local version")

for _, path in ipairs((file.Find("autorun/pac*", "LUA"))) do
local files = file_Find("autorun/pac*", "LUA")

for i = 1, #files do
local path = files[i]

if path:EndsWith("_init.lua") and path ~= "pac_init.lua" then
include("autorun/" .. path)
end
end

elseif sv_allowcslua:GetBool() or LocalPlayer():IsSuperAdmin() then
local loadingHit = false

Expand All @@ -150,25 +163,32 @@ function _G.pac_Restart()
pacLocal.Message("pac_restart: LocalPlayer() is superadmin, looking for PAC3 addon..")
end

local _, dirs = file.Find("addons/*", "MOD")
for _, dir in ipairs(dirs) do
if file.Exists("addons/" .. dir .. "/lua/autorun/pac_editor_init.lua", "MOD") then
local _, dirs = file_Find("addons/*", "MOD")

for i = 1, #dirs do
local dir = dirs[i]

if file_Exists("addons/" .. dir .. "/lua/autorun/pac_editor_init.lua", "MOD") then
pacLocal.Message("found PAC3 in garrysmod/addons/" .. dir)

local old_include = _G.include

local function include(path, ...)
local new_path = path
if not file.Exists("addons/" .. dir .. "/lua/" .. path, "MOD") then

if not file_Exists("addons/" .. dir .. "/lua/" .. path, "MOD") then
local src = debug.getinfo(2).source
local lua_dir = src:sub(2):match("(.+/)")

if lua_dir:StartWith("addons/" .. dir) then
lua_dir = lua_dir:match("addons/.-/lua/(.+)")
end

new_path = lua_dir .. path
end

if file.Exists("addons/" .. dir .. "/lua/" .. new_path, "MOD") then
local str = file.Read("addons/" .. dir .. "/lua/" .. new_path, "MOD")
if file_Exists("addons/" .. dir .. "/lua/" .. new_path, "MOD") then
local str = file_Read("addons/" .. dir .. "/lua/" .. new_path, "MOD")
if str then
local func = CompileString(str, "addons/" .. dir .. "/lua/" .. new_path)
if isfunction(func) then
Expand All @@ -192,7 +212,11 @@ function _G.pac_Restart()

_G.include = include

for _, path in ipairs((file.Find("autorun/pac_*", "LUA"))) do
local init_files = file_Find("autorun/pac_*", "LUA")

for i = 1, #init_files do
local path = init_files[i]

if path:EndsWith("_init.lua") and path ~= "pac_init.lua" then
pacLocal.Message("pac_restart: including autorun/" .. path .. "...")

Expand All @@ -213,11 +237,14 @@ function _G.pac_Restart()
end
end


if not loadingHit then
pacLocal.Message("sv_allowcslua is not enabled or unable to find PAC3 in addons/, loading PAC3 again from server lua")

for _, path in ipairs((file.Find("autorun/pac*", "LUA"))) do
local init_files = file_Find("autorun/pac_*", "LUA")

for i = 1, #init_files do
local path = init_files[i]

if path:EndsWith("_init.lua") and path ~= "pac_init.lua" then
include("autorun/" .. path)
end
Expand Down
19 changes: 12 additions & 7 deletions lua/autorun/pac_version.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ if SERVER then
local function pacVersion()
local addonFound = false

for k,v in pairs(select(2, file.Find( "addons/*", "GAME" ))) do
if file.Exists("addons/"..v.."/lua/autorun/pac_init.lua", "GAME") then
local _, dirs = file.Find("addons/*", "GAME")

for i = 1, #dirs do
local v = dirs[i]

if file.Exists("addons/" .. v .. "/lua/autorun/pac_init.lua", "GAME") then
addonFound = true
local dir = "addons/"..v.."/.git/"
local head = file.Read(dir.."HEAD", "GAME") -- Where head points to

local dir = "addons/" .. v .. "/.git/"
local head = file.Read(dir .. "HEAD", "GAME") -- Where head points to
if not head then break end

head = string.match(head, "ref:%s+(%S+)")
if not head then break end

local lastCommit = string.match(file.Read( dir..head, "GAME") or "", "%S+")
local lastCommit = string.match(file.Read(dir..head, "GAME") or "", "%S+")
if not lastCommit then break end

return "Git: " .. string.GetFileFromFilename(head) .. " (" .. lastCommit .. ")"
Expand All @@ -37,15 +42,15 @@ end

concommand.Add("pac_version", function()
print(PAC_VERSION())

if CLIENT and PAC_VERSION() == "workshop" then
print("Fetching workshop info...")
steamworks.FileInfo( "104691717", function(result)
steamworks.FileInfo("104691717", function(result)
print("Updated: " .. os.date("%x %X", result.updated))
end)
end
end)


--accessed in the editor under pac-help-version
function pac.OpenMOTD(mode)
local pnl = vgui.Create("DFrame")
Expand Down
Loading
Loading