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

View File

@@ -1272,6 +1272,8 @@ namespace Barotrauma.Networking
{
GameMain.NetLobbyScreen.ChatInput.Enabled = true;
}
GameMain.Lua.Initialize();
}
private IEnumerable<CoroutineStatus> WaitInServerQueue()

View File

@@ -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)
{

View File

@@ -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);

View File

@@ -34,6 +34,7 @@
"BindingFlags",
"UserData",
"LuaUserData",
"CLIENT"
"CLIENT",
"ContentPackageManager"
]
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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")

View File

@@ -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 ()

View File

@@ -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<string> files = new List<string>();

View File

@@ -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<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();
}
}
}
}

View File

@@ -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()