Decompiled source of BetterInventory v0.2.3

BetterInventory.dll

Decompiled 10 months ago
using System;
using System.Collections;
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 BetterInventory.Patches;
using GameNetcodeStuff;
using HarmonyLib;
using LethalCompanyInputUtils.Api;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BetterInventory2")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Better Inventory 2")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterInventory2")]
[assembly: AssemblyTitle("BetterInventory2")]
[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 BetterInventory2
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "BetterInventory2";

		public const string PLUGIN_NAME = "BetterInventory2";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BetterInventory
{
	[BepInPlugin("Croquette.BetterInventory", "Better Inventory", "1.0.0")]
	public class BetterInventory : BaseUnityPlugin
	{
		private const string modGUID = "Croquette.BetterInventory";

		private const string modName = "Better Inventory";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("Croquette.BetterInventory");

		private static BetterInventory Instance;

		internal static ManualLogSource logger;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			logger = Logger.CreateLogSource("Croquette.BetterInventory");
			harmony.PatchAll(typeof(BetterInventory));
			harmony.PatchAll(typeof(PlayerControllerBPatch));
			harmony.PatchAll(typeof(EasyInventoryInputClass));
			harmony.PatchAll(typeof(GlobalFlashlightInputClass));
			harmony.PatchAll(typeof(SwapModeInputClass));
		}

		private static ManualLogSource GetLogger()
		{
			return logger;
		}
	}
}
namespace BetterInventory.Patches
{
	internal class EasyInventoryInputClass : LcInputActions
	{
		public static EasyInventoryInputClass Instance = new EasyInventoryInputClass();

		[InputAction("<Keyboard>/1", Name = "Inventory 1")]
		public InputAction Inventory1Key { get; set; }

		[InputAction("<Keyboard>/2", Name = "Inventory 2")]
		public InputAction Inventory2Key { get; set; }

		[InputAction("<Keyboard>/3", Name = "Inventory 3")]
		public InputAction Inventory3Key { get; set; }

		[InputAction("<Keyboard>/4", Name = "Inventory 4")]
		public InputAction Inventory4Key { get; set; }

		public override void CreateInputActions(in InputActionMapBuilder builder)
		{
			BetterInventory.logger.LogInfo((object)"Creating Input Actions");
		}
	}
	internal class GlobalFlashlightInputClass : LcInputActions
	{
		public static GlobalFlashlightInputClass Instance = new GlobalFlashlightInputClass();

		[InputAction("<Keyboard>/T", Name = "Global Flashlight")]
		public InputAction GlobalFlashlightKey { get; set; }
	}
	internal class SwapModeInputClass : LcInputActions
	{
		public static SwapModeInputClass Instance = new SwapModeInputClass();

		[InputAction("<Keyboard>/X", Name = "Inventory Swap")]
		public InputAction SwapModeKey { get; set; }
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		private static EasyInventoryInputClass easyInventoryInputSystem = EasyInventoryInputClass.Instance;

		private static GlobalFlashlightInputClass globalFlashlightInputSystem = GlobalFlashlightInputClass.Instance;

		private static PlayerControllerB _localPlayer;

		private static MethodInfo _switchToItemMethod;

		private static MethodInfo _nextItemSlot;

		private static MethodInfo _switchItemSlotsServerRpc;

		private static bool _shouldDeactivateSwapMode = false;

		private static bool swapModeActive = false;

		private static int initialSwapIndex = -1;

		private static Color _inventoryFrameColor = Color.black;

