Skip to content

Commit

Permalink
re-enable try hook last resort
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Aug 15, 2024
1 parent 7baa4fc commit 8f5f6ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
18 changes: 8 additions & 10 deletions UniTAS/Patcher/Patches/Preloader/UnityInitInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ public UnityInitInvoke()
TargetDLLs = targetDLLs;

// why can't I even load config with BepInEx's own ConfigFile????
var targetClass = ConfigUtils.GetEntryKey(bepInExConfig, entryPoint, "Type") ??
// better late than never
"UnityEngine.Camera";
var targetMethod = ConfigUtils.GetEntryKey(bepInExConfig, entryPoint, "Method") ?? ".cctor";
_targetClass = ConfigUtils.GetEntryKey(bepInExConfig, entryPoint, "Type") ??
// better late than never
"UnityEngine.Camera";
_targetMethod = ConfigUtils.GetEntryKey(bepInExConfig, entryPoint, "Method") ?? ".cctor";
StaticLogger.Log.LogInfo(
$"UniTAS will be hooked on {targetClass}.{targetMethod} in {string.Join(", ", targetDLLs)} as a last resort init hook");
$"UniTAS will be hooked on {_targetClass}.{_targetMethod} in {string.Join(", ", targetDLLs)} as a last resort init hook");
}

// private readonly string _targetClass;
// private readonly string _targetMethod;
private readonly string _targetClass;
private readonly string _targetMethod;

public override void Patch(ref AssemblyDefinition assembly)
{
TryHookAwakes(assembly);
// TryHookRuntimeInits(assembly);
// this probably interferes with actual loading since it could initialise too early
// TryHookLastResort(assembly);
TryHookLastResort(assembly);
}

// doing this seems to fail so Awake hooks are enough
Expand Down Expand Up @@ -91,7 +91,6 @@ private static void TryHookAwakes(AssemblyDefinition assembly)
}
}

/*
private void TryHookLastResort(AssemblyDefinition assembly)
{
StaticLogger.Log.LogDebug("Trying to hook last resort init method defined in BepInEx config");
Expand All @@ -117,7 +116,6 @@ private void TryHookLastResort(AssemblyDefinition assembly)
ILCodeUtils.MethodInvokeHook(assembly, method,
AccessTools.Method(typeof(InvokeTracker), nameof(InvokeTracker.OnUnityInit)));
}
*/

private static void LogHook(AssemblyDefinition assembly, string type, string method)
{
Expand Down
16 changes: 8 additions & 8 deletions UniTAS/Patcher/Utils/ILCodeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace UniTAS.Patcher.Utils;

public static class ILCodeUtils
{
// public static void MethodInvokeHookOnCctor(AssemblyDefinition assembly, TypeDefinition type, MethodBase method)
// {
// if (type == null) return;
//
// var staticCtor = FindOrAddCctor(assembly, type);
// MethodInvokeHook(assembly, staticCtor, method);
// }
public static void MethodInvokeHookOnCctor(AssemblyDefinition assembly, TypeDefinition type, MethodBase method)
{
if (type == null) return;

var staticCtor = FindOrAddCctor(assembly, type);
MethodInvokeHook(assembly, staticCtor, method);
}

public static void MethodInvokeHook(AssemblyDefinition assembly, MethodDefinition methodDefinition,
MethodBase method)
Expand Down Expand Up @@ -62,4 +62,4 @@ public static MethodDefinition FindOrAddCctor(AssemblyDefinition assembly, TypeD
il.Append(il.Create(OpCodes.Ret));
return staticCtor;
}
}
}

0 comments on commit 8f5f6ba

Please sign in to comment.