Decompiled source of EasyBackpackHold v1.0.1

plugins\youxia173.EasyBackpackHold.dll

Decompiled a day ago
using System;
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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("游侠173")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("EasyBackpackHold")]
[assembly: AssemblyTitle("youxia173.EasyBackpackHold")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.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]
	[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]
	[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 EasyBackpackHold
{
	[BepInPlugin("youxia173.EasyBackpackHold", "EasyBackpackHold", "1.0.1")]
	public sealed class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static Plugin Instance;

		private ConfigEntry<KeyCode> _backpackKey;

		private ConfigEntry<KeyCode> _altBackpackKey;

		internal static bool OpenedByMod;

		private static bool _fakedInteract;

		private static bool _savedInteractPressed;

		private void Awake()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			_backpackKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "BackpackKey", (KeyCode)98, "按住此键打开背上的背包(松开关闭,与原版长按交互一致)");
			_altBackpackKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "AlternativeBackpackKey", (KeyCode)0, "可选备用键");
			new Harmony("youxia173.EasyBackpackHold").PatchAll(typeof(BackpackWheelUpdatePatch));
			Log.LogInfo((object)"EasyBackpackHold v1.0.1 loaded");
		}

		private void Update()
		{
			if (!Application.isFocused)
			{
				OpenedByMod = false;
				return;
			}
			Character localCharacter = Character.localCharacter;
			if ((Object)(object)localCharacter == (Object)null)
			{
				OpenedByMod = false;
				return;
			}
			bool flag = IsBackpackKeyHeld();
			if (OpenedByMod)
			{
				if (!flag)
				{
					OpenedByMod = false;
				}
			}
			else
			{
				if (!IsBackpackKeyDown() || localCharacter.data.passedOut || localCharacter.data.dead || localCharacter.data.currentItem is Backpack)
				{
					return;
				}
				if (TryOpenEquippedBackpack(localCharacter))
				{
					OpenedByMod = true;
					return;
				}
				Character carriedPlayer = localCharacter.data.carriedPlayer;
				if ((Object)(object)carriedPlayer != (Object)null && TryOpenEquippedBackpack(carriedPlayer))
				{
					OpenedByMod = true;
				}
			}
		}

		internal bool IsBackpackKeyHeld()
		{
			//IL_0006: 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_0027: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKey(_backpackKey.Value))
			{
				return true;
			}
			if ((int)_altBackpackKey.Value != 0)
			{
				return Input.GetKey(_altBackpackKey.Value);
			}
			return false;
		}

		private bool IsBackpackKeyDown()
		{
			//IL_0006: 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_0027: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKeyDown(_backpackKey.Value))
			{
				return true;
			}
			if ((int)_altBackpackKey.Value != 0)
			{
				return Input.GetKeyDown(_altBackpackKey.Value);
			}
			return false;
		}

		private static bool TryOpenEquippedBackpack(Character character)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)((character != null) ? character.player : null) == (Object)null)
			{
				return false;
			}
			BackpackSlot backpackSlot = character.player.backpackSlot;
			if (backpackSlot == null || ((ItemSlot)backpackSlot).IsEmpty() || (int)backpackSlot.backpackType == 0)
			{
				return false;
			}
			GUIManager instance = GUIManager.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return false;
			}
			int num = ResolveSlotCount(backpackSlot);
			BackpackReference fromEquippedBackpack = BackpackReference.GetFromEquippedBackpack(character);
			instance.OpenBackpackWheel(fromEquippedBackpack, num, backpackSlot.backpackType);
			return true;
		}

		private static int ResolveSlotCount(BackpackSlot slot)
		{
			//IL_001d: 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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected I4, but got Unknown
			Item prefab = ((ItemSlot)slot).prefab;
			Backpack val = (Backpack)(object)((prefab is Backpack) ? prefab : null);
			if (val != null)
			{
				return Mathf.Max(0, val.slotCount);
			}
			BackpackType backpackType = slot.backpackType;
			switch (backpackType - 1)
			{
			case 2:
			case 3:
				return 0;
			case 1:
				return 1;
			case 0:
				return 4;
			default:
				return 0;
			}
		}

		internal static void BeginFakeInteractIfNeeded()
		{
			if (OpenedByMod && !((Object)(object)Instance == (Object)null) && Instance.IsBackpackKeyHeld())
			{
				Character localCharacter = Character.localCharacter;
				if (!((Object)(object)localCharacter?.input == (Object)null))
				{
					_savedInteractPressed = localCharacter.input.interactIsPressed;
					localCharacter.input.interactIsPressed = true;
					_fakedInteract = true;
				}
			}
		}

		internal static void EndFakeInteractIfNeeded()
		{
			if (_fakedInteract)
			{
				_fakedInteract = false;
				Character localCharacter = Character.localCharacter;
				if ((Object)(object)localCharacter?.input != (Object)null)
				{
					localCharacter.input.interactIsPressed = _savedInteractPressed;
				}
			}
		}
	}
	[HarmonyPatch(typeof(BackpackWheel), "Update")]
	internal static class BackpackWheelUpdatePatch
	{
		[HarmonyPrefix]
		private static void Prefix()
		{
			Plugin.BeginFakeInteractIfNeeded();
		}

		[HarmonyPostfix]
		private static void Postfix()
		{
			Plugin.EndFakeInteractIfNeeded();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "youxia173.EasyBackpackHold";

		public const string PLUGIN_NAME = "EasyBackpackHold";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}