Bunch of fixes

This commit is contained in:
Evil Factory
2022-04-09 09:06:29 -03:00
parent b37fd8d9c9
commit e1e0fd6acf
11 changed files with 174 additions and 146 deletions
@@ -1272,6 +1272,8 @@ namespace Barotrauma.Networking
{ {
GameMain.NetLobbyScreen.ChatInput.Enabled = true; GameMain.NetLobbyScreen.ChatInput.Enabled = true;
} }
GameMain.Lua.Initialize();
} }
private IEnumerable<CoroutineStatus> WaitInServerQueue() private IEnumerable<CoroutineStatus> WaitInServerQueue()
@@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; 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) => 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; return;
} }
try 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"); File.Move("Barotrauma.dll", "Barotrauma.dll.old", true);
System.IO.File.Move("Barotrauma.deps.json.original", "Barotrauma.deps.json"); File.Move("Barotrauma.deps.json", "Barotrauma.deps.json.old", true);
System.IO.File.Move("Barotrauma.dll.temp", "Barotrauma.dll.original", true); foreach (string file in filesToMove)
System.IO.File.Move("Barotrauma.deps.json.temp", "Barotrauma.deps.json.original", true); {
File.Move(Path.Combine(path, "Binary", file), file, true);
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -68,10 +68,10 @@ namespace Barotrauma
} }
string executableDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); string executableDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Directory.SetCurrentDirectory(executableDir); if (!File.Exists(Path.Combine(executableDir, "workshop.txt")))
{
if (File.Exists("filelist.xml")) Directory.SetCurrentDirectory(executableDir);
Directory.SetCurrentDirectory(executableDir + "/../.."); // sweet }
Game = new GameMain(args); Game = new GameMain(args);
+2 -1
View File
@@ -34,6 +34,7 @@
"BindingFlags", "BindingFlags",
"UserData", "UserData",
"LuaUserData", "LuaUserData",
"CLIENT" "CLIENT",
"ContentPackageManager"
] ]
} }
@@ -46,10 +46,6 @@ Hook.HookMethod(
Hook.HookMethod( Hook.HookMethod(
"Barotrauma.Item", "Drop", "Barotrauma.Item", "Drop",
{
"Barotrauma.Character",
"System.Boolean"
},
function (instance, p) function (instance, p)
if Hook.Call("item.drop", instance, p.dropper) == true then if Hook.Call("item.drop", instance, p.dropper) == true then
return false return false
@@ -48,6 +48,10 @@ defaultLib["ClientPermissions"] = CreateEnum("Barotrauma.Networking.ClientPermis
defaultLib["InputType"] = CreateStatic("Barotrauma.InputType") 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["WayPoint"] = CreateStatic("Barotrauma.WayPoint", true)
defaultLib["Submarine"] = CreateStatic("Barotrauma.Submarine", true) defaultLib["Submarine"] = CreateStatic("Barotrauma.Submarine", true)
defaultLib["Client"] = CreateStatic("Barotrauma.Networking.Client", 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["ChatMessage"] = CreateStatic("Barotrauma.Networking.ChatMessage")
defaultLib["Hull"] = CreateStatic("Barotrauma.Hull", true) defaultLib["Hull"] = CreateStatic("Barotrauma.Hull", true)
defaultLib["Gap"] = CreateStatic("Barotrauma.Gap", true) defaultLib["Gap"] = CreateStatic("Barotrauma.Gap", true)
defaultLib["ContentPackage"] = CreateStatic("Barotrauma.ContentPackage", true)
defaultLib["Signal"] = CreateStatic("Barotrauma.Items.Components.Signal", true) defaultLib["Signal"] = CreateStatic("Barotrauma.Items.Components.Signal", true)
defaultLib["SubmarineInfo"] = CreateStatic("Barotrauma.SubmarineInfo", true) defaultLib["SubmarineInfo"] = CreateStatic("Barotrauma.SubmarineInfo", true)
defaultLib["Entity"] = CreateStatic("Barotrauma.Entity", true) defaultLib["Entity"] = CreateStatic("Barotrauma.Entity", true)
@@ -24,6 +24,7 @@ RegisterBarotrauma("LuaFloat")
RegisterBarotrauma("Level+InterestingPosition") RegisterBarotrauma("Level+InterestingPosition")
RegisterBarotrauma("Identifier")
RegisterBarotrauma("Job") RegisterBarotrauma("Job")
RegisterBarotrauma("JobPrefab") RegisterBarotrauma("JobPrefab")
RegisterBarotrauma("Level") RegisterBarotrauma("Level")
@@ -60,7 +61,12 @@ RegisterBarotrauma("InputType")
RegisterBarotrauma("StatusEffect") RegisterBarotrauma("StatusEffect")
RegisterBarotrauma("FireSource") RegisterBarotrauma("FireSource")
RegisterBarotrauma("ContentPackageManager")
RegisterBarotrauma("ContentPackageManager+PackageSource")
RegisterBarotrauma("ContentPackageManager+EnabledPackages")
RegisterBarotrauma("ContentPackage") RegisterBarotrauma("ContentPackage")
RegisterBarotrauma("RegularPackage")
RegisterBarotrauma("CorePackage")
RegisterBarotrauma("SubmarineBody") RegisterBarotrauma("SubmarineBody")
RegisterBarotrauma("Explosion") RegisterBarotrauma("Explosion")
RegisterBarotrauma("Networking.ServerSettings") RegisterBarotrauma("Networking.ServerSettings")
+11 -44
View File
@@ -1,6 +1,8 @@
-- Config -- Config
local runDisabledMods = false
local modulePaths = {"Lua/?.lua", "Mods/LuaForBarotrauma/Lua/?.lua"} local path = table.pack(...)[1]
local modulePaths = {path .. "/?.lua"}
setmodulepaths(modulePaths) setmodulepaths(modulePaths)
-- Setup Libraries -- Setup Libraries
@@ -21,19 +23,7 @@ require("DefaultHook")
-- Execute Mods -- Execute Mods
if SERVER and Game.IsDedicated then local enabledPackages = ContentPackageManager.EnabledPackages.All
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 function endsWith(str, suffix) local function endsWith(str, suffix)
return str:sub(-string.len(suffix)) == suffix return str:sub(-string.len(suffix)) == suffix
@@ -57,28 +47,14 @@ local function runFolder(folder)
end end
if SERVER then 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")) table.insert(modulePaths, (d .. "/Lua/?.lua"))
if File.DirectoryExists(d .. "/Lua/Autorun") then if File.DirectoryExists(d .. "/Lua/Autorun") then
runFolder(d .. "/Lua/Autorun") runFolder(d .. "/Lua/Autorun")
end end
@@ -87,15 +63,6 @@ if SERVER then
end 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) setmodulepaths(modulePaths)
Hook.Add("stop", "luaSetup.stop", function () Hook.Add("stop", "luaSetup.stop", function ()
@@ -51,43 +51,93 @@ namespace Barotrauma
partial class LuaFile partial class LuaFile
{ {
// TODO: SANDBOXING public static bool CanReadFromPath(string path)
public static bool IsPathAllowed(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; return true;
if (path.StartsWith(Path.GetFullPath("Submarines").CleanUpPath())) if (pathStartsWith(workshopModsDir))
return true; return true;
if (path.StartsWith(Path.GetFullPath("Data").CleanUpPath())) #if CLIENT
if (pathStartsWith(tempDownloadDir))
return true; return true;
#endif
if (path.StartsWith(Path.GetFullPath("Lua").CleanUpPath())) if (pathStartsWith(getFullPath(".")))
return true;
if (path.StartsWith(Path.GetFullPath("Content").CleanUpPath()))
return true; return true;
return false; 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; 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; return false;
} }
public static string Read(string path) public static string Read(string path)
{ {
if (!IsPathAllowedLuaException(path)) if (!IsPathAllowedLuaException(path, false))
return ""; return "";
return File.ReadAllText(path); return File.ReadAllText(path);
@@ -103,7 +153,7 @@ namespace Barotrauma
public static bool Exists(string path) public static bool Exists(string path)
{ {
if (!IsPathAllowedLuaException(path)) if (!IsPathAllowedLuaException(path, false))
return false; return false;
return File.Exists(path); return File.Exists(path);
@@ -121,7 +171,7 @@ namespace Barotrauma
public static bool DirectoryExists(string path) public static bool DirectoryExists(string path)
{ {
if (!IsPathAllowedLuaException(path)) if (!IsPathAllowedLuaException(path, false))
return false; return false;
return Directory.Exists(path); return Directory.Exists(path);
@@ -129,12 +179,15 @@ namespace Barotrauma
public static string[] GetFiles(string path) public static string[] GetFiles(string path)
{ {
if (!IsPathAllowedLuaException(path, false))
return null;
return Directory.GetFiles(path); return Directory.GetFiles(path);
} }
public static string[] GetDirectories(string path) public static string[] GetDirectories(string path)
{ {
if (!IsPathAllowedLuaException(path)) if (!IsPathAllowedLuaException(path, false))
return new string[] { }; return new string[] { };
return Directory.GetDirectories(path); return Directory.GetDirectories(path);
@@ -142,7 +195,7 @@ namespace Barotrauma
public static string[] DirSearch(string sDir) public static string[] DirSearch(string sDir)
{ {
if (!IsPathAllowedLuaException(sDir)) if (!IsPathAllowedLuaException(sDir, false))
return new string[] { }; return new string[] { };
List<string> files = new List<string>(); List<string> files = new List<string>();
@@ -21,68 +21,18 @@ namespace Barotrauma
public override object LoadFile(string file, Table globalContext) public override object LoadFile(string file, Table globalContext)
{ {
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
return File.ReadAllText(file); return File.ReadAllText(file);
} }
public override bool ScriptFileExists(string file) public override bool ScriptFileExists(string file)
{ {
if (!LuaFile.IsPathAllowedLuaException(file, false)) return false;
return File.Exists(file); 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<string> files = new List<string>();
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();
}
} }
} }
} }
@@ -13,6 +13,8 @@ namespace Barotrauma
{ {
partial class LuaSetup partial class LuaSetup
{ {
public const string LUA_PATH = "Lua/LuaSetup.lua";
public Script lua; public Script lua;
public LuaHook hook; public LuaHook hook;
@@ -124,8 +126,6 @@ namespace Barotrauma
public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null) public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null)
{ {
if(!LuaFile.IsPathAllowedLuaException(file)) return null;
try try
{ {
return lua.DoFile(file, globalContext, codeStringFriendly); return lua.DoFile(file, globalContext, codeStringFriendly);
@@ -157,8 +157,6 @@ namespace Barotrauma
public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null) public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null)
{ {
if (!LuaFile.IsPathAllowedLuaException(file)) return null;
try try
{ {
return lua.LoadFile(file, globalContext, codeStringFriendly); return lua.LoadFile(file, globalContext, codeStringFriendly);
@@ -222,7 +220,7 @@ namespace Barotrauma
public void Initialize() public void Initialize()
{ {
Stop(); Stop();
PrintMessage("Lua! Version " + AssemblyInfo.GitRevision); PrintMessage("Lua! Version " + AssemblyInfo.GitRevision);
luaScriptLoader = new LuaScriptLoader(this); luaScriptLoader = new LuaScriptLoader(this);
@@ -278,12 +276,45 @@ namespace Barotrauma
// LuaDocs.GenerateDocsAll(); // LuaDocs.GenerateDocsAll();
if (File.Exists("Lua/LuaSetup.lua")) // try the default loader ContentPackage luaPackage = null;
DoFile("Lua/LuaSetup.lua");
else if (File.Exists("Mods/LuaForBarotrauma/Lua/LuaSetup.lua")) // in case its the workshop version foreach (ContentPackage package in ContentPackageManager.AllPackages)
DoFile("Mods/LuaForBarotrauma/Lua/LuaSetup.lua"); {
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 else
{
PrintError("Lua loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work."); PrintError("Lua loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
}
} }
public LuaSetup() public LuaSetup()