Decompiled source of GrabRecharge v2.0.0

PlayerGrabRecharge.dll

Decompiled 6 months 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("PlayerGrabRecharge")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PlayerGrabRecharge")]
[assembly: AssemblyTitle("PlayerGrabRecharge")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace PlayerGrabRecharge
{
	[BepInPlugin("modx.playergrabrecharge", "Player Grab Recharge", "2.0.0")]
	public class PlayerGrabRechargePlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(LevelGenerator), "Generate")]
		internal class LevelGeneratorGeneratePatch
		{
			[HarmonyPostfix]
			public static void Postfix()
			{
				ResetModPowerAndState();
				Log.LogInfo((object)"[PGR:Level] Mod Power reset to 100% on LevelGenerator.Generate.");
			}
		}

		[HarmonyPatch(typeof(LevelGenerator), "Start")]
		internal class LevelGeneratorStartPatch
		{
			[HarmonyPostfix]
			public static void Postfix()
			{
				ResetModPowerAndState();
				Log.LogInfo((object)"[PGR:Level] Mod Power reset to 100% on LevelGenerator.Start.");
			}
		}

		[HarmonyPatch(typeof(GoalUI))]
		internal class GoalUIPatch
		{
			[HarmonyPatch("Update")]
			[HarmonyPostfix]
			public static void AddChargeBar(ref TextMeshProUGUI ___Text)
			{
				if (ShowModPowerUI.Value && SemiFunc.RunIsLevel() && !((Object)(object)___Text == (Object)null))
				{
					InjectBar(___Text);
				}
			}
		}

		[HarmonyPatch(typeof(EnergyUI))]
		internal class EnergyUIPatch
		{
			[HarmonyPatch("Update")]
			[HarmonyPostfix]
			public static void AddChargeBar(ref TextMeshProUGUI ___Text)
			{
				if (ShowModPowerUI.Value && SemiFunc.RunIsShop() && !((Object)(object)___Text == (Object)null))
				{
					InjectBar(___Text);
				}
			}
		}

		internal static ManualLogSource Log;

		private static Harmony _harmony;

		public static ConfigEntry<KeyCode> RechargeKey;

		public static ConfigEntry<bool> ToggleRechargeMode;

		public static ConfigEntry<int> ManualRechargeRate;

		public static ConfigEntry<bool> DebugLogging;

		public static ConfigEntry<bool> ShowModPowerUI;

		private const int RECHARGE_RATE_MIN = 1;

		private const int RECHARGE_RATE_MAX = 30;

		private const int MOD_POWER_DRAIN_PER_SECOND = 1;

		private const int MOD_POWER_REGEN_PERCENT_PER_SECOND = 1;

		internal static bool _isRecharging = false;

		internal static float _currentModPower = 10f;

		internal const float MAX_MOD_POWER = 10f;

		internal static float _cooldownTimer = 0f;

		private const float COOLDOWN_DURATION = 2f;

		internal static ItemBattery _locallyGrabbedBattery = null;

		private static GameObject _previousGrabbedObject = null;

		private void Awake()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
			RechargeKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Recharge Settings", "Recharge Key", (KeyCode)122, "Key to manually recharge grabbed item.");
			ToggleRechargeMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Recharge Settings", "Toggle Mode", false, "True = Toggle, False = Hold to recharge.");
			ManualRechargeRate = ((BaseUnityPlugin)this).Config.Bind<int>("Recharge Settings", "Recharge Rate", 10, new ConfigDescription("Battery charge per second.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 30), Array.Empty<object>()));
			DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Enable Debug Logging", false, "Enable detailed log output for troubleshooting.");
			ShowModPowerUI = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "Show Charge Bar", true, "Enable on-screen Charge UI.");
			PatchMethods();
			Log.LogInfo((object)"[PGR] Plugin initialized.");
		}

		private void PatchMethods()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			_harmony.Patch((MethodBase)AccessTools.Method(typeof(PlayerController), "Update", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(PlayerGrabRechargePlugin), "UpdatePostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			_harmony.PatchAll(typeof(GoalUIPatch));
			_harmony.PatchAll(typeof(EnergyUIPatch));
			_harmony.PatchAll(typeof(LevelGeneratorGeneratePatch));
			_harmony.PatchAll(typeof(LevelGeneratorStartPatch));
			Log.LogInfo((object)"[PGR] All patches applied.");
		}

		private void OnDestroy()
		{
			_harmony.UnpatchSelf();
		}

		private static void UpdatePostfix(PlayerController __instance)
		{
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			PhotonView component = ((Component)__instance).GetComponent<PhotonView>();
			if ((!((Object)(object)component == (Object)null) || PhotonNetwork.IsConnected) && (!((Object)(object)component != (Object)null) || !component.IsMine))
			{
				return;
			}
			if ((Object)(object)__instance.physGrabObject != (Object)(object)_previousGrabbedObject)
			{
				_isRecharging = false;
				if ((Object)(object)_locallyGrabbedBattery != (Object)null && DebugLogging.Value)
				{
					Log.LogInfo((object)("[PGR:Release] Releasing grab of " + ((Object)_locallyGrabbedBattery).name));
				}
				GameObject physGrabObject = __instance.physGrabObject;
				_locallyGrabbedBattery = ((physGrabObject != null) ? physGrabObject.GetComponent<ItemBattery>() : null);
				if ((Object)(object)_locallyGrabbedBattery != (Object)null)
				{
					PhotonView component2 = ((Component)_locallyGrabbedBattery).GetComponent<PhotonView>();
					if (PhotonNetwork.IsConnectedAndReady && (Object)(object)component2 != (Object)null && !component2.IsMine && PhotonNetwork.MasterClient != null)
					{
						if (DebugLogging.Value)
						{
							Log.LogInfo((object)$"[PGR:Ownership] Requesting ownership of {((Object)_locallyGrabbedBattery).name} (ID: {component2.ViewID}) from MasterClient.");
						}
						component2.RequestOwnership();
					}
				}
				_previousGrabbedObject = __instance.physGrabObject;
			}
			if (SemiFunc.RunIsLevel())
			{
				if (_cooldownTimer > 0f)
				{
					_cooldownTimer -= Time.deltaTime;
					if (DebugLogging.Value)
					{
						Log.LogInfo((object)$"[PGR:Cooldown] Cooldown: {_cooldownTimer:F2}s remaining.");
					}
				}
				else if (_currentModPower < 10f)
				{
					float num = 0.099999994f * Time.deltaTime;
					_currentModPower = Mathf.Min(_currentModPower + num, 10f);
					if (DebugLogging.Value)
					{
						Log.LogInfo((object)$"[PGR:Regen] Mod Power: {_currentModPower:F2}");
					}
				}
			}
			bool flag = false;
			if (!ToggleRechargeMode.Value)
			{
				flag = (_isRecharging = Input.GetKey(RechargeKey.Value));
			}
			else
			{
				if (Input.GetKeyDown(RechargeKey.Value))
				{
					_isRecharging = !_isRecharging;
					if (_isRecharging && _currentModPower <= 0.01f)
					{
						_isRecharging = false;
						if (DebugLogging.Value)
						{
							Log.LogInfo((object)"[PGR:Toggle] Mod Power too low to start recharge. Auto-stopping recharge.");
						}
					}
					if (DebugLogging.Value)
					{
						Log.LogInfo((object)$"[PGR:Toggle] Recharge toggled: {_isRecharging}, Mod Power: {_currentModPower:F1}");
					}
				}
				flag = _isRecharging;
			}
			if (!flag || !((Object)(object)_locallyGrabbedBattery != (Object)null) || !SemiFunc.BatteryChargeCondition(_locallyGrabbedBattery) || !(_cooldownTimer <= 0f))
			{
				_isRecharging = false;
				return;
			}
			if (_currentModPower <= 0.01f)
			{
				_cooldownTimer = 2f;
				_currentModPower = 0f;
				_isRecharging = false;
				if (DebugLogging.Value)
				{
					Log.LogInfo((object)"[PGR:Recharge] Mod Power depleted -- cooldown started. Auto-stopping recharge.");
				}
				return;
			}
			PhotonView component3 = ((Component)_locallyGrabbedBattery).GetComponent<PhotonView>();
			if (PhotonNetwork.IsConnectedAndReady && ((Object)(object)component3 == (Object)null || !component3.IsMine))
			{
				if (DebugLogging.Value)
				{
					if ((Object)(object)component3 == (Object)null)
					{
						Log.LogWarning((object)("[PGR:Recharge] Cannot recharge, batteryPV is null for " + ((Object)_locallyGrabbedBattery).name + "."));
					}
					else if (!component3.IsMine)
					{
						Log.LogWarning((object)$"[PGR:Recharge] Cannot recharge, not owner of {((Object)_locallyGrabbedBattery).name} (ID: {component3.ViewID}).");
					}
				}
				_isRecharging = false;
				return;
			}
			float num2 = (float)ManualRechargeRate.Value * Time.deltaTime;
			float num3 = 1f * Time.deltaTime;
			if (num3 > _currentModPower)
			{
				num3 = _currentModPower;
			}
			float batteryLife = _locallyGrabbedBattery.batteryLife;
			_locallyGrabbedBattery.batteryLife = Mathf.Clamp(_locallyGrabbedBattery.batteryLife + num2, 0f, 100f);
			_currentModPower -= num3;
			if (DebugLogging.Value)
			{
				Log.LogInfo((object)$"[PGR:Recharge] {((Object)_locallyGrabbedBattery).name}: {batteryLife:F1} -> {_locallyGrabbedBattery.batteryLife:F1} (+{num2:F2})");
				Log.LogInfo((object)$"[PGR:Recharge] Mod Power: {_currentModPower:F1} (-{num3:F2})");
			}
			if (_locallyGrabbedBattery.batteryLife >= 99.99f && ToggleRechargeMode.Value)
			{
				_isRecharging = false;
				if (DebugLogging.Value)
				{
					Log.LogInfo((object)"[PGR:Recharge] Battery full. Auto-stopped recharge.");
				}
			}
		}

		private static void ResetModPowerAndState()
		{
			_currentModPower = 10f;
			_isRecharging = false;
			_cooldownTimer = 0f;
			_locallyGrabbedBattery = null;
			_previousGrabbedObject = null;
		}

		private static void InjectBar(TextMeshProUGUI tmp)
		{
			float num = Mathf.Clamp01(_currentModPower / 10f);
			string text = Mathf.RoundToInt(num * 100f).ToString();
			if (_cooldownTimer > 0f)
			{
				text += $" (CD {_cooldownTimer:F1}s)";
			}
			string text2 = ((num < 0.25f) ? "#ff3333" : ((num < 0.6f) ? "#ffaa00" : "#00ff66"));
			string text3 = "<line-height=70%>\n<color=" + text2 + "><size=22>Charge: " + text + "%</size></color>";
			if (!((TMP_Text)tmp).text.Contains("Charge:"))
			{
				((TMP_Text)tmp).text = ((TMP_Text)tmp).text + text3;
				return;
			}
			int num2 = ((TMP_Text)tmp).text.IndexOf("Charge:");
			if (num2 != -1)
			{
				int num3 = ((TMP_Text)tmp).text.IndexOf('\n', num2);
				if (num3 == -1)
				{
					num3 = ((TMP_Text)tmp).text.Length;
				}
				((TMP_Text)tmp).text = ((TMP_Text)tmp).text.Substring(0, num2) + text3 + ((TMP_Text)tmp).text.Substring(num3);
			}
		}
	}
}