Decompiled source of TandamRandomEnemyHP v1.2.6

BepInEx\plugins\TandamRandomEnemyHP\RandomEnemyHP.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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("RandomEnemyHP")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RandomEnemyHP")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("81f347d3-4680-40b7-b9e6-41997f3df91b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("tandam.randomenemyhp", "Tandam Random Enemy HP", "1.2.5")]
public class RandomEnemyHP : BaseUnityPlugin
{
	public static ConfigEntry<bool> Enable;

	public static ConfigEntry<float> MinimumMultiplier;

	public static ConfigEntry<float> MaximumMultiplier;

	public static ConfigEntry<string> ExcludedEnemies;

	private static readonly Dictionary<EnemyHealth, int> OriginalHealthCache = new Dictionary<EnemyHealth, int>();

	private void Awake()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Expected O, but got Unknown
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Expected O, but got Unknown
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Expected O, but got Unknown
		Enable = ((BaseUnityPlugin)this).Config.Bind<bool>("Random HP", "Enable", true, new ConfigDescription("敵HPのランダム化を有効にするか", (AcceptableValueBase)null, Array.Empty<object>()));
		MinimumMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Random HP", "Minimum Multiplier", 0.5f, new ConfigDescription("敵HPの最低倍率", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
		MaximumMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Random HP", "Maximum Multiplier", 2f, new ConfigDescription("敵HPの最高倍率", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
		ExcludedEnemies = ((BaseUnityPlugin)this).Config.Bind<string>("Exclusion", "Excluded Enemies", "", new ConfigDescription("ランダムHPの対象外にする敵。複数指定する場合はカンマ区切り。例: Hunter,Tick,Gnome", (AcceptableValueBase)null, Array.Empty<object>()));
		Harmony val = new Harmony("tandam.randomenemyhp");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Tandam Random Enemy HP v1.2.5 loaded!");
	}

	public static bool IsExcluded(string enemyName)
	{
		if (string.IsNullOrWhiteSpace(enemyName))
		{
			return false;
		}
		string value = ExcludedEnemies.Value;
		if (string.IsNullOrWhiteSpace(value))
		{
			return false;
		}
		string[] array = value.Split(new char[1] { ',' });
		string[] array2 = array;
		foreach (string text in array2)
		{
			string text2 = text.Trim();
			if (!string.IsNullOrEmpty(text2) && string.Equals(enemyName.Trim(), text2, StringComparison.OrdinalIgnoreCase))
			{
				return true;
			}
		}
		return false;
	}

	public static int GetOriginalHealth(EnemyHealth enemyHealth)
	{
		if ((Object)(object)enemyHealth == (Object)null)
		{
			return 1;
		}
		if (OriginalHealthCache.TryGetValue(enemyHealth, out var value))
		{
			return value;
		}
		value = Mathf.Max(1, enemyHealth.health);
		OriginalHealthCache[enemyHealth] = value;
		Debug.Log((object)("[Tandam Random Enemy HP] Original HP recorded: " + value));
		return value;
	}
}
[HarmonyPatch(typeof(EnemyHealth), "OnSpawn")]
public static class EnemyHealthPatch
{
	[HarmonyPrefix]
	private static void OnSpawn(EnemyHealth __instance)
	{
		if (!RandomEnemyHP.Enable.Value || !SemiFunc.IsMasterClientOrSingleplayer() || (Object)(object)__instance == (Object)null)
		{
			return;
		}
		string enemyName = GetEnemyName(__instance);
		Debug.Log((object)("[Tandam Random Enemy HP] Detected enemy name = [" + enemyName + "]"));
		if (string.IsNullOrEmpty(enemyName))
		{
			Debug.LogWarning((object)"[Tandam Random Enemy HP] Enemy name could not be detected.");
			return;
		}
		int originalHealth = RandomEnemyHP.GetOriginalHealth(__instance);
		if (RandomEnemyHP.IsExcluded(enemyName))
		{
			Debug.Log((object)("[Tandam Random Enemy HP] EXCLUDED: " + enemyName + " (Original HP: " + originalHealth + ")"));
			return;
		}
		float num = RandomEnemyHP.MinimumMultiplier.Value;
		float num2 = RandomEnemyHP.MaximumMultiplier.Value;
		if (num > num2)
		{
			float num3 = num;
			num = num2;
			num2 = num3;
		}
		float num4 = Random.Range(num, num2);
		int num5 = (__instance.health = Mathf.Max(1, Mathf.RoundToInt((float)originalHealth * num4)));
		Debug.Log((object)("[Tandam Random Enemy HP] " + enemyName + ": " + originalHealth + " -> " + num5 + " (x" + num4.ToString("F2") + ")"));
	}

	private static string GetEnemyName(EnemyHealth enemyHealth)
	{
		if ((Object)(object)enemyHealth == (Object)null)
		{
			return null;
		}
		Transform val = ((Component)enemyHealth).transform;
		while ((Object)(object)val != (Object)null)
		{
			string name = ((Object)val).name;
			if (name.StartsWith("Enemy - ", StringComparison.OrdinalIgnoreCase))
			{
				string text = name.Substring("Enemy - ".Length);
				return text.Replace("(Clone)", "").Trim();
			}
			val = val.parent;
		}
		string name2 = ((Object)((Component)enemyHealth).gameObject).name;
		return name2.Replace("(Clone)", "").Trim();
	}
}