Decompiled source of KeyboardInventory v0.0.1

BackendIain.LC.KeyboardInventory.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BackendIain.LC.KeyboardInventory.Models;
using BackendIain.LC.KeyboardInventory.Patches;
using BackendIain.LC.KeyboardInventory.UI;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BackendIain.LC.KeyboardInventory")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Tweaks keybinds so that the number keys will change the current item held by the player.My very first modding shitpost")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+bbff4d481ec8a18de721bd348137c22a370886c3")]
[assembly: AssemblyProduct("BackendIain.LC.KeyboardInventory")]
[assembly: AssemblyTitle("BackendIain.LC.KeyboardInventory")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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 BackendIain.LC.KeyboardInventory
{
	[BepInPlugin("BackendIain.LC.KeyboardInventory", "Keyboard Inventory", "0.1.0")]
	public class KeyboardInventoryModBase : BaseUnityPlugin
	{
		internal const string ModGuid = "BackendIain.LC.KeyboardInventory";

		internal const string ModName = "Keyboard Inventory";

		internal const string ModVersion = "0.1.0";

		private readonly Harmony _harmony = new Harmony("BackendIain.LC.KeyboardInventory");

		internal static KeyboardInventoryModBase Instance;

		internal static ManualLogSource _mls;

		internal static bool _isHost;

		private static ConfigEntry<bool> _enableNumKeyInventoryBindings;

		internal static GUILoader _myGUI;

		private static bool _hasGUISynced;

		internal static PlayerControllerB _playerRef;

		internal static ConfigEntry<bool> EnableNumKeyInventoryBindings => _enableNumKeyInventoryBindings;

		private void Awake()
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			_mls = Logger.CreateLogSource("BackendIain.LC.KeyboardInventory");
			_mls.LogInfo((object)"Loaded Keyboard Inventory. Patching...");
			_harmony.PatchAll(typeof(KeyboardInventoryModBase));
			_harmony.PatchAll(typeof(PlayerControllerBPatch));
			GameObject val = new GameObject("GUILoader");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<GUILoader>();
			_myGUI = val.GetComponent<GUILoader>();
			SetBindings();
			SetGUIVars();
			SetupKeyBindings();
		}

		private void Update()
		{
		}

		private void SetBindings()
		{
			_enableNumKeyInventoryBindings = ((BaseUnityPlugin)this).Config.Bind<bool>("General Settings", "Enable num key inventory bindings?", false, "Enable num key inventory bindings?");
		}

		private void SetGUIVars()
		{
			_myGUI.GuiEnableNumKeyInventoryBindings = _enableNumKeyInventoryBindings.Value;
		}

		internal void UpdateCFGVarsFromGUI()
		{
			if (!_hasGUISynced)
			{
				SetGUIVars();
			}
			_enableNumKeyInventoryBindings.Value = _myGUI.GuiEnableNumKeyInventoryBindings;
		}

		private void SetupKeyBindings()
		{
			((Component)this).GetComponent<GUILoader>();
		}

		[HarmonyPatch(typeof(RoundManager), "Start")]
		[HarmonyPrefix]
		private static void SetIsHost()
		{
			_mls.LogInfo((object)("Host Status: " + ((NetworkBehaviour)RoundManager.Instance).NetworkManager.IsHost));
			_isHost = ((NetworkBehaviour)RoundManager.Instance).NetworkManager.IsHost;
		}
	}
}
namespace BackendIain.LC.KeyboardInventory.UI
{
	internal class GUILoader : MonoBehaviour
	{
		private KeyboardShortcut _openCloseMenu;

		internal bool _wasKeyDown;

		private bool _isMenuOpen;

		private int _toolbarInt;

		private string[] _toolbarStrings = new string[1] { "General Settings" };

		private int MENUWIDTH = 600;

		private int MENUHEIGHT = 800;

		private int MENUX;

		private int MENUY;

		private int ITEMWIDTH = 300;

		private int CENTERX;

		private GUIStyle _menuStyle;

		private GUIStyle _buttonStyle;

		private GUIStyle _labelStyle;

		private GUIStyle _toggleStyle;

		private GUIStyle _hScrollStyle;

		internal ManualLogSource _mls;

		public bool GuiEnableNumKeyInventoryBindings;

		public bool GuiIsHost;

