Decompiled source of ItemQuickSwitch v1.1.0

BepInEx/plugins/ItemQuickSwitchMod.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
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 GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[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("ItemQuickSwitchMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Mod for selecting item slots directly")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("ItemQuickSwitchMod")]
[assembly: AssemblyTitle("ItemQuickSwitchMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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 ItemQuickSwitchMod
{
	public sealed class CustomAction
	{
		public class ActionItem
		{
			public string Id { get; }

			public Key Shortcut { get; }

			public string Description { get; }

			public int SlotNumber { get; }

			public ConfigEntry<Key> ConfigEntry { get; set; }

			public ActionItem(string id, Key config, string description, int slotNumber)
			{
				//IL_0009: Unknown result type (might be due to invalid IL or missing references)
				//IL_000a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0018: Unknown result type (might be due to invalid IL or missing references)
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				Id = id;
				Shortcut = config;
				Description = description;
				SlotNumber = slotNumber;
			}
		}

		public static readonly ActionItem Emote1 = new ActionItem("Emote1", (Key)94, "Dance emote", 0);

		public static readonly ActionItem Emote2 = new ActionItem("Emote2", (Key)95, "Point emote", 0);

		public static readonly ActionItem Slot1 = new ActionItem("Slot1", (Key)41, "Equip Slot 1", 0);

		public static readonly ActionItem Slot2 = new ActionItem("Slot2", (Key)42, "Equip Slot 2", 1);

		public static readonly ActionItem Slot3 = new ActionItem("Slot3", (Key)43, "Equip Slot 3", 2);

		public static readonly ActionItem Slot4 = new ActionItem("Slot4", (Key)44, "Equip Slot 4", 3);

		public static readonly ActionItem[] AllActions = new ActionItem[6] { Emote1, Emote2, Slot1, Slot2, Slot3, Slot4 };
	}
	[BepInPlugin("de.vasanex.ItemQuickSwitchMod", "ItemQuickSwitchMod", "1.1.0")]
	public class ItemQuickSwitchMod : BaseUnityPlugin
	{
		private Harmony _harmony;

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ItemQuickSwitchMod is creating binds!");
			CustomAction.ActionItem[] allActions = CustomAction.AllActions;
			foreach (CustomAction.ActionItem actionItem in allActions)
			{
				ConfigEntry<Key> configEntry = ((BaseUnityPlugin)this).Config.Bind<Key>("Bindings", actionItem.Id, actionItem.Shortcut, actionItem.Description);
				actionItem.ConfigEntry = configEntry;
			}
			_harmony = new Harmony("de.vasanex.ItemQuickSwitchMod");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin ItemQuickSwitchMod is loaded!");
		}
	}
	public static class StaticPluginInfo
	{
		public const string GUID = "de.vasanex.ItemQuickSwitchMod";

		public const string NAME = "ItemQuickSwitchMod";
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ItemQuickSwitchMod";

		public const string PLUGIN_NAME = "ItemQuickSwitchMod";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace ItemQuickSwitchMod.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		private static readonly Dictionary<string, MethodInfo> MethodCache = new Dictionary<string, MethodInfo>();

		private static readonly object[] BackwardsParam = new object[1] { false };

		private static readonly object[] ForwardsParam = new object[1] { true };

		private static object InvokePrivateMethod(PlayerControllerB instance, string methodName, object[] parameters = null)
		{
			MethodCache.TryGetValue(methodName, out var value);
			if ((object)value == null)
			{
				value = typeof(PlayerControllerB).GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
			}
			MethodCache[methodName] = value;
			return value?.Invoke(instance, parameters);
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		public static void PlayerControllerB_Update(PlayerControllerB __instance, ref float ___timeSinceSwitchingSlots, ref bool ___throwingObject)
		{
			if ((!((NetworkBehaviour)__instance).IsOwner || !__instance.isPlayerControlled || (((NetworkBehaviour)__instance).IsServer && !__instance.isHostPlayerObject)) && !__instance.isTestingPlayer)
			{
				return;
			}
			CustomAction.ActionItem actionItem = Array.Find(CustomAction.AllActions, (CustomAction.ActionItem it) => ((ButtonControl)Keyboard.current[it.ConfigEntry.Value]).wasPressedThisFrame);
			if (actionItem == null)
			{
				return;
			}
			string id = actionItem.Id;
			string text = id;
			if (!(text == "Emote1"))
			{
				if (text == "Emote2")
				{
					PerformEmote(__instance, 2);
					return;
				}
				StopEmotes(__instance);
				if (SwitchItemSlots(__instance, actionItem.SlotNumber, ___timeSinceSwitchingSlots, ___throwingObject))
				{
					___timeSinceSwitchingSlots = 0f;
				}
			}
			else
			{
				PerformEmote(__instance, 1);
			}
		}

		private static void PerformEmote(PlayerControllerB __instance, int emoteId)
		{
			__instance.timeSinceStartingEmote = 0f;
			__instance.performingEmote = true;
			__instance.playerBodyAnimator.SetInteger("emoteNumber", emoteId);
			__instance.StartPerformingEmoteServerRpc();
		}

		private static bool SwitchItemSlots(PlayerControllerB __instance, int requestedSlot, float timeSinceSwitchingSlots, bool isThrowingObject)
		{
			if (!IsItemSwitchPossible(__instance, timeSinceSwitchingSlots, isThrowingObject) || __instance.currentItemSlot == requestedSlot)
			{
				return false;
			}
			int num = __instance.currentItemSlot - requestedSlot;
			bool flag = num > 0;
			if (Math.Abs(num) == __instance.ItemSlots.Length - 1)
			{
				object[] parameters = (flag ? ForwardsParam : BackwardsParam);
				InvokePrivateMethod(__instance, "SwitchItemSlotsServerRpc", parameters);
			}
			else
			{
				object[] parameters2 = (flag ? BackwardsParam : ForwardsParam);
				do
				{
					InvokePrivateMethod(__instance, "SwitchItemSlotsServerRpc", parameters2);
					num += ((!flag) ? 1 : (-1));
				}
				while (num != 0);
			}
			ShipBuildModeManager.Instance.CancelBuildMode(true);
			__instance.playerBodyAnimator.SetBool("GrabValidated", false);
			object[] parameters3 = new object[2] { requestedSlot, null };
			InvokePrivateMethod(__instance, "SwitchToItemSlot", parameters3);
			if ((Object)(object)__instance.currentlyHeldObjectServer != (Object)null)
			{
				((Component)__instance.currentlyHeldObjectServer).gameObject.GetComponent<AudioSource>().PlayOneShot(__instance.currentlyHeldObjectServer.itemProperties.grabSFX, 0.6f);
			}
			return true;
		}

		private static bool IsItemSwitchPossible(PlayerControllerB __instance, float timeSinceSwitchingSlots, bool isThrowingObject)
		{
			return !((double)timeSinceSwitchingSlots < 0.01 || __instance.inTerminalMenu || __instance.isGrabbingObjectAnimation || __instance.inSpecialInteractAnimation || isThrowingObject) && !__instance.isTypingChat && !__instance.twoHanded && !__instance.activatingItem && !__instance.jetpackControls && !__instance.disablingJetpackControls;
		}

		private static void StopEmotes(PlayerControllerB __instance)
		{
			__instance.performingEmote = false;
			__instance.StopPerformingEmoteServerRpc();
			__instance.timeSinceStartingEmote = 0f;
		}
	}
}