Decompiled source of ValheimHotKeys v1.1.1

ValheimHotKeys.dll

Decompiled a week 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 HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ValheimHotKeys")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Configurable hotkeys for various Valheim actions.")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1+8029b491212d67d6c81b11f7ea0e734888b2c12e")]
[assembly: AssemblyProduct("ValheimHotKeys")]
[assembly: AssemblyTitle("ValheimHotKeys")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 ValheimHotKeys
{
	[HarmonyPatch]
	public static class HotKeyPatches
	{
		private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput", (Type[])null, (Type[])null);

		[HarmonyPatch(typeof(Hud), "Update")]
		[HarmonyPrefix]
		public static void Hud_Update_Prefix(Hud __instance)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			if (InputHelpers.IsDownExact(Plugin.ToggleHUDConfig.Value))
			{
				__instance.m_userHidden = !__instance.m_userHidden;
			}
		}

		[HarmonyPatch(typeof(Player), "Update")]
		[HarmonyPostfix]
		public static void Player_Update_Postfix(Player __instance)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || !(bool)TakeInputMethod.Invoke(__instance, null))
			{
				return;
			}
			for (int i = 0; i < 8; i++)
			{
				if (InputHelpers.IsDownPermissive(Plugin.HotbarConfigs[i].Value))
				{
					__instance.UseHotbarItem(i + 1);
					return;
				}
			}
			for (int j = 0; j < Plugin.CustomItemBindings.Count; j++)
			{
				Plugin.ItemBinding value = Plugin.CustomItemBindings[j].Value;
				if (!InputHelpers.IsDownPermissive(value.Shortcut))
				{
					continue;
				}
				string itemName = value.ItemName;
				if (string.IsNullOrEmpty(itemName))
				{
					continue;
				}
				Inventory inventory = ((Humanoid)__instance).GetInventory();
				List<ItemData> allItems = inventory.GetAllItems();
				string searchName = itemName.ToLower();
				List<ItemData> list = allItems.FindAll((ItemData iData) => Localization.instance.Localize(iData.m_shared.m_name).ToLower().Contains(searchName) || iData.m_shared.m_name.ToLower().Contains(searchName));
				if (list.Count > 0)
				{
					ItemData val = list[0];
					if (list.Count > 1)
					{
						int num = list.FindIndex((ItemData m) => m.m_equipped);
						if (num != -1)
						{
							val = list[(num + 1) % list.Count];
						}
					}
					((Humanoid)__instance).UseItem(inventory, val, true);
					break;
				}
				ZLog.LogWarning((object)("[ValheimHotKeys] No item matching '" + itemName + "' found in inventory."));
			}
		}
	}
	public static class InputHelpers
	{
		private static readonly KeyCode[] ModifierKeys;

		public static bool IsDownPermissive(KeyboardShortcut shortcut)
		{
			//IL_0000: 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)
			if (!IsMainKeyDown(shortcut))
			{
				return false;
			}
			foreach (KeyCode modifier in ((KeyboardShortcut)(ref shortcut)).Modifiers)
			{
				if (!ZInput.GetKey(modifier, false))
				{
					return false;
				}
			}
			return true;
		}

		public static bool IsDownExact(KeyboardShortcut shortcut)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: 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)
			if (!IsMainKeyDown(shortcut))
			{
				return false;
			}
			KeyCode[] modifierKeys = ModifierKeys;
			foreach (KeyCode val in modifierKeys)
			{
				bool flag = false;
				foreach (KeyCode modifier in ((KeyboardShortcut)(ref shortcut)).Modifiers)
				{
					if (modifier == val)
					{
						flag = true;
						break;
					}
				}
				if (ZInput.GetKey(val, false) != flag)
				{
					return false;
				}
			}
			return true;
		}

		private static bool IsMainKeyDown(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_000b: 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)
			KeyCode mainKey = ((KeyboardShortcut)(ref shortcut)).MainKey;
			if ((int)mainKey == 0 || !ZInput.IsKeyCodeValid(mainKey))
			{
				return false;
			}
			return ZInput.GetKeyDown(mainKey, false);
		}

		static InputHelpers()
		{
			KeyCode[] array = new KeyCode[6];
			RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
			ModifierKeys = (KeyCode[])(object)array;
		}
	}
	[BepInPlugin("com.malafein.valheimhotkeys", "Valheim HotKeys", "1.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		public class ItemBinding
		{
			public string ItemName = "";

			public KeyboardShortcut Shortcut = KeyboardShortcut.Empty;

			public string Serialize()
			{
				if (string.IsNullOrEmpty(ItemName))
				{
					return "";
				}
				return ItemName + "|" + ((KeyboardShortcut)(ref Shortcut)).Serialize();
			}

			public static ItemBinding Deserialize(string str)
			{
				//IL_004e: 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_0040: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: Unknown result type (might be due to invalid IL or missing references)
				ItemBinding itemBinding = new ItemBinding();
				if (string.IsNullOrEmpty(str))
				{
					return itemBinding;
				}
				int num = str.LastIndexOf('|');
				if (num == -1)
				{
					itemBinding.ItemName = str;
					return itemBinding;
				}
				itemBinding.ItemName = str.Substring(0, num);
				string text = str.Substring(num + 1);
				try
				{
					itemBinding.Shortcut = KeyboardShortcut.Deserialize(text);
				}
				catch
				{
					itemBinding.Shortcut = KeyboardShortcut.Empty;
				}
				return itemBinding;
			}
		}

		public class ConfigurationManagerAttributes
		{
			public Action<ConfigEntryBase> CustomDrawer;

			public bool? ShowMultilineText;

			public string DispName;

			public int? Order;

			public bool? HideDefaultButton;

			public bool? HideSettingName;
		}

		public const string ModGUID = "com.malafein.valheimhotkeys";

		public const string ModName = "Valheim HotKeys";

		public const string ModVersion = "1.1.1";

		public static ConfigEntry<KeyboardShortcut> ToggleHUDConfig;

		public static ConfigEntry<KeyboardShortcut>[] HotbarConfigs = new ConfigEntry<KeyboardShortcut>[8];

		public static List<ConfigEntry<ItemBinding>> CustomItemBindings = new List<ConfigEntry<ItemBinding>>();

		private readonly Harmony harmony = new Harmony("com.malafein.valheimhotkeys");

		private static ConfigEntryBase _waitingEntry;

		private static int _activationFrame = -1;

		private static int _forbiddenFrame = -1;

		private void Awake()
		{
			//IL_000a: 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_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Expected O, but got Unknown
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			TomlTypeConverter.AddConverter(typeof(ItemBinding), new TypeConverter
			{
				ConvertToObject = (string str, Type type) => ItemBinding.Deserialize(str),
				ConvertToString = (object obj, Type type) => ((ItemBinding)obj).Serialize()
			});
			ToggleHUDConfig = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "ToggleHUD", new KeyboardShortcut((KeyCode)284, Array.Empty<KeyCode>()), "Hotkey to toggle the HUD visibility.");
			for (int num = 0; num < 8; num++)
			{
				int num2 = num + 1;
				HotbarConfigs[num] = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotbar", $"Slot{num2}", new KeyboardShortcut((KeyCode)0, Array.Empty<KeyCode>()), $"Hotkey for hotbar slot {num2}.");
			}
			for (int num3 = 0; num3 < 8; num3++)
			{
				int num4 = num3 + 1;
				ConfigurationManagerAttributes configurationManagerAttributes = new ConfigurationManagerAttributes
				{
					CustomDrawer = DrawItemBindingElement,
					HideDefaultButton = false
				};
				ItemBinding itemBinding = new ItemBinding();
				switch (num3)
				{
				case 0:
					itemBinding.ItemName = "Healing Mead";
					itemBinding.Shortcut = new KeyboardShortcut((KeyCode)326, Array.Empty<KeyCode>());
					break;
				case 1:
					itemBinding.ItemName = "Arrow";
					itemBinding.Shortcut = new KeyboardShortcut((KeyCode)327, Array.Empty<KeyCode>());
					break;
				}
				CustomItemBindings.Add(((BaseUnityPlugin)this).Config.Bind<ItemBinding>("Custom Item Bindings", $"Slot {num4}", itemBinding, new ConfigDescription($"Custom item binding for slot {num4}.", (AcceptableValueBase)null, new object[1] { configurationManagerAttributes })));
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Valheim HotKeys 1.1.1 is loading...");
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Valheim HotKeys loaded!");
		}

		private void DrawItemBindingElement(ConfigEntryBase entry)
		{
			//IL_0054: 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_031e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0323: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Invalid comparison between Unknown and I4
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Invalid comparison between Unknown and I4
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Invalid comparison between Unknown and I4
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Invalid comparison between Unknown and I4
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Invalid comparison between Unknown and I4
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Invalid comparison between Unknown and I4
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Invalid comparison between Unknown and I4
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Invalid comparison between Unknown and I4
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Invalid comparison between Unknown and I4
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Invalid comparison between Unknown and I4
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Invalid comparison between Unknown and I4
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Invalid comparison between Unknown and I4
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Invalid comparison between Unknown and I4
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Invalid comparison between Unknown and I4
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Invalid comparison between Unknown and I4
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Invalid comparison between Unknown and I4
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry<ItemBinding> val = (ConfigEntry<ItemBinding>)(object)entry;
			ItemBinding value = val.Value;
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			string text = GUILayout.TextField(value.ItemName, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(200f) });
			if (text != value.ItemName)
			{
				val.Value = new ItemBinding
				{
					ItemName = text,
					Shortcut = value.Shortcut
				};
			}
			GUILayout.Space(10f);
			bool flag = _waitingEntry == entry;
			GUI.enabled = !flag;
			string text2 = (flag ? "Press any key..." : ((object)Unsafe.As<KeyboardShortcut, KeyboardShortcut>(ref value.Shortcut)/*cast due to .constrained prefix*/).ToString());
			if (!flag && (string.IsNullOrEmpty(text2) || text2 == "None"))
			{
				text2 = "Click to bind";
			}
			if (GUILayout.Button(text2, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(150f) }))
			{
				_waitingEntry = entry;
				_activationFrame = Time.frameCount;
			}
			GUI.enabled = true;
			if (flag)
			{
				if (GUILayout.Button("Cancel", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(60f) }))
				{
					_waitingEntry = null;
					_forbiddenFrame = Time.frameCount;
					Event.current.Use();
				}
				else
				{
					Event current = Event.current;
					if (Time.frameCount > _activationFrame && ((int)current.type == 4 || (int)current.type == 0 || (int)current.type == 12) && ((int)current.type == 12 || ((int)current.type != 1 && (int)current.type != 3 && (int)current.type != 6)))
					{
						KeyCode val2 = (KeyCode)0;
						if ((int)current.keyCode != 0)
						{
							val2 = current.keyCode;
						}
						else if (current.button >= 0 && current.button <= 4)
						{
							val2 = (KeyCode)(323 + current.button);
						}
						if ((int)val2 != 0 && (int)val2 != 323)
						{
							if ((int)val2 == 27)
							{
								_waitingEntry = null;
								current.Use();
							}
							else if ((int)val2 != 306 && (int)val2 != 305 && (int)val2 != 304 && (int)val2 != 303 && (int)val2 != 308 && (int)val2 != 307 && (int)val2 != 310 && (int)val2 != 309)
							{
								List<KeyCode> list = new List<KeyCode>();
								if (current.control)
								{
									list.Add((KeyCode)306);
								}
								if (current.shift)
								{
									list.Add((KeyCode)304);
								}
								if (current.alt)
								{
									list.Add((KeyCode)308);
								}
								val.Value = new ItemBinding
								{
									ItemName = value.ItemName,
									Shortcut = new KeyboardShortcut(val2, list.ToArray())
								};
								_waitingEntry = null;
								current.Use();
							}
						}
					}
				}
			}
			else if (GUILayout.Button("Clear", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(60f) }) && Time.frameCount != _forbiddenFrame)
			{
				val.Value = new ItemBinding
				{
					ItemName = "",
					Shortcut = KeyboardShortcut.Empty
				};
				Event.current.Use();
			}
			GUILayout.EndHorizontal();
		}
	}
}