3 using System.Reflection;
5 using System.Collections.Generic;
6 using MoonSharp.Interpreter;
7 using LuaCsCompatPatchFunc =
Barotrauma.LuaCsPatch;
12 public delegate
object LuaCsPatch(
object self, Dictionary<string, object> args);
16 private Dictionary<long, HashSet<(string, LuaCsCompatPatchFunc,
ACsMod)>> compatHookPrefixMethods =
new Dictionary<
long, HashSet<(
string, LuaCsCompatPatchFunc,
ACsMod)>>();
17 private Dictionary<long, HashSet<(string, LuaCsCompatPatchFunc,
ACsMod)>> compatHookPostfixMethods =
new Dictionary<
long, HashSet<(
string, LuaCsCompatPatchFunc,
ACsMod)>>();
19 private static void _hookLuaCsPatch(MethodBase __originalMethod,
object[] __args,
object __instance, out
object result,
HookMethodType hookType)
25 var funcAddr = ((long)__originalMethod.MethodHandle.GetFunctionPointer());
26 HashSet<(string, LuaCsCompatPatchFunc,
ACsMod)> methodSet =
null;
30 instance.compatHookPrefixMethods.TryGetValue(funcAddr, out methodSet);
33 instance.compatHookPostfixMethods.TryGetValue(funcAddr, out methodSet);
36 throw new ArgumentException($
"Invalid {nameof(HookMethodType)} enum value.", nameof(hookType));
39 if (methodSet !=
null)
41 var @params = __originalMethod.GetParameters();
42 var args =
new Dictionary<string, object>();
43 for (
int i = 0; i < @params.Length; i++)
45 args.Add(@params[i].Name, __args[i]);
48 var outOfSocpe =
new HashSet<(string, LuaCsCompatPatchFunc, ACsMod)>();
49 foreach (var tuple
in methodSet)
51 if (tuple.Item3 !=
null && tuple.Item3.IsDisposed)
53 outOfSocpe.Add(tuple);
57 var _result = tuple.Item2(__instance, args);
60 if (_result is DynValue res)
64 if (__originalMethod is MethodInfo mi && mi.ReturnType != typeof(
void))
66 result = res.ToObject(mi.ReturnType);
70 result = res.ToObject();
81 foreach (var tuple
in outOfSocpe) { methodSet.Remove(tuple); }
86 LuaCsLogger.LogError($
"Error in {__originalMethod.Name}:", LuaCsMessageOrigin.Unknown);
87 LuaCsLogger.HandleException(ex, LuaCsMessageOrigin.Unknown);
92 private static bool HookLuaCsPatchPrefix(MethodBase __originalMethod,
object[] __args,
object __instance)
94 _hookLuaCsPatch(__originalMethod, __args, __instance, out
object result,
HookMethodType.Before);
95 return result ==
null;
98 private static void HookLuaCsPatchPostfix(MethodBase __originalMethod,
object[] __args,
object __instance) =>
99 _hookLuaCsPatch(__originalMethod, __args, __instance, out
object _,
HookMethodType.After);
101 private static bool HookLuaCsPatchRetPrefix(MethodBase __originalMethod,
object[] __args, ref
object __result,
object __instance)
103 _hookLuaCsPatch(__originalMethod, __args, __instance, out
object result,
HookMethodType.Before);
112 private static void HookLuaCsPatchRetPostfix(MethodBase __originalMethod,
object[] __args, ref
object __result,
object __instance)
114 _hookLuaCsPatch(__originalMethod, __args, __instance, out
object result,
HookMethodType.After);
115 if (result !=
null) __result = result;
118 private static MethodInfo _miHookLuaCsPatchPrefix = typeof(LuaCsHook).GetMethod(
"HookLuaCsPatchPrefix", BindingFlags.NonPublic | BindingFlags.Static);
119 private static MethodInfo _miHookLuaCsPatchPostfix = typeof(LuaCsHook).GetMethod(
"HookLuaCsPatchPostfix", BindingFlags.NonPublic | BindingFlags.Static);
120 private static MethodInfo _miHookLuaCsPatchRetPrefix = typeof(LuaCsHook).GetMethod(
"HookLuaCsPatchRetPrefix", BindingFlags.NonPublic | BindingFlags.Static);
121 private static MethodInfo _miHookLuaCsPatchRetPostfix = typeof(LuaCsHook).GetMethod(
"HookLuaCsPatchRetPostfix", BindingFlags.NonPublic | BindingFlags.Static);
126 if (identifier ==
null || method ==
null || patch ==
null)
128 LuaCsLogger.
HandleException(
new ArgumentNullException(
"Identifier, Method and Patch arguments must not be null."), LuaCsMessageOrigin.Unknown);
131 ValidatePatchTarget(method);
133 var funcAddr = ((long)method.MethodHandle.GetFunctionPointer());
134 var patches = Harmony.GetPatchInfo(method);
138 if (method is MethodInfo mi && mi.ReturnType != typeof(
void))
140 if (patches ==
null || patches.Prefixes ==
null || patches.Prefixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchRetPrefix) ==
null)
142 harmony.Patch(method, prefix:
new HarmonyMethod(_miHookLuaCsPatchRetPrefix));
147 if (patches ==
null || patches.Prefixes ==
null || patches.Prefixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchPrefix) ==
null)
149 harmony.Patch(method, prefix:
new HarmonyMethod(_miHookLuaCsPatchPrefix));
153 if (compatHookPrefixMethods.TryGetValue(funcAddr, out HashSet<(
string, LuaCsCompatPatchFunc,
ACsMod)> methodSet))
155 if (identifier !=
"")
157 methodSet.RemoveWhere(tuple => tuple.Item1 == identifier);
160 methodSet.Add((identifier, patch, owner));
162 else if (patch !=
null)
164 compatHookPrefixMethods.Add(funcAddr,
new HashSet<(
string, LuaCsCompatPatchFunc,
ACsMod)>() { (identifier, patch, owner) });
170 if (method is MethodInfo mi && mi.ReturnType != typeof(
void))
172 if (patches ==
null || patches.Postfixes ==
null || patches.Postfixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchRetPostfix) ==
null)
174 harmony.Patch(method, postfix:
new HarmonyMethod(_miHookLuaCsPatchRetPostfix));
179 if (patches ==
null || patches.Postfixes ==
null || patches.Postfixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchPostfix) ==
null)
181 harmony.Patch(method, postfix:
new HarmonyMethod(_miHookLuaCsPatchPostfix));
185 if (compatHookPostfixMethods.TryGetValue(funcAddr, out HashSet<(
string, LuaCsCompatPatchFunc,
ACsMod)> methodSet))
187 if (identifier !=
"")
189 methodSet.RemoveWhere(tuple => tuple.Item1 == identifier);
192 methodSet.Add((identifier, patch, owner));
194 else if (patch !=
null)
196 compatHookPostfixMethods.Add(funcAddr,
new HashSet<(
string, LuaCsCompatPatchFunc,
ACsMod)>() { (identifier, patch, owner) });
202 var method = ResolveMethod(className, methodName, parameterNames);
203 if (method ==
null)
return;
204 if (method.GetParameters().Any(x => x.ParameterType.IsByRef))
206 throw new InvalidOperationException($
"{nameof(HookMethod)} doesn't support ByRef parameters; use {nameof(Patch)} instead.");
208 HookMethod(identifier, method, patch, hookMethodType);
211 HookMethod(identifier, className, methodName,
null, patch, hookMethodType);
213 HookMethod(
"", className, methodName,
null, patch, hookMethodType);
215 HookMethod(
"", className, methodName, parameterNames, patch, hookMethodType);
220 var funcAddr = (long)method.MethodHandle.GetFunctionPointer();
222 Dictionary<long, HashSet<(string, LuaCsCompatPatchFunc,
ACsMod)>> methods;
223 if (hookType ==
HookMethodType.Before) methods = compatHookPrefixMethods;
224 else if (hookType ==
HookMethodType.After) methods = compatHookPostfixMethods;
227 if (methods.ContainsKey(funcAddr)) methods[funcAddr]?.RemoveWhere(t => t.Item1 == identifier);
231 var method = ResolveMethod(className, methodName, parameterNames);
232 if (method ==
null)
return;
void HookMethod(string identifier, string className, string methodName, string[] parameterNames, LuaCsCompatPatchFunc patch, HookMethodType hookMethodType=HookMethodType.Before)
void HookMethod(string identifier, MethodBase method, LuaCsCompatPatchFunc patch, HookMethodType hookType=HookMethodType.Before, ACsMod owner=null)
void HookMethod(string className, string methodName, string[] parameterNames, LuaCsCompatPatchFunc patch, HookMethodType hookMethodType=HookMethodType.Before)
void HookMethod(string className, string methodName, LuaCsCompatPatchFunc patch, HookMethodType hookMethodType=HookMethodType.Before)
void HookMethod(string identifier, string className, string methodName, LuaCsCompatPatchFunc patch, HookMethodType hookMethodType=HookMethodType.Before)
void UnhookMethod(string identifier, string className, string methodName, string[] parameterNames, HookMethodType hookType=HookMethodType.Before)
void UnhookMethod(string identifier, MethodBase method, HookMethodType hookType=HookMethodType.Before)
static void HandleException(Exception ex, LuaCsMessageOrigin origin)
delegate object LuaCsPatch(object self, Dictionary< string, object > args)