Decompiled source of MoreBattery v1.2.3

MoreBattery.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using MoreBatteryMod.Patches;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace MoreBatteryMod
{
	[BepInPlugin("MoreBattery", "MoreBattery", "1.2.4")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance;

		internal ManualLogSource Log;

		private Harmony harmony;

		public static ConfigEntry<float> BatteryMultiplier;

		public static ConfigEntry<bool> ApplyToAllFlashlights;

		public static ConfigEntry<string> CustomItemNames;

		public static ConfigEntry<string> ExcludedItemNames;

		public static ConfigEntry<bool> ShowDebugUI;

		private void Awake()
		{
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Log = ((BaseUnityPlugin)this).Logger;
			BatteryMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "BatteryMultiplier", 1.5f, "The multiplier applied to the battery life (duration in seconds) of flashlights and light sources. 1.5 = 50% increase, 2.0 = 100% increase, etc. Setting it to 1.0 keeps it vanilla.");
			ApplyToAllFlashlights = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ApplyToAllFlashlights", true, "If true, automatically applies the battery multiplier to all items inheriting from FlashlightItem (e.g. standard flashlight, pro-flashlight, laser pointer, and modded flashlights).");
			CustomItemNames = ((BaseUnityPlugin)this).Config.Bind<string>("General", "CustomItemNames", "Lantern,Elite Flashlight,EliteFlashlight", "A comma-separated list of additional item names (case-insensitive) to apply the battery multiplier to (e.g. 'Lantern, Green Flashlight').");
			ExcludedItemNames = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ExcludedItemNames", "", "A comma-separated list of item names to exclude from receiving the battery multiplier.");
			ShowDebugUI = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowDebugUI", false, "If true, shows a basic text overlay on the screen when holding or using a battery-operated item, displaying its original and current battery durations.");
			try
			{
				harmony = new Harmony("MoreBattery");
				harmony.PatchAll(typeof(MoreBatteryModPatches));
				Log.LogInfo((object)("MoreBattery v1.2.4 by Dausen_Bugg (updated by Antigravity) loaded successfully! Multiplier: " + BatteryMultiplier.Value + "x"));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to apply patches: " + ex.ToString()));
			}
		}

		private void OnGUI()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a6: Unknown result type (might be due to invalid IL or missing references)
			if (!ShowDebugUI.Value)
			{
				return;
			}
			int num = 330;
			int num2 = 110;
			int num3 = 25;
			int num4 = Screen.height - num2 - 150;
			GUI.Box(new Rect((float)num3, (float)num4, (float)num, (float)num2), "");
			GUIStyle val = new GUIStyle(GUI.skin.label);
			val.fontStyle = (FontStyle)1;
			val.normal.textColor = Color.yellow;
			val.fontSize = 14;
			GUIStyle val2 = new GUIStyle(GUI.skin.label);
			val2.normal.textColor = Color.white;
			val2.fontSize = 12;
			GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 10), (float)(num - 30), 20f), "MoreBattery Debug Info", val);
			PlayerControllerB localPlayer = GetLocalPlayer();
			if ((Object)(object)localPlayer == (Object)null)
			{
				GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 30), (float)(num - 30), 20f), "Status: Waiting for player spawn...", val2);
				return;
			}
			GrabbableObject activeBatteryItem = GetActiveBatteryItem(localPlayer);
			if ((Object)(object)activeBatteryItem == (Object)null || (Object)(object)activeBatteryItem.itemProperties == (Object)null)
			{
				GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 30), (float)(num - 30), 20f), "Status: Active & Loaded", val2);
				GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 48), (float)(num - 30), 20f), "No active battery item detected.", val2);
				GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 66), (float)(num - 30), 20f), "Hold/use a flashlight to see stats.", val2);
				return;
			}
			Item itemProperties = activeBatteryItem.itemProperties;
			float num5 = itemProperties.batteryUsage;
			if (MoreBatteryModPatches.OriginalDurations.TryGetValue(itemProperties, out var value))
			{
				num5 = value;
			}
			float batteryUsage = itemProperties.batteryUsage;
			float num6 = ((activeBatteryItem.insertedBattery != null) ? (activeBatteryItem.insertedBattery.charge * 100f) : 0f);
			float num7 = ((activeBatteryItem.insertedBattery != null) ? (activeBatteryItem.insertedBattery.charge * batteryUsage) : 0f);
			GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 30), (float)(num - 30), 20f), "Item Name: " + itemProperties.itemName, val2);
			GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 48), (float)(num - 30), 20f), "Vanilla Max Duration: " + num5.ToString("F1") + " seconds", val2);
			GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 66), (float)(num - 30), 20f), "Current Max Duration: " + batteryUsage.ToString("F1") + " seconds (" + (batteryUsage / num5).ToString("F2") + "x)", val2);
			string text = "Battery Charge: " + num6.ToString("F1") + "% (approx. " + num7.ToString("F1") + "s left)";
			if (activeBatteryItem.insertedBattery != null && activeBatteryItem.insertedBattery.empty)
			{
				text = "Battery Charge: EMPTY (0.0s)";
			}
			GUI.Label(new Rect((float)(num3 + 15), (float)(num4 + 84), (float)(num - 30), 20f), text, val2);
		}

		private PlayerControllerB GetLocalPlayer()
		{
			if ((Object)(object)GameNetworkManager.Instance != (Object)null && (Object)(object)GameNetworkManager.Instance.localPlayerController != (Object)null)
			{
				return GameNetworkManager.Instance.localPlayerController;
			}
			if ((Object)(object)StartOfRound.Instance != (Object)null && (Object)(object)StartOfRound.Instance.localPlayerController != (Object)null)
			{
				return StartOfRound.Instance.localPlayerController;
			}
			return null;
		}

		private GrabbableObject GetActiveBatteryItem(PlayerControllerB player)
		{
			if ((Object)(object)player.currentlyHeldObject != (Object)null && (Object)(object)player.currentlyHeldObject.itemProperties != (Object)null && player.currentlyHeldObject.itemProperties.requiresBattery)
			{
				return player.currentlyHeldObject;
			}
			if (player.ItemSlots != null)
			{
				for (int i = 0; i < player.ItemSlots.Length; i++)
				{
					GrabbableObject val = player.ItemSlots[i];
					if ((Object)(object)val != (Object)null && (Object)(object)val.itemProperties != (Object)null && val.itemProperties.requiresBattery && val.isBeingUsed)
					{
						return val;
					}
				}
			}
			return null;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MoreBattery";

		public const string PLUGIN_NAME = "MoreBattery";

		public const string PLUGIN_VERSION = "1.2.4";
	}
}
namespace MoreBatteryMod.Patches
{
	[HarmonyPatch(typeof(GrabbableObject))]
	public static class MoreBatteryModPatches
	{
		public static readonly Dictionary<Item, float> OriginalDurations = new Dictionary<Item, float>();

