Decompiled source of StaminUp v1.0.0

BepInEx/plugins/OverexertStamina/OverexertStamina.dll

Decompiled a month ago
using System;
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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("OverexertStamina")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("OverexertStamina")]
[assembly: AssemblyTitle("OverexertStamina")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace OverexertStamina
{
	internal static class GameFontsDebugger
	{
		internal static void DumpFonts()
		{
			Font[] array = Resources.FindObjectsOfTypeAll<Font>();
			Font[] array2 = array;
			foreach (Font val in array2)
			{
				Plugin.Log.LogInfo((object)("[OverexertStamina] Found game font: " + ((Object)val).name));
			}
		}
	}
	internal static class HealthUseUI
	{
		private static GameObject _canvas;

		private static Text _text;

		private static Font _gameFont;

		private static HealthUseUIController _controller;

		public static void ShowUsingHealth()
		{
			EnsureUI();
			_text.text = "Stamina depleted - using health instead!";
			((Behaviour)_text).enabled = true;
			_controller.StartDisplay();
		}

		private static void EnsureUI()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_canvas != (Object)null))
			{
				_canvas = new GameObject("OverexertStamina_UI");
				Canvas val = _canvas.AddComponent<Canvas>();
				val.renderMode = (RenderMode)0;
				_canvas.AddComponent<CanvasScaler>();
				_canvas.AddComponent<GraphicRaycaster>();
				Object.DontDestroyOnLoad((Object)(object)_canvas);
				_controller = _canvas.AddComponent<HealthUseUIController>();
				GameObject val2 = new GameObject("OverexertStamina_Text");
				val2.transform.SetParent(_canvas.transform, false);
				_text = val2.AddComponent<Text>();
				_text.font = GetGameFont() ?? Resources.GetBuiltinResource<Font>("Arial.ttf");
				_text.fontSize = 28;
				_text.alignment = (TextAnchor)4;
				((Graphic)_text).color = Color.red;
				((Graphic)_text).raycastTarget = false;
				RectTransform rectTransform = ((Graphic)_text).rectTransform;
				rectTransform.anchorMin = new Vector2(0.5f, 0.8f);
				rectTransform.anchorMax = new Vector2(0.5f, 0.8f);
				rectTransform.anchoredPosition = Vector2.zero;
				rectTransform.sizeDelta = new Vector2(800f, 100f);
				((Behaviour)_text).enabled = false;
				_controller.SetText(_text);
			}
		}

		private static Font GetGameFont()
		{
			if ((Object)(object)_gameFont != (Object)null)
			{
				return _gameFont;
			}
			Font[] array = Resources.FindObjectsOfTypeAll<Font>();
			if (array != null && array.Length != 0)
			{
				_gameFont = array[0];
				Plugin.Log.LogInfo((object)("OverexertStamina: using in-game font '" + ((Object)_gameFont).name + "' for UI."));
				return _gameFont;
			}
			Plugin.Log.LogInfo((object)"OverexertStamina: no in-game font found, using Arial.");
			return null;
		}
	}
	internal class HealthUseUIController : MonoBehaviour
	{
		private Text _text;

		private float _timer;

		private const float DISPLAY_TIME = 1f;

		public void SetText(Text text)
		{
			_text = text;
		}

		public void StartDisplay()
		{
			_timer = 1f;
			if ((Object)(object)_text != (Object)null)
			{
				((Behaviour)_text).enabled = true;
			}
		}

		private void Update()
		{
			if (!((Object)(object)_text == (Object)null) && ((Behaviour)_text).enabled)
			{
				_timer -= Time.deltaTime;
				if (_timer <= 0f)
				{
					((Behaviour)_text).enabled = false;
				}
			}
		}
	}
	internal class OverexertStaminaUI : MonoBehaviour
	{
		private static OverexertStaminaUI _instance;

		private Text _text;

		private float _showUntil;

		internal static void Init()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			if (!((Object)(object)_instance != (Object)null))
			{
				GameObject val = new GameObject("OverexertStamina_UI");
				Object.DontDestroyOnLoad((Object)(object)val);
				_instance = val.AddComponent<OverexertStaminaUI>();
			}
		}

		private void Awake()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			Canvas val = ((Component)this).gameObject.AddComponent<Canvas>();
			val.renderMode = (RenderMode)0;
			((Component)this).gameObject.AddComponent<CanvasScaler>();
			((Component)this).gameObject.AddComponent<GraphicRaycaster>();
			GameObject val2 = new GameObject("Text");
			val2.transform.SetParent(((Component)this).transform, false);
			_text = val2.AddComponent<Text>();
			_text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
			_text.fontSize = 32;
			_text.alignment = (TextAnchor)1;
			((Graphic)_text).color = Color.red;
			_text.text = "";
			((Behaviour)_text).enabled = false;
			RectTransform rectTransform = ((Graphic)_text).rectTransform;
			rectTransform.anchorMin = new Vector2(0.5f, 1f);
			rectTransform.anchorMax = new Vector2(0.5f, 1f);
			rectTransform.pivot = new Vector2(0.5f, 1f);
			rectTransform.anchoredPosition = new Vector2(0f, -40f);
		}

		private void Update()
		{
			if (!((Object)(object)_text == (Object)null) && Time.unscaledTime > _showUntil)
			{
				((Behaviour)_text).enabled = false;
			}
		}

		internal static void Show(string message, float seconds = 1.5f)
		{
			if (!((Object)(object)_instance == (Object)null) && !((Object)(object)_instance._text == (Object)null))
			{
				_instance._text.text = message;
				((Behaviour)_instance._text).enabled = true;
				_instance._showUntil = Time.unscaledTime + seconds;
			}
		}
	}
	[BepInPlugin("OverexertStamina", "OverexertStamina", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony = null;

		public const string Id = "OverexertStamina";

		internal static ManualLogSource Log { get; private set; }

		public static string Name => "OverexertStamina";

		public static string Version => "1.0.0";

		private void Awake()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("OverexertStamina");
			_harmony.PatchAll();
			Log.LogInfo((object)"Plugin OverexertStamina is loaded!");
		}
	}
	[HarmonyPatch(typeof(Character), "UseStamina")]
	public static class StaminaPatch
	{
		private const BindingFlags BF = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

		private static void Prefix(Character __instance, float usage, bool useBonusStamina)
		{
			if (!__instance.OutOfStamina())
			{
				return;
			}
			object obj = ((object)__instance).GetType().GetField("data", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(__instance);
			if (obj != null)
			{
				FieldInfo field = obj.GetType().GetField("extraStamina", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					float num = (float)field.GetValue(obj);
					if (num < 0.05f)
					{
						field.SetValue(obj, 0.05f);
						Plugin.Log.LogInfo((object)"OverexertStamina: topped up extraStamina to 0.05 so action can continue.");
					}
				}
			}
			object obj2 = ((object)__instance).GetType().GetField("refs", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(__instance);
			if (obj2 == null)
			{
				Plugin.Log.LogInfo((object)"OverexertStamina: no refs on Character, can't apply injury.");
				return;
			}
			object obj3 = obj2.GetType().GetField("afflictions", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(obj2);
			if (obj3 == null)
			{
				Plugin.Log.LogInfo((object)"OverexertStamina: refs.afflictions is null, can't apply injury.");
				return;
			}
			Type type = obj3.GetType();
			Type nestedType = type.GetNestedType("STATUSTYPE", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (nestedType == null)
			{
				Plugin.Log.LogInfo((object)"OverexertStamina: STATUSTYPE enum not found.");
				return;
			}
			object obj4;
			try
			{
				obj4 = Enum.Parse(nestedType, "Injury");
			}
			catch
			{
				Plugin.Log.LogInfo((object)"OverexertStamina: could not parse STATUSTYPE.Injury.");
				return;
			}
			MethodInfo method = type.GetMethod("AddStatus", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[4]
			{
				nestedType,
				typeof(float),
				typeof(bool),
				typeof(bool)
			}, null);
			if (method == null)
			{
				Plugin.Log.LogInfo((object)"OverexertStamina: AddStatus method with (STATUSTYPE, float, bool, bool) not found.");
				return;
			}
			float num2 = 0.05f;
			object arg = method.Invoke(obj3, new object[4] { obj4, num2, false, true });
			Plugin.Log.LogInfo((object)$"OverexertStamina: stamina was 0, applied injury {num2}, AddStatus returned {arg}.");
			HealthUseUI.ShowUsingHealth();
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}