Decompiled source of HotbarSwitch v0.5.9

HotbarSwitch.dll

Decompiled 2 days ago
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
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: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.5.9")]
[assembly: AssemblyInformationalVersion("0.5.9+a4e9a1f94fbcc5e27b7e4122dfa3136026d20b70")]
[assembly: AssemblyVersion("0.5.9.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				if ((Object)(object)Chat.instance != (Object)null)
				{
					return Chat.instance.HasFocus();
				}
				return false;
			}
			return true;
		}
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog() && (!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
		{
			if ((Object)(object)TextViewer.instance != (Object)null)
			{
				return TextViewer.instance.IsVisible();
			}
			return false;
		}
		return true;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			return Input.GetKeyDown(value);
		}
		catch
		{
			return false;
		}
	}
}
namespace Properties
{
	[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
	[DebuggerNonUserCode]
	[CompilerGenerated]
	internal class Resources
	{
		private static ResourceManager resourceMan;

		private static CultureInfo resourceCulture;

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		internal static ResourceManager ResourceManager
		{
			get
			{
				if (resourceMan == null)
				{
					resourceMan = new ResourceManager("Properties.Resources", typeof(Resources).Assembly);
				}
				return resourceMan;
			}
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		internal static CultureInfo Culture
		{
			get
			{
				return resourceCulture;
			}
			set
			{
				resourceCulture = value;
			}
		}

		internal Resources()
		{
		}
	}
}
namespace HotbarSwitch
{
	[BepInPlugin("cjayride.HotbarSwitch", "Hotbar Switch", "0.5.9")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BepInExPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Inventory), "GetItemAt", new Type[]
		{
			typeof(int),
			typeof(int)
		})]
		private static class GetItemAt_Patch
		{
			private static void Prefix(ref int y)
			{
				if (readingHotbar && y == 0)
				{
					y = HotbarY();
				}
			}
		}

		[HarmonyPatch(typeof(Inventory), "GetBoundItems")]
		private static class GetBoundItems_Patch
		{
			private static bool Prefix(Inventory __instance, List<ItemData> bound)
			{
				if (!readingHotbar)
				{
					return true;
				}
				FillRow(__instance, bound, includeEmpty: false);
				return false;
			}
		}

		[HarmonyPatch(typeof(Inventory), "GetHotbar", new Type[] { typeof(bool) })]
		private static class GetHotbar_Patch
		{
			private static bool Prefix(Inventory __instance, bool includeEmpty, ref List<ItemData> __result)
			{
				if (!readingHotbar)
				{
					return true;
				}
				List<ItemData> list = new List<ItemData>();
				FillRow(__instance, list, includeEmpty);
				__result = list;
				return false;
			}
		}

		[HarmonyPatch(typeof(Player), "UseHotbarItem")]
		private static class UseHotbarItem_Patch
		{
			private static void Prefix()
			{
				readingHotbar = true;
			}

			private static void Postfix()
			{
				readingHotbar = false;
			}
		}

		[HarmonyPatch(typeof(HotkeyBar), "UpdateIcons")]
		private static class UpdateIcons_Patch
		{
			private static void Prefix()
			{
				readingHotbar = true;
			}

			private static void Postfix()
			{
				readingHotbar = false;
			}
		}

		public static ConfigEntry<bool> modEnabled;

		public static ConfigEntry<int> rowsToSwitch;

		public static ConfigEntry<KeyboardShortcut> hotKey;

		internal static int activeRow;

		private Harmony harmony;

		private static bool readingHotbar;

		private static FieldInfo seneaSlots;

		private static MethodInfo seneaSet;

		private static MethodInfo seneaClear;

		private static readonly object[] seneaSetArgs = new object[1];

		private static bool seneaPatched;

		private static bool keyHeld;

		private static object seneaView;

