Decompiled source of KnowEncumbrance v1.0.2

plugins/KnowEncumbrance.dll

Decompiled a year ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using KnowEncumbrance.Components;
using TMPro;
using UnityEngine;

[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: Guid("72007888-3C4D-47F5-B062-8EAFD8109C0B")]
[assembly: ComVisible(false)]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyProduct("KnowEncumbrance")]
[assembly: AssemblyCompany("")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyTitle("KnowEncumbrance")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyDescription("")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
namespace KnowEncumbrance
{
	[BepInPlugin("com.vapok.KnowEncumbrance", "KnowEncumbrance", "1.0.2")]
	public class KnowEncumbrance : BaseUnityPlugin
	{
		private const string MyGUID = "com.vapok.KnowEncumbrance";

		private const string PluginName = "KnowEncumbrance";

		private const string VersionString = "1.0.2";

		private static Harmony _harmony = new Harmony("com.vapok.KnowEncumbrance");

		public static ManualLogSource Log = new ManualLogSource("KnowEncumbrance");

		private static KnowEncumbrance _instance;

		private void Awake()
		{
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			_instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"KnowEncumbrance [1.0.2] is loading...");
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony.PatchAll();
		}
	}
}
namespace KnowEncumbrance.Patches
{
	public class AddComponentPatches
	{
		[HarmonyPatch(typeof(Player), "Awake")]
		public static class PlayerAwakeAddComponent
		{
			public static void Postfix(Player __instance)
			{
				if ((Object)(object)__instance != (Object)null)
				{
					((Component)__instance).gameObject.AddComponent<EncumbranceManager>().PlayerController = __instance;
				}
			}
		}

		[HarmonyPatch(typeof(InventoryNavigator), "Init")]
		public static class InventoryNavigatorInitAddComponent
		{
			public static void Postfix(InventoryNavigator __instance)
			{
				if ((Object)(object)__instance != (Object)null)
				{
					InventoryManager inventoryManager = ((Component)__instance).gameObject.AddComponent<InventoryManager>();
					Transform obj = ((Component)__instance).transform.Find("Top Level Container/Container/Player Inventory/Title (Inventory)");
					inventoryManager.Title = ((obj != null) ? ((Component)obj).gameObject : null);
				}
			}
		}

		[HarmonyPatch(typeof(InventoryPageUI), "Start")]
		public static class InventoryPageUIStartAddComponent
		{
			public static void Postfix(InventoryNavigator __instance)
			{
				if ((Object)(object)__instance != (Object)null)
				{
					InventoryManager inventoryManager = ((Component)__instance).gameObject.AddComponent<InventoryManager>();
					Transform obj = ((Component)__instance).transform.Find("Container/Title Bar/Title (Inventory)");
					inventoryManager.Title = ((obj != null) ? ((Component)obj).gameObject : null);
				}
			}
		}
	}
}
namespace KnowEncumbrance.Components
{
	public class EncumbranceManager : MonoBehaviour
	{
		public static bool Active;

		private int previousSlotCount;

		private static EncumbranceManager _instance;

		public Player PlayerController;

		public static EncumbranceManager Instance => _instance;

		public void Awake()
		{
			_instance = this;
			KnowEncumbrance.Log.LogDebug((object)"EncumbranceComponent Added");
			Active = true;
		}

		public void Start()
		{
			int numConsolidatedSlots = ((InventoryWrapper)Player.instance.inventory).myInv.numConsolidatedSlots;
			int nonEncumberedSlotsCount = GameState.instance.nonEncumberedSlotsCount;
			KnowEncumbrance.Log.LogDebug((object)$"Player Encumbrance: Current Slot Count: {numConsolidatedSlots} / Max Slot Count: {nonEncumberedSlotsCount}");
			previousSlotCount = numConsolidatedSlots;
			((MonoBehaviour)this).InvokeRepeating("UpdateEncumbrance", 0f, 0.4f);
		}

		private void UpdateEncumbrance()
		{
			if (EncumbranceChanged())
			{
				int numConsolidatedSlots = ((InventoryWrapper)Player.instance.inventory).myInv.numConsolidatedSlots;
				int nonEncumberedSlotsCount = GameState.instance.nonEncumberedSlotsCount;
				KnowEncumbrance.Log.LogDebug((object)$"Player Encumbrance Changed: Current Slot Count: {numConsolidatedSlots} / Max Slot Count: {nonEncumberedSlotsCount}");
			}
		}

		private bool EncumbranceChanged()
		{
			if (((InventoryWrapper)Player.instance.inventory).myInv.numConsolidatedSlots == previousSlotCount)
			{
				return false;
			}
			previousSlotCount = ((InventoryWrapper)Player.instance.inventory).myInv.numConsolidatedSlots;
			return true;
		}

		public string GetCurrentEncumbrance()
		{
			return $"{((InventoryWrapper)Player.instance.inventory).myInv.numConsolidatedSlots}/{GameState.instance.nonEncumberedSlotsCount}";
		}

		public bool IsEncumbered()
		{
			return ((InventoryWrapper)Player.instance.inventory).myInv.numConsolidatedSlots > GameState.instance.nonEncumberedSlotsCount;
		}
	}
	public class InventoryManager : MonoBehaviour
	{
		public GameObject Title;

		private TMP_Text _TitleText;

		private string _originalTitleText;

		private Color _originalTitleColor;

		private bool _titleActive;

		public void Awake()
		{
			KnowEncumbrance.Log.LogDebug((object)"InventoryManager Active!");
		}

		public void Start()
		{
			//IL_004d: 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)
			KnowEncumbrance.Log.LogDebug((object)"InventoryManager Started!");
			if ((Object)(object)Title != (Object)null)
			{
				_titleActive = true;
				_TitleText = Title.GetComponent<TMP_Text>();
				_originalTitleText = _TitleText.text;
				_originalTitleColor = _TitleText.color;
				((MonoBehaviour)this).InvokeRepeating("RefreshText", 0f, 0.4f);
			}
			else
			{
				KnowEncumbrance.Log.LogWarning((object)"Inventory Title Not Found");
			}
		}

		private void RefreshText()
		{
			if (_titleActive)
			{
				RefreshEndcumbranceText();
			}
		}

		public void RefreshEndcumbranceText()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			_TitleText.text = _originalTitleText + " (" + EncumbranceManager.Instance.GetCurrentEncumbrance() + ")";
			if (EncumbranceManager.Instance.IsEncumbered())
			{
				_TitleText.color = Color.red;
			}
			else
			{
				_TitleText.color = _originalTitleColor;
			}
		}
	}
}