		private static bool skipNextCall = false;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void Start()
		{
			_switchToItemMethod = typeof(PlayerControllerB).GetMethod("SwitchToItemSlot", BindingFlags.Instance | BindingFlags.NonPublic);
			_nextItemSlot = typeof(PlayerControllerB).GetMethod("NextItemSlot", BindingFlags.Instance | BindingFlags.NonPublic);
			_switchItemSlotsServerRpc = typeof(PlayerControllerB).GetMethod("SwitchItemSlotsServerRpc", BindingFlags.Instance | BindingFlags.NonPublic);
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePatch()
		{
			if (Object.op_Implicit((Object)(object)(_localPlayer = GameNetworkManager.Instance.localPlayerController)))
			{
				EasyInventoryPatch();
				GlobalFlashlight();
				ToggleSwapMode();
			}
		}

		private static void EasyInventoryPatch()
		{
			if (_localPlayer.twoHanded || _localPlayer.inTerminalMenu)
			{
				return;
			}
			int num = -1;
			if (easyInventoryInputSystem == null)
			{
				return;
			}
			if (easyInventoryInputSystem.Inventory1Key.triggered)
			{
				num = 0;
			}
			else if (easyInventoryInputSystem.Inventory2Key.triggered)
			{
				num = 1;
			}
			else if (easyInventoryInputSystem.Inventory3Key.triggered)
			{
				num = 2;
			}
			else if (easyInventoryInputSystem.Inventory4Key.triggered)
			{
				num = 3;
			}
			if (num == -1)
			{
				return;
			}
			int num2 = _localPlayer.currentItemSlot - num;
			if (num2 > 0)
			{
				object[] parameters = new object[1] { true };
				_switchToItemMethod.Invoke(_localPlayer, new object[2] { num, null });
				for (int i = 0; i < num2; i++)
				{
					_switchItemSlotsServerRpc.Invoke(_localPlayer, parameters);
				}
			}
			else if (num2 < 0)
			{
				object[] parameters2 = new object[1] { false };
				_switchToItemMethod.Invoke(_localPlayer, new object[2] { num, null });
				for (int num3 = 0; num3 > num2; num3--)
				{
					_switchItemSlotsServerRpc.Invoke(_localPlayer, parameters2);
				}
			}
			_shouldDeactivateSwapMode = true;
		}

		private static void GlobalFlashlight()
		{
			if (_localPlayer.inTerminalMenu || !globalFlashlightInputSystem.GlobalFlashlightKey.triggered)
			{
				return;
			}
			for (int i = 0; i < _localPlayer.ItemSlots.Length; i++)
			{
				if (!(_localPlayer.ItemSlots[i] is FlashlightItem))
				{
					continue;
				}
				GrabbableObject obj = _localPlayer.ItemSlots[i];
				FlashlightItem val = (FlashlightItem)(object)((obj is FlashlightItem) ? obj : null);
				if (((GrabbableObject)val).insertedBattery.charge > 0f)
				{
					((GrabbableObject)val).UseItemOnClient(true);
					if (_localPlayer.currentItemSlot != i)
					{
						((GrabbableObject)val).PocketItem();
					}
					break;
				}
			}
		}

		private static void ToggleSwapMode()
		{
			if (SwapModeInputClass.Instance.SwapModeKey.triggered)
			{
				if (!swapModeActive)
				{
					ActivateSwapMode();
				}
				else
				{
					DeactivateSwapMode();
				}
			}
			if (swapModeActive && (_shouldDeactivateSwapMode || _localPlayer.twoHanded || _localPlayer.inTerminalMenu))
			{
				DeactivateSwapMode();
			}
		}

		[HarmonyPatch("SwitchToItemSlot")]
		[HarmonyPrefix]
		private static void ManageSwapMode(int slot, ref GrabbableObject fillSlotWithItem)
		{
			if (!swapModeActive || _localPlayer.twoHanded)
			{
				return;
			}
			if (skipNextCall)
			{
				skipNextCall = false;
				return;
			}
			if ((Object)(object)fillSlotWithItem != (Object)null)
			{
				_shouldDeactivateSwapMode = true;
				return;
			}
			GrabbableObject newObject = _localPlayer.ItemSlots[initialSwapIndex];
			GrabbableObject newObject2 = _localPlayer.ItemSlots[slot];
			skipNextCall = true;
			ChangeItemInSlot(initialSwapIndex, newObject2);
			skipNextCall = true;
			ChangeItemInSlot(slot, newObject);
			if (!_shouldDeactivateSwapMode)
			{
				HighlightInventoryForSwapMode();
				initialSwapIndex = slot;
			}
		}

		[HarmonyPatch("SwitchToItemSlot")]
		[HarmonyPostfix]
		private static void PostSlotSwitch()
		{
			if (swapModeActive)
			{
				HUDManager.Instance.PingHUDElement(HUDManager.Instance.Inventory, 0.1f, 1f, 1f);
			}
			else
			{
				HUDManager.Instance.PingHUDElement(HUDManager.Instance.Inventory, 1.5f, 1f, 0.13f);
			}
		}

		private static void HighlightInventoryForSwapMode()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			if (_inventoryFrameColor == Color.black)
			{
				_inventoryFrameColor = ((Graphic)HUDManager.Instance.itemSlotIconFrames[_localPlayer.currentItemSlot]).color;
			}
			for (int i = 0; i < _localPlayer.ItemSlots.Length; i++)
			{
				Color color = ((i == _localPlayer.currentItemSlot) ? Color.green : Color.gray);
				((Graphic)HUDManager.Instance.itemSlotIconFrames[i]).color = color;
			}
		}

		private static void ActivateSwapMode()
		{
			swapModeActive = true;
			_shouldDeactivateSwapMode = false;
			initialSwapIndex = _localPlayer.currentItemSlot;
			HighlightInventoryForSwapMode();
			PostSlotSwitch();
		}

		private static void DeactivateSwapMode()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			for (int i = 0; i < _localPlayer.ItemSlots.Length; i++)
			{
				((Graphic)HUDManager.Instance.itemSlotIconFrames[i]).color = _inventoryFrameColor;
			}
			swapModeActive = false;
			PostSlotSwitch();
		}

		private static void ChangeItemInSlot(int slotIndex, GrabbableObject newObject)
		{
			_switchToItemMethod.Invoke(_localPlayer, new object[2] { slotIndex, newObject });
			if ((Object)(object)newObject == (Object)null)
			{
				_localPlayer.ItemSlots[slotIndex] = null;
				((Behaviour)HUDManager.Instance.itemSlotIcons[slotIndex]).enabled = false;
			}
		}
	}
	internal class ItemPulsing : MonoBehaviour
	{
		public Vector3 minScale = new Vector3(0.75f, 0.75f, 0.75f);

		public Vector3 maxScale = new Vector3(1.25f, 1.25f, 1.25f);

		public float scalingSpeed = 0.7f;

		public float scalingDuration = 0.5f;

		private IEnumerator Start()
		{
			yield return RepeatLerping(((Component)this).transform.localScale, minScale, scalingDuration);
			while (true)
			{
				yield return RepeatLerping(minScale, maxScale, scalingDuration);
				yield return RepeatLerping(maxScale, minScale, scalingDuration);
			}
		}

		private IEnumerator RepeatLerping(Vector3 startScale, Vector3 endScale, float time, bool destroyWhenDone = false)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			float currentTime = 0f;
			float rate = 1f / time * scalingSpeed;
			while (currentTime < 1f)
			{
				currentTime += Time.deltaTime * rate;
				((Component)this).transform.localScale = Vector3.Lerp(startScale, endScale, currentTime);
				yield return null;
			}
			if (destroyWhenDone)
			{
				Object.Destroy((Object)(object)this);
			}
		}

		public void StopAnimationAndDestroy()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			((Component)this).transform.localScale = Vector3.one;
			((MonoBehaviour)this).StopAllCoroutines();
			Object.Destroy((Object)(object)this);
		}
	}
}