Decompiled source of RestorePlayerStatus v1.1.2

BepInEx\plugins\RestorePlayerStatus\MyFirstRepoMod.dll

Decompiled 11 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("MyFirstRepoMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MyFirstRepoMod")]
[assembly: AssemblyTitle("MyFirstRepoMod")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MyFirstRepoMod;

public class Class1
{
}
[BepInPlugin("com.peachh.restorestatus", "RestorePlayerStatus", "1.1.2")]
public class Plugin : BaseUnityPlugin
{
	private class CanvasCoroutineHost : MonoBehaviour
	{
	}

	internal static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.peachh.restorestatus";

		public const string PLUGIN_NAME = "RestorePlayerStatus";

		public const string PLUGIN_VERSION = "1.1.2";

		public const string PLUGIN_AUTHOR = "peachh";
	}

	private ConfigEntry<KeyCode> _restoreKey;

	private ConfigEntry<float> _messageDuration;

	private ConfigEntry<string> _successMessage;

	private ConfigEntry<string> _noPlayerMessage;

	private ConfigEntry<float> _messagePosY;

	private Harmony _harmony;

	private static Plugin _instance;

	private bool _keyWasDown;

	private static Text _uiText;

	private static Coroutine _hideCoroutine;

	private static MonoBehaviour _coroutineHost;

	private void Awake()
	{
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Expected O, but got Unknown
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Expected O, but got Unknown
		_instance = this;
		_restoreKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "RestoreKey", (KeyCode)104, "一键恢复玩家状态(血量 + 体力)的触发按键");
		_messageDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Messages", "DisplayDuration", 2f, "屏幕提示显示时长(秒)");
		_successMessage = ((BaseUnityPlugin)this).Config.Bind<string>("Messages", "SuccessText", "已将血量与体力恢复至最大值", "恢复成功时显示的提示文字");
		_noPlayerMessage = ((BaseUnityPlugin)this).Config.Bind<string>("Messages", "NoPlayerText", "未检测到玩家,请进入对局后再操作", "未找到玩家时显示的提示文字");
		_messagePosY = ((BaseUnityPlugin)this).Config.Bind<float>("Messages", "TextPosY", -250f, "提示文字在屏幕垂直方向上的偏移(0=正中,负值=向下,参考分辨率1920x1080)");
		_harmony = new Harmony("com.peachh.restorestatus");
		try
		{
			MethodInfo method = typeof(PlayerAvatar).GetMethod("Update", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method != null)
			{
				_harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnPlayerUpdatePostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"已挂载 PlayerAvatar.Update 后置钩子,按键检测就绪。");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"未找到 PlayerAvatar.Update 方法。");
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Harmony 挂载失败: " + ex.Message));
		}
		CreateCanvasUI();
		SceneManager.sceneLoaded += OnSceneLoaded;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"插件 RestorePlayerStatus v1.1.2 加载成功!");
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		if ((Object)(object)_uiText == (Object)null)
		{
			CreateCanvasUI();
		}
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}

	private void CreateCanvasUI()
	{
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if ((Object)(object)_uiText != (Object)null && (Object)(object)((Component)_uiText).gameObject != (Object)null)
			{
				return;
			}
			Font val = null;
			try
			{
				val = Font.CreateDynamicFontFromOSFont("Arial", 16);
			}
			catch
			{
			}
			if ((Object)(object)val == (Object)null)
			{
				try
				{
					val = Resources.GetBuiltinResource<Font>("Arial.ttf");
				}
				catch
				{
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				try
				{
					Object builtinResource = Resources.GetBuiltinResource(typeof(Font), "LegacyRuntime.ttf");
					val = (Font)(object)((builtinResource is Font) ? builtinResource : null);
				}
				catch
				{
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[CreateCanvasUI] 所有字体加载方式均失败!");
				return;
			}
			GameObject val2 = new GameObject("RestorePlayerStatus_Canvas");
			Object.DontDestroyOnLoad((Object)(object)val2);
			val2.layer = 5;
			Canvas obj4 = val2.AddComponent<Canvas>();
			obj4.renderMode = (RenderMode)0;
			obj4.sortingOrder = 32767;
			CanvasScaler obj5 = val2.AddComponent<CanvasScaler>();
			obj5.uiScaleMode = (ScaleMode)1;
			obj5.referenceResolution = new Vector2(1920f, 1080f);
			val2.AddComponent<GraphicRaycaster>();
			_coroutineHost = (MonoBehaviour)(object)val2.AddComponent<CanvasCoroutineHost>();
			GameObject val3 = new GameObject("NotificationText");
			val3.transform.SetParent(val2.transform, false);
			val3.layer = 5;
			RectTransform obj6 = val3.AddComponent<RectTransform>();
			obj6.anchorMin = new Vector2(0.5f, 0.5f);
			obj6.anchorMax = new Vector2(0.5f, 0.5f);
			obj6.pivot = new Vector2(0.5f, 0.5f);
			obj6.anchoredPosition = new Vector2(0f, _messagePosY.Value);
			obj6.sizeDelta = new Vector2(900f, 60f);
			_uiText = val3.AddComponent<Text>();
			_uiText.font = val;
			_uiText.fontSize = 22;
			_uiText.alignment = (TextAnchor)4;
			((Graphic)_uiText).color = Color.white;
			_uiText.text = "";
			Outline obj7 = val3.AddComponent<Outline>();
			((Shadow)obj7).effectColor = Color.black;
			((Shadow)obj7).effectDistance = new Vector2(2f, -2f);
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("[CreateCanvasUI] 创建失败: " + ex.Message));
		}
	}

	private static void OnPlayerUpdatePostfix()
	{
		_instance?.CheckRestoreKey();
	}

	private void CheckRestoreKey()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		bool key = Input.GetKey(_restoreKey.Value);
		if (key && !_keyWasDown)
		{
			if (SemiFunc.MenuLevel())
			{
				ShowMessage(_noPlayerMessage.Value, Color.red, _messageDuration.Value);
			}
			else
			{
				TryRestoreLocalPlayer();
			}
		}
		_keyWasDown = key;
	}

	private void ShowMessage(string text, Color color, float duration)
	{
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_uiText == (Object)null || (Object)(object)((Component)_uiText).gameObject == (Object)null)
		{
			CreateCanvasUI();
		}
		if (!((Object)(object)_uiText == (Object)null) && !((Object)(object)((Component)_uiText).gameObject == (Object)null) && !((Object)(object)_coroutineHost == (Object)null))
		{
			_uiText.text = text;
			((Graphic)_uiText).color = color;
			if (_hideCoroutine != null)
			{
				_coroutineHost.StopCoroutine(_hideCoroutine);
				_hideCoroutine = null;
			}
			_hideCoroutine = _coroutineHost.StartCoroutine(HideMessage(duration));
		}
	}

	private IEnumerator HideMessage(float delay)
	{
		yield return (object)new WaitForSeconds(Math.Max(0.5f, delay));
		if ((Object)(object)_uiText != (Object)null)
		{
			_uiText.text = "";
		}
	}

	private void TryRestoreLocalPlayer()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0299: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		PlayerAvatar instance = PlayerAvatar.instance;
		if ((Object)(object)instance == (Object)null)
		{
			ShowMessage(_noPlayerMessage.Value, Color.red, _messageDuration.Value);
			return;
		}
		Type type = ((object)instance).GetType();
		FieldInfo fieldInfo = FindField(type, "isLocal");
		if (fieldInfo != null && !(bool)fieldInfo.GetValue(instance))
		{
			ShowMessage(_noPlayerMessage.Value, Color.red, _messageDuration.Value);
			return;
		}
		TrySetFieldToMaxNested(instance, "playerHealth", "health", "maxHealth", "血量");
		try
		{
			bool flag = false;
			FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo2 in fields)
			{
				if (fieldInfo2.FieldType.IsValueType || fieldInfo2.FieldType == typeof(string))
				{
					continue;
				}
				object value = fieldInfo2.GetValue(instance);
				if (value != null)
				{
					Type type2 = value.GetType();
					FieldInfo fieldInfo3 = FindField(type2, "EnergyCurrent");
					FieldInfo fieldInfo4 = FindField(type2, "EnergyStart");
					if (fieldInfo3 != null && fieldInfo4 != null)
					{
						object value2 = fieldInfo4.GetValue(value);
						fieldInfo3.SetValue(value, Convert.ChangeType(value2, fieldInfo3.FieldType));
						flag = true;
						break;
					}
				}
			}
			if (!flag)
			{
				fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (FieldInfo fieldInfo5 in fields)
				{
					if (fieldInfo5.FieldType.IsValueType || fieldInfo5.FieldType == typeof(string))
					{
						continue;
					}
					object value3 = fieldInfo5.GetValue(instance);
					if (value3 == null)
					{
						continue;
					}
					FieldInfo[] fields2 = fieldInfo5.FieldType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					foreach (FieldInfo fieldInfo6 in fields2)
					{
						if (fieldInfo6.FieldType.IsValueType || fieldInfo6.FieldType == typeof(string))
						{
							continue;
						}
						object value4 = fieldInfo6.GetValue(value3);
						if (value4 != null)
						{
							Type type3 = value4.GetType();
							FieldInfo fieldInfo7 = FindField(type3, "EnergyCurrent");
							FieldInfo fieldInfo8 = FindField(type3, "EnergyStart");
							if (fieldInfo7 != null && fieldInfo8 != null)
							{
								object value5 = fieldInfo8.GetValue(value4);
								fieldInfo7.SetValue(value4, Convert.ChangeType(value5, fieldInfo7.FieldType));
								flag = true;
								break;
							}
						}
					}
					if (flag)
					{
						break;
					}
				}
			}
		}
		catch
		{
		}
		ShowMessage(_successMessage.Value, Color.white, _messageDuration.Value);
	}

	private bool TrySetFieldToMaxNested(object player, string nestField, string currentField, string maxField, string label)
	{
		FieldInfo field = player.GetType().GetField(nestField, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (field == null)
		{
			return false;
		}
		object value = field.GetValue(player);
		if (value == null)
		{
			return false;
		}
		Type type = value.GetType();
		FieldInfo fieldInfo = FindField(type, currentField);
		FieldInfo fieldInfo2 = FindField(type, maxField);
		if (fieldInfo == null || fieldInfo2 == null)
		{
			return false;
		}
		object value2 = fieldInfo2.GetValue(value);
		fieldInfo.SetValue(value, Convert.ChangeType(value2, fieldInfo.FieldType));
		return true;
	}

	private static FieldInfo FindField(Type type, string fieldName)
	{
		FieldInfo field = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (field != null)
		{
			return field;
		}
		FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		foreach (FieldInfo fieldInfo in fields)
		{
			if (string.Equals(fieldInfo.Name, fieldName, StringComparison.OrdinalIgnoreCase))
			{
				return fieldInfo;
			}
		}
		return null;
	}
}