hook fixes, keybinds are not working

This commit is contained in:
Oiltanker
2022-04-15 20:17:35 +03:00
parent 3eba20ecb7
commit 2f0e83badd
3 changed files with 48 additions and 44 deletions
@@ -102,10 +102,10 @@ namespace Barotrauma
return null; return null;
} }
public static implicit operator bool(LuaResult res) => res.Bool(); //public static implicit operator bool(LuaResult res) => res.Bool();
public static implicit operator float(LuaResult res) => res.Float(); //public static implicit operator float(LuaResult res) => res.Float();
public static implicit operator string(LuaResult res) => res.String(); //public static implicit operator string(LuaResult res) => res.String();
public static implicit operator double(LuaResult res) => res.Double(); //public static implicit operator double(LuaResult res) => res.Double();
public static implicit operator DynValue(LuaResult res) => res.DynValue(); //public static implicit operator DynValue(LuaResult res) => res.DynValue();
} }
} }
@@ -23,10 +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(Closure), v => v.Function);
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(CsAction), v => (CsAction)( args => GameMain.LuaCs.CallLuaFunction(v.Function, args) )); 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)).Object() )); 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)).Object() )); Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(CsPatch), v => (CsPatch)( (self, args) => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, self, args)) ));
#if CLIENT #if CLIENT
RegisterAction<float>(); RegisterAction<float>();
@@ -67,8 +67,9 @@ namespace Barotrauma
static LuaCsHook() => _inst = new LuaCsHook(); static LuaCsHook() => _inst = new LuaCsHook();
public static LuaCsHook Instance { get => _inst; } public static LuaCsHook Instance { get => _inst; }
private static void _hookLuaCsPatch(MethodBase __originalMethod, object[] __args, object __instance, ref object result, HookMethodType hookMethodType) private static void _hookLuaCsPatch(MethodBase __originalMethod, object[] __args, object __instance, out object result, HookMethodType hookMethodType)
{ {
result = null;
#if CLIENT #if CLIENT
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer) if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
return; return;
@@ -106,8 +107,18 @@ namespace Barotrauma
else else
{ {
var _result = tuple.Item2(__instance, args); var _result = tuple.Item2(__instance, args);
if (_result is LuaResult res && !res.IsNull()) result = _result; if (_result != null)
else if (_result != null) result = _result; {
if (_result is LuaResult res)
{
if (!res.IsNull())
{
if (__originalMethod is MethodInfo mi) result = res.DynValue().ToObject(mi.ReturnType);
else result = res.DynValue().ToObject();
}
}
else result = _result;
}
} }
} }
foreach (var tuple in outOfSocpe) methodSet.Remove(tuple); foreach (var tuple in outOfSocpe) methodSet.Remove(tuple);
@@ -122,45 +133,26 @@ namespace Barotrauma
private static bool HookLuaCsPatchPrefix(MethodBase __originalMethod, object[] __args, object __instance) private static bool HookLuaCsPatchPrefix(MethodBase __originalMethod, object[] __args, object __instance)
{ {
object result = null; _hookLuaCsPatch(__originalMethod, __args, __instance, out object result, HookMethodType.Before);
_hookLuaCsPatch(__originalMethod, __args, __instance, ref result, HookMethodType.Before); return result == null;
if (result != null)
{
if (result is LuaResult res) return res.IsNull();
return false;
}
else return true;
}
private static void HookLuaCsPatchPostfix(MethodBase __originalMethod, object[] __args, object __instance)
{
object result = null;
_hookLuaCsPatch(__originalMethod, __args, __instance, ref result, HookMethodType.After);
} }
private static void HookLuaCsPatchPostfix(MethodBase __originalMethod, object[] __args, object __instance) =>
_hookLuaCsPatch(__originalMethod, __args, __instance, out object _, HookMethodType.After);
private static bool HookLuaCsPatchRetPrefix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance) private static bool HookLuaCsPatchRetPrefix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance)
{ {
_hookLuaCsPatch(__originalMethod, __args, __instance, ref __result, HookMethodType.Before); _hookLuaCsPatch(__originalMethod, __args, __instance, out object result, HookMethodType.Before);
if (__result != null) if (result != null)
{ {
if (__result is LuaResult res) __result = result;
{
if (!res.IsNull() && __originalMethod is MethodInfo mi) __result = res.DynValue().ToObject(mi.ReturnType);
else __result = res.Object();
}
return false; return false;
} }
else return true; else return true;
} }
private static void HookLuaCsPatchRetPostfix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance) private static void HookLuaCsPatchRetPostfix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance)
{ {
_hookLuaCsPatch(__originalMethod, __args, __instance, ref __result, HookMethodType.After); _hookLuaCsPatch(__originalMethod, __args, __instance, out object result, HookMethodType.After);
if (__result != null) if (result != null) __result = result;
{
if (__result is LuaResult res)
{
if (!res.IsNull() && __originalMethod is MethodInfo mi) __result = res.DynValue().ToObject(mi.ReturnType);
else __result = res.Object();
}
}
} }
@@ -204,6 +196,7 @@ namespace Barotrauma
public void HookMethod(string identifier, MethodInfo method, CsPatch patch, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null) public void HookMethod(string identifier, MethodInfo method, CsPatch patch, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null)
{ {
Console.WriteLine($" --== '{identifier}' {method.ReflectedType.Name}.{method.Name} -> {method.ReturnType.Name} | {hookType.ToString("G")}");
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.");
var funcAddr = ((long)method.MethodHandle.GetFunctionPointer()); var funcAddr = ((long)method.MethodHandle.GetFunctionPointer());
@@ -371,10 +364,12 @@ namespace Barotrauma
{ {
if ( if (
typeof(T) != typeof(object) && typeof(T) != typeof(object) &&
!name.StartsWith("think") &&
!name.StartsWith("gapOxygenUpdate") && !name.StartsWith("gapOxygenUpdate") &&
!name.StartsWith("signal") &&
!name.StartsWith("statusEffect") !name.StartsWith("statusEffect")
) Console.WriteLine($" --== '{name}'"); )
{
}
#if CLIENT #if CLIENT
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer) if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
//return null; //return null;
@@ -407,8 +402,18 @@ namespace Barotrauma
try try
{ {
var result = tuple.Item1.func(args); var result = tuple.Item1.func(args);
if (result is LuaResult lRes && !lRes.IsNull()) lastResult = lRes.DynValue().ToObject<T>(); if (result != null)
else if (result != null && result is T cRes) lastResult = cRes; {
if (typeof(object) != typeof(T))
{
if (result is LuaResult lRes)
{
if (!lRes.IsNull()) lastResult = lRes.DynValue().ToObject<T>();
}
else if (result is T cRes && cRes != null) lastResult = cRes;
}
else if (result is T res && res != null) lastResult = res;
}
} }
catch (Exception e) catch (Exception e)
{ {