From 217f7b63a29325f321b985d5d9279ce0bd9c9e01 Mon Sep 17 00:00:00 2001 From: EvilFactory Date: Sun, 6 Nov 2022 17:49:17 -0300 Subject: [PATCH] Added Md5Hash and made FIFO queue less cursed Starting to run out of food. --- .../Lua/DefaultLib/LibShared.lua | 1 + .../Lua/DefaultRegister/RegisterShared.lua | 1 + Barotrauma/BarotraumaShared/Lua/ModLoader.lua | 76 +++++++++++++++---- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib/LibShared.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib/LibShared.lua index b06ca1710..0d040fe86 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib/LibShared.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib/LibShared.lua @@ -106,6 +106,7 @@ defaultLib["AutoItemPlacer"] = CreateStatic("Barotrauma.AutoItemPlacer") defaultLib["PropertyConditional"] = CreateStatic("Barotrauma.PropertyConditional", true) defaultLib["StatusEffect"] = CreateStatic("Barotrauma.StatusEffect", true) +defaultLib["Md5Hash"] = CreateStatic("Barotrauma.Md5Hash", true) defaultLib["ContentXElement"] = CreateStatic("Barotrauma.ContentXElement", true) defaultLib["ContentPath"] = CreateStatic("Barotrauma.ContentPath", true) defaultLib["XElement"] = CreateStatic("System.Xml.Linq.XElement", true) diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultRegister/RegisterShared.lua b/Barotrauma/BarotraumaShared/Lua/DefaultRegister/RegisterShared.lua index 7f3c87660..f5bbd2cfd 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultRegister/RegisterShared.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultRegister/RegisterShared.lua @@ -160,6 +160,7 @@ RegisterBarotrauma("ContentXElement") RegisterBarotrauma("ContentPath") RegisterBarotrauma("ContentPackageId") RegisterBarotrauma("SteamWorkshopId") +RegisterBarotrauma("Md5Hash") RegisterBarotrauma("SoundsFile") diff --git a/Barotrauma/BarotraumaShared/Lua/ModLoader.lua b/Barotrauma/BarotraumaShared/Lua/ModLoader.lua index f962c3d57..37016964a 100644 --- a/Barotrauma/BarotraumaShared/Lua/ModLoader.lua +++ b/Barotrauma/BarotraumaShared/Lua/ModLoader.lua @@ -56,25 +56,27 @@ local function AssertTypes(expectedTypes, ...) end local function ExecutionQueue() - local queue = {} - local function processQueueFIFO() - while queue[1] ~= nil do - local folder, rootFolder, package = table.unpack(table.remove(queue, 1)) + local executionQueue = {} + executionQueue.Queue = {} + + executionQueue.Process = function() + while executionQueue.Queue[1] ~= nil do + local folder, rootFolder, package = table.unpack(table.remove(executionQueue.Queue, 1)) print(string.format("%s %s", package.Name, package.ModVersion)) RunFolder(folder, rootFolder, package) end end - local function queueExecutionFIFO(...) + executionQueue.Add = function(...) AssertTypes({ 'string', 'string', 'userdata' }, ...) - table.insert(queue, table.pack(...)) + table.insert(executionQueue.Queue, table.pack(...)) end - return queueExecutionFIFO, processQueueFIFO + return executionQueue end -local QueueAutorun, ProcessAutorun = ExecutionQueue() -local QueueForcedAutorun, ProcessForcedAutorun = ExecutionQueue() +local QueueAutorun = ExecutionQueue() +local QueueForcedAutorun = ExecutionQueue() local function ProcessPackages(packages, fn) for pkg in packages do @@ -93,7 +95,7 @@ ProcessPackages( table.insert(package.path, pkgPath .. LUA_MOD_REQUIRE_PATH) local autorunPath = pkgPath .. LUA_MOD_AUTORUN_PATH if File.DirectoryExists(autorunPath) then - QueueAutorun(autorunPath, pkgPath, pkg) + QueueAutorun.Add(autorunPath, pkgPath, pkg) end end ) @@ -107,7 +109,7 @@ ProcessPackages( table.insert(package.path, pkgPath .. LUA_MOD_REQUIRE_PATH) local forcedAutorunPath = pkgPath .. LUA_MOD_FORCEDAUTORUN_PATH if File.DirectoryExists(forcedAutorunPath) then - QueueForcedAutorun(forcedAutorunPath, pkgPath, pkg) + QueueForcedAutorun.Add(forcedAutorunPath, pkgPath, pkg) executedLocalPackages[pkg.Name] = true end end @@ -120,7 +122,7 @@ ProcessPackages( table.insert(package.path, pkgPath .. LUA_MOD_REQUIRE_PATH) local forcedAutorunPath = pkgPath .. LUA_MOD_FORCEDAUTORUN_PATH if File.DirectoryExists(forcedAutorunPath) then - QueueForcedAutorun(forcedAutorunPath, pkgPath, pkg) + QueueForcedAutorun.Add(forcedAutorunPath, pkgPath, pkg) executedLocalPackages[pkg.Name] = true end end @@ -134,7 +136,7 @@ ProcessPackages( table.insert(package.path, pkgPath .. LUA_MOD_REQUIRE_PATH) local forcedAutorunPath = pkgPath .. LUA_MOD_FORCEDAUTORUN_PATH if File.DirectoryExists(forcedAutorunPath) then - QueueForcedAutorun(forcedAutorunPath, pkgPath, pkg) + QueueForcedAutorun.Add(forcedAutorunPath, pkgPath, pkg) end end end @@ -142,11 +144,53 @@ ProcessPackages( setmodulepaths(package.path) setmodulepaths = nil -ProcessAutorun() -ProcessForcedAutorun() + +local allExecuted = {} +for key, value in pairs(QueueAutorun.Queue) do table.insert(allExecuted, value[3]) end +for key, value in pairs(QueueForcedAutorun.Queue) do table.insert(allExecuted, value[3]) end + +if SERVER then + Networking.Receive("_luastart", function (message, client) + local num = message.ReadUInt16() + + local packages = {} + + for i = 1, num, 1 do + table.insert(packages, { + Name = message.ReadString(), + Version = message.ReadString(), + Id = message.ReadUInt64(), + Hash = message.ReadString() + }) + end + + Hook.Call("client.packages", client, packages) + end) +else + local message = Networking.Start("_luastart") + + message.WriteUInt16(#allExecuted) + + for key, package in pairs(allExecuted) do + local id = package.UgcId + local hash = package.Hash and package.Hash.StringRepresentation or "" + + if id == none then id = 0 end + + message.WriteString(package.Name) + message.WriteString(package.ModVersion) + message.WriteUInt64(UInt64(id)) + message.WriteString(hash) + end + + Networking.Send(message) +end + +QueueAutorun.Process() +QueueForcedAutorun.Process() Hook.Add("stop", "luaSetup.stop", function() print("Stopping Lua...") end) -Hook.Call("loaded") +Hook.Call("loaded") \ No newline at end of file