Decompiled source of HostHealthRegen v1.0.0

HostHealthRegen.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
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 Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Xuaun")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Host-side passive health regeneration for R.E.P.O.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HostHealthRegen")]
[assembly: AssemblyTitle("HostHealthRegen")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 HostHealthRegen
{
	[BepInPlugin("Xuaun.HostHealthRegen", "HostHealthRegen", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "Xuaun.HostHealthRegen";

		public const string Name = "HostHealthRegen";

		public const string Version = "1.0.0";

		private const float PollSeconds = 0.25f;

		internal static ManualLogSource Log;

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<float> _interval;

		private ConfigEntry<int> _baseAmount;

		private ConfigEntry<int> _bonusPerUpgrade;

		private ConfigEntry<float> _delayAfterDamage;

		private ConfigEntry<bool> _slowerWithMorePlayers;

		private ConfigEntry<int> _maxHealthPercent;

		private ConfigEntry<bool> _inShop;

		private ConfigEntry<bool> _inTruckLobby;

		private ConfigEntry<bool> _healEffect;

		private ConfigEntry<bool> _debugLogs;

		private readonly Dictionary<PlayerAvatar, int> _lastHealth = new Dictionary<PlayerAvatar, int>();

		private readonly Dictionary<PlayerAvatar, float> _lastDamageTime = new Dictionary<PlayerAvatar, float>();

		private float _pollTimer;

		private float _tickTimer;

		private void Awake()
		{
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Expected O, but got Unknown
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			((Component)this).transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turns the regeneration on or off.");
			_interval = ((BaseUnityPlugin)this).Config.Bind<float>("Regeneration", "IntervalSeconds", 20f, new ConfigDescription("Seconds between regeneration ticks.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 300f), Array.Empty<object>()));
			_baseAmount = ((BaseUnityPlugin)this).Config.Bind<int>("Regeneration", "BaseAmount", 1, new ConfigDescription("Health restored per tick, before the upgrade bonus.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 50), Array.Empty<object>()));
			_bonusPerUpgrade = ((BaseUnityPlugin)this).Config.Bind<int>("Regeneration", "BonusPerHealthUpgrade", 1, new ConfigDescription("Extra health per tick for each health upgrade the player owns. 0 gives everyone the same amount.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
			_delayAfterDamage = ((BaseUnityPlugin)this).Config.Bind<float>("Regeneration", "DelayAfterDamageSeconds", 0f, new ConfigDescription("A player who took damage is skipped until this many seconds have passed. 0 turns it off.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 120f), Array.Empty<object>()));
			_slowerWithMorePlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("Regeneration", "SlowerWithMorePlayers", false, "Multiplies the interval by the number of living players.");
			_maxHealthPercent = ((BaseUnityPlugin)this).Config.Bind<int>("Regeneration", "MaxHealthPercent", 100, new ConfigDescription("Regeneration stops at this percentage of the player's max health.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			_inShop = ((BaseUnityPlugin)this).Config.Bind<bool>("Where", "RegenInShop", false, "Also regenerate in the shop.");
			_inTruckLobby = ((BaseUnityPlugin)this).Config.Bind<bool>("Where", "RegenInTruckLobby", false, "Also regenerate in the truck lobby between levels.");
			_healEffect = ((BaseUnityPlugin)this).Config.Bind<bool>("Effects", "HealEffect", false, "Plays the game's heal sound and screen effect on every tick.");
			_debugLogs = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DebugLogs", false, "Logs every regeneration tick.");
			Log.LogInfo((object)"HostHealthRegen 1.0.0 loaded.");
		}

		private void Update()
		{
			_pollTimer += Time.deltaTime;
			if (_pollTimer < 0.25f)
			{
				return;
			}
			float pollTimer = _pollTimer;
			_pollTimer = 0f;
			if (!_enabled.Value || !IsHostInPlayableScene())
			{
				ResetState();
				return;
			}
			List<PlayerAvatar> playerList = GameDirector.instance.PlayerList;
			int num = WatchDamage(playerList);
			float num2 = Mathf.Max(1f, _interval.Value);
			if (_slowerWithMorePlayers.Value)
			{
				num2 *= (float)Mathf.Max(1, num);
			}
			_tickTimer += pollTimer;
			if (!(_tickTimer < num2))
			{
				_tickTimer = 0f;
				HealAll(playerList);
			}
		}

		private bool IsHostInPlayableScene()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Invalid comparison between Unknown and I4
			if (!Object.op_Implicit((Object)(object)GameManager.instance) || !Object.op_Implicit((Object)(object)RunManager.instance) || !Object.op_Implicit((Object)(object)GameDirector.instance) || !Object.op_Implicit((Object)(object)LevelGenerator.Instance))
			{
				return false;
			}
			if (!SemiFunc.IsMasterClientOrSingleplayer() || !LevelGenerator.Instance.Generated)
			{
				return false;
			}
			if ((int)GameDirector.instance.currentState != 2)
			{
				return false;
			}
			if (SemiFunc.RunIsShop())
			{
				return _inShop.Value;
			}
			if (SemiFunc.RunIsLobby())
			{
				return _inTruckLobby.Value;
			}
			if (SemiFunc.RunIsLevel())
			{
				return !SemiFunc.RunIsArena();
			}
			return false;
		}

		private int WatchDamage(List<PlayerAvatar> players)
		{
			int num = 0;
			foreach (PlayerAvatar player in players)
			{
				if (IsAlive(player))
				{
					num++;
					int health = player.playerHealth.health;
					if (_lastHealth.TryGetValue(player, out var value) && health < value)
					{
						_lastDamageTime[player] = Time.time;
					}
					_lastHealth[player] = health;
				}
			}
			return num;
		}

		private void HealAll(List<PlayerAvatar> players)
		{
			foreach (PlayerAvatar player in players)
			{
				if (!IsAlive(player))
				{
					continue;
				}
				float value = _delayAfterDamage.Value;
				if (value > 0f && _lastDamageTime.TryGetValue(player, out var value2) && Time.time - value2 < value)
				{
					continue;
				}
				PlayerHealth playerHealth = player.playerHealth;
				int num = Mathf.CeilToInt((float)playerHealth.maxHealth * ((float)_maxHealthPercent.Value / 100f));
				int num2 = num - playerHealth.health;
				if (num2 <= 0)
				{
					continue;
				}
				int value3 = 0;
				if (Object.op_Implicit((Object)(object)StatsManager.instance) && player.steamID != null)
				{
					StatsManager.instance.playerUpgradeHealth.TryGetValue(player.steamID, out value3);
				}
				int num3 = Mathf.Min(_baseAmount.Value + _bonusPerUpgrade.Value * value3, num2);
				if (num3 > 0)
				{
					playerHealth.HealOther(num3, _healEffect.Value);
					if (_debugLogs.Value)
					{
						Log.LogInfo((object)$"{player.playerName}: +{num3} ({playerHealth.health}/{playerHealth.maxHealth} before the heal)");
					}
				}
			}
		}

		private static bool IsAlive(PlayerAvatar player)
		{
			if (Object.op_Implicit((Object)(object)player) && Object.op_Implicit((Object)(object)player.playerHealth) && !player.deadSet && !player.isDisabled)
			{
				return player.playerHealth.health > 0;
			}
			return false;
		}

		private void ResetState()
		{
			_tickTimer = 0f;
			if (_lastHealth.Count > 0)
			{
				_lastHealth.Clear();
				_lastDamageTime.Clear();
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}