Decompiled source of HealthComponentAPI v0.1.1

HealthComponentAPI.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using IL.RoR2;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using RoR2;

[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("HealthComponentAPI")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c26279f4d2985bc9ba47b4bf9af0553cc60388cf")]
[assembly: AssemblyProduct("HealthComponentAPI")]
[assembly: AssemblyTitle("HealthComponentAPI")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace HDeMods
{
	public static class HealthComponentAPI
	{
		public class UpdateHealthEventArgs : EventArgs
		{
			public float TOTALregenMultAdd;

			public float TOTALregenFlatAdd;

			public float barrierDecayRateMultAdd;

			public float barrierDecayRateFlatAdd;

			public float shieldRechargeRateMultAdd;

			public float shieldRechargeRateFlatAdd;

			public float adaptiveArmorDecayRateMultAdd;

			public float adaptiveArmorDecayRateFlatAdd;
		}

		public class HealEventArgs : EventArgs
		{
			public bool enableEclipseHealReduction;

			public float damageCoyoteTimerMultAdd;

			public float damageCoyoteTimerFlatAdd;

			public float critHealMultAdd;

			public float critHealFlatAdd;

			public float TOTALhealAmountMultAdd;

			public float TOTALhealAmountFlatAdd;
		}

		public delegate void UpdateHealthEventHandler(HealthComponent sender, UpdateHealthEventArgs args);

		public delegate void HealEventHandler(HealthComponent sender, HealEventArgs args);

		[CompilerGenerated]
		private static class <>O
		{
			public static Manipulator <0>__HealthComponent_ServerFixedUpdate;

			public static Manipulator <1>__HealthComponent_Heal;

			public static Manipulator <2>__HealthComponent_TakeDamageProcess;

			public static Action<HealthComponent> <3>__GetHealthMod;

			public static Action<HealthComponent> <4>__GetHealMod;
		}

		public const string PluginGUID = "HDeDeDe.HealthComponentAPI";

		public const string PluginAuthor = "HDeDeDe";

		public const string PluginName = "HealthComponentAPI";

		public const string PluginVersion = "0.1.1";

		private static UpdateHealthEventArgs HealthStats;

		private static HealEventArgs HealStats;

		private static bool _healthHookSet;

		private static bool _healHooksSet;

		private static event UpdateHealthEventHandler _getHealthStats;

		private static event HealEventHandler _getHealStats;

		public static event UpdateHealthEventHandler GetHealthStats
		{
			add
			{
				SetHealthHook();
				_getHealthStats += value;
			}
			remove
			{
				_getHealthStats -= value;
				if (HealthComponentAPI._getHealthStats != null)
				{
					Delegate[] invocationList = HealthComponentAPI._getHealthStats.GetInvocationList();
					if (invocationList == null || invocationList.Length != 0)
					{
						return;
					}
				}
				UnsetHealthHook();
			}
		}

		public static event HealEventHandler GetHealStats
		{
			add
			{
				SetHealHooks();
				_getHealStats += value;
			}
			remove
			{
				_getHealStats -= value;
				if (HealthComponentAPI._getHealStats != null)
				{
					Delegate[] invocationList = HealthComponentAPI._getHealStats.GetInvocationList();
					if (invocationList == null || invocationList.Length != 0)
					{
						return;
					}
				}
				UnsetHealHooks();
			}
		}

		internal static void SetHealthHook()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			if (!_healthHookSet)
			{
				object obj = <>O.<0>__HealthComponent_ServerFixedUpdate;
				if (obj == null)
				{
					Manipulator val = HealthComponent_ServerFixedUpdate;
					<>O.<0>__HealthComponent_ServerFixedUpdate = val;
					obj = (object)val;
				}
				HealthComponent.ServerFixedUpdate += (Manipulator)obj;
				_healthHookSet = true;
			}
		}

		internal static void UnsetHealthHook()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			object obj = <>O.<0>__HealthComponent_ServerFixedUpdate;
			if (obj == null)
			{
				Manipulator val = HealthComponent_ServerFixedUpdate;
				<>O.<0>__HealthComponent_ServerFixedUpdate = val;
				obj = (object)val;
			}
			HealthComponent.ServerFixedUpdate -= (Manipulator)obj;
			_healthHookSet = false;
		}

		internal static void SetHealHooks()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			if (!_healHooksSet)
			{
				object obj = <>O.<1>__HealthComponent_Heal;
				if (obj == null)
				{
					Manipulator val = HealthComponent_Heal;
					<>O.<1>__HealthComponent_Heal = val;
					obj = (object)val;
				}
				HealthComponent.Heal += (Manipulator)obj;
				object obj2 = <>O.<2>__HealthComponent_TakeDamageProcess;
				if (obj2 == null)
				{
					Manipulator val2 = HealthComponent_TakeDamageProcess;
					<>O.<2>__HealthComponent_TakeDamageProcess = val2;
					obj2 = (object)val2;
				}
				HealthComponent.TakeDamageProcess += (Manipulator)obj2;
				_healHooksSet = true;
			}
		}

		internal static void UnsetHealHooks()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			object obj = <>O.<1>__HealthComponent_Heal;
			if (obj == null)
			{
				Manipulator val = HealthComponent_Heal;
				<>O.<1>__HealthComponent_Heal = val;
				obj = (object)val;
			}
			HealthComponent.Heal -= (Manipulator)obj;
			object obj2 = <>O.<2>__HealthComponent_TakeDamageProcess;
			if (obj2 == null)
			{
				Manipulator val2 = HealthComponent_TakeDamageProcess;
				<>O.<2>__HealthComponent_TakeDamageProcess = val2;
				obj2 = (object)val2;
			}
			HealthComponent.TakeDamageProcess -= (Manipulator)obj2;
			_healHooksSet = false;
		}

		private static void GetHealthMod(HealthComponent hc)
		{
			HealthStats = new UpdateHealthEventArgs();
			if (HealthComponentAPI._getHealthStats == null)
			{
				return;
			}
			Delegate[] invocationList = HealthComponentAPI._getHealthStats.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				UpdateHealthEventHandler updateHealthEventHandler = (UpdateHealthEventHandler)invocationList[i];
				try
				{
					updateHealthEventHandler(hc, HealthStats);
				}
				catch (Exception arg)
				{
					Log.Error($"Exception thrown by : {updateHealthEventHandler.Method.DeclaringType.Name}.{updateHealthEventHandler.Method.Name}:\n{arg}");
				}
			}
		}

		private static void GetHealMod(HealthComponent hc)
		{
			HealStats = new HealEventArgs();
			if (HealthComponentAPI._getHealStats == null)
			{
				return;
			}
			Delegate[] invocationList = HealthComponentAPI._getHealStats.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				HealEventHandler healEventHandler = (HealEventHandler)invocationList[i];
				try
				{
					healEventHandler(hc, HealStats);
				}
				catch (Exception arg)
				{
					Log.Error($"Exception thrown by : {healEventHandler.Method.DeclaringType.Name}.{healEventHandler.Method.Name}:\n{arg}");
				}
			}
		}

		private static void HealthComponent_ServerFixedUpdate(ILContext il)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_004b: Expected O, but got Unknown
			ILCursor val = new ILCursor(il);
			val.Emit(OpCodes.Ldarg_0);
			val.EmitDelegate<Action<HealthComponent>>((Action<HealthComponent>)GetHealthMod);
			RecalcTOTALRegenAcumulator(val);
			RecalcBarrierDecayRate(val);
			RecalcShieldRechargeRate(val);
			RecalcAdaptiveArmorDecayRate(val);
		}

		private static void HealthComponent_Heal(ILContext il)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_004b: Expected O, but got Unknown
			ILCursor val = new ILCursor(il);
			val.Emit(OpCodes.Ldarg_0);
			val.EmitDelegate<Action<HealthComponent>>((Action<HealthComponent>)GetHealMod);
			RecalcCoyoteTimer(val);
			RecalcCritHeal(val);
			HalveHealing(val);
			RecalcTOTALHeal(val);
		}

		private static void HealthComponent_TakeDamageProcess(ILContext il)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			ILCursor val = new ILCursor(il);
			val.Emit(OpCodes.Ldarg_0);
			val.EmitDelegate<Action<HealthComponent>>((Action<HealthComponent>)GetHealMod);
			RecalcCoyoteTimer(val);
		}

		private static void RecalcCoyoteTimer(ILCursor c)
		{
			c.GotoNext(new Func<Instruction, bool>[3]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 0.2f),
				(Instruction x) => ILPatternMatchingExt.MatchStfld<HealthComponent>(x, "recentlyTookDamageCoyoteTimer")
			});
			c.Index += 2;
			c.EmitDelegate<Func<float, float>>((Func<float, float>)((float consume) => 0.2f * (1f + HealStats.damageCoyoteTimerMultAdd) + HealStats.damageCoyoteTimerFlatAdd));
		}

		private static void RecalcCritHeal(ILCursor c)
		{
			c.GotoNext(new Func<Instruction, bool>[4]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 1),
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 2f),
				(Instruction x) => ILPatternMatchingExt.MatchMul(x),
				(Instruction x) => ILPatternMatchingExt.MatchStarg(x, 1)
			});
			c.Index += 2;
			c.EmitDelegate<Func<float, float>>((Func<float, float>)((float consume) => 2f * (1f + HealStats.critHealMultAdd) + HealStats.critHealFlatAdd));
		}

		private static void HalveHealing(ILCursor c)
		{
			c.GotoNext(new Func<Instruction, bool>[3]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCall<Run>(x, "get_instance"),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Run>(x, "get_selectedDifficulty"),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 7)
			});
			c.Index += 2;
			c.EmitDelegate<Func<int, int>>((Func<int, int>)((int consume) => (!HealStats.enableEclipseHealReduction) ? consume : 7));
		}

		private static void RecalcTOTALHeal(ILCursor c)
		{
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			c.GotoNext(new Func<Instruction, bool>[6]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 1),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, 4),
				(Instruction x) => ILPatternMatchingExt.MatchMul(x),
				(Instruction x) => ILPatternMatchingExt.MatchStarg(x, 1),
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 1),
				(Instruction x) => ILPatternMatchingExt.MatchStloc(x, 2)
			});
			c.Index += 5;
			c.EmitDelegate<Func<float, float>>((Func<float, float>)((float TOTALHeal) => TOTALHeal * (1f + HealStats.TOTALhealAmountMultAdd) + HealStats.TOTALhealAmountFlatAdd));
			c.Emit(OpCodes.Starg, 1);
			c.Emit(OpCodes.Ldarg_1);
		}

		private static void RecalcTOTALRegenAcumulator(ILCursor c)
		{
			c.GotoNext(new Func<Instruction, bool>[7]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "regenAccumulator"),
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "body"),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterBody>(x, "get_regen"),
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 1),
				(Instruction x) => ILPatternMatchingExt.MatchMul(x)
			});
			c.Index += 5;
			c.EmitDelegate<Func<float, float>>((Func<float, float>)((float regen) => regen * (1f + HealthStats.TOTALregenMultAdd) + HealthStats.TOTALregenFlatAdd));
		}

		private static void RecalcBarrierDecayRate(ILCursor c)
		{
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			if (OptionalMods.SandSwept.enabled)
			{
				Log.Warning("Sandswept detected, attempting alternative hook.");
				c.GotoNext(new Func<Instruction, bool>[5]
				{
					(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
					(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "barrier"),
					(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
					(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "body"),
					(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterBody>(x, "get_barrierDecayRate")
				});
				c.Index += 5;
			}
			else
			{
				c.GotoNext(new Func<Instruction, bool>[5]
				{
					(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
					(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "barrier"),
					(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
					(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "body"),
					(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterBody>(x, "get_barrierDecayRate")
				});
				c.Index += 5;
			}
			c.Emit(OpCodes.Ldarg_0);
			c.EmitDelegate<Func<float, HealthComponent, float>>((Func<float, HealthComponent, float>)((float consume, HealthComponent hc) => hc.body.maxBarrier / (30f * (1f + HealthStats.barrierDecayRateMultAdd) + HealthStats.barrierDecayRateFlatAdd)));
		}

		private static void RecalcShieldRechargeRate(ILCursor c)
		{
			c.GotoNext(new Func<Instruction, bool>[6]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, 4),
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "body"),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<CharacterBody>(x, "get_maxShield"),
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 0.5f),
				(Instruction x) => ILPatternMatchingExt.MatchMul(x)
			});
			c.Index += 5;
			c.EmitDelegate<Func<float, float>>((Func<float, float>)((float consume) => 0.5f * (1f + HealthStats.shieldRechargeRateMultAdd) + HealthStats.shieldRechargeRateFlatAdd));
		}

		private static void RecalcAdaptiveArmorDecayRate(ILCursor c)
		{
			c.GotoNext(new Func<Instruction, bool>[6]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 0f),
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<HealthComponent>(x, "adaptiveArmorValue"),
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 40f),
				(Instruction x) => ILPatternMatchingExt.MatchLdarg(x, 1)
			});
			c.Index += 5;
			c.EmitDelegate<Func<float, float>>((Func<float, float>)((float consume) => 40f * (1f + HealthStats.adaptiveArmorDecayRateMultAdd) + HealthStats.adaptiveArmorDecayRateFlatAdd));
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("HDeDeDe.HealthComponentAPI", "HealthComponentAPI", "0.1.1")]
	public sealed class HealthComponentAPIPlugin : BaseUnityPlugin
	{
		private void Awake()
		{
			Log.Init(((BaseUnityPlugin)this).Logger);
		}

		private void OnDestroy()
		{
			HealthComponentAPI.UnsetHealthHook();
			HealthComponentAPI.UnsetHealHooks();
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
	public class OptionalMods
	{
		internal class SandSwept
		{
			private static bool? _enabled;

			public static bool enabled
			{
				get
				{
					if (!_enabled.HasValue)
					{
						_enabled = Chainloader.PluginInfos.ContainsKey("com.TeamSandswept.Sandswept");
					}
					return _enabled.Value;
				}
			}
		}
	}
	internal static class RefVal
	{
		public const float shieldRechargeRate = 0.5f;

		public const float barrierDecayRate = 30f;

		public const float adaptiveArmorDecayRate = 40f;

		public const DifficultyIndex e5 = 7;

		public const float critHealMultiplier = 2f;

		public const float damageCoyoteTimer = 0.2f;
	}
}