From 1d1c15f808a7b91c475c21a60242584ee34a88c9 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 4 Jan 2012 02:43:46 -0700 Subject: [PATCH] Added a basic implementation of a hooking system. Related to TSHOCKPLUGINS-6 --- TShockAPI/Commands.cs | 13 +++--- TShockAPI/LuaSystem/LuaLoader.cs | 65 +++++++++++++++++++--------- TShockAPI/Properties/AssemblyInfo.cs | 4 +- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 8a6d974d..45c95d6b 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -211,6 +211,7 @@ namespace TShockAPI add(Permissions.runlua, RunLuaFile, "luarun"); add(Permissions.runlua, RunLuaString, "lua"); add(Permissions.runlua, ReloadLua, "luareload"); + add(Permissions.runlua, TestLuaHook, "testhook"); } public static bool HandleCommand(TSPlayer player, string text) @@ -330,6 +331,11 @@ namespace TShockAPI #region Lua Commands + public static void TestLuaHook(CommandArgs args) + { + TShock.LuaLoader.HookCalls.OnHookTest(); + } + public static void ReloadLua(CommandArgs args) { TShock.LuaLoader.LoadServerAutoruns(); @@ -345,11 +351,8 @@ namespace TShockAPI return; } - string lua = ""; - foreach (string s in args.Parameters) - { - lua += s; - } + TShock.LuaLoader.RunLuaString(args.Parameters[0]); + args.Player.SendMessage("Lua run: " + args.Parameters[0]); } public static void RunLuaFile(CommandArgs args) diff --git a/TShockAPI/LuaSystem/LuaLoader.cs b/TShockAPI/LuaSystem/LuaLoader.cs index 8d1ae225..9a72f24b 100644 --- a/TShockAPI/LuaSystem/LuaLoader.cs +++ b/TShockAPI/LuaSystem/LuaLoader.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading; +using System.ComponentModel; using LuaInterface; namespace TShockAPI.LuaSystem @@ -13,7 +14,8 @@ namespace TShockAPI.LuaSystem private Lua Lua = null; public string LuaPath = ""; public string LuaAutorunPath = ""; - + public LuaHookBackend HookBackend = new LuaHookBackend(); + public LuaHooks HookCalls = new LuaHooks(); public Dictionary> Hooks = new Dictionary >(); public LuaLoader(string path) @@ -34,13 +36,14 @@ namespace TShockAPI.LuaSystem RegisterLuaFunctions(); LoadServerAutoruns(); + HookTest(); } static void test() { var loader = new LuaLoader(""); loader.RunLuaString(@" function hookme() - Print('Hook test') + Print('Hook test') end Hook(""doesntmatter"", ""hookmeee"", hookme)"); @@ -101,26 +104,9 @@ Hook(""doesntmatter"", ""hookmeee"", hookme)"); { LuaFunctions LuaFuncs = new LuaFunctions(this); Lua.RegisterFunction("Print", LuaFuncs, LuaFuncs.GetType().GetMethod("Print")); - Lua.RegisterFunction("Hook", LuaFuncs, LuaFuncs.GetType().GetMethod("Hook")); + Lua.RegisterFunction("RegisterHook", LuaFuncs, LuaFuncs.GetType().GetMethod("Hook")); } - - public void HookTest() - { - - Console.WriteLine("Running hook test."); - - foreach (KeyValuePair> kv in Hooks) - { - KeyValuePair hook = kv.Value; - LuaFunction lf = hook.Value; - - if (lf != null) - { - lf.Call(); - } - } - } } public class LuaFunctions @@ -144,4 +130,43 @@ Hook(""doesntmatter"", ""hookmeee"", hookme)"); Parent.Hooks.Add(key, internalhook); } } + + public class LuaHookBackend + { + public void HookRun(string call, object parameters) + { + foreach (KeyValuePair> kv in TShock.LuaLoader.Hooks) + { + KeyValuePair hook = kv.Value; + if (call == hook.Key) + { + LuaFunction lf = hook.Value; + + if (lf != null) + { + try + { + lf.Call(parameters); + } + catch (LuaException e) + { + TShock.LuaLoader.SendLuaDebugMsg(e.Message); + } + } + } + } + } + } + + public class LuaHooks + { + [Description("Called on debug hook test.")] + public void OnHookTest() + { + object[] response = new object[2]; + response[0] = true; + response[1] = "Hook win!"; + TShock.LuaLoader.HookBackend.HookRun("HookTest", response); + } + } } diff --git a/TShockAPI/Properties/AssemblyInfo.cs b/TShockAPI/Properties/AssemblyInfo.cs index 05b4b6aa..c7232ab2 100644 --- a/TShockAPI/Properties/AssemblyInfo.cs +++ b/TShockAPI/Properties/AssemblyInfo.cs @@ -48,5 +48,5 @@ using System.Runtime.InteropServices; // Build Number // MMdd of the build -[assembly: AssemblyVersion("3.4.2.0103")] -[assembly: AssemblyFileVersion("3.4.2.0103")] \ No newline at end of file +[assembly: AssemblyVersion("3.4.2.0104")] +[assembly: AssemblyFileVersion("3.4.2.0104")] \ No newline at end of file