made the loader a lua script and added a few useful functions

This commit is contained in:
Evil Factory
2021-09-06 23:30:52 -03:00
parent 995d0d9488
commit 5df4207521
3 changed files with 76 additions and 13 deletions

View File

@@ -106,7 +106,10 @@ namespace Barotrauma
}
}
public static bool CheckPermission(Client client, ClientPermissions permissions)
{
return client.Permissions.HasFlag(permissions);
}
}
public class LuaGame
@@ -431,6 +434,16 @@ namespace Barotrauma
return File.Exists(path);
}
public static bool DirectoryExists(string path)
{
return Directory.Exists(path);
}
public static string[] GetDirectories(string path)
{
return Directory.GetDirectories(path);
}
public static string[] DirSearch(string sDir)
{
List<string> files = new List<string>();

View File

@@ -18,11 +18,16 @@ namespace Barotrauma
public LuaHook hook;
public LuaGame game;
public LuaScriptLoader luaScriptLoader;
public void HandleLuaException(Exception ex)
{
if(ex is InterpreterException)
{
PrintMessage(((InterpreterException)ex).DecoratedMessage);
if(((InterpreterException)ex).DecoratedMessage == null)
PrintMessage(((InterpreterException)ex).Message);
else
PrintMessage(((InterpreterException)ex).DecoratedMessage);
}
else
{
@@ -32,6 +37,7 @@ namespace Barotrauma
public void PrintMessage(object message)
{
if (message == null) { message = "nil"; }
Console.WriteLine(message.ToString());
if (GameMain.Server != null)
{
@@ -46,6 +52,7 @@ namespace Barotrauma
public void PrintMessageNoLog(object message)
{
if (message == null) { message = "nil"; }
Console.WriteLine(message.ToString());
}
@@ -134,11 +141,16 @@ namespace Barotrauma
}
public void SetModulePaths(string[] str)
{
luaScriptLoader.ModulePaths = str;
}
public LuaSetup()
{
PrintMessage("Lua!");
LuaScriptLoader luaScriptLoader = new LuaScriptLoader(this);
luaScriptLoader = new LuaScriptLoader(this);
luaScriptLoader.ModulePaths = new string[] { };
LuaCustomConverters.RegisterAll();
@@ -228,6 +240,8 @@ namespace Barotrauma
lua.Globals["dostring"] = (Func<string, Table, string, DynValue>)DoString;
lua.Globals["load"] = (Func<string, Table, string, DynValue>)LoadString;
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
lua.Globals["Player"] = new LuaPlayer();
lua.Globals["Game"] = game;
lua.Globals["Hook"] = hook;
@@ -263,19 +277,26 @@ namespace Barotrauma
lua.Globals["ContentPackage"] = UserData.CreateStatic<ContentPackage>();
lua.Globals["ClientPermissions"] = UserData.CreateStatic<ClientPermissions>();
List<string> modulePaths = new List<string>();
if (File.Exists("Lua/MoonsharpSetup.lua")) // try the default loader
DoFile("Lua/MoonsharpSetup.lua");
else if(File.Exists("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua")) // in case its the workshop version
DoFile("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua");
else // fallback to c# script loading
{
List<string> modulePaths = new List<string>();
foreach (string d in Directory.GetDirectories("Mods"))
{
modulePaths.Add(d + "/Lua/?");
if (Directory.Exists(d + "/Lua/Autorun"))
foreach (string d in Directory.GetDirectories("Mods"))
{
luaScriptLoader.RunFolder(d + "/Lua/Autorun");
}
}
modulePaths.Add(d + "/Lua/?.lua");
luaScriptLoader.ModulePaths = modulePaths.ToArray();
if (Directory.Exists(d + "/Lua/Autorun"))
{
luaScriptLoader.RunFolder(d + "/Lua/Autorun");
}
}
luaScriptLoader.ModulePaths = modulePaths.ToArray();
}
}

View File

@@ -0,0 +1,29 @@
local modulePaths = {}
local function endsWith(str, suffix)
return str:sub(-string.len(suffix)) == suffix
end
local function runFolder(folder)
for k, str in pairs(File.DirSearch(folder)) do
local s = str:gsub("\\", "/")
if endsWith(str, ".lua") then
print(s);
dofile(s);
end
end
end
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
end
setmodulepaths(modulePaths)