Skip to content

Commit

Permalink
fix: Some issues with hook changes
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Dec 16, 2024
1 parent 2436d3f commit 7c3bcc7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static void Initialize() {
}

if (ModUtils.GetModule("DeadzoneConfig")?.GetType() is { } deadzoneConfigModuleType) {
HookHelper.SkipMethod(typeof(DesyncFixer), nameof(SkipDeadzoneConfig), deadzoneConfigModuleType.GetMethod("OnInputInitialize"));
deadzoneConfigModuleType.GetMethod("OnInputInitialize").SkipMethod(SkipDeadzoneConfig);
}

if (ModUtils.GetType("StrawberryJam2021", "Celeste.Mod.StrawberryJam2021.Entities.CustomAscendManager") is { } ascendManagerType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TAS.EverestInterop;
public static class DisableRumble {
[Load]
private static void Load() {
HookHelper.SkipMethod(typeof(DisableRumble), nameof(IsDisableRumble), typeof(MInput.GamePadData).GetMethodInfo("Rumble"));
typeof(MInput.GamePadData).GetMethodInfo("Rumble").SkipMethod(IsDisableRumble);
}

private static bool IsDisableRumble() {
Expand Down
40 changes: 3 additions & 37 deletions CelesteTAS-EverestInterop/Source/Utils/HookHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static void OverrideReturn<T>(this MethodInfo method, Func<bool> conditio
}

/// Creates a callback to conditionally override the return value of the original method without ever even calling it
public static void OverrideReturn<T>(this MethodInfo method, Func<bool> condition, Action<T> valueProvider) {
public static void OverrideReturn<T>(this MethodInfo method, Func<bool> condition, Func<T> valueProvider) {
#if DEBUG
Debug.Assert(method.ReturnType == typeof(T));
#endif
Expand All @@ -176,58 +176,24 @@ public static void OverrideReturn<T>(this MethodInfo method, Func<bool> conditio

// Put the return value onto the stack
cursor.EmitDelegate(valueProvider);

cursor.EmitRet();
});
}

/// Creates a callback to conditionally override the return value of the original methods without ever even calling them
public static void OverrideReturns<T>(Func<bool> condition, T value, [ItemCanBeNull] params MethodInfo[] methods) {
public static void OverrideReturns<T>(Func<bool> condition, T value, params MethodInfo?[] methods) {
foreach (var method in methods) {
method?.OverrideReturn(condition, value);
}
}

/// Creates a callback to conditionally override the return value of the original methods without ever even calling them
public static void OverrideReturns<T>(Func<bool> condition, Action<T> valueProvider, [ItemCanBeNull] params MethodInfo[] methods) {
public static void OverrideReturns<T>(Func<bool> condition, Func<T> valueProvider, params MethodInfo?[] methods) {
foreach (var method in methods) {
method?.OverrideReturn(condition, valueProvider);
}
}

public static void SkipMethod(Type conditionType, string conditionMethodName, string methodName, params Type[] types) {
foreach (Type type in types) {
if (type?.GetMethodInfo(methodName) is { } method) {
SkipMethod(conditionType, conditionMethodName, method);
}
}
}

public static void SkipMethod(Type conditionType, string conditionMethodName, params MethodInfo[] methodInfos) {
foreach (MethodInfo methodInfo in methodInfos) {
methodInfo.IlHook(il => {
ILCursor ilCursor = new(il);
Instruction start = ilCursor.Next;
ilCursor.Emit(OpCodes.Call, conditionType.GetMethodInfo(conditionMethodName));
ilCursor.Emit(OpCodes.Brfalse, start).Emit(OpCodes.Ret);
});
}
}

public static void ReturnZeroMethod(Type conditionType, string conditionMethodName, params MethodInfo[] methods) {
foreach (MethodInfo methodInfo in methods) {
if (methodInfo != null && !methodInfo.IsGenericMethod && methodInfo.DeclaringType?.IsGenericType != true &&
methodInfo.ReturnType == typeof(float)) {
methodInfo.IlHook(il => {
ILCursor ilCursor = new(il);
Instruction start = ilCursor.Next;
ilCursor.Emit(OpCodes.Call, conditionType.GetMethodInfo(conditionMethodName));
ilCursor.Emit(OpCodes.Brfalse, start).Emit(OpCodes.Ldc_R4, 0f).Emit(OpCodes.Ret);
});
}
}
}

/// Emits a call to a static delegate function.
/// Accessing captures is not allowed
public static void EmitStaticDelegate<T>(this ILCursor cursor, T cb) where T : Delegate
Expand Down

0 comments on commit 7c3bcc7

Please sign in to comment.