Decompiled source of BackpacksVRFix v1.0.1

BackpacksVRFix.dll

Decompiled 8 hours ago
using System;
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(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BackpacksVRFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BackpacksVRFix")]
[assembly: AssemblyTitle("BackpacksVRFix")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BackpacksVRFix
{
	[BepInPlugin("Avo.BackpacksVRFix", "Backpacks VR Fix", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class BackpacksVRFixPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "Avo.BackpacksVRFix";

		public const string PluginName = "Backpacks VR Fix";

		public const string PluginVersion = "1.0.1";

		internal static BackpacksVRFixPlugin Instance;

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> EnableFix;

		internal static ConfigEntry<bool> DebugLogging;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			EnableFix = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable Fix", true, "Keep ValheimVR's inventory wrist attachment active when Backpacks opens its fake backpack container.");
			DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Debug Logging", false, "Enable extra diagnostic logging.");
			if (EnableFix.Value)
			{
				_harmony = new Harmony("Avo.BackpacksVRFix");
				_harmony.PatchAll();
				Log.LogInfo((object)"BackpacksVRFix loaded");
			}
		}

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

		internal static void Debug(string message)
		{
			ConfigEntry<bool> debugLogging = DebugLogging;
			if (debugLogging != null && debugLogging.Value)
			{
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogInfo((object)message);
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class KeepBackpackInventoryOnWristPatch
	{
		private static Type _customContainerType;

		private static FieldInfo _openContainerField;

		private static Type _vhvrConfigType;

		private static MethodInfo _attachInventoryToHandMethod;

		private static MethodInfo _useVrControlsMethod;

		private static FieldInfo _playerInCraftingStationField;

		private static bool _cached;

		private static MethodBase TargetMethod()
		{
			return AccessTools.Method("ValheimVRMod.VRCore.UI.VRGUI:shouldInstantlyDetachPanelFromHand", (Type[])null, (Type[])null);
		}

		private static void Postfix(ref bool __result)
		{
			try
			{
				if (BackpacksVRFixPlugin.EnableFix.Value && __result && IsBackpacksFakeContainerOpen() && InventoryGui.IsVisible() && !((Object)(object)InventoryGui.instance == (Object)null) && CallBool(_attachInventoryToHandMethod, defaultValue: true) && CallBool(_useVrControlsMethod, defaultValue: true))
				{
					Player localPlayer = Player.m_localPlayer;
					if (!((Object)(object)localPlayer == (Object)null) && !IsPlayerInCraftingStation(localPlayer) && !((Character)localPlayer).IsAttachedToShip())
					{
						__result = false;
						BackpacksVRFixPlugin.Debug("Prevented VHVR wrist inventory detach for Backpacks fake container");
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log = BackpacksVRFixPlugin.Log;
				if (log != null)
				{
					log.LogError((object)("BackpacksVRFix patch error: " + ex));
				}
			}
		}

		private static bool IsBackpacksFakeContainerOpen()
		{
			CacheReflection();
			return _openContainerField?.GetValue(null) != null;
		}

		private static void CacheReflection()
		{
			if (!_cached)
			{
				_cached = true;
				_customContainerType = AccessTools.TypeByName("Backpacks.CustomContainer");
				_openContainerField = AccessTools.Field(_customContainerType, "OpenContainer");
				_vhvrConfigType = AccessTools.TypeByName("ValheimVRMod.Utilities.VHVRConfig");
				_attachInventoryToHandMethod = AccessTools.Method(_vhvrConfigType, "AttachInventoryToHand", (Type[])null, (Type[])null);
				_useVrControlsMethod = AccessTools.Method(_vhvrConfigType, "UseVrControls", (Type[])null, (Type[])null);
				_playerInCraftingStationField = AccessTools.Field(typeof(Player), "m_inCraftingStation");
				BackpacksVRFixPlugin.Debug("Reflection cache: CustomContainer=" + (_customContainerType != null) + ", OpenContainer=" + (_openContainerField != null) + ", Player.m_inCraftingStation=" + (_playerInCraftingStationField != null) + ", VHVRConfig=" + (_vhvrConfigType != null));
			}
		}

		private static bool IsPlayerInCraftingStation(Player player)
		{
			CacheReflection();
			try
			{
				object obj = _playerInCraftingStationField?.GetValue(player);
				bool flag = default(bool);
				int num;
				if (obj is bool)
				{
					flag = (bool)obj;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
			}
			catch
			{
				return false;
			}
		}

		private static bool CallBool(MethodInfo method, bool defaultValue)
		{
			if (method == null)
			{
				return defaultValue;
			}
			object obj = method.Invoke(null, Array.Empty<object>());
			if (obj is bool)
			{
				return (bool)obj;
			}
			return defaultValue;
		}
	}
}