Added a basic implementation of a hooking system.
Related to TSHOCKPLUGINS-6
This commit is contained in:
parent
4edea352a0
commit
1d1c15f808
3 changed files with 55 additions and 27 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<string, KeyValuePair<string, LuaFunction>> Hooks = new Dictionary
|
||||
<string, KeyValuePair<string, LuaFunction>>();
|
||||
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<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
|
||||
|
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")]
|
||||
[assembly: AssemblyVersion("3.4.2.0104")]
|
||||
[assembly: AssemblyFileVersion("3.4.2.0104")]
|
||||
Loading…
Add table
Add a link
Reference in a new issue