Decompiled source of ToolKeybinds v0.1.0

ToolKeybinds.dll

Decompiled 4 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
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 Silksong.ModMenu.Elements;
using Silksong.ModMenu.Models;
using Silksong.ModMenu.Plugin;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ToolKeybinds")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+dfb0437eaebbdaf45afc6f6d3c2e135bcbab87e9")]
[assembly: AssemblyProduct("ToolKeybinds")]
[assembly: AssemblyTitle("ToolKeybinds")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://codeberg.org/KikuGie/silksong-tool-keybinds")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace ToolKeybinds
{
	[BepInPlugin("dev.kikugie.toolkeybinds", "ToolKeybinds", "0.1.0")]
	public class ToolKeybindsPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(HeroController), "GetWillThrowTool")]
		private static class GetWillThrowToolPatch
		{
			[CompilerGenerated]
			private sealed class <Transpiler>d__2 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IEnumerator, IDisposable
			{
				private int <>1__state;

				private CodeInstruction <>2__current;

				private int <>l__initialThreadId;

				private IEnumerable<CodeInstruction> instructions;

				public IEnumerable<CodeInstruction> <>3__instructions;

				private IEnumerator<CodeInstruction> <>7__wrap1;

				CodeInstruction IEnumerator<CodeInstruction>.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				object IEnumerator.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				[DebuggerHidden]
				public <Transpiler>d__2(int <>1__state)
				{
					this.<>1__state = <>1__state;
					<>l__initialThreadId = Environment.CurrentManagedThreadId;
				}

				[DebuggerHidden]
				void IDisposable.Dispose()
				{
					int num = <>1__state;
					if (num == -3 || num == 1)
					{
						try
						{
						}
						finally
						{
							<>m__Finally1();
						}
					}
					<>7__wrap1 = null;
					<>1__state = -2;
				}

				private bool MoveNext()
				{
					try
					{
						switch (<>1__state)
						{
						default:
							return false;
						case 0:
							<>1__state = -1;
							<>7__wrap1 = instructions.GetEnumerator();
							<>1__state = -3;
							break;
						case 1:
							<>1__state = -3;
							break;
						}
						if (<>7__wrap1.MoveNext())
						{
							CodeInstruction current = <>7__wrap1.Current;
							if (current.opcode == OpCodes.Call && object.Equals(current.operand, getAttackToolMethod))
							{
								current.operand = patchAttackToolMethod;
							}
							<>2__current = current;
							<>1__state = 1;
							return true;
						}
						<>m__Finally1();
						<>7__wrap1 = null;
						return false;
					}
					catch
					{
						//try-fault
						((IDisposable)this).Dispose();
						throw;
					}
				}

				bool IEnumerator.MoveNext()
				{
					//ILSpy generated this explicit interface implementation from .override directive in MoveNext
					return this.MoveNext();
				}

				private void <>m__Finally1()
				{
					<>1__state = -1;
					if (<>7__wrap1 != null)
					{
						<>7__wrap1.Dispose();
					}
				}

				[DebuggerHidden]
				void IEnumerator.Reset()
				{
					throw new NotSupportedException();
				}

				[DebuggerHidden]
				IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
				{
					<Transpiler>d__2 <Transpiler>d__;
					if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
					{
						<>1__state = 0;
						<Transpiler>d__ = this;
					}
					else
					{
						<Transpiler>d__ = new <Transpiler>d__2(0);
					}
					<Transpiler>d__.instructions = <>3__instructions;
					return <Transpiler>d__;
				}

				[DebuggerHidden]
				IEnumerator IEnumerable.GetEnumerator()
				{
					return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
				}
			}

			private static MethodInfo getAttackToolMethod = AccessTools.Method(typeof(ToolItemManager), "GetBoundAttackTool", new Type[3]
			{
				typeof(AttackToolBinding),
				typeof(ToolEquippedReadSource),
				typeof(AttackToolBinding).MakeByRefType()
			}, (Type[])null);

			private static MethodInfo patchAttackToolMethod = AccessTools.Method(typeof(ToolKeybindsPlugin), "GetBoundAttackTool", (Type[])null, (Type[])null);

			[IteratorStateMachine(typeof(<Transpiler>d__2))]
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
				return new <Transpiler>d__2(-2)
				{
					<>3__instructions = instructions
				};
			}
		}

		[CompilerGenerated]
		private static class <>O
		{
			public static MenuElementGenerator <0>__CreateUpperKeybindElement;

			public static MenuElementGenerator <1>__CreateLowerKeybindElement;
		}

		private const string KeyRestriction = "Must be a valid Unity KeyCode (https://docs.unity3d.com/ScriptReference/KeyCode.html).";

		private const string UpperKeyDesc = "Key to use the upper tool.";

		private const string LowerKeyDesc = "Key to use the lower tool.";

		private static ToolKeybindsPlugin? instance;

		private ConfigEntry<bool> modEnabled;

		private ConfigEntry<KeyboardShortcut> upperToolKey;

		private ConfigEntry<KeyboardShortcut> lowerToolKey;

		public const string Id = "dev.kikugie.toolkeybinds";

		public static string Name => "ToolKeybinds";

		public static string Version => "0.1.0";

		private void Awake()
		{
			//IL_000b: 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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0041: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_009c: Expected O, but got Unknown
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: 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_0091: Expected O, but got Unknown
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Expected O, but got Unknown
			//IL_00f7: Expected O, but got Unknown
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			instance = this;
			new Harmony("dev.kikugie.toolkeybinds").PatchAll();
			modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>(new ConfigDefinition("General", "ModEnabled"), true, new ConfigDescription("Toggles the functionality of the mod.", (AcceptableValueBase)null, Array.Empty<object>()));
			ConfigFile config = ((BaseUnityPlugin)this).Config;
			ConfigDefinition val = new ConfigDefinition("Keybinds", "UpperToolKey");
			KeyboardShortcut val2 = new KeyboardShortcut((KeyCode)103, Array.Empty<KeyCode>());
			object[] array = new object[1];
			object obj = <>O.<0>__CreateUpperKeybindElement;
			if (obj == null)
			{
				MenuElementGenerator val3 = CreateUpperKeybindElement;
				<>O.<0>__CreateUpperKeybindElement = val3;
				obj = (object)val3;
			}
			array[0] = obj;
			upperToolKey = config.Bind<KeyboardShortcut>(val, val2, new ConfigDescription("Key to use the upper tool.\nMust be a valid Unity KeyCode (https://docs.unity3d.com/ScriptReference/KeyCode.html).", (AcceptableValueBase)null, array));
			ConfigFile config2 = ((BaseUnityPlugin)this).Config;
			ConfigDefinition val4 = new ConfigDefinition("Keybinds", "LowerToolKey");
			KeyboardShortcut val5 = new KeyboardShortcut((KeyCode)104, Array.Empty<KeyCode>());
			object[] array2 = new object[1];
			object obj2 = <>O.<1>__CreateLowerKeybindElement;
			if (obj2 == null)
			{
				MenuElementGenerator val6 = CreateLowerKeybindElement;
				<>O.<1>__CreateLowerKeybindElement = val6;
				obj2 = (object)val6;
			}
			array2[0] = obj2;
			lowerToolKey = config2.Bind<KeyboardShortcut>(val4, val5, new ConfigDescription("Key to use the lower tool.\nMust be a valid Unity KeyCode (https://docs.unity3d.com/ScriptReference/KeyCode.html).", (AcceptableValueBase)null, array2));
		}

		private void Update()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			if (modEnabled.Value)
			{
				HeroController controller = HeroController.instance;
				KeyboardShortcut value = upperToolKey.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					TryThrowTool(controller, (AttackToolBinding)1);
				}
				value = lowerToolKey.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					TryThrowTool(controller, (AttackToolBinding)2);
				}
			}
		}

		private static bool CreateUpperKeybindElement(ConfigEntryBase entry, out MenuElement element)
		{
			return CreateKeybindElement((ConfigEntry<KeyboardShortcut>)(object)entry, out element, "Upper Tool Key", "Key to use the upper tool.");
		}

		private static bool CreateLowerKeybindElement(ConfigEntryBase entry, out MenuElement element)
		{
			return CreateKeybindElement((ConfigEntry<KeyboardShortcut>)(object)entry, out element, "Lower Tool Key", "Key to use the lower tool.");
		}

		private static bool CreateKeybindElement(ConfigEntry<KeyboardShortcut> entry, out MenuElement element, string name, string desc)
		{
			//IL_0059: 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_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: 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_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry<KeyboardShortcut> entry2 = entry;
			ParserTextModel<KeyCode> val = new ParserTextModel<KeyCode>((Parse<KeyCode>)delegate(string text, out KeyCode value)
			{
				return Enum.TryParse<KeyCode>(text, ignoreCase: true, out value);
			}, (Unparse<KeyCode>)delegate(KeyCode value, out string text)
			{
				text = ((object)(KeyCode)(ref value)).ToString();
				return true;
			}, (KeyCode)0);
			KeyboardShortcut value2 = entry2.Value;
			val.SetValue(((KeyboardShortcut)(ref value2)).MainKey);
			val.OnValueChanged += delegate(KeyCode code)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				entry2.Value = new KeyboardShortcut(code, Array.Empty<KeyCode>());
			};
			TextInput<KeyCode> val2 = new TextInput<KeyCode>(LocalizedText.op_Implicit(name), (ITextModel<KeyCode>)(object)val, LocalizedText.op_Implicit(desc));
			InputField inputField = val2.InputField;
			value2 = entry2.Value;
			KeyCode mainKey = ((KeyboardShortcut)(ref value2)).MainKey;
			inputField.text = ((object)(KeyCode)(ref mainKey)).ToString();
			element = (MenuElement)(object)val2;
			return true;
		}

		private static void TryThrowTool(HeroController controller, AttackToolBinding variant)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			if (TryAssignThrownTool(controller, variant))
			{
				controller.ThrowTool(false);
				return;
			}
			controller.toolThrowQueueSteps = 0;
			controller.toolThrowQueueing = true;
		}

		private static bool TryAssignThrownTool(HeroController controller, AttackToolBinding variant)
		{
			//IL_001b: 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)
			if (!controller.acceptingInput || !controller.ThrowToolCooldownReady() || !controller.CanAttackAction())
			{
				return false;
			}
			AttackToolBinding val = default(AttackToolBinding);
			controller.willThrowTool = ToolItemManager.GetBoundAttackTool(variant, (ToolEquippedReadSource)0, ref val);
			return controller.CanThrowTool(controller.willThrowTool, val, true);
		}

		private static ToolItem GetBoundAttackTool(AttackToolBinding binding, ToolEquippedReadSource source, out AttackToolBinding result)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			return ToolItemManager.GetBoundAttackTool((AttackToolBinding)((!((Object)(object)instance != (Object)null) || !instance.modEnabled.Value) ? ((int)binding) : 0), source, ref result);
		}
	}
}