		private static readonly HashSet<Item> modifiedItems = new HashSet<Item>();

		private static readonly HashSet<string> customNamesList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private static readonly HashSet<string> excludedNamesList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private static bool listsInitialized = false;

		private static void InitializeLists()
		{
			customNamesList.Clear();
			excludedNamesList.Clear();
			string value = Plugin.CustomItemNames.Value;
			if (!string.IsNullOrEmpty(value))
			{
				string[] array = value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string text in array)
				{
					string text2 = text.Trim();
					if (!string.IsNullOrEmpty(text2))
					{
						customNamesList.Add(text2);
					}
				}
			}
			string value2 = Plugin.ExcludedItemNames.Value;
			if (!string.IsNullOrEmpty(value2))
			{
				string[] array = value2.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string text in array)
				{
					string text2 = text.Trim();
					if (!string.IsNullOrEmpty(text2))
					{
						excludedNamesList.Add(text2);
					}
				}
			}
			listsInitialized = true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		public static void MoreBatteryPatch(GrabbableObject __instance)
		{
			if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.itemProperties == (Object)null)
			{
				return;
			}
			Item itemProperties = __instance.itemProperties;
			if (!itemProperties.requiresBattery)
			{
				return;
			}
			if (!OriginalDurations.ContainsKey(itemProperties))
			{
				OriginalDurations[itemProperties] = itemProperties.batteryUsage;
			}
			if (!modifiedItems.Contains(itemProperties))
			{
				if (!listsInitialized)
				{
					InitializeLists();
				}
				bool flag = false;
				if (Plugin.ApplyToAllFlashlights.Value && __instance is FlashlightItem)
				{
					flag = true;
				}
				if (!flag && !string.IsNullOrEmpty(itemProperties.itemName) && customNamesList.Contains(itemProperties.itemName))
				{
					flag = true;
				}
				if (flag && !string.IsNullOrEmpty(itemProperties.itemName) && excludedNamesList.Contains(itemProperties.itemName))
				{
					flag = false;
				}
				if (flag)
				{
					float batteryUsage = itemProperties.batteryUsage;
					float value = Plugin.BatteryMultiplier.Value;
					itemProperties.batteryUsage = batteryUsage * value;
					modifiedItems.Add(itemProperties);
					Plugin.Instance.Log.LogInfo((object)("Applied battery multiplier of " + value + "x to item: '" + itemProperties.itemName + "' (Original battery life: " + batteryUsage + "s -> New battery life: " + itemProperties.batteryUsage + "s)"));
				}
			}
		}
	}
}