Decompiled source of BindableItemSlots v1.0.4

BepInEx/plugins/BindableItemSlots.dll

Decompiled 11 months ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalCompanyInputUtils.Api;
using NumberItemSwitch.Patches;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("NumberItemSwitch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NumberItemSwitch")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ba6e3195-d25c-4a9b-9ac6-425e9390a7ce")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NumberItemSwitch
{
	internal class BindableItemSlotsInputs : LcInputActions
	{
		[InputAction("<Keyboard>/3", Name = "Item 1")]
		public InputAction ItemOneKey { get; set; }

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

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

		[InputAction("<Keyboard>/6", Name = "Item 4")]
		public InputAction ItemFourKey { get; set; }
	}
	[BepInPlugin("bctix.bindableItemSlots", "Bindable Item Slots", "1.0.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BindableItemSlots : BaseUnityPlugin
	{
		private const string modGUID = "bctix.bindableItemSlots";

		private const string modName = "Bindable Item Slots";

		private const string modVersion = "1.0.4";

		private readonly Harmony harmony = new Harmony("bctix.bindableItemSlots");

		private static BindableItemSlots Instance;

		internal static ManualLogSource mls;

		internal static BindableItemSlotsInputs InputActionsInstance = new BindableItemSlotsInputs();

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("bctix.bindableItemSlots");
			harmony.PatchAll(typeof(BindableItemSlots));
			harmony.PatchAll(typeof(PlayerControllerBPatch));
		}

		public void log(string text)
		{
			mls.LogInfo((object)text);
		}
	}
}
namespace NumberItemSwitch.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		private static readonly MethodInfo switchItemMethod = typeof(PlayerControllerB).GetMethod("SwitchToItemSlot", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly MethodInfo switchItemMethodRpc = typeof(PlayerControllerB).GetMethod("SwitchItemSlotsServerRpc", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo timeSinceSwitch = typeof(PlayerControllerB).GetField("timeSinceSwitchingSlots", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo isThrowing = typeof(PlayerControllerB).GetField("throwingObject", BindingFlags.Instance | BindingFlags.NonPublic);

		public static float switchCooldown = 0f;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void itemKeyPatch(PlayerControllerB __instance)
		{
			if ((BindableItemSlots.InputActionsInstance.ItemOneKey.triggered || BindableItemSlots.InputActionsInstance.ItemTwoKey.triggered || BindableItemSlots.InputActionsInstance.ItemThreeKey.triggered || BindableItemSlots.InputActionsInstance.ItemFourKey.triggered) && CheckForMatchingSlot(__instance))
			{
				checkItemKey(__instance);
			}
		}

		private static bool CheckForMatchingSlot(PlayerControllerB __instance)
		{
			if ((BindableItemSlots.InputActionsInstance.ItemOneKey.triggered && __instance.currentItemSlot == 0) || (BindableItemSlots.InputActionsInstance.ItemTwoKey.triggered && __instance.currentItemSlot == 1) || (BindableItemSlots.InputActionsInstance.ItemThreeKey.triggered && __instance.currentItemSlot == 2) || (BindableItemSlots.InputActionsInstance.ItemFourKey.triggered && __instance.currentItemSlot == 3))
			{
				return false;
			}
			return true;
		}

		public static void checkItemKey(PlayerControllerB __instance)
		{
			timeSinceSwitch = typeof(PlayerControllerB).GetField("timeSinceSwitchingSlots", BindingFlags.Instance | BindingFlags.NonPublic);
			isThrowing = typeof(PlayerControllerB).GetField("throwingObject", BindingFlags.Instance | BindingFlags.NonPublic);
			if (((!((NetworkBehaviour)__instance).IsOwner || !__instance.isPlayerControlled || (((NetworkBehaviour)__instance).IsServer && !__instance.isHostPlayerObject)) && !__instance.isTestingPlayer) || (float)timeSinceSwitch.GetValue(__instance) < switchCooldown || __instance.isGrabbingObjectAnimation || __instance.quickMenuManager.isMenuOpen || __instance.inSpecialInteractAnimation || (bool)isThrowing.GetValue(__instance) || __instance.isTypingChat || __instance.twoHanded || __instance.activatingItem || __instance.jetpackControls || __instance.disablingJetpackControls)
			{
				return;
			}
			ShipBuildModeManager.Instance.CancelBuildMode(true);
			__instance.playerBodyAnimator.SetBool("GrabValidated", false);
			int num = 0;
			int currentItemSlot = __instance.currentItemSlot;
			if (BindableItemSlots.InputActionsInstance.ItemOneKey.triggered)
			{
				num = 0;
			}
			if (BindableItemSlots.InputActionsInstance.ItemTwoKey.triggered)
			{
				num = 1;
			}
			if (BindableItemSlots.InputActionsInstance.ItemThreeKey.triggered)
			{
				num = 2;
			}
			if (BindableItemSlots.InputActionsInstance.ItemFourKey.triggered)
			{
				num = 3;
			}
			switchItemMethod.Invoke(__instance, new object[2] { num, null });
			if (num > currentItemSlot)
			{
				for (int i = currentItemSlot; i < num; i++)
				{
					switchItemMethodRpc.Invoke(__instance, new object[1] { true });
				}
			}
			if (num < currentItemSlot)
			{
				for (int num2 = currentItemSlot; num2 > num; num2--)
				{
					switchItemMethodRpc.Invoke(__instance, new object[1] { false });
				}
			}
			if ((Object)(object)__instance.currentlyHeldObjectServer != (Object)null)
			{
				((Component)__instance.currentlyHeldObjectServer).gameObject.GetComponent<AudioSource>().PlayOneShot(__instance.currentlyHeldObjectServer.itemProperties.grabSFX, 0.6f);
			}
			timeSinceSwitch.SetValue(__instance, 0f);
		}
	}
}