Decompiled source of Enemy And Valuable Spawn Manager TempFix v1.0.0

EmpressSpawnManagerFix.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Empress.SpawnManagerFix
{
	[BepInPlugin("0.empress.spawnmanagerfix", "Empress Spawn Manager Fix", "1.0.0")]
	public sealed class EmpressSpawnManagerFixPlugin : BaseUnityPlugin
	{
		private delegate void LevelGeneratorItemSetupPrefixDelegate();

		private delegate void ValuableDirectorSpawnPrefixDelegate(GameObject valuable, ValuableVolume volume, ref string path);

		private const string PluginGuid = "0.empress.spawnmanagerfix";

		private const string PluginName = "Empress Spawn Manager Fix";

		private const string PluginVersion = "1.0.0";

		private const string SpawnManagerAssemblyName = "SpawnManager";

		private const string SpawnManagerBaseTypeName = "SpawnManager.SpawnManagerBase";

		private const string SpawnManagerSettingsTypeName = "SpawnManager.Settings";

		private const string SpawnManagerPluginManagerTypeName = "SpawnManager.Managers.PluginManager";

		private const string SpawnManagerMenuManagerTypeName = "SpawnManager.Managers.MenuModManager";

		private static readonly Harmony BootstrapHarmony = new Harmony("0.empress.spawnmanagerfix.bootstrap");

		private static readonly Harmony PatchHarmony = new Harmony("0.empress.spawnmanagerfix.patches");

		private static ManualLogSource EmpressLogger;

		private static bool AwakeHookApplied;

		private static bool SpawnManagerPatchesApplied;

		private static bool SpawnManagerItemSetupWarningLogged;

		private static LevelGeneratorItemSetupPrefixDelegate LevelGeneratorItemSetupDelegate;

		private static ValuableDirectorSpawnPrefixDelegate ValuableDirectorSpawnDelegate;

		private void Awake()
		{
			EmpressLogger = ((BaseUnityPlugin)this).Logger;
			AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i = 0; i < assemblies.Length; i++)
			{
				TryHookSpawnManagerAwake(assemblies[i]);
			}
		}

		private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)
		{
			TryHookSpawnManagerAwake(args.LoadedAssembly);
		}

		private static void TryHookSpawnManagerAwake(Assembly assembly)
		{
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			if (AwakeHookApplied || assembly == null || !string.Equals(assembly.GetName().Name, "SpawnManager", StringComparison.OrdinalIgnoreCase))
			{
				return;
			}
			Type type = assembly.GetType("SpawnManager.SpawnManagerBase", throwOnError: false);
			if (type == null)
			{
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Awake", (Type[])null, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(typeof(EmpressSpawnManagerFixPlugin), "SpawnManagerBaseAwakePrefix", (Type[])null, (Type[])null);
			if (!(methodInfo == null) && !(methodInfo2 == null))
			{
				BootstrapHarmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				AwakeHookApplied = true;
				ManualLogSource empressLogger = EmpressLogger;
				if (empressLogger != null)
				{
					empressLogger.LogInfo((object)"Empress Spawn Manager Fix hooked SpawnManager startup.");
				}
			}
		}

		private static bool SpawnManagerBaseAwakePrefix(object __instance)
		{
			try
			{
				BaseUnityPlugin val = (BaseUnityPlugin)((__instance is BaseUnityPlugin) ? __instance : null);
				if ((Object)(object)val == (Object)null)
				{
					return false;
				}
				Assembly assembly = __instance.GetType().Assembly;
				InitializeSettings(assembly, val.Config);
				InitializeMenuManager(assembly);
				ApplySpawnManagerPatches(assembly);
				ManualLogSource empressLogger = EmpressLogger;
				if (empressLogger != null)
				{
					empressLogger.LogInfo((object)"Enemy/Valuable Spawn Manager Empress compatibility loaded.");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource empressLogger2 = EmpressLogger;
				if (empressLogger2 != null)
				{
					empressLogger2.LogError((object)("Enemy/Valuable Spawn Manager Empress compatibility failed: " + ex));
				}
			}
			return false;
		}

		private static void InitializeSettings(Assembly assembly, ConfigFile config)
		{
			Type type = assembly.GetType("SpawnManager.Settings", throwOnError: false);
			if (!(type == null))
			{
				MethodInfo methodInfo = AccessTools.Method(type, "Initialize", new Type[2]
				{
					typeof(ConfigFile),
					typeof(ManualLogSource)
				}, (Type[])null);
				if (!(methodInfo == null))
				{
					((Action<ConfigFile, ManualLogSource>)Delegate.CreateDelegate(typeof(Action<ConfigFile, ManualLogSource>), methodInfo))(config, EmpressLogger);
				}
			}
		}

		private static void InitializeMenuManager(Assembly assembly)
		{
			Type type = assembly.GetType("SpawnManager.Managers.PluginManager", throwOnError: false);
			if (type == null)
			{
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "IsPluginInstalled", new Type[1] { typeof(string) }, (Type[])null);
			if (methodInfo == null)
			{
				return;
			}
			if (!((Func<string, bool>)Delegate.CreateDelegate(typeof(Func<string, bool>), methodInfo))("nickklmao.menulib"))
			{
				ManualLogSource empressLogger = EmpressLogger;
				if (empressLogger != null)
				{
					empressLogger.LogWarning((object)"MenuLib not loaded. Cannot add button to main menu.");
				}
				return;
			}
			Type type2 = assembly.GetType("SpawnManager.Managers.MenuModManager", throwOnError: false);
			MethodInfo methodInfo2 = ((type2 != null) ? AccessTools.Method(type2, "Initialize", (Type[])null, (Type[])null) : null);
			if (!(methodInfo2 == null))
			{
				((Action)Delegate.CreateDelegate(typeof(Action), methodInfo2))();
			}
		}

		private static void ApplySpawnManagerPatches(Assembly assembly)
		{
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Expected O, but got Unknown
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Expected O, but got Unknown
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ee: Expected O, but got Unknown
			if (SpawnManagerPatchesApplied)
			{
				return;
			}
			PatchOriginalPrefix(assembly, "SpawnManager.Patches.RunManagerPatches", "RunManagerAwakePrefix", AccessTools.Method(typeof(RunManager), "Awake", Type.EmptyTypes, (Type[])null));
			PatchOriginalPrefix(assembly, "SpawnManager.Patches.RunManagerPatches", "RunManagerChangeLevelPrefix", AccessTools.Method(typeof(RunManager), "ChangeLevel", new Type[3]
			{
				typeof(bool),
				typeof(bool),
				typeof(ChangeLevelType)
			}, (Type[])null));
			PatchOriginalPostfix(assembly, "SpawnManager.Patches.EnemyDirectorPatches", "EnemyDirectorStartPostfix", AccessTools.Method(typeof(EnemyDirector), "Start", Type.EmptyTypes, (Type[])null));
			PatchOriginalPostfix(assembly, "SpawnManager.Patches.EnemyDirectorPatches", "EnemyDirectorPickEnemiesPostfix", AccessTools.Method(typeof(EnemyDirector), "PickEnemies", new Type[1] { typeof(List<EnemySetup>) }, (Type[])null));
			PatchOriginalPrefix(assembly, "SpawnManager.Patches.EnemyDirectorPatches", "EnemyDirectorGetEnemyPrefix", AccessTools.Method(typeof(EnemyDirector), "GetEnemy", Type.EmptyTypes, (Type[])null));
			Type type = assembly.GetType("SpawnManager.Settings", throwOnError: false);
			MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "InitializeItemsLevels", (Type[])null, (Type[])null) : null);
			MethodInfo methodInfo2 = AccessTools.Method(typeof(EmpressSpawnManagerFixPlugin), "SettingsInitializeItemsLevelsPrefix", (Type[])null, (Type[])null);
			if (methodInfo != null && methodInfo2 != null)
			{
				PatchHarmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			Type type2 = assembly.GetType("SpawnManager.Patches.LevelGeneratorPatches", throwOnError: false);
			MethodInfo methodInfo3 = ((type2 != null) ? AccessTools.Method(type2, "LevelGeneratorItemSetupPrefix", (Type[])null, (Type[])null) : null);
			if (methodInfo3 != null)
			{
				LevelGeneratorItemSetupDelegate = AccessTools.MethodDelegate<LevelGeneratorItemSetupPrefixDelegate>(methodInfo3, (object)null, true);
				MethodInfo methodInfo4 = AccessTools.Method(typeof(LevelGenerator), "ItemSetup", new Type[1] { typeof(PhotonMessageInfo) }, (Type[])null);
				MethodInfo methodInfo5 = AccessTools.Method(typeof(EmpressSpawnManagerFixPlugin), "LevelGeneratorItemSetupSafePrefix", (Type[])null, (Type[])null);
				if (methodInfo4 != null && methodInfo5 != null)
				{
					PatchHarmony.Patch((MethodBase)methodInfo4, new HarmonyMethod(methodInfo5), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
			Type type3 = assembly.GetType("SpawnManager.Patches.ValuableDirectorPatches", throwOnError: false);
			MethodInfo methodInfo6 = ((type3 != null) ? AccessTools.Method(type3, "ValuableDirectorSpawnPrefix", (Type[])null, (Type[])null) : null);
			if (methodInfo6 != null)
			{
				ValuableDirectorSpawnDelegate = AccessTools.MethodDelegate<ValuableDirectorSpawnPrefixDelegate>(methodInfo6, (object)null, true);
			}
			MethodInfo methodInfo7 = AccessTools.Method(typeof(ValuableDirector), "SpawnValuable", new Type[3]
			{
				typeof(PrefabRef),
				typeof(ValuableVolume),
				typeof(string)
			}, (Type[])null);
			MethodInfo methodInfo8 = AccessTools.Method(typeof(EmpressSpawnManagerFixPlugin), "ValuableDirectorSpawnValuablePrefix", (Type[])null, (Type[])null);
			if (methodInfo7 != null && methodInfo8 != null)
			{
				PatchHarmony.Patch((MethodBase)methodInfo7, new HarmonyMethod(methodInfo8), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			SpawnManagerPatchesApplied = true;
		}

		private static bool SettingsInitializeItemsLevelsPrefix()
		{
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Expected O, but got Unknown
			Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly candidate) => candidate != null && string.Equals(candidate.GetName().Name, "SpawnManager", StringComparison.OrdinalIgnoreCase));
			if (assembly == null)
			{
				return false;
			}
			Type type = assembly.GetType("SpawnManager.Settings", throwOnError: false);
			if (type == null)
			{
				return false;
			}
			MethodInfo methodInfo = AccessTools.PropertyGetter(type, "Config");
			MethodInfo methodInfo2 = AccessTools.PropertyGetter(type, "DisabledLevelItems");
			if (methodInfo == null || methodInfo2 == null)
			{
				return false;
			}
			object? obj = methodInfo.Invoke(null, null);
			ConfigFile val = (ConfigFile)((obj is ConfigFile) ? obj : null);
			IDictionary<string, ConfigEntry<string>> disabledLevelItems = methodInfo2.Invoke(null, null) as IDictionary<string, ConfigEntry<string>>;
			if (val == null || disabledLevelItems == null)
			{
				return false;
			}
			List<Level> list = new List<Level>();
			if (RunManager.instance?.levels != null)
			{
				list.AddRange(RunManager.instance.levels.Where((Level level) => (Object)(object)level != (Object)null));
			}
			if (RunManager.instance?.levelShop != null)
			{
				list.AddRange(RunManager.instance.levelShop.Where((Level level) => (Object)(object)level != (Object)null));
			}
			foreach (Level item in list.Where((Level level) => (Object)(object)level != (Object)null && !disabledLevelItems.ContainsKey(((Object)level).name)))
			{
				ConfigEntry<string> value = val.Bind<string>("Items", ((Object)item).name + " - Disabled Items", "", new ConfigDescription("Comma-separated list of item names to disable in this level. (e.g. \"Cart Medium\")", (AcceptableValueBase)null, new object[1] { "HideREPOConfig" }));
				disabledLevelItems.Add(((Object)item).name, value);
			}
			return false;
		}

		private static void LevelGeneratorItemSetupSafePrefix()
		{
			if (LevelGeneratorItemSetupDelegate == null)
			{
				return;
			}
			try
			{
				LevelGeneratorItemSetupDelegate();
			}
			catch (MissingFieldException ex)
			{
				if (!SpawnManagerItemSetupWarningLogged)
				{
					SpawnManagerItemSetupWarningLogged = true;
					ManualLogSource empressLogger = EmpressLogger;
					if (empressLogger != null)
					{
						empressLogger.LogWarning((object)("Enemy/Valuable Spawn Manager item/level setup is skipping an outdated field path on current REPO: " + ex.Message));
					}
				}
			}
			catch (MissingMemberException ex2)
			{
				if (!SpawnManagerItemSetupWarningLogged)
				{
					SpawnManagerItemSetupWarningLogged = true;
					ManualLogSource empressLogger2 = EmpressLogger;
					if (empressLogger2 != null)
					{
						empressLogger2.LogWarning((object)("Enemy/Valuable Spawn Manager item/level setup is skipping an outdated member path on current REPO: " + ex2.Message));
					}
				}
			}
		}

		private static void PatchOriginalPrefix(Assembly assembly, string typeName, string methodName, MethodInfo targetMethod)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			if (!(targetMethod == null))
			{
				Type type = assembly.GetType(typeName, throwOnError: false);
				MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, methodName, (Type[])null, (Type[])null) : null);
				if (!(methodInfo == null))
				{
					PatchHarmony.Patch((MethodBase)targetMethod, new HarmonyMethod(methodInfo), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}

		private static void PatchOriginalPostfix(Assembly assembly, string typeName, string methodName, MethodInfo targetMethod)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			if (!(targetMethod == null))
			{
				Type type = assembly.GetType(typeName, throwOnError: false);
				MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, methodName, (Type[])null, (Type[])null) : null);
				if (!(methodInfo == null))
				{
					PatchHarmony.Patch((MethodBase)targetMethod, (HarmonyMethod)null, new HarmonyMethod(methodInfo), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}

		private static void ValuableDirectorSpawnValuablePrefix(PrefabRef _valuable, ValuableVolume _volume, ref string _path)
		{
			if (_valuable != null && ValuableDirectorSpawnDelegate != null)
			{
				GameObject prefab = _valuable.Prefab;
				if (!((Object)(object)prefab == (Object)null))
				{
					ValuableDirectorSpawnDelegate(prefab, _volume, ref _path);
				}
			}
		}
	}
}