fix patch hooks and rename delegates

This commit is contained in:
Evil Factory
2022-04-16 12:44:50 -03:00
parent 0d8d88b7fb
commit f439cabc9a
4 changed files with 38 additions and 38 deletions
@@ -324,7 +324,7 @@ namespace Barotrauma
} }
} }
public void AddCommand(string name, string help, CsAction onExecute, CsFunc getValidArgs = null, bool isCheat = false) public void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs = null, bool isCheat = false)
{ {
var cmd = new DebugConsole.Command(name, help, (string[] arg1) => { onExecute(arg1); }, var cmd = new DebugConsole.Command(name, help, (string[] arg1) => { onExecute(arg1); },
() => () =>
@@ -23,9 +23,9 @@ namespace Barotrauma
var function = v.Function; var function = v.Function;
return (Func<Fixture, Vector2, Vector2, float, float>)((Fixture a, Vector2 b, Vector2 c, float d) => new LuaResult(function.Call(a, b, c, d)).Float()); return (Func<Fixture, Vector2, Vector2, float, float>)((Fixture a, Vector2 b, Vector2 c, float d) => new LuaResult(function.Call(a, b, c, d)).Float());
}); });
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(CsAction), v => (CsAction)( args => GameMain.LuaCs.CallLuaFunction(v.Function, args) )); Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsAction), v => (LuaCsAction)( args => GameMain.LuaCs.CallLuaFunction(v.Function, args) ));
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(CsFunc), v => (CsFunc)( args => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, args)) )); Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsFunc), v => (LuaCsFunc)( args => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, args)) ));
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(CsPatch), v => (CsPatch)( (self, args) => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, self, args)) )); Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsPatch), v => (LuaCsPatch)( (self, args) => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, self, args)) ));
#if CLIENT #if CLIENT
RegisterAction<float>(); RegisterAction<float>();
@@ -9,9 +9,9 @@ using MoonSharp.Interpreter.Interop;
namespace Barotrauma namespace Barotrauma
{ {
public delegate void CsAction(params object[] args); public delegate void LuaCsAction(params object[] args);
public delegate object CsFunc(params object[] args); public delegate object LuaCsFunc(params object[] args);
public delegate object CsPatch(object self, params object[] args); public delegate object LuaCsPatch(object self, object args);
public class LuaCsHook public class LuaCsHook
{ {
@@ -37,9 +37,9 @@ namespace Barotrauma
{ {
public string name; public string name;
public string hookName; public string hookName;
public CsFunc func; public LuaCsFunc func;
public LuaCsHookCallback(string name, string hookName, CsFunc func) public LuaCsHookCallback(string name, string hookName, LuaCsFunc func)
{ {
this.name = name; this.name = name;
this.hookName = hookName; this.hookName = hookName;
@@ -51,10 +51,10 @@ namespace Barotrauma
private Dictionary<string, Dictionary<string, (LuaCsHookCallback, ACsMod)>> hookFunctions; private Dictionary<string, Dictionary<string, (LuaCsHookCallback, ACsMod)>> hookFunctions;
private Dictionary<long, HashSet<(string, CsPatch, ACsMod)>> hookPrefixMethods; private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPrefixMethods;
private Dictionary<long, HashSet<(string, CsPatch, ACsMod)>> hookPostfixMethods; private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPostfixMethods;
private Queue<(float, CsAction, object[])> queuedFunctionCalls; private Queue<(float, LuaCsAction, object[])> queuedFunctionCalls;
private static LuaCsHook instance; private static LuaCsHook instance;
@@ -63,10 +63,10 @@ namespace Barotrauma
hookFunctions = new Dictionary<string, Dictionary<string, (LuaCsHookCallback, ACsMod)>>(); hookFunctions = new Dictionary<string, Dictionary<string, (LuaCsHookCallback, ACsMod)>>();
hookPrefixMethods = new Dictionary<long, HashSet<(string, CsPatch, ACsMod)>>(); hookPrefixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>();
hookPostfixMethods = new Dictionary<long, HashSet<(string, CsPatch, ACsMod)>>(); hookPostfixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>();
queuedFunctionCalls = new Queue<(float, CsAction, object[])>(); queuedFunctionCalls = new Queue<(float, LuaCsAction, object[])>();
} }
public void Initialize() public void Initialize()
@@ -85,7 +85,7 @@ namespace Barotrauma
try try
{ {
var funcAddr = ((long)__originalMethod.MethodHandle.GetFunctionPointer()); var funcAddr = ((long)__originalMethod.MethodHandle.GetFunctionPointer());
HashSet<(string, CsPatch, ACsMod)> methodSet = null; HashSet<(string, LuaCsPatch, ACsMod)> methodSet = null;
switch (hookMethodType) switch (hookMethodType)
{ {
case HookMethodType.Before: case HookMethodType.Before:
@@ -107,7 +107,7 @@ namespace Barotrauma
args.Add(@params[i].Name, __args[i]); args.Add(@params[i].Name, __args[i]);
} }
var outOfSocpe = new HashSet<(string, CsPatch, ACsMod)>(); var outOfSocpe = new HashSet<(string, LuaCsPatch, ACsMod)>();
foreach (var tuple in methodSet) foreach (var tuple in methodSet)
{ {
if (tuple.Item3 != null && tuple.Item3.IsDisposed) if (tuple.Item3 != null && tuple.Item3.IsDisposed)
@@ -202,7 +202,7 @@ namespace Barotrauma
return methodInfo; return methodInfo;
} }
public void HookMethod(string identifier, MethodInfo method, CsPatch patch, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null) public void HookMethod(string identifier, MethodInfo method, LuaCsPatch patch, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null)
{ {
if (identifier == null || method == null || patch == null) throw new ArgumentNullException("Identifier, Method and Patch arguments must not be null."); if (identifier == null || method == null || patch == null) throw new ArgumentNullException("Identifier, Method and Patch arguments must not be null.");
@@ -226,14 +226,14 @@ namespace Barotrauma
} }
} }
if (hookPrefixMethods.TryGetValue(funcAddr, out HashSet<(string, CsPatch, ACsMod)> methodSet)) if (hookPrefixMethods.TryGetValue(funcAddr, out HashSet<(string, LuaCsPatch, ACsMod)> methodSet))
{ {
methodSet.RemoveWhere(tuple => tuple.Item1 == identifier); methodSet.RemoveWhere(tuple => tuple.Item1 == identifier);
methodSet.Add((identifier, patch, owner)); methodSet.Add((identifier, patch, owner));
} }
else if (patch != null) else if (patch != null)
{ {
hookPrefixMethods.Add(funcAddr, new HashSet<(string, CsPatch, ACsMod)>() { (identifier, patch, owner) }); hookPrefixMethods.Add(funcAddr, new HashSet<(string, LuaCsPatch, ACsMod)>() { (identifier, patch, owner) });
} }
} }
@@ -254,32 +254,32 @@ namespace Barotrauma
} }
} }
if (hookPostfixMethods.TryGetValue(funcAddr, out HashSet<(string, CsPatch, ACsMod)> methodSet)) if (hookPostfixMethods.TryGetValue(funcAddr, out HashSet<(string, LuaCsPatch, ACsMod)> methodSet))
{ {
methodSet.RemoveWhere(tuple => tuple.Item1 == identifier); methodSet.RemoveWhere(tuple => tuple.Item1 == identifier);
methodSet.Add((identifier, patch, owner)); methodSet.Add((identifier, patch, owner));
} }
else if (patch != null) else if (patch != null)
{ {
hookPostfixMethods.Add(funcAddr, new HashSet<(string, CsPatch, ACsMod)>() { (identifier, patch, owner) }); hookPostfixMethods.Add(funcAddr, new HashSet<(string, LuaCsPatch, ACsMod)>() { (identifier, patch, owner) });
} }
} }
} }
protected void HookMethod(string identifier, string className, string methodName, string[] parameterNames, CsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) protected void HookMethod(string identifier, string className, string methodName, string[] parameterNames, LuaCsPatch patch, HookMethodType hookMethodType = HookMethodType.Before)
{ {
MethodInfo methodInfo = ResolveMethod("HookMethod", className, methodName, parameterNames); MethodInfo methodInfo = ResolveMethod("HookMethod", className, methodName, parameterNames);
if (methodInfo == null) return; if (methodInfo == null) return;
HookMethod(identifier, methodInfo, patch, hookMethodType); HookMethod(identifier, methodInfo, patch, hookMethodType);
} }
protected void HookMethod(string identifier, string className, string methodName, CsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) => protected void HookMethod(string identifier, string className, string methodName, LuaCsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) =>
HookMethod(identifier, className, methodName, null, patch, hookMethodType); HookMethod(identifier, className, methodName, null, patch, hookMethodType);
protected void HookMethod(string className, string methodName, CsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) => protected void HookMethod(string className, string methodName, LuaCsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) =>
HookMethod("", className, methodName, null, patch, hookMethodType); HookMethod("", className, methodName, null, patch, hookMethodType);
protected void HookMethod(string className, string methodName, string[] parameterNames, CsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) => protected void HookMethod(string className, string methodName, string[] parameterNames, LuaCsPatch patch, HookMethodType hookMethodType = HookMethodType.Before) =>
HookMethod("", className, methodName, parameterNames, patch, hookMethodType); HookMethod("", className, methodName, parameterNames, patch, hookMethodType);
@@ -287,7 +287,7 @@ namespace Barotrauma
{ {
var funcAddr = ((long)method.MethodHandle.GetFunctionPointer()); var funcAddr = ((long)method.MethodHandle.GetFunctionPointer());
Dictionary<long, HashSet<(string, CsPatch, ACsMod)>> methods; Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> methods;
if (hookType == HookMethodType.Before) methods = hookPrefixMethods; if (hookType == HookMethodType.Before) methods = hookPrefixMethods;
else if (hookType == HookMethodType.After) methods = hookPostfixMethods; else if (hookType == HookMethodType.After) methods = hookPostfixMethods;
else throw null; else throw null;
@@ -302,20 +302,20 @@ namespace Barotrauma
} }
public void Enqueue(CsAction action, params object[] args) public void Enqueue(LuaCsAction action, params object[] args)
{ {
queuedFunctionCalls.Enqueue((0, action, args)); queuedFunctionCalls.Enqueue((0, action, args));
} }
public void EnqueueTimed(float time, CsAction action, params object[] args) public void EnqueueTimed(float time, LuaCsAction action, params object[] args)
{ {
queuedFunctionCalls.Enqueue((time, action, args)); queuedFunctionCalls.Enqueue((time, action, args));
} }
protected void EnqueueFunction(CsAction function, params object[] args) => Enqueue(function, args); protected void EnqueueFunction(LuaCsAction function, params object[] args) => Enqueue(function, args);
protected void EnqueueTimedFunction(float time, CsAction function, params object[] args) => EnqueueTimed(time, function, args); protected void EnqueueTimedFunction(float time, LuaCsAction function, params object[] args) => EnqueueTimed(time, function, args);
public void Add(string name, string hookName, CsFunc hook, ACsMod owner = null) public void Add(string name, string hookName, LuaCsFunc hook, ACsMod owner = null)
{ {
name = name.ToLower(); name = name.ToLower();
@@ -354,7 +354,7 @@ namespace Barotrauma
{ {
try try
{ {
if (queuedFunctionCalls.TryPeek(out (float, CsAction, object[]) result)) if (queuedFunctionCalls.TryPeek(out (float, LuaCsAction, object[]) result))
{ {
if (Timing.TotalTime >= result.Item1) if (Timing.TotalTime >= result.Item1)
{ {
@@ -29,7 +29,7 @@ namespace Barotrauma
} }
} }
public void Wait(CsAction action, int millisecondDelay) public void Wait(LuaCsAction action, int millisecondDelay)
{ {
GameMain.LuaCs.Hook.EnqueueTimed((float)Timing.TotalTime + (millisecondDelay / 1000f), action); GameMain.LuaCs.Hook.EnqueueTimed((float)Timing.TotalTime + (millisecondDelay / 1000f), action);
} }
@@ -232,7 +232,7 @@ namespace Barotrauma
partial class LuaCsNetworking partial class LuaCsNetworking
{ {
public bool restrictMessageSize = true; public bool restrictMessageSize = true;
public Dictionary<string, CsAction> LuaCsNetReceives = new Dictionary<string, CsAction>(); public Dictionary<string, LuaCsAction> LuaCsNetReceives = new Dictionary<string, LuaCsAction>();
#if SERVER #if SERVER
[MoonSharpHidden] [MoonSharpHidden]
@@ -264,7 +264,7 @@ namespace Barotrauma
} }
} }
#endif #endif
public void Receive(string netMessageName, CsAction callback) public void Receive(string netMessageName, LuaCsAction callback)
{ {
LuaCsNetReceives[netMessageName] = callback; LuaCsNetReceives[netMessageName] = callback;
} }
@@ -310,7 +310,7 @@ namespace Barotrauma
} }
#endif #endif
public void RequestPostHTTP(string url, CsAction callback, string data, string contentType = "application/json") public void RequestPostHTTP(string url, LuaCsAction callback, string data, string contentType = "application/json")
{ {
try try
{ {
@@ -342,7 +342,7 @@ namespace Barotrauma
} }
} }
public void RequestGetHTTP(string url, CsAction callback) public void RequestGetHTTP(string url, LuaCsAction callback)
{ {
try try
{ {