Added a basic implementation of a hooking system.

Related to TSHOCKPLUGINS-6
This commit is contained in:
Lucas Nicodemus 2012-01-04 02:43:46 -07:00
parent 4edea352a0
commit 1d1c15f808
3 changed files with 55 additions and 27 deletions

View file

@ -211,6 +211,7 @@ namespace TShockAPI
add(Permissions.runlua, RunLuaFile, "luarun"); add(Permissions.runlua, RunLuaFile, "luarun");
add(Permissions.runlua, RunLuaString, "lua"); add(Permissions.runlua, RunLuaString, "lua");
add(Permissions.runlua, ReloadLua, "luareload"); add(Permissions.runlua, ReloadLua, "luareload");
add(Permissions.runlua, TestLuaHook, "testhook");
} }
public static bool HandleCommand(TSPlayer player, string text) public static bool HandleCommand(TSPlayer player, string text)
@ -330,6 +331,11 @@ namespace TShockAPI
#region Lua Commands #region Lua Commands
public static void TestLuaHook(CommandArgs args)
{
TShock.LuaLoader.HookCalls.OnHookTest();
}
public static void ReloadLua(CommandArgs args) public static void ReloadLua(CommandArgs args)
{ {
TShock.LuaLoader.LoadServerAutoruns(); TShock.LuaLoader.LoadServerAutoruns();
@ -345,11 +351,8 @@ namespace TShockAPI
return; return;
} }
string lua = ""; TShock.LuaLoader.RunLuaString(args.Parameters[0]);
foreach (string s in args.Parameters) args.Player.SendMessage("Lua run: " + args.Parameters[0]);
{
lua += s;
}
} }
public static void RunLuaFile(CommandArgs args) public static void RunLuaFile(CommandArgs args)

View file

@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.ComponentModel;
using LuaInterface; using LuaInterface;
namespace TShockAPI.LuaSystem namespace TShockAPI.LuaSystem
@ -13,7 +14,8 @@ namespace TShockAPI.LuaSystem
private Lua Lua = null; private Lua Lua = null;
public string LuaPath = ""; public string LuaPath = "";
public string LuaAutorunPath = ""; public string LuaAutorunPath = "";
public LuaHookBackend HookBackend = new LuaHookBackend();
public LuaHooks HookCalls = new LuaHooks();
public Dictionary<string, KeyValuePair<string, LuaFunction>> Hooks = new Dictionary public Dictionary<string, KeyValuePair<string, LuaFunction>> Hooks = new Dictionary
<string, KeyValuePair<string, LuaFunction>>(); <string, KeyValuePair<string, LuaFunction>>();
public LuaLoader(string path) public LuaLoader(string path)
@ -34,13 +36,14 @@ namespace TShockAPI.LuaSystem
RegisterLuaFunctions(); RegisterLuaFunctions();
LoadServerAutoruns(); LoadServerAutoruns();
HookTest();
} }
static void test() static void test()
{ {
var loader = new LuaLoader(""); var loader = new LuaLoader("");
loader.RunLuaString(@" loader.RunLuaString(@"
function hookme() function hookme()
Print('Hook test') Print('Hook test')
end end
Hook(""doesntmatter"", ""hookmeee"", hookme)"); Hook(""doesntmatter"", ""hookmeee"", hookme)");
@ -101,26 +104,9 @@ Hook(""doesntmatter"", ""hookmeee"", hookme)");
{ {
LuaFunctions LuaFuncs = new LuaFunctions(this); LuaFunctions LuaFuncs = new LuaFunctions(this);
Lua.RegisterFunction("Print", LuaFuncs, LuaFuncs.GetType().GetMethod("Print")); 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<string, KeyValuePair<string, LuaFunction>> kv in Hooks)
{
KeyValuePair<string, LuaFunction> hook = kv.Value;
LuaFunction lf = hook.Value;
if (lf != null)
{
lf.Call();
}
}
}
} }
public class LuaFunctions public class LuaFunctions
@ -144,4 +130,43 @@ Hook(""doesntmatter"", ""hookmeee"", hookme)");
Parent.Hooks.Add(key, internalhook); Parent.Hooks.Add(key, internalhook);
} }
} }
public class LuaHookBackend
{
public void HookRun(string call, object parameters)
{
foreach (KeyValuePair<string, KeyValuePair<string, LuaFunction>> kv in TShock.LuaLoader.Hooks)
{
KeyValuePair<string, LuaFunction> 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);
}
}
} }

View file

@ -48,5 +48,5 @@ using System.Runtime.InteropServices;
// Build Number // Build Number
// MMdd of the build // MMdd of the build
[assembly: AssemblyVersion("3.4.2.0103")] [assembly: AssemblyVersion("3.4.2.0104")]
[assembly: AssemblyFileVersion("3.4.2.0103")] [assembly: AssemblyFileVersion("3.4.2.0104")]