Decompiled source of EverythingCanDieAlternative v1.0.0

EverythingCanDieAlternative.dll

Decompiled 5 hours 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.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
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("EverythingCanDieAlternative")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyDescription("A mod that makes everything in Lethal Company damageable")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace EverythingCanDie;

public class Patches
{
	public static List<string> DamagableEnemies = new List<string>();

	public static List<string> InvalidEnemies = new List<string>();

	private static readonly int Damage = Animator.StringToHash("damage");

	public static void StartOfRoundPatch()
	{
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Expected O, but got Unknown
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Expected O, but got Unknown
		Plugin.enemies = (from EnemyType e in Resources.FindObjectsOfTypeAll(typeof(EnemyType))
			where (Object)(object)e != (Object)null
			select e).ToList();
		Plugin.items = (from Item i in Resources.FindObjectsOfTypeAll(typeof(Item))
			where (Object)(object)i != (Object)null
			select i).ToList();
		foreach (EnemyType enemy in Plugin.enemies)
		{
			string text = Plugin.RemoveInvalidCharacters(enemy.enemyName).ToUpper();
			try
			{
				if (!((BaseUnityPlugin)Plugin.Instance).Config.ContainsKey(new ConfigDefinition("Mobs", text + ".Unimmortal")))
				{
					((BaseUnityPlugin)Plugin.Instance).Config.Bind<bool>("Mobs", text + ".Unimmortal", true, "If true this mob will be damageable");
				}
				if (!((BaseUnityPlugin)Plugin.Instance).Config.ContainsKey(new ConfigDefinition("Mobs", text + ".Health")))
				{
					EnemyAI component = enemy.enemyPrefab.GetComponent<EnemyAI>();
					((BaseUnityPlugin)Plugin.Instance).Config.Bind<int>("Mobs", text + ".Health", component.enemyHP, "The value of the mobs health");
					component.enemyHP = component.enemyHP;
					Plugin.Log.LogInfo((object)$"Set {((Object)enemy).name} HP to {component.enemyHP}");
				}
				if (Plugin.CanMob(".Unimmortal", text))
				{
					enemy.canDie = true;
					DamagableEnemies.Add(enemy.enemyName);
					Plugin.Log.LogInfo((object)("Made " + enemy.enemyName + " damageable"));
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogInfo((object)("Error configuring enemy " + enemy.enemyName + ": " + ex.Message));
				InvalidEnemies.Add(enemy.enemyName);
			}
		}
	}

	public static bool IsEnemyDamageable(EnemyAI enemy)
	{
		if ((Object)(object)enemy == (Object)null || enemy.isEnemyDead || !((Behaviour)enemy).enabled)
		{
			return false;
		}
		string mobName = Plugin.RemoveInvalidCharacters(enemy.enemyType.enemyName).ToUpper();
		return Plugin.CanMob(".Unimmortal", mobName);
	}

	public static void HitEnemyPatch(ref EnemyAI __instance, int force = 1, PlayerControllerB playerWhoHit = null)
	{
		if ((Object)(object)__instance == (Object)null || __instance.isEnemyDead || InvalidEnemies.Contains(__instance.enemyType.enemyName))
		{
			return;
		}
		string enemyName = __instance.enemyType.enemyName;
		Plugin.Log.LogInfo((object)("Attempting to hit " + enemyName));
		if (!IsEnemyDamageable(__instance))
		{
			Plugin.Log.LogInfo((object)(enemyName + " is not damageable"));
			return;
		}
		if ((Object)(object)__instance.creatureAnimator != (Object)null)
		{
			__instance.creatureAnimator.SetTrigger(Damage);
		}
		int enemyHP = __instance.enemyHP;
		__instance.enemyHP = Math.Max(0, __instance.enemyHP - force);
		Plugin.Log.LogInfo((object)$"Hit {enemyName}: HP {enemyHP} -> {__instance.enemyHP}");
		if (__instance.enemyHP <= 0)
		{
			Plugin.Log.LogInfo((object)("Killing " + enemyName));
			__instance.KillEnemyOnOwnerClient(false);
		}
	}

	public static void KillEnemyPatch(ref EnemyAI __instance)
	{
		if ((Object)(object)__instance != (Object)null && !InvalidEnemies.Contains(__instance.enemyType.enemyName))
		{
			Plugin.Log.LogInfo((object)(__instance.enemyType.enemyName + " died"));
		}
	}
}
[BepInPlugin("nwnt.EverythingCanDieAlternative", "EverythingCanDieAlternative", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "nwnt.EverythingCanDieAlternative";

	public const string Name = "EverythingCanDieAlternative";

	public const string Version = "1.0.0";

	public static Plugin Instance;

	public static Harmony Harmony;

	public static ManualLogSource Log;

	public static List<EnemyType> enemies;

	public static List<Item> items;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		Harmony = new Harmony("nwnt.EverythingCanDieAlternative");
		Instance = this;
		Harmony.PatchAll(typeof(Plugin));
		Log = ((BaseUnityPlugin)this).Logger;
		CreateHarmonyPatch(Harmony, typeof(StartOfRound), "Start", null, typeof(Patches), "StartOfRoundPatch", isPrefix: false);
		CreateHarmonyPatch(Harmony, typeof(EnemyAI), "HitEnemy", new Type[4]
		{
			typeof(int),
			typeof(PlayerControllerB),
			typeof(bool),
			typeof(int)
		}, typeof(Patches), "HitEnemyPatch", isPrefix: false);
		CreateHarmonyPatch(Harmony, typeof(EnemyAI), "KillEnemy", new Type[1] { typeof(bool) }, typeof(Patches), "KillEnemyPatch", isPrefix: false);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Patching complete");
	}

	public static void CreateHarmonyPatch(Harmony harmony, Type typeToPatch, string methodToPatch, Type[] parameters, Type patchType, string patchMethod, bool isPrefix)
	{
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Expected O, but got Unknown
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Expected O, but got Unknown
		if (typeToPatch == null || patchType == null)
		{
			Log.LogInfo((object)"Type is either incorrect or does not exist!");
			return;
		}
		MethodInfo methodInfo = AccessTools.Method(typeToPatch, methodToPatch, parameters, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(patchType, patchMethod, (Type[])null, (Type[])null);
		if (isPrefix)
		{
			harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)("Prefix " + methodInfo.Name + " Patched!"));
		}
		else
		{
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)("Postfix " + methodInfo.Name + " Patched!"));
		}
	}

	public static string RemoveInvalidCharacters(string source)
	{
		StringBuilder stringBuilder = new StringBuilder();
		foreach (char c in source)
		{
			if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
			{
				stringBuilder.Append(c);
			}
		}
		return string.Join("", stringBuilder.ToString().Split((string[]?)null, StringSplitOptions.RemoveEmptyEntries));
	}

	public static bool CanMob(string identifier, string mobName)
	{
		try
		{
			string text = RemoveInvalidCharacters(mobName).ToUpper();
			string text2 = text + identifier.ToUpper();
			foreach (ConfigDefinition key in ((BaseUnityPlugin)Instance).Config.Keys)
			{
				if (RemoveInvalidCharacters(key.Key.ToUpper()).Equals(RemoveInvalidCharacters(text2)))
				{
					bool flag = ((BaseUnityPlugin)Instance).Config[key].BoxedValue.ToString().ToUpper().Equals("TRUE");
					Log.LogInfo((object)$"Mob config: [Mobs] {text2} = {flag}");
					return flag;
				}
			}
			Log.LogInfo((object)("No config found for [Mobs] " + text2 + ", defaulting to true"));
			return true;
		}
		catch (Exception ex)
		{
			Log.LogInfo((object)("Error in config check for mob " + mobName + ": " + ex.Message));
			return true;
		}
	}
}