		private void Awake()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			_mls = Logger.CreateLogSource("BackendIain.LC.KeyboardInventory");
			_mls.LogInfo((object)"GUILoader loaded.");
			_openCloseMenu = new KeyboardShortcut((KeyCode)268, Array.Empty<KeyCode>());
			_isMenuOpen = false;
			MENUX = Screen.width / 2;
			MENUY = Screen.height / 2;
			CENTERX = MENUX + (MENUWIDTH / 2 - ITEMWIDTH / 2);
			_mls.LogInfo((object)$"MENUX: {MENUX}, MENUY: {MENUY}, CENTERX: {CENTERX}, ITEMWIDTH: {ITEMWIDTH}");
		}

		private void IntitializeMenu()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			if (_menuStyle == null)
			{
				_mls.LogInfo((object)"Initializing menu...");
				_menuStyle = new GUIStyle(GUI.skin.box);
				_buttonStyle = new GUIStyle(GUI.skin.button);
				_labelStyle = new GUIStyle(GUI.skin.label);
				_toggleStyle = new GUIStyle(GUI.skin.toggle);
				_hScrollStyle = new GUIStyle(GUI.skin.horizontalSlider);
				_menuStyle.normal.textColor = Color.white;
				_menuStyle.normal.background = MakeTex(2, 2, new Color(0.01f, 0.01f, 0.1f, 0.9f));
				_menuStyle.fontSize = 18;
				((Object)_menuStyle.normal.background).hideFlags = (HideFlags)61;
				_buttonStyle.normal.textColor = Color.white;
				_buttonStyle.fontSize = 18;
				_labelStyle.normal.textColor = Color.white;
				_labelStyle.normal.background = MakeTex(2, 2, new Color(0.01f, 0.01f, 0.1f, 0.9f));
				_labelStyle.fontSize = 18;
				_labelStyle.alignment = (TextAnchor)4;
				((Object)_labelStyle.normal.background).hideFlags = (HideFlags)61;
				_toggleStyle.normal.textColor = Color.white;
				_toggleStyle.fontSize = 18;
				_hScrollStyle.normal.textColor = Color.white;
				_hScrollStyle.normal.background = MakeTex(2, 2, new Color(0f, 0f, 0.2f, 0.9f));
				((Object)_hScrollStyle.normal.background).hideFlags = (HideFlags)61;
			}
		}

		private Texture2D MakeTex(int width, int height, Color col)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			Color[] array = (Color[])(object)new Color[width * height];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = col;
			}
			Texture2D val = new Texture2D(width, height);
			val.SetPixels(array);
			val.Apply();
			return val;
		}

		public void OnDestroy()
		{
			_mls.LogInfo((object)"The GUILoader was destroyed :(");
		}

		public void Update()
		{
			if (((KeyboardShortcut)(ref _openCloseMenu)).IsDown() && !_wasKeyDown)
			{
				_wasKeyDown = true;
			}
			if (((KeyboardShortcut)(ref _openCloseMenu)).IsUp() && _wasKeyDown)
			{
				_mls.LogInfo((object)"Update - Menu Is Up and Key Was Not Down");
				_wasKeyDown = false;
				_isMenuOpen = !_isMenuOpen;
				if (_isMenuOpen)
				{
					_mls.LogInfo((object)"Menu was open");
					Cursor.visible = true;
					Cursor.lockState = (CursorLockMode)2;
				}
				else
				{
					_mls.LogInfo((object)"Menu was not open");
					Cursor.visible = false;
				}
			}
		}

		public void OnGUI()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			if (_menuStyle == null)
			{
				IntitializeMenu();
			}
			if (_isMenuOpen)
			{
				GUI.Box(new Rect((float)MENUX, (float)MENUY, (float)MENUWIDTH, (float)MENUHEIGHT), "Inventory Keyboard Bindings", _menuStyle);
				_toolbarInt = GUI.Toolbar(new Rect((float)MENUX, (float)(MENUY - 30), (float)MENUWIDTH, 30f), _toolbarInt, _toolbarStrings, _buttonStyle);
				if (_toolbarInt == 0)
				{
					GuiEnableNumKeyInventoryBindings = GUI.Toggle(new Rect((float)CENTERX, (float)(MENUY + 440), (float)ITEMWIDTH, 30f), GuiEnableNumKeyInventoryBindings, "Enable num key inventory bindings?", _toggleStyle);
				}
			}
		}
	}
}
namespace BackendIain.LC.KeyboardInventory.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	public class PlayerControllerBPatch
	{
		private static readonly IDictionary<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 };

		internal static ControlActionState _itemSlot1Action;

		internal static ControlActionState _itemSlot2Action;

		internal static ControlActionState _itemSlot3Action;

		internal static ControlActionState _itemSlot4Action;

		internal static ControlActionState _emote1Action;

		internal static ControlActionState _emote2Action;

		internal static ControllerState _allActions;

		internal static ControllerState _itemSlotActions;

		internal static ControllerState _emoteActions;

		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		public static void PatchStart(ref PlayerControllerB __instance)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.isHostPlayerObject)
			{
				KeyboardInventoryModBase._playerRef = __instance;
			}
			ControlActionState itemSlot1Action = default(ControlActionState);
			itemSlot1Action.Key = new KeyboardShortcut((KeyCode)49, Array.Empty<KeyCode>());
			itemSlot1Action.Description = "Item slot 1";
			itemSlot1Action.ItemSlotIndex = 0;
			_itemSlot1Action = itemSlot1Action;
			itemSlot1Action = default(ControlActionState);
			itemSlot1Action.Key = new KeyboardShortcut((KeyCode)50, Array.Empty<KeyCode>());
			itemSlot1Action.Description = "Item slot 2";
			itemSlot1Action.ItemSlotIndex = 1;
			_itemSlot2Action = itemSlot1Action;
			itemSlot1Action = default(ControlActionState);
			itemSlot1Action.Key = new KeyboardShortcut((KeyCode)51, Array.Empty<KeyCode>());
			itemSlot1Action.Description = "Item slot 3";
			itemSlot1Action.ItemSlotIndex = 2;
			_itemSlot3Action = itemSlot1Action;
			itemSlot1Action = default(ControlActionState);
			itemSlot1Action.Key = new KeyboardShortcut((KeyCode)52, Array.Empty<KeyCode>());
			itemSlot1Action.Description = "Item slot 4";
			itemSlot1Action.ItemSlotIndex = 3;
			_itemSlot4Action = itemSlot1Action;
			ControllerState itemSlotActions = default(ControllerState);
			itemSlotActions.Inputs = new ControlActionState[4] { _itemSlot1Action, _itemSlot2Action, _itemSlot3Action, _itemSlot4Action };
			_itemSlotActions = itemSlotActions;
			itemSlot1Action = default(ControlActionState);
			itemSlot1Action.Key = new KeyboardShortcut((KeyCode)45, Array.Empty<KeyCode>());
			itemSlot1Action.Description = "Emote 1";
			itemSlot1Action.ItemSlotIndex = 4;
			_emote1Action = itemSlot1Action;
			itemSlot1Action = default(ControlActionState);
			itemSlot1Action.Key = new KeyboardShortcut((KeyCode)61, Array.Empty<KeyCode>());
			itemSlot1Action.Description = "Emote 2";
			itemSlot1Action.ItemSlotIndex = 5;
			_emote2Action = itemSlot1Action;
			itemSlotActions = default(ControllerState);
			itemSlotActions.Inputs = new ControlActionState[2] { _emote1Action, _emote2Action };
			_emoteActions = itemSlotActions;
			itemSlotActions = default(ControllerState);
			itemSlotActions.Inputs = _itemSlotActions.Inputs.Concat(_emoteActions.Inputs);
			_allActions = itemSlotActions;
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		public static void PatchUpdate(ref PlayerControllerB __instance, ref float ___timeSinceSwitchingSlots, ref bool ___throwingObject)
		{
			KeyboardInventoryModBase._myGUI.GuiIsHost = KeyboardInventoryModBase._isHost;
			KeyboardInventoryModBase.Instance.UpdateCFGVarsFromGUI();
			if (!KeyboardInventoryModBase.EnableNumKeyInventoryBindings.Value || ((!((NetworkBehaviour)__instance).IsOwner || !__instance.isPlayerControlled || (((NetworkBehaviour)__instance).IsServer && !__instance.isHostPlayerObject)) && !__instance.isTestingPlayer) || _allActions.Inputs == null || !_allActions.Inputs.Any(delegate(ControlActionState x)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				KeyboardShortcut key = x.Key;
				return ((KeyboardShortcut)(ref key)).IsDown();
			}))
			{
				return;
			}
			foreach (ControlActionState input in _allActions.Inputs)
			{
				HandleActionInput(input, ref __instance, ref ___timeSinceSwitchingSlots, ref ___throwingObject);
			}
		}

		private static ControlActionState HandleActionInput(ControlActionState action, ref PlayerControllerB __instance, ref float __timeSinceSwitchingSlots, ref bool __throwingObject)
		{
			//IL_0013: 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_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Invalid comparison between Unknown and I4
			KeyboardShortcut key = action.Key;
			if (((KeyboardShortcut)(ref key)).IsDown() && !action.EndedDown)
			{
				action.EndedDown = true;
			}
			key = action.Key;
			if (((KeyboardShortcut)(ref key)).IsUp() && action.EndedDown)
			{
				action.EndedDown = false;
			}
			if (action.EndedDown && _emoteActions.Inputs.Any(delegate(ControlActionState x)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0007: 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_0015: Unknown result type (might be due to invalid IL or missing references)
				//IL_001a: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				KeyboardShortcut key2 = x.Key;
				KeyCode mainKey = ((KeyboardShortcut)(ref key2)).MainKey;
				key2 = action.Key;
				return mainKey == ((KeyboardShortcut)(ref key2)).MainKey;
			}))
			{
				key = action.Key;
				PerformEmote(ref __instance, ((int)((KeyboardShortcut)(ref key)).MainKey == 45) ? 1 : 2);
				return action;
			}
			if (action.EndedDown && CanSwitchItem(__instance, __timeSinceSwitchingSlots, __throwingObject))
			{
				StopAllEmotes(ref __instance, __timeSinceSwitchingSlots, __throwingObject);
				SwitchItemSlot(__instance, action.ItemSlotIndex, __timeSinceSwitchingSlots, __throwingObject);
				__timeSinceSwitchingSlots = 0f;
			}
			return action;
		}

		private static bool SwitchItemSlot(PlayerControllerB instance, int slotIndex, float timeSinceSwitchingSlots, bool isThrowingObject)
		{
			if (!CanSwitchItem(instance, timeSinceSwitchingSlots, isThrowingObject) || instance.currentItemSlot == slotIndex)
			{
				return false;
			}
			int num = instance.currentItemSlot - slotIndex;
			bool flag = num > 0;
			if (Math.Abs(num) != instance.ItemSlots.Length - 1)
			{
				object[] parameters = (flag ? _backwardsParam : _forwardsParam);
				do
				{
					InvokePrivateMethod(instance, "SwitchItemSlotsServerRpc", parameters);
					num += ((!flag) ? 1 : (-1));
				}
				while (num != 0);
			}
			ShipBuildModeManager.Instance.CancelBuildMode(true);
			instance.playerBodyAnimator.SetBool("GrabValidated", false);
			InvokePrivateMethod(instance, "SwitchToItemSlot", new object[2] { slotIndex, null });
			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 CanSwitchItem(PlayerControllerB instance, float timeSinceSwitchingSlots, bool isThrowingObject)
		{
			if (!((double)timeSinceSwitchingSlots < 0.01 || instance.inTerminalMenu || instance.isGrabbingObjectAnimation || instance.inSpecialInteractAnimation || isThrowingObject) && !instance.isTypingChat && !instance.twoHanded && !instance.activatingItem && !instance.jetpackControls)
			{
				return !instance.disablingJetpackControls;
			}
			return false;
		}

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

		private static void StopAllEmotes(ref PlayerControllerB __instance, float timeSinceSwitchingSlots, bool isThrowingObject)
		{
			__instance.performingEmote = false;
			__instance.timeSinceStartingEmote = 0f;
			__instance.StopPerformingEmoteServerRpc();
		}

		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);
		}
	}
}
namespace BackendIain.LC.KeyboardInventory.Models
{
	internal struct ControlActionState
	{
		public KeyboardShortcut Key { get; internal set; }

		public bool EndedDown { get; internal set; }

		public string Description { get; internal set; }

		public int ItemSlotIndex { get; internal set; }
	}
	internal struct ControllerState
	{
		public IEnumerable<ControlActionState> Inputs { get; set; }
	}
}