Decompiled source of Mimic Patcher v1.0.0

BepInEx/plugins/MimicPatcher.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("MimicPatcher")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MimicPatcher")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7f3bfb2e-7d53-49cc-87b4-b77db6ad4801")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MimicsPatcher;

[BepInPlugin("com.randomlygenerated.MimicsPatcher", "3.0.0-Final", "3.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Applying surgical patch for Mimics mod...");
		Harmony val = new Harmony("com.randomlygenerated.MimicsPatcher.Surgical");
		try
		{
			MethodInfo methodInfo = Type.GetType("EnemyDirector, Assembly-CSharp")?.GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Could not find EnemyDirector.Start() method. Aborting.");
				return;
			}
			Patches patchInfo = Harmony.GetPatchInfo((MethodBase)methodInfo);
			Patch val2 = ((IEnumerable<Patch>)patchInfo.Postfixes).FirstOrDefault((Func<Patch, bool>)((Patch p) => p.PatchMethod.DeclaringType.Assembly.GetName().Name == "Mimics"));
			if (val2 != null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Found a potentially conflicting Mimics patch: " + val2.PatchMethod.Name + ". Removing it..."));
				val.Unpatch((MethodBase)methodInfo, val2.PatchMethod);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Successfully removed conflicting patch.");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find the conflicting Mimics patch. The game might still crash if it exists.");
			}
			val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(MimicsCorrectPatch), "ExecuteMimicsLogic", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Surgical patch for Mimics has been successfully applied!");
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to apply surgical patch: {arg}");
		}
	}
}
public static class MimicsCorrectPatch
{
	public static void ExecuteMimicsLogic(object __instance)
	{
		ManualLogSource val = Logger.CreateLogSource("MimicsCorrectPatch");
		val.LogInfo((object)"Executing replacement logic for Mimics...");
		try
		{
			Type type = Type.GetType("Mimics.patches.EnemyDirectorStartPatch, Mimics");
			FieldInfo fieldInfo = type?.GetField("setupComplete", BindingFlags.Static | BindingFlags.NonPublic);
			FieldInfo fieldInfo2 = type?.GetField("filterEnemies", BindingFlags.Static | BindingFlags.Public);
			MethodInfo methodInfo = type?.GetMethod("SetupEnemyConfig", BindingFlags.Static | BindingFlags.NonPublic);
			if (fieldInfo == null || fieldInfo2 == null || methodInfo == null)
			{
				val.LogError((object)"Could not find required fields/methods in Mimics' patch type. Cannot continue.");
			}
			else
			{
				if ((bool)fieldInfo.GetValue(null))
				{
					return;
				}
				Type type2 = __instance.GetType();
				Type type3 = Type.GetType("PrefabRef, Assembly-CSharp");
				Type type4 = Type.GetType("EnemySetup, Assembly-CSharp");
				FieldInfo field = type2.GetField("enemiesDifficulty1");
				FieldInfo field2 = type2.GetField("enemiesDifficulty2");
				FieldInfo field3 = type2.GetField("enemiesDifficulty3");
				FieldInfo field4 = type4.GetField("spawnObjects");
				PropertyInfo property = type3.GetProperty("Prefab");
				object[] array = new object[3]
				{
					field.GetValue(__instance),
					field2.GetValue(__instance),
					field3.GetValue(__instance)
				};
				HashSet<string> hashSet = new HashSet<string>();
				object[] array2 = array;
				foreach (object obj in array2)
				{
					if (!(obj is IList list))
					{
						continue;
					}
					foreach (object item in list)
					{
						if (!(field4.GetValue(item) is IList list2) || list2.Count == 0)
						{
							continue;
						}
						object obj2 = list2[0];
						object? value = property.GetValue(obj2);
						GameObject val2 = (GameObject)((value is GameObject) ? value : null);
						if ((Object)(object)val2 == (Object)null)
						{
							continue;
						}
						if (((Object)val2).name.Contains("Director") && list2.Count > 1)
						{
							object obj3 = list2[1];
							object? value2 = property.GetValue(obj3);
							GameObject val3 = (GameObject)((value2 is GameObject) ? value2 : null);
							if ((Object)(object)val3 != (Object)null)
							{
								hashSet.Add(((Object)val3).name);
							}
						}
						else
						{
							hashSet.Add(((Object)val2).name);
						}
					}
				}
				fieldInfo2.SetValue(null, hashSet);
				methodInfo.Invoke(null, null);
				fieldInfo.SetValue(null, true);
				val.LogInfo((object)$"Successfully replaced Mimics logic. Processed {hashSet.Count} enemy types.");
			}
		}
		catch (Exception arg)
		{
			val.LogError((object)$"CRITICAL ERROR inside replacement patch logic: {arg}");
		}
	}
}