diff --git a/TShockAPI/LuaSystem/LuaLoader.cs b/TShockAPI/LuaSystem/LuaLoader.cs index ca8c20f4..e1414a8d 100644 --- a/TShockAPI/LuaSystem/LuaLoader.cs +++ b/TShockAPI/LuaSystem/LuaLoader.cs @@ -11,19 +11,45 @@ namespace TShockAPI.LuaSystem { private Lua Lua = null; public string LuaPath = ""; + public string LuaAutorunPath = ""; public LuaLoader(string path) { Lua = new Lua(); LuaPath = path; + LuaAutorunPath = Path.Combine(LuaPath, "autorun"); SendLuaDebugMsg("Lua 5.1 (serverside) initialized."); + + if (!Directory.Exists(LuaPath)) + { + Directory.CreateDirectory(LuaPath); + Directory.CreateDirectory(LuaAutorunPath); + } + + RegisterLuaFunctions(); + LoadServerAutoruns(); } public void LoadServerAutoruns() { - foreach (string s in Directory.GetFiles(Path.Combine(LuaPath, "autorun"))) + try { - Lua.DoFile(s); - SendLuaDebugMsg("Loaded file: " + s); + foreach (string s in Directory.GetFiles(LuaAutorunPath)) + { + SendLuaDebugMsg("Loading: " + s); + try + { + Lua.DoFile(s); + } + catch (LuaException e) + { + SendLuaDebugMsg(e.Message); + } + } + } + catch (Exception e) + { + SendLuaDebugMsg(e.Message); + SendLuaDebugMsg(e.StackTrace); } } @@ -34,5 +60,22 @@ namespace TShockAPI.LuaSystem Console.WriteLine("Lua: " + s); Console.ForegroundColor = previousColor; } + + public void RegisterLuaFunctions() + { + LuaFunctions LuaFuncs = new LuaFunctions(); + Lua.RegisterFunction("Print", LuaFuncs, LuaFuncs.GetType().GetMethod("Print")); + } + } + + public class LuaFunctions + { + public void Print(string s) + { + ConsoleColor previousColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine(s); + Console.ForegroundColor = previousColor; + } } }