made the loader a lua script and added a few useful functions
This commit is contained in:
@@ -106,7 +106,10 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool CheckPermission(Client client, ClientPermissions permissions)
|
||||||
|
{
|
||||||
|
return client.Permissions.HasFlag(permissions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LuaGame
|
public class LuaGame
|
||||||
@@ -431,6 +434,16 @@ namespace Barotrauma
|
|||||||
return File.Exists(path);
|
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)
|
public static string[] DirSearch(string sDir)
|
||||||
{
|
{
|
||||||
List<string> files = new List<string>();
|
List<string> files = new List<string>();
|
||||||
|
|||||||
@@ -18,11 +18,16 @@ namespace Barotrauma
|
|||||||
public LuaHook hook;
|
public LuaHook hook;
|
||||||
public LuaGame game;
|
public LuaGame game;
|
||||||
|
|
||||||
|
public LuaScriptLoader luaScriptLoader;
|
||||||
|
|
||||||
public void HandleLuaException(Exception ex)
|
public void HandleLuaException(Exception ex)
|
||||||
{
|
{
|
||||||
if(ex is InterpreterException)
|
if(ex is InterpreterException)
|
||||||
{
|
{
|
||||||
PrintMessage(((InterpreterException)ex).DecoratedMessage);
|
if(((InterpreterException)ex).DecoratedMessage == null)
|
||||||
|
PrintMessage(((InterpreterException)ex).Message);
|
||||||
|
else
|
||||||
|
PrintMessage(((InterpreterException)ex).DecoratedMessage);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -32,6 +37,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void PrintMessage(object message)
|
public void PrintMessage(object message)
|
||||||
{
|
{
|
||||||
|
if (message == null) { message = "nil"; }
|
||||||
Console.WriteLine(message.ToString());
|
Console.WriteLine(message.ToString());
|
||||||
if (GameMain.Server != null)
|
if (GameMain.Server != null)
|
||||||
{
|
{
|
||||||
@@ -46,6 +52,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void PrintMessageNoLog(object message)
|
public void PrintMessageNoLog(object message)
|
||||||
{
|
{
|
||||||
|
if (message == null) { message = "nil"; }
|
||||||
Console.WriteLine(message.ToString());
|
Console.WriteLine(message.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,11 +141,16 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void SetModulePaths(string[] str)
|
||||||
|
{
|
||||||
|
luaScriptLoader.ModulePaths = str;
|
||||||
|
}
|
||||||
|
|
||||||
public LuaSetup()
|
public LuaSetup()
|
||||||
{
|
{
|
||||||
PrintMessage("Lua!");
|
PrintMessage("Lua!");
|
||||||
|
|
||||||
LuaScriptLoader luaScriptLoader = new LuaScriptLoader(this);
|
luaScriptLoader = new LuaScriptLoader(this);
|
||||||
luaScriptLoader.ModulePaths = new string[] { };
|
luaScriptLoader.ModulePaths = new string[] { };
|
||||||
|
|
||||||
LuaCustomConverters.RegisterAll();
|
LuaCustomConverters.RegisterAll();
|
||||||
@@ -228,6 +240,8 @@ namespace Barotrauma
|
|||||||
lua.Globals["dostring"] = (Func<string, Table, string, DynValue>)DoString;
|
lua.Globals["dostring"] = (Func<string, Table, string, DynValue>)DoString;
|
||||||
lua.Globals["load"] = (Func<string, Table, string, DynValue>)LoadString;
|
lua.Globals["load"] = (Func<string, Table, string, DynValue>)LoadString;
|
||||||
|
|
||||||
|
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
|
||||||
|
|
||||||
lua.Globals["Player"] = new LuaPlayer();
|
lua.Globals["Player"] = new LuaPlayer();
|
||||||
lua.Globals["Game"] = game;
|
lua.Globals["Game"] = game;
|
||||||
lua.Globals["Hook"] = hook;
|
lua.Globals["Hook"] = hook;
|
||||||
@@ -263,19 +277,26 @@ namespace Barotrauma
|
|||||||
lua.Globals["ContentPackage"] = UserData.CreateStatic<ContentPackage>();
|
lua.Globals["ContentPackage"] = UserData.CreateStatic<ContentPackage>();
|
||||||
lua.Globals["ClientPermissions"] = UserData.CreateStatic<ClientPermissions>();
|
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"))
|
foreach (string d in Directory.GetDirectories("Mods"))
|
||||||
{
|
|
||||||
modulePaths.Add(d + "/Lua/?");
|
|
||||||
|
|
||||||
if (Directory.Exists(d + "/Lua/Autorun"))
|
|
||||||
{
|
{
|
||||||
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
Reference in New Issue
Block a user