Decompiled source of UnkillableEnemyFixer v1.0.0

UnkillableEnemyFixer.dll

Decompiled a month 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("UnkillableEnemyFixer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("UnkillableEnemyFixer")]
[assembly: AssemblyTitle("UnkillableEnemyFixer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 UnkillableEnemyFixer
{
	internal static class EnemyConfigManager
	{
		public static Dictionary<string, ConfigEntry<bool>> EnemyFixEntries = new Dictionary<string, ConfigEntry<bool>>();

		public static List<Type> EnemyTypes = new List<Type>();

		public static void BuildEnemyListAndConfig()
		{
			EnemyTypes.Clear();
			EnemyFixEntries.Clear();
			foreach (Type item in (from t in AppDomain.CurrentDomain.GetAssemblies().SelectMany((Assembly a) => a.GetTypes())
				where t.IsClass && t.Name.StartsWith("Enemy") && t.GetMethod("OnDeath", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null
				select t).Distinct())
			{
				EnemyTypes.Add(item);
				string name = item.Name;
				EnemyFixEntries[name] = Plugin.PluginConfig.Bind<bool>(name, "ForceDespawnOnDeath", false, "Force despawn enemy on death");
			}
			Plugin.Log.LogInfo((object)$"Discovered {EnemyTypes.Count} enemies for configuration.");
		}
	}
	internal static class EnemyDeathPatcher
	{
		public static void ApplyPatches(Harmony harmony)
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			foreach (KeyValuePair<string, ConfigEntry<bool>> kv in EnemyConfigManager.EnemyFixEntries)
			{
				if (!kv.Value.Value)
				{
					continue;
				}
				Type type = EnemyConfigManager.EnemyTypes.Find((Type t) => t.Name == kv.Key);
				if (type != null)
				{
					MethodInfo method = type.GetMethod("OnDeath", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (method != null)
					{
						harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(typeof(EnemyDeathPatcher), "ForceDespawn_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						Plugin.Log.LogInfo((object)("Patched " + type.Name + ".OnDeath to force despawn."));
					}
				}
			}
		}

		public static void ForceDespawn_Postfix(object __instance)
		{
			object obj = __instance.GetType().GetField("enemy", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(__instance);
			if (obj != null)
			{
				object obj2 = obj.GetType().GetProperty("EnemyParent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(obj);
				(obj2?.GetType().GetMethod("Despawn", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))?.Invoke(obj2, null);
			}
		}
	}
	[BepInPlugin("FluxTeam.UnkillableEnemyFixer", "Unkillable Enemy Fixer", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource? Log;

		internal static ConfigFile? PluginConfig;

		private Harmony? _harmony;

		private void Awake()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			PluginConfig = ((BaseUnityPlugin)this).Config;
			_harmony = new Harmony("FluxTeam.UnkillableEnemyFixer");
			EnemyConfigManager.BuildEnemyListAndConfig();
			EnemyDeathPatcher.ApplyPatches(_harmony);
			Log.LogInfo((object)"Unkillable Enemy Fixer loaded and active!");
		}
	}
	internal static class PluginInfo
	{
		public const string PLUGIN_GUID = "FluxTeam.UnkillableEnemyFixer";

		public const string PLUGIN_NAME = "Unkillable Enemy Fixer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "UnkillableEnemyFixer";

		public const string PLUGIN_NAME = "UnkillableEnemyFixer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}