From e1e0fd6acfd59ef1a84348e0a0f8950e3ea2a85e Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Sat, 9 Apr 2022 09:06:29 -0300 Subject: [PATCH] Bunch of fixes --- .../ClientSource/Networking/GameClient.cs | 2 + .../ServerSource/DebugConsole.cs | 37 ++++++-- .../BarotraumaServer/ServerSource/Program.cs | 8 +- .../Lua/.vscode/settings.json | 3 +- .../BarotraumaShared/Lua/DefaultHook.lua | 4 - .../BarotraumaShared/Lua/DefaultLib.lua | 5 +- .../BarotraumaShared/Lua/DefaultRegister.lua | 6 ++ Barotrauma/BarotraumaShared/Lua/LuaSetup.lua | 55 +++-------- .../SharedSource/Lua/LuaClasses/LuaClasses.cs | 93 +++++++++++++++---- .../SharedSource/Lua/LuaScriptLoader.cs | 58 +----------- .../SharedSource/Lua/LuaSetup.cs | 49 ++++++++-- 11 files changed, 174 insertions(+), 146 deletions(-) diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs index 126512508..3f2804910 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs @@ -1272,6 +1272,8 @@ namespace Barotrauma.Networking { GameMain.NetLobbyScreen.ChatInput.Enabled = true; } + + GameMain.Lua.Initialize(); } private IEnumerable WaitInServerQueue() diff --git a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs index 6a963e27e..dbba0edae 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.IO; using System.Linq; using System.Text; @@ -1252,26 +1253,44 @@ namespace Barotrauma commands.Add(new Command("install_cl_lua", "Installs client-Side Lua into your client.", (string[] args) => { - if (!System.IO.File.Exists("Mods/LuaForBarotrauma/clientside_files.zip")) + ContentPackage luaPackage = null; + + foreach (ContentPackage package in ContentPackageManager.AllPackages) { - GameMain.Server.SendChatMessage("clientside_files.zip doesn't exist, Github version?", ChatMessageType.ServerMessageBox); + if (package.NameMatches(new Identifier("LuaForBarotraumaUnstable"))) + { + luaPackage = package; + } + } + if (luaPackage == null) + { + GameMain.Server.SendChatMessage("Couldn't find the LuaForBarotrauma package.", ChatMessageType.ServerMessageBox); return; } try { + string path = Path.GetDirectoryName(luaPackage.Path); - System.IO.Compression.ZipFile.ExtractToDirectory("Mods/LuaForBarotrauma/clientside_files.zip", ".", true); + string[] filesToMove = new string[] + { + "Barotrauma.dll", "Barotrauma.deps.json", + "0harmony.dll", "Mono.Cecil.dll", + "Mono.Cecil.Mdb.dll", "Mono.Cecil.Pdb", + "Mono.Cecil.Rocks", "MonoMod.Common.dll", + "MoonSharp.Interpreter.dll", + "mscordaccore_amd64_amd64_4.700.22.11601", + }; - System.IO.File.Move("Barotrauma.dll", "Barotrauma.dll.temp", true); - System.IO.File.Move("Barotrauma.deps.json", "Barotrauma.deps.json.temp", true); - System.IO.File.Move("Barotrauma.dll.original", "Barotrauma.dll"); - System.IO.File.Move("Barotrauma.deps.json.original", "Barotrauma.deps.json"); + File.Move("Barotrauma.dll", "Barotrauma.dll.old", true); + File.Move("Barotrauma.deps.json", "Barotrauma.deps.json.old", true); - System.IO.File.Move("Barotrauma.dll.temp", "Barotrauma.dll.original", true); - System.IO.File.Move("Barotrauma.deps.json.temp", "Barotrauma.deps.json.original", true); + foreach (string file in filesToMove) + { + File.Move(Path.Combine(path, "Binary", file), file, true); + } } catch (Exception e) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/Program.cs b/Barotrauma/BarotraumaServer/ServerSource/Program.cs index fb5f11a54..dde9fa258 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Program.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Program.cs @@ -68,10 +68,10 @@ namespace Barotrauma } string executableDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); - Directory.SetCurrentDirectory(executableDir); - - if (File.Exists("filelist.xml")) - Directory.SetCurrentDirectory(executableDir + "/../.."); // sweet + if (!File.Exists(Path.Combine(executableDir, "workshop.txt"))) + { + Directory.SetCurrentDirectory(executableDir); + } Game = new GameMain(args); diff --git a/Barotrauma/BarotraumaShared/Lua/.vscode/settings.json b/Barotrauma/BarotraumaShared/Lua/.vscode/settings.json index 6bff2e0b1..237e6445e 100644 --- a/Barotrauma/BarotraumaShared/Lua/.vscode/settings.json +++ b/Barotrauma/BarotraumaShared/Lua/.vscode/settings.json @@ -34,6 +34,7 @@ "BindingFlags", "UserData", "LuaUserData", - "CLIENT" + "CLIENT", + "ContentPackageManager" ] } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultHook.lua b/Barotrauma/BarotraumaShared/Lua/DefaultHook.lua index 633909087..c5c964a4a 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultHook.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultHook.lua @@ -46,10 +46,6 @@ Hook.HookMethod( Hook.HookMethod( "Barotrauma.Item", "Drop", - { - "Barotrauma.Character", - "System.Boolean" - }, function (instance, p) if Hook.Call("item.drop", instance, p.dropper) == true then return false diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua index f50f4d87e..01db8ace6 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua @@ -48,6 +48,10 @@ defaultLib["ClientPermissions"] = CreateEnum("Barotrauma.Networking.ClientPermis defaultLib["InputType"] = CreateStatic("Barotrauma.InputType") +defaultLib["ContentPackageManager"] = CreateStatic("Barotrauma.ContentPackageManager") +defaultLib["GameSettings"] = CreateStatic("Barotrauma.GameSettings") +defaultLib["Identifier"] = CreateStatic("Barotrauma.Identifier", true) +defaultLib["ContentPackage"] = CreateStatic("Barotrauma.ContentPackage", true) defaultLib["WayPoint"] = CreateStatic("Barotrauma.WayPoint", true) defaultLib["Submarine"] = CreateStatic("Barotrauma.Submarine", true) defaultLib["Client"] = CreateStatic("Barotrauma.Networking.Client", true) @@ -63,7 +67,6 @@ defaultLib["AfflictionPrefab"] = CreateStatic("Barotrauma.AfflictionPrefab", tru defaultLib["ChatMessage"] = CreateStatic("Barotrauma.Networking.ChatMessage") defaultLib["Hull"] = CreateStatic("Barotrauma.Hull", true) defaultLib["Gap"] = CreateStatic("Barotrauma.Gap", true) -defaultLib["ContentPackage"] = CreateStatic("Barotrauma.ContentPackage", true) defaultLib["Signal"] = CreateStatic("Barotrauma.Items.Components.Signal", true) defaultLib["SubmarineInfo"] = CreateStatic("Barotrauma.SubmarineInfo", true) defaultLib["Entity"] = CreateStatic("Barotrauma.Entity", true) diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua b/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua index 268ab2d3d..14b1364cb 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua @@ -24,6 +24,7 @@ RegisterBarotrauma("LuaFloat") RegisterBarotrauma("Level+InterestingPosition") +RegisterBarotrauma("Identifier") RegisterBarotrauma("Job") RegisterBarotrauma("JobPrefab") RegisterBarotrauma("Level") @@ -60,7 +61,12 @@ RegisterBarotrauma("InputType") RegisterBarotrauma("StatusEffect") RegisterBarotrauma("FireSource") +RegisterBarotrauma("ContentPackageManager") +RegisterBarotrauma("ContentPackageManager+PackageSource") +RegisterBarotrauma("ContentPackageManager+EnabledPackages") RegisterBarotrauma("ContentPackage") +RegisterBarotrauma("RegularPackage") +RegisterBarotrauma("CorePackage") RegisterBarotrauma("SubmarineBody") RegisterBarotrauma("Explosion") RegisterBarotrauma("Networking.ServerSettings") diff --git a/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua b/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua index 3fb8d6fad..d7245fe69 100644 --- a/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua +++ b/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua @@ -1,6 +1,8 @@ -- Config -local runDisabledMods = false -local modulePaths = {"Lua/?.lua", "Mods/LuaForBarotrauma/Lua/?.lua"} + +local path = table.pack(...)[1] + +local modulePaths = {path .. "/?.lua"} setmodulepaths(modulePaths) -- Setup Libraries @@ -21,19 +23,7 @@ require("DefaultHook") -- Execute Mods -if SERVER and Game.IsDedicated then - runDisabledMods = true - - print("LUA LOADER: Dedicated server detected, loading mods regardless being disabled.") -end - -if runDisabledMods then - print("LUA LOADER: Mods will be executed regardless being enabled or not. Lua/LuaSetup.lua") -else - print("LUA LOADER: Only enabled mods will be executed. Lua/LuaSetup.lua") -end - -local enabledPackages = Game.GameSettings.AllEnabledPackages +local enabledPackages = ContentPackageManager.EnabledPackages.All local function endsWith(str, suffix) return str:sub(-string.len(suffix)) == suffix @@ -57,28 +47,14 @@ local function runFolder(folder) end if SERVER then + + for package in enabledPackages do + if package then + local d = package.Path:gsub("\\", "/") + d = d:gsub("/filelist.xml", "") - if not runDisabledMods then - - for package in enabledPackages do - if package then - local d = package.path:gsub("\\", "/") - d = d:gsub("/filelist.xml", "") - - table.insert(modulePaths, (d .. "/Lua/?.lua")) - - if File.DirectoryExists(d .. "/Lua/Autorun") then - runFolder(d .. "/Lua/Autorun") - end - end - end - - else - for _, d in pairs(File.GetDirectories("Mods")) do - d = d:gsub("\\", "/") - table.insert(modulePaths, (d .. "/Lua/?.lua")) - + if File.DirectoryExists(d .. "/Lua/Autorun") then runFolder(d .. "/Lua/Autorun") end @@ -87,15 +63,6 @@ if SERVER then end -for _, d in pairs(File.GetDirectories("Mods")) do - d = d:gsub("\\", "/") - - if File.DirectoryExists(d .. "/Lua/ForcedAutorun") then - table.insert(modulePaths, (d .. "/Lua/?.lua")) - runFolder(d .. "/Lua/ForcedAutorun") - end -end - setmodulepaths(modulePaths) Hook.Add("stop", "luaSetup.stop", function () diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs index fd4272b3f..399f979de 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs @@ -51,43 +51,93 @@ namespace Barotrauma partial class LuaFile { - // TODO: SANDBOXING - - public static bool IsPathAllowed(string path) + public static bool CanReadFromPath(string path) { - path = Path.GetFullPath(path).CleanUpPath(); + string getFullPath(string p) => System.IO.Path.GetFullPath(p).CleanUpPath(); - if (path.StartsWith(Path.GetFullPath("Mods").CleanUpPath())) + path = getFullPath(path); + + bool pathStartsWith(string prefix) => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase); + + string localModsDir = getFullPath(ContentPackage.LocalModsDir); + string workshopModsDir = getFullPath(ContentPackage.WorkshopModsDir); +#if CLIENT + string tempDownloadDir = getFullPath(ModReceiver.DownloadFolder); +#endif + + + if (pathStartsWith(localModsDir)) return true; - if (path.StartsWith(Path.GetFullPath("Submarines").CleanUpPath())) + if (pathStartsWith(workshopModsDir)) return true; - if (path.StartsWith(Path.GetFullPath("Data").CleanUpPath())) +#if CLIENT + if (pathStartsWith(tempDownloadDir)) return true; +#endif - if (path.StartsWith(Path.GetFullPath("Lua").CleanUpPath())) - return true; - - if (path.StartsWith(Path.GetFullPath("Content").CleanUpPath())) + if (pathStartsWith(getFullPath("."))) return true; return false; } - public static bool IsPathAllowedLuaException(string path) + public static bool CanWriteToPath(string path) { - if (IsPathAllowed(path)) + string getFullPath(string p) => System.IO.Path.GetFullPath(p).CleanUpPath(); + + path = getFullPath(path); + + bool pathStartsWith(string prefix) => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase); + + + if (pathStartsWith(getFullPath(ContentPackage.LocalModsDir + "LuaForBarotraumaUnstable"))) + return false; + + if (pathStartsWith(getFullPath(ContentPackage.WorkshopModsDir + "LuaForBarotraumaUnstable"))) + return false; +#if CLIENT + if (pathStartsWith(getFullPath(ModReceiver.DownloadFolder + "LuaForBarotraumaUnstable"))) + return false; +#endif + + if (pathStartsWith(getFullPath(ContentPackage.LocalModsDir))) return true; - else - GameMain.Lua.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed.")); + + if (pathStartsWith(getFullPath(ContentPackage.WorkshopModsDir))) + return true; +#if CLIENT + if (pathStartsWith(getFullPath(ModReceiver.DownloadFolder))) + return true; +#endif + + return false; + } + + public static bool IsPathAllowedLuaException(string path, bool write = true) + { + if (write) + { + if (CanWriteToPath(path)) + return true; + else + GameMain.Lua.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed.")); + } + else + { + if (CanReadFromPath(path)) + return true; + else + GameMain.Lua.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed.")); + } return false; } public static string Read(string path) { - if (!IsPathAllowedLuaException(path)) + if (!IsPathAllowedLuaException(path, false)) return ""; return File.ReadAllText(path); @@ -103,7 +153,7 @@ namespace Barotrauma public static bool Exists(string path) { - if (!IsPathAllowedLuaException(path)) + if (!IsPathAllowedLuaException(path, false)) return false; return File.Exists(path); @@ -121,7 +171,7 @@ namespace Barotrauma public static bool DirectoryExists(string path) { - if (!IsPathAllowedLuaException(path)) + if (!IsPathAllowedLuaException(path, false)) return false; return Directory.Exists(path); @@ -129,12 +179,15 @@ namespace Barotrauma public static string[] GetFiles(string path) { + if (!IsPathAllowedLuaException(path, false)) + return null; + return Directory.GetFiles(path); } public static string[] GetDirectories(string path) { - if (!IsPathAllowedLuaException(path)) + if (!IsPathAllowedLuaException(path, false)) return new string[] { }; return Directory.GetDirectories(path); @@ -142,7 +195,7 @@ namespace Barotrauma public static string[] DirSearch(string sDir) { - if (!IsPathAllowedLuaException(sDir)) + if (!IsPathAllowedLuaException(sDir, false)) return new string[] { }; List files = new List(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs index 6947e31d1..8b87662b9 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs @@ -21,68 +21,18 @@ namespace Barotrauma public override object LoadFile(string file, Table globalContext) { + if (!LuaFile.IsPathAllowedLuaException(file, false)) return null; + return File.ReadAllText(file); } public override bool ScriptFileExists(string file) { + if (!LuaFile.IsPathAllowedLuaException(file, false)) return false; + return File.Exists(file); } - public void RunFolder(string folder) - { - foreach (var str in DirSearch(folder)) - { - var s = str.Replace("\\", "/"); - - if (s.EndsWith(".lua")) - { - lua.PrintMessage(s); - - try - { - lua.DoFile(s); - } - catch (Exception e) - { - lua.HandleLuaException(e); - } - } - - } - } - - static string[] DirSearch(string sDir) - { - List files = new List(); - - try - { - foreach (string f in Directory.GetFiles(sDir)) - { - files.Add(f); - } - - foreach (string d in Directory.GetDirectories(sDir)) - { - foreach (string f in Directory.GetFiles(d)) - { - files.Add(f); - } - DirSearch(d); - } - } - catch (System.Exception excpt) - { - Console.WriteLine(excpt.Message); - } - - return files.ToArray(); - } - - - - } } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs index 6cec54b10..65de6a2e4 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs @@ -13,6 +13,8 @@ namespace Barotrauma { partial class LuaSetup { + public const string LUA_PATH = "Lua/LuaSetup.lua"; + public Script lua; public LuaHook hook; @@ -124,8 +126,6 @@ namespace Barotrauma public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null) { - if(!LuaFile.IsPathAllowedLuaException(file)) return null; - try { return lua.DoFile(file, globalContext, codeStringFriendly); @@ -157,8 +157,6 @@ namespace Barotrauma public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null) { - if (!LuaFile.IsPathAllowedLuaException(file)) return null; - try { return lua.LoadFile(file, globalContext, codeStringFriendly); @@ -222,7 +220,7 @@ namespace Barotrauma public void Initialize() { Stop(); - + PrintMessage("Lua! Version " + AssemblyInfo.GitRevision); luaScriptLoader = new LuaScriptLoader(this); @@ -278,12 +276,45 @@ namespace Barotrauma // LuaDocs.GenerateDocsAll(); - if (File.Exists("Lua/LuaSetup.lua")) // try the default loader - DoFile("Lua/LuaSetup.lua"); - else if (File.Exists("Mods/LuaForBarotrauma/Lua/LuaSetup.lua")) // in case its the workshop version - DoFile("Mods/LuaForBarotrauma/Lua/LuaSetup.lua"); + ContentPackage luaPackage = null; + + foreach (ContentPackage package in ContentPackageManager.AllPackages) + { + if (package.NameMatches(new Identifier("LuaForBarotraumaUnstable"))) + { + luaPackage = package; + } + } + + if (File.Exists(LUA_PATH)) + { + try + { + lua.Call(lua.LoadFile(LUA_PATH), Path.GetDirectoryName(Path.GetFullPath(LUA_PATH))); + } + catch (Exception e) + { + HandleLuaException(e); + } + } + else if (luaPackage != null) + { + string path = Path.GetDirectoryName(luaPackage.Path); + + try + { + string luaPath = Path.Combine(path, "Binary/Lua/LuaSetup.lua"); + lua.Call(lua.LoadFile(luaPath), Path.GetDirectoryName(luaPath)); + } + catch (Exception e) + { + HandleLuaException(e); + } + } else + { PrintError("Lua loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work."); + } } public LuaSetup()