Decompiled source of EasyBackpackHold v1.0.3

plugins\youxia173.EasyBackpackHold.dll

Decompiled 2 months 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 Photon.Pun;
using UnityEngine;
using Zorro.Core;

[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.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyProduct("EasyBackpackHold")]
[assembly: AssemblyTitle("youxia173.EasyBackpackHold")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
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;
		}
	}
}
namespace EasyBackpackHold
{
	[HarmonyPatch(typeof(CharacterBackpackHandler), "AddFuelToJetpack")]
	internal static class AddFuelToJetpackPatch
	{
		[HarmonyPrefix]
		private static bool Prefix(CharacterBackpackHandler __instance, Character interactor, float addedFuel)
		{
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)interactor == (Object)null || !Object.op_Implicit((Object)(object)interactor.data.currentItem))
			{
				return false;
			}
			CharacterItems items = interactor.refs.items;
			if ((Object)(object)items == (Object)null || items.currentSelectedSlot.IsNone)
			{
				return false;
			}
			ItemSlot itemSlot = interactor.player.GetItemSlot(items.currentSelectedSlot.Value);
			if (itemSlot == null || itemSlot.IsEmpty())
			{
				return false;
			}
			float num = ResolveFuelAmount(interactor, addedFuel);
			PhotonView component = ((Component)__instance).GetComponent<PhotonView>();
			if ((Object)(object)component == (Object)null)
			{
				return true;
			}
			component.RPC("RPCAddFuelToJetpack", (RpcTarget)0, new object[1] { num });
			interactor.player.EmptySlot(items.currentSelectedSlot);
			if (items.currentSelectedSlot.IsSome && items.currentSelectedSlot.Value == 250)
			{
				((MonoBehaviourPun)interactor).photonView.RPC("DestroyHeldItemRpc", (RpcTarget)0, Array.Empty<object>());
			}
			else
			{
				items.EquipSlot(Optionable<byte>.None);
			}
			return false;
		}

		private static float ResolveFuelAmount(Character interactor, float fallback)
		{
			Item currentItem = interactor.data.currentItem;
			if ((Object)(object)currentItem == (Object)null)
			{
				return fallback;
			}
			try
			{
				RopeSpool val = default(RopeSpool);
				if (((Component)currentItem).TryGetComponent<RopeSpool>(ref val))
				{
					return Mathf.Ceil(val.RopeFuel);
				}
				if (currentItem.HasData((DataEntryKey)10))
				{
					return currentItem.overrideJetpackFuelAmount + Mathf.RoundToInt(currentItem.GetData<FloatItemData>((DataEntryKey)10).Value * currentItem.overrideJetpackFuelPerFuelMult);
				}
				if (currentItem is Backpack)
				{
					return currentItem.overrideJetpackFuelAmount;
				}
				if (currentItem.overrideJetpackFuel)
				{
					if (!currentItem.HasData((DataEntryKey)2))
					{
						return currentItem.overrideJetpackFuelAmount;
					}
					return currentItem.overrideJetpackFuelAmount + currentItem.GetData<OptionableIntItemData>((DataEntryKey)2).Value * currentItem.overrideJetpackFuelPerUse;
				}
			}
			catch
			{
			}
			if (!(fallback > 0f))
			{
				return 50f;
			}
			return fallback;
		}
	}
	[BepInPlugin("youxia173.EasyBackpackHold", "EasyBackpackHold", "1.0.3")]
	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();
			Log.LogInfo((object)"EasyBackpackHold v1.0.3 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.3";
	}
}