		private void Awake()
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
			rowsToSwitch = ((BaseUnityPlugin)this).Config.Bind<int>("General", "RowsToSwitch", 2, "How many inventory rows the hotkey cycles as the hotbar. Items stay in their original slots.");
			hotKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "HotKey", new KeyboardShortcut((KeyCode)96, Array.Empty<KeyCode>()), "Hotkey to switch which inventory row is shown on the hotbar. Accepts any Unity KeyCode (for example Keypad2) and modifier combos (Ctrl/Alt/Shift). With Configuration Manager, click the field and press the key, or use the key dropdown.");
			if (modEnabled.Value)
			{
				harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), ((BaseUnityPlugin)this).Info.Metadata.GUID);
				PatchSeneaL();
			}
		}

		private void Start()
		{
			PatchSeneaL();
		}

		private void OnDestroy()
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void PatchSeneaL()
		{
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			if (seneaPatched || harmony == null)
			{
				return;
			}
			Type type = AccessTools.TypeByName("SeneaLUI.Hud.HotbarView");
			Type type2 = AccessTools.TypeByName("SeneaLUI.Components.Slot");
			if (type == null)
			{
				return;
			}
			seneaSlots = AccessTools.Field(type, "_slots");
			if (type2 != null)
			{
				seneaSet = AccessTools.Method(type2, "Set", new Type[1] { typeof(ItemData) }, (Type[])null);
				seneaClear = AccessTools.Method(type2, "Clear", (Type[])null, (Type[])null);
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Tick", (Type[])null, (Type[])null);
			if (!(methodInfo == null))
			{
				HarmonyMethod val = new HarmonyMethod(typeof(BepInExPlugin), "BeginHotbarRead", (Type[])null);
				HarmonyMethod val2 = new HarmonyMethod(typeof(BepInExPlugin), "AfterSeneaLTick", (Type[])null);
				harmony.Patch((MethodBase)methodInfo, val, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				MethodInfo methodInfo2 = AccessTools.Method(type, "PadTick", (Type[])null, (Type[])null);
				if (methodInfo2 != null)
				{
					harmony.Patch((MethodBase)methodInfo2, val, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				seneaPatched = true;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"SeneaL hotbar hooked.");
			}
		}

		private static void BeginHotbarRead()
		{
			readingHotbar = true;
		}

		private static void AfterSeneaLTick(object __instance)
		{
			seneaView = __instance;
			try
			{
				PaintSeneaLHotbar(__instance);
			}
			finally
			{
				readingHotbar = false;
			}
		}

		private static void PaintSeneaLHotbar(object view)
		{
			if (view == null || seneaSlots == null || (Object)(object)Player.m_localPlayer == (Object)null || !(seneaSlots.GetValue(view) is Array array))
			{
				return;
			}
			Inventory inventory = ((Humanoid)Player.m_localPlayer).GetInventory();
			int num = HotbarY();
			int width = inventory.GetWidth();
			int num2 = array.Length;
			if (num2 > width)
			{
				num2 = width;
			}
			for (int i = 0; i < num2; i++)
			{
				object value = array.GetValue(i);
				if (value != null)
				{
					ItemData itemAt = inventory.GetItemAt(i, num);
					if (itemAt != null && seneaSet != null)
					{
						seneaSetArgs[0] = itemAt;
						seneaSet.Invoke(value, seneaSetArgs);
					}
					else if (seneaClear != null)
					{
						seneaClear.Invoke(value, null);
					}
				}
			}
		}

		private void Update()
		{
			TrySwitchRow();
		}

		private void LateUpdate()
		{
			if (!seneaPatched)
			{
				PatchSeneaL();
			}
			if (seneaView != null)
			{
				PaintSeneaLHotbar(seneaView);
			}
		}

		private void TrySwitchRow()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || (Object)(object)Player.m_localPlayer == (Object)null)
			{
				keyHeld = false;
				return;
			}
			bool flag = ShortcutIsDown(hotKey.Value);
			bool num = flag && !keyHeld;
			keyHeld = flag;
			if (num && !AedenthornUtils.IgnoreKeyPresses())
			{
				int num2 = Math.Max(1, Math.Min(((Humanoid)Player.m_localPlayer).GetInventory().GetHeight(), rowsToSwitch.Value));
				activeRow = (activeRow + 1) % num2;
			}
		}

		private static bool ShortcutIsDown(KeyboardShortcut shortcut)
		{
			//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_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Invalid comparison between Unknown and I4
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Invalid comparison between Unknown and I4
			KeyCode val = ((KeyboardShortcut)(ref shortcut)).MainKey;
			if ((int)val == 0)
			{
				val = (KeyCode)96;
			}
			foreach (KeyCode modifier in ((KeyboardShortcut)(ref shortcut)).Modifiers)
			{
				if ((int)modifier != 0 && !KeyHeld(modifier))
				{
					return false;
				}
			}
			if (KeyDown(val) || KeyHeld(val))
			{
				return true;
			}
			if ((int)val == 96 || (int)val == 126)
			{
				if (!KeyDown((KeyCode)96) && !KeyDown((KeyCode)126) && !KeyHeld((KeyCode)96) && !KeyHeld((KeyCode)126) && !Input.GetKey("`"))
				{
					return Input.GetKey("~");
				}
				return true;
			}
			return false;
		}

		private static bool KeyDown(KeyCode key)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (ZInput.GetKeyDown(key, false))
				{
					return true;
				}
			}
			catch
			{
			}
			return Input.GetKeyDown(key);
		}

		private static bool KeyHeld(KeyCode key)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (ZInput.GetKey(key, false))
				{
					return true;
				}
			}
			catch
			{
			}
			return Input.GetKey(key);
		}

		internal static int HotbarY()
		{
			if (!modEnabled.Value || (Object)(object)Player.m_localPlayer == (Object)null)
			{
				return 0;
			}
			int num = Math.Max(1, Math.Min(((Humanoid)Player.m_localPlayer).GetInventory().GetHeight(), rowsToSwitch.Value));
			if (activeRow >= num)
			{
				activeRow = 0;
			}
			return activeRow;
		}

		private static void FillRow(Inventory inventory, List<ItemData> dest, bool includeEmpty)
		{
			dest.Clear();
			int num = HotbarY();
			int width = inventory.GetWidth();
			for (int i = 0; i < width; i++)
			{
				ItemData itemAt = inventory.GetItemAt(i, num);
				if (itemAt != null || includeEmpty)
				{
					dest.Add(itemAt);
				}
			}
		}
	}
}