Browse Source

Added protection from Harmony

pull/32/head
Anairkoen Schno 5 years ago
parent
commit
f589a41d82
4 changed files with 50 additions and 3 deletions
  1. +2
    -0
      IPA.Injector/Injector.cs
  2. +1
    -0
      IPA.Loader/IPA.Loader.csproj
  3. +44
    -0
      IPA.Loader/Loader/HarmonyProtector.cs
  4. +3
    -3
      IPA.Loader/Logging/StdoutInterceptor.cs

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

@ -96,6 +96,8 @@ namespace IPA.Injector
GameVersionEarly.Load();
HarmonyProtector.Protect();
pluginAsyncLoadTask = PluginLoader.LoadTask();
permissionFixTask = PermissionFix.FixPermissions(new DirectoryInfo(Environment.CurrentDirectory));
}


+ 1
- 0
IPA.Loader/IPA.Loader.csproj View File

@ -98,6 +98,7 @@
<Compile Include="Loader\Features\InitInjectorFeature.cs" />
<Compile Include="Loader\Features\NoUpdateFeature.cs" />
<Compile Include="Loader\Features\PrintFeature.cs" />
<Compile Include="Loader\HarmonyProtector.cs" />
<Compile Include="Loader\PluginInitInjector.cs" />
<Compile Include="Loader\LibLoader.cs" />
<Compile Include="Loader\Features\Feature.cs" />


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

@ -0,0 +1,44 @@
using Harmony;
using System.Collections.Generic;
using System.Reflection;
namespace IPA.Loader
{
internal static class HarmonyProtector
{
private static HarmonyInstance instance;
private static Assembly selfAssem;
private static Assembly harmonyAssem;
public static void Protect(HarmonyInstance inst = null)
{
selfAssem = Assembly.GetExecutingAssembly();
harmonyAssem = typeof(HarmonyInstance).Assembly;
if (inst == null)
{
if (instance == null)
instance = HarmonyInstance.Create("BSIPA Safeguard");
inst = instance;
}
var target = typeof(PatchProcessor).GetMethod("Patch");
var patch = typeof(HarmonyProtector).GetMethod(nameof(PatchProcessor_Patch_Prefix));
inst.Patch(target, new HarmonyMethod(patch));
}
private static void PatchProcessor_Patch_Prefix(PatchProcessor __instance, ref List<MethodBase> ___originals)
{
for (int i = 0; i < ___originals.Count; i++)
{
var mi = ___originals[i];
var asm = mi.DeclaringType.Assembly;
if (asm.Equals(selfAssem) || asm.Equals(harmonyAssem))
___originals.RemoveAt(i--);
}
}
}
}

+ 3
- 3
IPA.Loader/Logging/StdoutInterceptor.cs View File

@ -125,11 +125,11 @@ namespace IPA.Logging
var getFg = foregroundProperty?.GetGetMethod();
if (resetColor != null)
harmony.Patch(resetColor, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchResetColor"));
harmony.Patch(resetColor, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchResetColor)));
if (foregroundProperty != null)
{
harmony.Patch(setFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchSetForegroundColor"));
harmony.Patch(getFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchGetForegroundColor"));
harmony.Patch(setFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchSetForegroundColor)));
harmony.Patch(getFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchGetForegroundColor)));
}
}


Loading…
Cancel
Save