From 15d9a2aa8c9fc3e1ad38783d9b69f082f5cb23ff Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Tue, 3 Jan 2012 17:44:54 -0700 Subject: [PATCH] More basic prep. TSHOCKPLUGINS-4 --- TShockAPI/LuaSystem/LuaLoader.cs | 49 ++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) 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; + } } }