Browse Source

Revert "Bring back `HarmonyProtector` and make it more robust"

This reverts commit 6a4d9ec922.
pull/100/head
Meivyn 1 year ago
parent
commit
7851d12b23
No known key found for this signature in database GPG Key ID: 8BDD3E48158B2F71
2 changed files with 0 additions and 86 deletions
  1. +0
    -7
      IPA.Injector/Injector.cs
  2. +0
    -79
      IPA.Loader/Loader/HarmonyProtector.cs

+ 0
- 7
IPA.Injector/Injector.cs View File

@ -137,11 +137,6 @@ namespace IPA.Injector
Loader.LibLoader.Configure();
}
private static void InstallHarmonyProtections()
{ // proxy function to delay resolution
HarmonyProtectorProxy.ProtectNull();
}
private static void InstallBootstrapPatch()
{
var sw = Stopwatch.StartNew();
@ -317,8 +312,6 @@ namespace IPA.Injector
// need to reinit streams singe Unity seems to redirect stdout
StdoutInterceptor.RedirectConsole();
InstallHarmonyProtections();
var bootstrapper = new GameObject("NonDestructiveBootstrapper").AddComponent<Bootstrapper>();
bootstrapper.Destroyed += Bootstrapper_Destroyed;
}


+ 0
- 79
IPA.Loader/Loader/HarmonyProtector.cs View File

@ -1,79 +0,0 @@
using HarmonyLib;
using IPA.Logging;
using MonoMod.RuntimeDetour;
using System;
using System.Reflection;
namespace IPA.Loader
{
internal static class HarmonyProtectorProxy
{
public static void ProtectNull() => HarmonyProtector.Protect();
}
internal static class HarmonyProtector
{
public static void Protect()
{
var guid = Guid.NewGuid().ToString("N");
var id = guid.Remove(new Random().Next(7, guid.Length - 1));
var harmony = new Harmony(id);
var unpatchByTypeOrId = AccessTools.Method(typeof(PatchProcessor), nameof(PatchProcessor.Unpatch), new[] { typeof(HarmonyPatchType), typeof(string) });
var unpatchMethod = AccessTools.Method(typeof(PatchProcessor), nameof(PatchProcessor.Unpatch), new[] { typeof(MethodInfo) });
var processPatchJob = AccessTools.Method(typeof(PatchClassProcessor), "ProcessPatchJob");
var patch = AccessTools.Method(typeof(PatchProcessor), nameof(PatchProcessor.Patch));
var unpatchPrefix = AccessTools.Method(typeof(HarmonyProtector), nameof(PatchProcessor_Unpatch_Prefix));
var processPatchJobPrefix = AccessTools.Method(typeof(HarmonyProtector), nameof(PatchClassProcessor_ProcessPatchJob_Prefix));
var patchPrefix = AccessTools.Method(typeof(HarmonyProtector), nameof(PatchProcessor_Patch_Prefix));
harmony.Patch(unpatchByTypeOrId, new HarmonyMethod(unpatchPrefix));
harmony.Patch(unpatchMethod, new HarmonyMethod(unpatchPrefix));
harmony.Patch(processPatchJob, new HarmonyMethod(processPatchJobPrefix));
harmony.Patch(patch, new HarmonyMethod(patchPrefix));
}
private static bool ShouldBlockExecution(MethodBase methodBase)
{
var getIdentifiable = AccessTools.Method(typeof(DetourHelper), nameof(DetourHelper.GetIdentifiable)).GetIdentifiable();
var getValue = AccessTools.Method(typeof(FieldInfo), nameof(FieldInfo.GetValue)).GetIdentifiable();
var declaringTypeGetter = AccessTools.PropertyGetter(typeof(MemberInfo), nameof(MemberInfo.DeclaringType)).GetIdentifiable();
var methodBaseEquals = AccessTools.Method(typeof(MethodBase), nameof(MethodBase.Equals)).GetIdentifiable();
var assemblyEquals = AccessTools.Method(typeof(Assembly), nameof(Assembly.Equals)).GetIdentifiable();
var assemblyGetter = AccessTools.PropertyGetter(typeof(Type), nameof(Type.Assembly)).GetIdentifiable();
var getExecutingAssembly = AccessTools.Method(typeof(Assembly), nameof(Assembly.GetExecutingAssembly)).GetIdentifiable();
var method = methodBase.GetIdentifiable();
var assembly = method.DeclaringType!.Assembly;
return method.Equals(getIdentifiable) ||
method.Equals(getValue) ||
method.Equals(declaringTypeGetter) ||
method.Equals(methodBaseEquals) ||
method.Equals(assemblyEquals) ||
method.Equals(assemblyGetter) ||
method.Equals(getExecutingAssembly) ||
assembly.Equals(Assembly.GetExecutingAssembly()) ||
assembly.Equals(typeof(Harmony).Assembly);
}
private static bool PatchProcessor_Patch_Prefix(MethodBase ___original, ref MethodInfo __result)
{
if (ShouldBlockExecution(___original))
{
__result = ___original as MethodInfo;
return false;
}
return true;
}
private static bool PatchClassProcessor_ProcessPatchJob_Prefix(object job)
{
var original = AccessTools.Field(job.GetType(), "original");
var methodBase = (MethodBase)original!.GetValue(job);
return !ShouldBlockExecution(methodBase);
}
private static bool PatchProcessor_Unpatch_Prefix(MethodBase ___original) => !ShouldBlockExecution(___original);
}
}

Loading…
Cancel
Save