diff --git a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs index 58dc9f957..24fddcec8 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs @@ -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 files = new List(); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs index 9dede48c5..fcc0e73e3 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs @@ -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)DoString; lua.Globals["load"] = (Func)LoadString; + lua.Globals["setmodulepaths"] = (Action)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(); lua.Globals["ClientPermissions"] = UserData.CreateStatic(); - List modulePaths = new List(); + 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 modulePaths = new List(); - 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(); + } } diff --git a/Barotrauma/BarotraumaShared/Lua/MoonsharpSetup.lua b/Barotrauma/BarotraumaShared/Lua/MoonsharpSetup.lua new file mode 100644 index 000000000..df343a8df --- /dev/null +++ b/Barotrauma/BarotraumaShared/Lua/MoonsharpSetup.lua @@ -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)