From f439cabc9a321fe178280c6073afb18f3def1db6 Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Sat, 16 Apr 2022 12:44:50 -0300 Subject: [PATCH] fix patch hooks and rename delegates --- .../LuaCs/Lua/LuaClasses/LuaGame.cs | 2 +- .../LuaCs/Lua/LuaCustomConverters.cs | 6 +- .../SharedSource/LuaCs/LuaCsHook.cs | 58 +++++++++---------- .../SharedSource/LuaCs/LuaCsUtility.cs | 10 ++-- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs index 82af1e3d9..c2f1aee12 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs @@ -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); }, () => diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaCustomConverters.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaCustomConverters.cs index 2e52a2921..cfb0a9984 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaCustomConverters.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaCustomConverters.cs @@ -23,9 +23,9 @@ namespace Barotrauma var function = v.Function; return (Func)((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(CsFunc), v => (CsFunc)( 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(LuaCsAction), v => (LuaCsAction)( args => 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(LuaCsPatch), v => (LuaCsPatch)( (self, args) => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, self, args)) )); #if CLIENT RegisterAction(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs index 11933eee5..741eb4d29 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs @@ -9,9 +9,9 @@ using MoonSharp.Interpreter.Interop; namespace Barotrauma { - public delegate void CsAction(params object[] args); - public delegate object CsFunc(params object[] args); - public delegate object CsPatch(object self, params object[] args); + public delegate void LuaCsAction(params object[] args); + public delegate object LuaCsFunc(params object[] args); + public delegate object LuaCsPatch(object self, object args); public class LuaCsHook { @@ -37,9 +37,9 @@ namespace Barotrauma { public string name; 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.hookName = hookName; @@ -51,10 +51,10 @@ namespace Barotrauma private Dictionary> hookFunctions; - private Dictionary> hookPrefixMethods; - private Dictionary> hookPostfixMethods; + private Dictionary> hookPrefixMethods; + private Dictionary> hookPostfixMethods; - private Queue<(float, CsAction, object[])> queuedFunctionCalls; + private Queue<(float, LuaCsAction, object[])> queuedFunctionCalls; private static LuaCsHook instance; @@ -63,10 +63,10 @@ namespace Barotrauma hookFunctions = new Dictionary>(); - hookPrefixMethods = new Dictionary>(); - hookPostfixMethods = new Dictionary>(); + hookPrefixMethods = new Dictionary>(); + hookPostfixMethods = new Dictionary>(); - queuedFunctionCalls = new Queue<(float, CsAction, object[])>(); + queuedFunctionCalls = new Queue<(float, LuaCsAction, object[])>(); } public void Initialize() @@ -85,7 +85,7 @@ namespace Barotrauma try { var funcAddr = ((long)__originalMethod.MethodHandle.GetFunctionPointer()); - HashSet<(string, CsPatch, ACsMod)> methodSet = null; + HashSet<(string, LuaCsPatch, ACsMod)> methodSet = null; switch (hookMethodType) { case HookMethodType.Before: @@ -107,7 +107,7 @@ namespace Barotrauma 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) { if (tuple.Item3 != null && tuple.Item3.IsDisposed) @@ -202,7 +202,7 @@ namespace Barotrauma 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."); @@ -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.Add((identifier, patch, owner)); } 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.Add((identifier, patch, owner)); } 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); if (methodInfo == null) return; 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); - 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); - 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); @@ -287,7 +287,7 @@ namespace Barotrauma { var funcAddr = ((long)method.MethodHandle.GetFunctionPointer()); - Dictionary> methods; + Dictionary> methods; if (hookType == HookMethodType.Before) methods = hookPrefixMethods; else if (hookType == HookMethodType.After) methods = hookPostfixMethods; 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)); } - 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)); } - protected void EnqueueFunction(CsAction function, params object[] args) => Enqueue(function, args); - protected void EnqueueTimedFunction(float time, CsAction function, params object[] args) => EnqueueTimed(time, function, args); + protected void EnqueueFunction(LuaCsAction function, params object[] args) => Enqueue(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(); @@ -354,7 +354,7 @@ namespace Barotrauma { try { - if (queuedFunctionCalls.TryPeek(out (float, CsAction, object[]) result)) + if (queuedFunctionCalls.TryPeek(out (float, LuaCsAction, object[]) result)) { if (Timing.TotalTime >= result.Item1) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs index 15560fb78..26d887eb6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs @@ -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); } @@ -232,7 +232,7 @@ namespace Barotrauma partial class LuaCsNetworking { public bool restrictMessageSize = true; - public Dictionary LuaCsNetReceives = new Dictionary(); + public Dictionary LuaCsNetReceives = new Dictionary(); #if SERVER [MoonSharpHidden] @@ -264,7 +264,7 @@ namespace Barotrauma } } #endif - public void Receive(string netMessageName, CsAction callback) + public void Receive(string netMessageName, LuaCsAction callback) { LuaCsNetReceives[netMessageName] = callback; } @@ -310,7 +310,7 @@ namespace Barotrauma } #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 { @@ -342,7 +342,7 @@ namespace Barotrauma } } - public void RequestGetHTTP(string url, CsAction callback) + public void RequestGetHTTP(string url, LuaCsAction callback) { try {