Decompiled source of CrouchHeal v2.0.0

BepInEx/plugins/CrouchHeal/CrouchHeal.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 CrouchHeal
{
	[BepInPlugin("xingKong.CrouchHeal", "Crouch Heal", "2.0.0")]
	[BepInProcess("REPO.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "xingKong.CrouchHeal";

		public const string PluginName = "Crouch Heal";

		public const string PluginVersion = "2.0.0";

		internal static ManualLogSource Log;

		internal static PluginConfig Cfg;

		private static readonly FieldInfo HealthField = typeof(PlayerHealth).GetField("health", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo MaxHealthField = typeof(PlayerHealth).GetField("maxHealth", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static int _healedThisLevel;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_00aa: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Cfg = new PluginConfig(((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("xingKong.CrouchHeal");
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(PlayerController), "FixedUpdate", (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					Log.LogError((object)"[下蹲恢复] 找不到 PlayerController.FixedUpdate,补丁未应用。");
					return;
				}
				_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "Prefix", (Type[])null, (Type[])null)), new HarmonyMethod(AccessTools.Method(typeof(Plugin), "Postfix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				SceneManager.sceneLoaded += delegate
				{
					_healedThisLevel = 0;
				};
				Log.LogInfo((object)string.Format("{0} v{1} 已加载:每次回 {2} 血 / 耗 {3} 耐力,上限 {4}。", "Crouch Heal", "2.0.0", Cfg.HealPerSquat.Value, Cfg.StaminaPerSquat.Value, Cfg.HealCap.Value));
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"[下蹲恢复] 挂接失败:{arg}");
			}
		}

		private static void Prefix(PlayerController __instance, out bool __state)
		{
			__state = (Object)(object)__instance != (Object)null && __instance.Crouching;
		}

		private static void Postfix(PlayerController __instance, bool __state)
		{
			if (Cfg == null || !Cfg.Enabled.Value || (Object)(object)__instance == (Object)null || !__state || __instance.Crouching)
			{
				return;
			}
			try
			{
				PlayerAvatar val = SemiFunc.PlayerAvatarLocal();
				if ((Object)(object)val == (Object)null || (Object)(object)val.playerHealth == (Object)null || (Object)(object)__instance.playerAvatarScript != (Object)(object)val)
				{
					return;
				}
				int num = ((HealthField != null && HealthField.GetValue(val.playerHealth) is int num2) ? num2 : 0);
				int num3 = ((MaxHealthField != null && MaxHealthField.GetValue(val.playerHealth) is int num4) ? num4 : 0);
				if (num3 <= 0 || num >= num3)
				{
					return;
				}
				int num5 = Mathf.Min(Cfg.HealPerSquat.Value, num3 - num);
				if (Cfg.HealCap.Value > 0)
				{
					num5 = Mathf.Min(num5, Cfg.HealCap.Value - _healedThisLevel);
				}
				if (num5 <= 0)
				{
					return;
				}
				float num6 = Cfg.StaminaPerSquat.Value;
				if (Cfg.ProportionalStamina.Value && Cfg.HealPerSquat.Value > 0)
				{
					num6 *= (float)num5 / (float)Cfg.HealPerSquat.Value;
				}
				if (!(num6 > 0f) || !(__instance.EnergyCurrent < num6))
				{
					if (num6 > 0f)
					{
						__instance.EnergyCurrent -= num6;
					}
					val.playerHealth.Heal(num5, true);
					_healedThisLevel += num5;
					if (Cfg.Verbose.Value)
					{
						Log.LogInfo((object)$"[下蹲恢复] 回血 {num5}(累计 {_healedThisLevel}/{Cfg.HealCap.Value}),消耗耐力 {num6:F1},当前血量 {num + num5}/{num3}");
					}
				}
			}
			catch (Exception ex)
			{
				if (Cfg.Verbose.Value)
				{
					Log.LogDebug((object)("[下蹲恢复] 执行失败:" + ex.Message));
				}
			}
		}

		private void OnDestroy()
		{
			try
			{
				Harmony harmony = _harmony;
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
			}
			catch
			{
			}
		}
	}
	internal sealed class PluginConfig
	{
		public readonly ConfigEntry<bool> Enabled;

		public readonly ConfigEntry<int> HealPerSquat;

		public readonly ConfigEntry<int> StaminaPerSquat;

		public readonly ConfigEntry<int> HealCap;

		public readonly ConfigEntry<bool> ProportionalStamina;

		public readonly ConfigEntry<bool> Verbose;

		public PluginConfig(ConfigFile cfg)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Expected O, but got Unknown
			Enabled = cfg.Bind<bool>("1. 通用", "启用", true, "总开关。");
			HealPerSquat = cfg.Bind<int>("2. 数值", "每次回血", 10, new ConfigDescription("每次蹲起恢复的生命值。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			StaminaPerSquat = cfg.Bind<int>("2. 数值", "每次消耗耐力", 10, new ConfigDescription("每次蹲起消耗的耐力值。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			HealCap = cfg.Bind<int>("2. 数值", "每关回血上限", 50, new ConfigDescription("每个关卡最多通过蹲起恢复的生命值,0 = 不限制。切换关卡时重置。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 999), Array.Empty<object>()));
			ProportionalStamina = cfg.Bind<bool>("2. 数值", "按实际回血比例扣耐力", true, "例如只差 4 点血时只恢复 4 点、扣 4 点耐力,而不是固定扣 10 点。");
			Verbose = cfg.Bind<bool>("3. 日志", "详细日志", false, "每次触发都写一行日志。");
		}
	}
}