Decompiled source of LifeRegen v1.0.0

plugins/LordValdomod.LifeRegen.dll

Decompiled a week ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace LordValdomod.LifeRegen;

[BepInPlugin("LordValdomod.LifeRegen", "LifeRegen", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "LordValdomod.LifeRegen";

	public const string Name = "LifeRegen";

	public const string Version = "1.0.0";

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<int> HealAmount;

	internal static ConfigEntry<float> Interval;

	private void Awake()
	{
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Expected O, but got Unknown
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Expected O, but got Unknown
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Expected O, but got Unknown
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable health regeneration.");
		HealAmount = ((BaseUnityPlugin)this).Config.Bind<int>("General", "HealAmount", 2, new ConfigDescription("HP restored per tick.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>()));
		Interval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Interval", 1f, new ConfigDescription("Seconds between ticks.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 30f), Array.Empty<object>()));
		GameObject val = new GameObject("LordValdomod.LifeRegen");
		((Object)val).hideFlags = (HideFlags)61;
		Object.DontDestroyOnLoad((Object)(object)val);
		val.AddComponent<RegenBehaviour>();
		((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1} loaded", "LifeRegen", "1.0.0"));
	}
}
internal class RegenBehaviour : MonoBehaviour
{
	private static readonly FieldInfo HealthField = AccessTools.Field(typeof(PlayerHealth), "health");

	private static readonly FieldInfo MaxHealthField = AccessTools.Field(typeof(PlayerHealth), "maxHealth");

	private static readonly FieldInfo DeadSetField = AccessTools.Field(typeof(PlayerAvatar), "deadSet");

	private static readonly FieldInfo DisabledField = AccessTools.Field(typeof(PlayerAvatar), "isDisabled");

	private float _timer;

	private void Update()
	{
		if (!TryGetLocalHealth(out var health))
		{
			_timer = 0f;
			return;
		}
		_timer += Time.deltaTime;
		if (!(_timer < Plugin.Interval.Value))
		{
			_timer = 0f;
			int num = (int)MaxHealthField.GetValue(health) - (int)HealthField.GetValue(health);
			health.Heal(Mathf.Min(Plugin.HealAmount.Value, num), false);
		}
	}

	private static bool TryGetLocalHealth(out PlayerHealth health)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Invalid comparison between Unknown and I4
		health = null;
		if (!Plugin.Enabled.Value)
		{
			return false;
		}
		GameDirector instance = GameDirector.instance;
		if ((Object)(object)instance == (Object)null || (int)instance.currentState != 2)
		{
			return false;
		}
		if (!SemiFunc.RunIsLevel())
		{
			return false;
		}
		PlayerAvatar instance2 = PlayerAvatar.instance;
		if ((Object)(object)instance2 == (Object)null || (bool)DeadSetField.GetValue(instance2) || (bool)DisabledField.GetValue(instance2))
		{
			return false;
		}
		health = instance2.playerHealth;
		if ((Object)(object)health == (Object)null)
		{
			return false;
		}
		int num = (int)HealthField.GetValue(health);
		return num > 0 && num < (int)MaxHealthField.GetValue(health);
	}
}