Decompiled source of EasyBackpack Fix v1.1.6

EasyBackpack.dll

Decompiled 4 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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("EasyBackpack")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1d921d7ec8e70bbd1dca2304f47251b468981950")]
[assembly: AssemblyProduct("EasyBackpack")]
[assembly: AssemblyTitle("EasyBackpack")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 EasyBackpack
{
	[BepInPlugin("nickklmao.easybackpack", "EasyBackpack", "1.1.6")]
	internal sealed class Entry : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(BackpackWheel), "Update")]
		private static class BackpackWheelUpdatePatch
		{
			private static bool s_forced;

			private static bool s_savedInteract;

			[HarmonyPrefix]
			private static void Prefix()
			{
				s_forced = false;
				if (isAccessingBackpack && !((Object)(object)Character.localCharacter == (Object)null))
				{
					s_savedInteract = Character.localCharacter.input.interactIsPressed;
					Character.localCharacter.input.interactIsPressed = true;
					s_forced = true;
				}
			}

			[HarmonyPostfix]
			private static void Postfix()
			{
				if (s_forced)
				{
					s_forced = false;
					if ((Object)(object)Character.localCharacter != (Object)null)
					{
						Character.localCharacter.input.interactIsPressed = s_savedInteract;
					}
				}
			}
		}

		[HarmonyPatch(typeof(CharacterBackpackHandler), "AddFuelToJetpack")]
		private static class SelfRefuelRollbackFix
		{
			private static Coroutine s_fixRoutine;

			private static float s_beforeFuel = float.NaN;

			private static bool s_baseSet;

			private static float s_baseFuel;

			private static float s_totalAdded;

			[HarmonyPrefix]
			private static void Prefix(CharacterBackpackHandler __instance, Character interactor, ref float addedFuel)
			{
				s_beforeFuel = float.NaN;
				if ((Object)(object)interactor == (Object)null || (Object)(object)__instance == (Object)null || interactor.refs == null || (Object)(object)interactor.refs.backpackHandler != (Object)(object)__instance)
				{
					return;
				}
				int num = 25;
				Item currentItem = interactor.data.currentItem;
				if ((Object)(object)currentItem != (Object)null)
				{
					RopeSpool val = default(RopeSpool);
					if (((Component)currentItem).TryGetComponent<RopeSpool>(ref val))
					{
						num = Mathf.CeilToInt(val.RopeFuel);
					}
					else if (currentItem.HasData((DataEntryKey)10))
					{
						num = currentItem.overrideJetpackFuelAmount + Mathf.RoundToInt(currentItem.GetData<FloatItemData>((DataEntryKey)10).Value * currentItem.overrideJetpackFuelPerFuelMult);
					}
					else if (currentItem is Backpack)
					{
						num = currentItem.overrideJetpackFuelAmount;
					}
					else if (currentItem.overrideJetpackFuel)
					{
						num = (currentItem.HasData((DataEntryKey)2) ? (currentItem.overrideJetpackFuelAmount + currentItem.GetData<OptionableIntItemData>((DataEntryKey)2).Value * currentItem.overrideJetpackFuelPerUse) : currentItem.overrideJetpackFuelAmount);
					}
				}
				addedFuel = num;
				Player player = interactor.player;
				FloatItemData val2 = default(FloatItemData);
				if (!((Object)(object)player == (Object)null) && player.backpackSlot != null && ((ItemSlot)player.backpackSlot).data != null && ((ItemSlot)player.backpackSlot).data.TryGetDataEntry<FloatItemData>((DataEntryKey)10, ref val2))
				{
					s_beforeFuel = val2.Value;
				}
			}

			[HarmonyPostfix]
			private static void Postfix(CharacterBackpackHandler __instance, Character interactor, float addedFuel)
			{
				if (float.IsNaN(s_beforeFuel) || (Object)(object)interactor == (Object)null || (Object)(object)__instance == (Object)null || interactor.refs == null || (Object)(object)interactor.refs.backpackHandler != (Object)(object)__instance)
				{
					return;
				}
				Player player = interactor.player;
				FloatItemData val = default(FloatItemData);
				if ((Object)(object)player == (Object)null || player.backpackSlot == null || ((ItemSlot)player.backpackSlot).data == null || !((ItemSlot)player.backpackSlot).data.TryGetDataEntry<FloatItemData>((DataEntryKey)10, ref val))
				{
					return;
				}
				float value = val.Value;
				if (!(value <= s_beforeFuel + 0.01f))
				{
					if (s_fixRoutine == null)
					{
						s_baseSet = false;
						s_totalAdded = 0f;
					}
					if (!s_baseSet)
					{
						s_baseSet = true;
						s_baseFuel = s_beforeFuel;
					}
					s_totalAdded += addedFuel;
					float targetFuel = Mathf.Min(s_baseFuel + s_totalAdded, 100f);
					if (s_fixRoutine != null)
					{
						((MonoBehaviour)__instance).StopCoroutine(s_fixRoutine);
					}
					s_fixRoutine = ((MonoBehaviour)__instance).StartCoroutine(FixLater(__instance, ((Component)__instance).GetComponent<Character>(), targetFuel));
				}
			}

			private static IEnumerator FixLater(CharacterBackpackHandler handler, Character targetCharacter, float targetFuel)
			{
				yield return (object)new WaitForSeconds(1.5f);
				s_fixRoutine = null;
				if ((Object)(object)handler == (Object)null || (Object)(object)targetCharacter == (Object)null)
				{
					yield break;
				}
				Player player = targetCharacter.player;
				if ((Object)(object)player == (Object)null || player.backpackSlot == null || ((ItemSlot)player.backpackSlot).data == null)
				{
					yield break;
				}
				ItemInstanceData data = ((ItemSlot)player.backpackSlot).data;
				FloatItemData val = default(FloatItemData);
				if (data.TryGetDataEntry<FloatItemData>((DataEntryKey)10, ref val) && val.Value < targetFuel - 0.01f)
				{
					val.Value = targetFuel;
					FloatItemData val2 = default(FloatItemData);
					if (data.TryGetDataEntry<FloatItemData>((DataEntryKey)11, ref val2))
					{
						val2.Value = targetFuel / 100f;
					}
				}
			}
		}

		private static readonly ManualLogSource logger = Logger.CreateLogSource("EasyBackpack");

		private static ConfigEntry<KeyCode> backpackKey;

		private static ConfigEntry<KeyCode> alternativeBackpackKey;

		private static bool isAccessingBackpack;

		private static bool isAccessingCarriedPlayersBackpack;

		private static float s_lastSlotCountWarn = float.MinValue;

		private readonly Harmony m_harmony = new Harmony("nickklmao.easybackpack");

		private void Awake()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			logger.LogInfo((object)"Creating config entries");
			backpackKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "Backpack Key", (KeyCode)98, new ConfigDescription("The key you want to use to access the backpack while it's on your back", (AcceptableValueBase)null, Array.Empty<object>()));
			alternativeBackpackKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "Alternative Backpack Key", (KeyCode)0, new ConfigDescription("An optional alternative key for accessing the backpack while it's on your back", (AcceptableValueBase)null, Array.Empty<object>()));
			logger.LogInfo((object)"Hooking BackpackWheel.Update");
			m_harmony.PatchAll(typeof(BackpackWheelUpdatePatch));
			m_harmony.PatchAll(typeof(SelfRefuelRollbackFix));
		}

		private void Update()
		{
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Invalid comparison between Unknown and I4
			//IL_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Invalid comparison between Unknown and I4
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			Character localCharacter = Character.localCharacter;
			if (!Application.isFocused || (Object)(object)localCharacter == (Object)null)
			{
				isAccessingCarriedPlayersBackpack = (isAccessingBackpack = false);
				return;
			}
			if (isAccessingCarriedPlayersBackpack)
			{
				if ((Object)(object)localCharacter.data.carriedPlayer == (Object)null)
				{
					isAccessingCarriedPlayersBackpack = (isAccessingBackpack = false);
					return;
				}
			}
			else if (localCharacter.data.passedOut || localCharacter.data.dead || localCharacter.data.currentItem is Backpack)
			{
				isAccessingCarriedPlayersBackpack = (isAccessingBackpack = false);
				return;
			}
			if (Input.GetKeyUp(backpackKey.Value) || Input.GetKeyUp(alternativeBackpackKey.Value))
			{
				isAccessingCarriedPlayersBackpack = (isAccessingBackpack = false);
			}
			else
			{
				if (!Input.GetKeyDown(backpackKey.Value) && !Input.GetKeyDown(alternativeBackpackKey.Value))
				{
					return;
				}
				if (!((ItemSlot)localCharacter.player.backpackSlot).IsEmpty())
				{
					BackpackType backpackType = localCharacter.player.backpackSlot.backpackType;
					if ((int)backpackType == 4)
					{
						return;
					}
					int num = 4;
					try
					{
						CharacterBackpackHandler backpackHandler = localCharacter.refs.backpackHandler;
						BackpackOnBackVisuals val = ((backpackHandler != null) ? backpackHandler.activeBackpackVisuals : null);
						if ((Object)(object)val != (Object)null)
						{
							num = val.slotCount;
						}
						BackpackReference fromEquippedBackpack = BackpackReference.GetFromEquippedBackpack(localCharacter);
						GUIManager.instance.OpenBackpackWheel(fromEquippedBackpack, num, backpackType);
						isAccessingBackpack = true;
						return;
					}
					catch
					{
						LogSlotCountFallback();
						return;
					}
				}
				Character carriedPlayer = localCharacter.data.carriedPlayer;
				if ((Object)(object)carriedPlayer == (Object)null)
				{
					return;
				}
				Player player = carriedPlayer.player;
				if (!((Object)(object)player != (Object)null))
				{
					return;
				}
				BackpackSlot backpackSlot = player.backpackSlot;
				if (backpackSlot == null || ((ItemSlot)backpackSlot).IsEmpty())
				{
					return;
				}
				BackpackType backpackType2 = player.backpackSlot.backpackType;
				if ((int)backpackType2 == 4)
				{
					return;
				}
				int num2 = 4;
				try
				{
					CharacterBackpackHandler backpackHandler2 = carriedPlayer.refs.backpackHandler;
					BackpackOnBackVisuals val2 = ((backpackHandler2 != null) ? backpackHandler2.activeBackpackVisuals : null);
					if ((Object)(object)val2 != (Object)null)
					{
						num2 = val2.slotCount;
					}
					BackpackReference fromEquippedBackpack2 = BackpackReference.GetFromEquippedBackpack(carriedPlayer);
					GUIManager.instance.OpenBackpackWheel(fromEquippedBackpack2, num2, backpackType2);
					isAccessingCarriedPlayersBackpack = (isAccessingBackpack = true);
				}
				catch
				{
					LogSlotCountFallback();
				}
			}
		}

		private static void LogSlotCountFallback()
		{
			if (Time.unscaledTime - s_lastSlotCountWarn > 5f)
			{
				s_lastSlotCountWarn = Time.unscaledTime;
				logger.LogWarning((object)"Failed to read backpack visuals slotCount / GetFromEquippedBackpack (refs or ragdoll not ready); backpack wheel was not opened.");
			}
		}
	}
}