Decompiled source of FixateHP v1.0.1

StartingHPTuner.dll

Decompiled a month ago
using System;
using System.Collections;
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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Igor_Does_Nothing")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("StartingHPTuner")]
[assembly: AssemblyTitle("StartingHPTuner")]
[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;
		}
	}
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
namespace Repo.StartingHP.Simple
{
	[BepInPlugin("com.yourname.repo.startinghp.simple", "R.E.P.O Starting HP Simple", "1.0.2")]
	public class StartingHPSimplePlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.yourname.repo.startinghp.simple";

		public const string PluginName = "R.E.P.O Starting HP Simple";

		public const string PluginVersion = "1.0.2";

		internal static volatile bool Enabled = true;

		internal static volatile int StartMaxHealth = 1;

		internal static volatile bool HealthEqualsMax = true;

		internal static volatile int StartHealth = 1;

		private ConfigEntry<bool>? _enabledCfg;

		private ConfigEntry<int>? _startMaxCfg;

		private ConfigEntry<bool>? _healthEqMaxCfg;

		private ConfigEntry<int>? _startHealthCfg;

		private Harmony? _harmony;

		private void Awake()
		{
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Expected O, but got Unknown
			_enabledCfg = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turn the starting HP override on/off.");
			_startMaxCfg = ((BaseUnityPlugin)this).Config.Bind<int>("General", "StartingMaxHealth", 1, "Initial MaxHealth (absolute).");
			_healthEqMaxCfg = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HealthEqualsMax", true, "If true, Health = MaxHealth.");
			_startHealthCfg = ((BaseUnityPlugin)this).Config.Bind<int>("General", "StartingHealth", 1, "If HealthEqualsMax is false, Health will be set to this value.");
			ApplyConfig();
			_enabledCfg.SettingChanged += delegate
			{
				ApplyConfig();
			};
			_startMaxCfg.SettingChanged += delegate
			{
				ApplyConfig();
			};
			_healthEqMaxCfg.SettingChanged += delegate
			{
				ApplyConfig();
			};
			_startHealthCfg.SettingChanged += delegate
			{
				ApplyConfig();
			};
			BuildOrAttachHost();
			_harmony = new Harmony("com.yourname.repo.startinghp.simple");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1} loaded. Enabled={2}, StartMax={3}, EqMax={4}, StartHP={5}", "R.E.P.O Starting HP Simple", "1.0.2", Enabled, StartMaxHealth, HealthEqualsMax, StartHealth));
			SceneManager.activeSceneChanged += OnActiveSceneChanged;
		}

		private void OnDestroy()
		{
			SceneManager.activeSceneChanged -= OnActiveSceneChanged;
			((BaseUnityPlugin)this).Logger.LogWarning((object)"R.E.P.O Starting HP Simple OnDestroy() — patches left active, host persists.");
		}

		private void OnActiveSceneChanged(Scene from, Scene to)
		{
			BuildOrAttachHost();
		}

		private void ApplyConfig()
		{
			Enabled = _enabledCfg.Value;
			StartMaxHealth = Mathf.Max(1, _startMaxCfg.Value);
			HealthEqualsMax = _healthEqMaxCfg.Value;
			StartHealth = Mathf.Clamp(_startHealthCfg.Value, 1, StartMaxHealth);
		}

		private void BuildOrAttachHost()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			GameObject val = GameObject.Find("StartingHPSimpleHost (BepInEx)");
			if ((Object)(object)val == (Object)null)
			{
				val = new GameObject("StartingHPSimpleHost (BepInEx)")
				{
					hideFlags = (HideFlags)61
				};
				Object.DontDestroyOnLoad((Object)(object)val);
			}
			if ((Object)(object)val.GetComponent<StartingHPSimpleHost>() == (Object)null)
			{
				val.AddComponent<StartingHPSimpleHost>();
			}
		}
	}
	public sealed class StartingHPSimpleHost : MonoBehaviour
	{
		internal static ManualLogSource Log = Logger.CreateLogSource("StartingHPSimple");
	}
	internal static class PHAccess
	{
		internal static readonly FieldInfo? FiHealthSet = AccessTools.Field(typeof(PlayerHealth), "healthSet");

		internal static readonly FieldInfo? FiHealth = AccessTools.Field(typeof(PlayerHealth), "health");

		internal static readonly FieldInfo? FiMaxHealth = AccessTools.Field(typeof(PlayerHealth), "maxHealth");

		internal static readonly FieldInfo? FiPlayerAvatar = AccessTools.Field(typeof(PlayerHealth), "playerAvatar");

		internal static readonly FieldInfo? FiIsMenuAvatar = AccessTools.Field(typeof(PlayerHealth), "isMenuAvatar");

		internal static bool TryGetHealthSet(PlayerHealth ph, out bool ready)
		{
			ready = false;
			if (FiHealthSet?.GetValue(ph) is bool flag)
			{
				ready = flag;
				return true;
			}
			return false;
		}

		internal static bool TryGetInts(PlayerHealth ph, out int hp, out int max)
		{
			hp = (max = 0);
			object obj = FiHealth?.GetValue(ph);
			object obj2 = FiMaxHealth?.GetValue(ph);
			if (obj is int num && obj2 is int num2)
			{
				hp = num;
				max = num2;
				return true;
			}
			return false;
		}

		internal static void SetInts(PlayerHealth ph, int hp, int max)
		{
			FiMaxHealth?.SetValue(ph, max);
			FiHealth?.SetValue(ph, Mathf.Clamp(hp, 0, max));
		}

		internal static bool IsMenuAvatar(PlayerHealth ph)
		{
			object obj = FiIsMenuAvatar?.GetValue(ph);
			bool flag = default(bool);
			int num;
			if (obj is bool)
			{
				flag = (bool)obj;
				num = 1;
			}
			else
			{
				num = 0;
			}
			return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
		}

		internal static bool IsLocal(PlayerHealth ph)
		{
			try
			{
				object obj = FiPlayerAvatar?.GetValue(ph);
				if (obj != null)
				{
					Type type = obj.GetType();
					PropertyInfo property = type.GetProperty("isLocal", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (property != null && property.PropertyType == typeof(bool))
					{
						return (bool)(property.GetValue(obj) ?? ((object)false));
					}
					FieldInfo field = type.GetField("isLocal", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (field != null && field.FieldType == typeof(bool))
					{
						return (bool)(field.GetValue(obj) ?? ((object)false));
					}
				}
				PlayerController instance = PlayerController.instance;
				if ((Object)(object)instance != (Object)null && obj != null)
				{
					object obj2 = AccessTools.Field(typeof(PlayerController), "playerAvatarScript")?.GetValue(instance);
					if (obj2 != null && obj == obj2)
					{
						return true;
					}
				}
			}
			catch
			{
			}
			return true;
		}
	}
	internal sealed class StartingHPSimpleApplier : MonoBehaviour
	{
		private PlayerHealth? _ph;

		private bool _applied;

		public static void Attach(PlayerHealth ph)
		{
			if (!((Object)(object)ph == (Object)null))
			{
				StartingHPSimpleApplier startingHPSimpleApplier = ((Component)ph).gameObject.GetComponent<StartingHPSimpleApplier>();
				if ((Object)(object)startingHPSimpleApplier == (Object)null)
				{
					startingHPSimpleApplier = ((Component)ph).gameObject.AddComponent<StartingHPSimpleApplier>();
				}
				startingHPSimpleApplier._ph = ph;
				if (!startingHPSimpleApplier._applied)
				{
					((MonoBehaviour)startingHPSimpleApplier).StartCoroutine(startingHPSimpleApplier.ApplyWhenReady());
				}
			}
		}

		private IEnumerator ApplyWhenReady()
		{
			float timeout = 15f;
			while ((Object)(object)_ph != (Object)null && timeout > 0f)
			{
				if (!StartingHPSimplePlugin.Enabled)
				{
					yield break;
				}
				bool ready;
				bool flag = PHAccess.TryGetHealthSet(_ph, out ready);
				if (flag && ready)
				{
					break;
				}
				timeout -= 0.1f;
				yield return (object)new WaitForSeconds(0.1f);
			}
			if (!((Object)(object)_ph == (Object)null) && !_applied && StartingHPSimplePlugin.Enabled && !PHAccess.IsMenuAvatar(_ph) && PHAccess.IsLocal(_ph) && PHAccess.TryGetInts(_ph, out var hp, out var max))
			{
				int startMaxHealth = StartingHPSimplePlugin.StartMaxHealth;
				int num = (StartingHPSimplePlugin.HealthEqualsMax ? startMaxHealth : Mathf.Clamp(StartingHPSimplePlugin.StartHealth, 1, startMaxHealth));
				PHAccess.SetInts(_ph, num, startMaxHealth);
				_applied = true;
				StartingHPSimpleHost.Log.LogInfo((object)$"[StartingHPSimple] LOCAL swap: {hp}/{max} -> {num}/{startMaxHealth}");
			}
		}
	}
	[HarmonyPatch(typeof(PlayerHealth), "Awake")]
	internal static class PlayerHealth_Awake_Postfix
	{
		private static void Postfix(PlayerHealth __instance)
		{
			if (StartingHPSimplePlugin.Enabled && !((Object)(object)__instance == (Object)null))
			{
				StartingHPSimpleApplier.Attach(__instance);
			}
		}
	}
}