From 47cbe4fd3354859749c9bc99cc72f534e0161ccc Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Tue, 3 Jan 2012 18:51:22 -0700 Subject: [PATCH] Committing what I have for Lua hooking. Any ideas? --- TShockAPI/LuaSystem/LuaLoader.cs | 39 ++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/TShockAPI/LuaSystem/LuaLoader.cs b/TShockAPI/LuaSystem/LuaLoader.cs index fe14ad0c..3d1e4c02 100644 --- a/TShockAPI/LuaSystem/LuaLoader.cs +++ b/TShockAPI/LuaSystem/LuaLoader.cs @@ -13,6 +13,9 @@ namespace TShockAPI.LuaSystem private Lua Lua = null; public string LuaPath = ""; public string LuaAutorunPath = ""; + + public Dictionary> Hooks = new Dictionary + >(); public LuaLoader(string path) { Lua = new Lua(); @@ -28,6 +31,7 @@ namespace TShockAPI.LuaSystem RegisterLuaFunctions(); LoadServerAutoruns(); + HookTest(); } public void LoadServerAutoruns() @@ -83,11 +87,36 @@ namespace TShockAPI.LuaSystem { LuaFunctions LuaFuncs = new LuaFunctions(); Lua.RegisterFunction("Print", LuaFuncs, LuaFuncs.GetType().GetMethod("Print")); + Lua.RegisterFunction("Hook", LuaFuncs, LuaFuncs.GetType().GetMethod("Hook")); + } - public void Shutdown() + public void HookTest() { - SendLuaDebugMsg("Lua 5.1 shutting down. Terminating all Lua threads."); + + Console.WriteLine("Running hook test."); + + foreach (KeyValuePair> kv in Hooks) + { + KeyValuePair hook = kv.Value; + LuaFunction lf = FindLuaFunction(hook.Value); + + if (lf != null) + { + lf.Call(); + } + } + } + + public LuaFunction FindLuaFunction(string name) + { + try + { + return Lua.GetFunction(name); + } catch (Exception) + { + return null; + } } } @@ -100,5 +129,11 @@ namespace TShockAPI.LuaSystem Console.WriteLine(s); Console.ForegroundColor = previousColor; } + + public void Hook(string hook, string key, string callback) + { + KeyValuePair hhook = new KeyValuePair(hook, callback); + TShock.LuaLoader.Hooks.Add(key, hhook); + } } }