Decompiled source of PeakQuickBackpack v1.0.1

PeakQuickBackpack.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("PeakQuickBackpack")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+467b1027a4fbc0ddb98a643aa1f47e2de9ebdd57")]
[assembly: AssemblyProduct("PeakQuickBackpack")]
[assembly: AssemblyTitle("PeakQuickBackpack")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace PeakQuickBackpack
{
	[BepInPlugin("com.mountainfarmer.peakquickbackpack", "Peak Quick Backpack", "1.0.0")]
	[BepInProcess("PEAK.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGuid = "com.mountainfarmer.peakquickbackpack";

		public const string ModName = "Peak Quick Backpack";

		public const string ModVersion = "1.0.0";

		public static KeyCode backpackKey = (KeyCode)105;

		private Harmony harmony;

		private bool wasHoldingKey;

		private MethodInfo getFromEquippedBackpackMethod;

		private MethodInfo openBackpackWheelMethod;

		private FieldInfo localPlayerField;

		private PropertyInfo characterProperty;

		private FieldInfo backpackSlotField;

		private FieldInfo hasBackpackField;

		private void Awake()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Peak Quick Backpack v1.0.0 loaded!");
			harmony = new Harmony("com.mountainfarmer.peakquickbackpack");
			harmony.PatchAll();
			CacheReflection();
		}

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

		private void CacheReflection()
		{
			try
			{
				Type typeFromHandle = typeof(Player);
				localPlayerField = typeFromHandle.GetField("localPlayer", BindingFlags.Static | BindingFlags.Public);
				characterProperty = typeFromHandle.GetProperty("character", BindingFlags.Instance | BindingFlags.Public);
				backpackSlotField = typeFromHandle.GetField("backpackSlot", BindingFlags.Instance | BindingFlags.Public);
				Type type = Type.GetType("BackpackSlot, Assembly-CSharp");
				if (type != null)
				{
					hasBackpackField = type.GetField("hasBackpack", BindingFlags.Instance | BindingFlags.Public);
				}
				Type type2 = Type.GetType("BackpackReference, Assembly-CSharp");
				if (type2 != null)
				{
					getFromEquippedBackpackMethod = type2.GetMethod("GetFromEquippedBackpack", BindingFlags.Static | BindingFlags.Public);
				}
				Type type3 = Type.GetType("GUIManager, Assembly-CSharp");
				if (type3 != null)
				{
					openBackpackWheelMethod = type3.GetMethod("OpenBackpackWheel", BindingFlags.Instance | BindingFlags.Public);
				}
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Reflection cached successfully");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to cache reflection: " + ex.Message));
			}
		}

		private void Update()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			bool key = Input.GetKey(backpackKey);
			if (key && !wasHoldingKey && HasBackpack())
			{
				OpenBackpack();
			}
			wasHoldingKey = key;
		}

		private bool HasBackpack()
		{
			try
			{
				if (localPlayerField == null)
				{
					return false;
				}
				object value = localPlayerField.GetValue(null);
				if (value == null)
				{
					return false;
				}
				if (backpackSlotField == null)
				{
					return false;
				}
				object value2 = backpackSlotField.GetValue(value);
				if (value2 == null)
				{
					return false;
				}
				if (hasBackpackField == null)
				{
					return false;
				}
				return (bool)hasBackpackField.GetValue(value2);
			}
			catch
			{
				return false;
			}
		}

		private void OpenBackpack()
		{
			try
			{
				if (localPlayerField == null)
				{
					return;
				}
				object value = localPlayerField.GetValue(null);
				if (value == null || characterProperty == null)
				{
					return;
				}
				object value2 = characterProperty.GetValue(value);
				if (value2 == null || getFromEquippedBackpackMethod == null)
				{
					return;
				}
				object obj = getFromEquippedBackpackMethod.Invoke(null, new object[1] { value2 });
				Type type = Type.GetType("GUIManager, Assembly-CSharp");
				if (type == null)
				{
					return;
				}
				FieldInfo field = type.GetField("instance", BindingFlags.Static | BindingFlags.Public);
				if (!(field == null))
				{
					object value3 = field.GetValue(null);
					if (value3 != null && !(openBackpackWheelMethod == null))
					{
						openBackpackWheelMethod.Invoke(value3, new object[1] { obj });
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to open backpack: " + ex.Message));
			}
		}
	}
	[HarmonyPatch]
	public class BackpackWheelPatch
	{
		private static MethodBase TargetMethod()
		{
			Type type = Type.GetType("BackpackWheel, Assembly-CSharp");
			if (type == null)
			{
				return null;
			}
			return AccessTools.Method(type, "Update", (Type[])null, (Type[])null);
		}

		private static bool Prefix(object __instance)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			if (!Input.GetKey(Plugin.backpackKey))
			{
				return true;
			}
			try
			{
				Type type = Type.GetType("Character, Assembly-CSharp");
				if (type == null)
				{
					return true;
				}
				FieldInfo field = type.GetField("localCharacter", BindingFlags.Static | BindingFlags.Public);
				if (field == null)
				{
					return true;
				}
				object value = field.GetValue(null);
				if (value == null)
				{
					return true;
				}
				FieldInfo field2 = type.GetField("input", BindingFlags.Instance | BindingFlags.Public);
				if (field2 == null)
				{
					return true;
				}
				object value2 = field2.GetValue(value);
				if (value2 == null)
				{
					return true;
				}
				FieldInfo field3 = value2.GetType().GetField("interactIsPressed", BindingFlags.Instance | BindingFlags.Public);
				if (field3 == null)
				{
					return true;
				}
				field3.SetValue(value2, true);
			}
			catch
			{
			}
			return true;
		}
	}
}