Decompiled source of HealthRegen v2.9.6

plugins/HealthRegen.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
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("HealthRegen")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HealthRegen")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("93961c9d-b41e-48ae-ab31-28ef4e27b5dc")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("repo.healthregen", "Auto Health Regen", "1.0.0")]
public class AutoHealthRegen : BaseUnityPlugin
{
	[HarmonyPatch(typeof(PlayerHealth), "Start")]
	public class RegenPatch
	{
		private static void Postfix(PlayerHealth __instance)
		{
			if (!((Object)(object)__instance == (Object)null) && !((Object)(object)((Component)__instance).gameObject.GetComponent<RegenUpdater>() != (Object)null))
			{
				((Component)__instance).gameObject.AddComponent<RegenUpdater>().Init(__instance);
				Log.LogInfo((object)("[Regen] Adicionado RegenUpdater em " + ((Object)__instance).name));
			}
		}
	}

	public class RegenUpdater : MonoBehaviour
	{
		private FieldInfo healthField;

		private FieldInfo maxHealthField;

		private MethodInfo healMethod;

		private PlayerHealth playerHealth;

		private bool isRegenActive = false;

		public void Init(PlayerHealth instance)
		{
			playerHealth = instance;
			Type type = ((object)instance).GetType();
			healthField = type.GetField("health", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			maxHealthField = type.GetField("maxHealth", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			healMethod = type.GetMethod("Heal", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[2]
			{
				typeof(int),
				typeof(bool)
			}, null);
			if (healthField == null || maxHealthField == null || healMethod == null)
			{
				Log.LogError((object)("[Regen] Erro ao inicializar reflection. " + $"healthField nulo? {healthField == null} | " + $"maxHealthField nulo? {maxHealthField == null} | " + $"healMethod nulo? {healMethod == null}"));
			}
		}

		private void Update()
		{
			if (!((Object)(object)playerHealth == (Object)null) && !(healthField == null) && !(maxHealthField == null))
			{
				int num = (int)healthField.GetValue(playerHealth);
				int num2 = (int)maxHealthField.GetValue(playerHealth);
				if (!isRegenActive && num < num2)
				{
					isRegenActive = true;
					((MonoBehaviour)this).InvokeRepeating("RegenTick", (float)RegenInterval.Value, (float)RegenInterval.Value);
				}
				if (isRegenActive && num >= num2)
				{
					((MonoBehaviour)this).CancelInvoke("RegenTick");
					isRegenActive = false;
				}
			}
		}

		private void RegenTick()
		{
			if ((Object)(object)playerHealth == (Object)null || healthField == null || maxHealthField == null || healMethod == null)
			{
				((MonoBehaviour)this).CancelInvoke("RegenTick");
				isRegenActive = false;
				return;
			}
			int num = (int)healthField.GetValue(playerHealth);
			int num2 = (int)maxHealthField.GetValue(playerHealth);
			if (num <= 0 || num >= num2)
			{
				((MonoBehaviour)this).CancelInvoke("RegenTick");
				isRegenActive = false;
				return;
			}
			try
			{
				object[] parameters = new object[2] { RegenAmount.Value, false };
				healMethod.Invoke(playerHealth, parameters);
				if (DebugLog.Value)
				{
					int num3 = (int)healthField.GetValue(playerHealth);
					Log.LogInfo((object)$"[Regen] +{RegenAmount.Value} HP → {num3}/{num2}");
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("[Regen] Erro ao aplicar cura: " + ex));
				((MonoBehaviour)this).CancelInvoke("RegenTick");
				isRegenActive = false;
			}
		}
	}

	public static ConfigEntry<int> RegenInterval;

	public static ConfigEntry<int> RegenAmount;

	public static ConfigEntry<bool> DebugLog;

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Expected O, but got Unknown
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Log.LogInfo((object)"[Init] AutoHealthRegen carregando...");
		RegenInterval = ((BaseUnityPlugin)this).Config.Bind<int>("Regen Settings", "RegenInterval", 60, new ConfigDescription("Intervalo em segundos entre ticks de regeneração.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 60), Array.Empty<object>()));
		RegenAmount = ((BaseUnityPlugin)this).Config.Bind<int>("Regen Settings", "RegenAmount", 20, new ConfigDescription("Quantidade de HP restaurada por tick.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
		DebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Regen Settings", "DebugLog", true, "Ativar log de debug.");
		Harmony val = new Harmony("repo.healthregen");
		val.PatchAll();
		Log.LogInfo((object)"[Init] Patch aplicado com sucesso.");
	}
}