Decompiled source of EpicLootVRFix v1.0.0

plugins/EpicLootVRFix.dll

Decompiled 2 months 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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("EpicLootVRFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EpicLootVRFix")]
[assembly: AssemblyTitle("EpicLootVRFix")]
[assembly: AssemblyVersion("1.0.0.0")]
public static class EpicLootVRControls
{
	private static Coroutine _inputCoroutine;

	private static Type _vrControlsType;

	private static PropertyInfo _instanceProperty;

	private static MethodInfo _getButtonDownMethod;

	private static bool _reflectionCached;

	public static void Initialize(Canvas canvas)
	{
		if (_inputCoroutine != null)
		{
			((MonoBehaviour)UIVRPatch.Instance).StopCoroutine(_inputCoroutine);
			_inputCoroutine = null;
		}
		CacheReflection();
		_inputCoroutine = ((MonoBehaviour)UIVRPatch.Instance).StartCoroutine(WaitForVRInput());
		UIVRPatch.LogInfo("EpicLoot VR controls initialized");
	}

	private static void CacheReflection()
	{
		if (_reflectionCached)
		{
			return;
		}
		try
		{
			_vrControlsType = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRControls");
			if (_vrControlsType != null)
			{
				_instanceProperty = AccessTools.Property(_vrControlsType, "instance");
				if (_instanceProperty != null)
				{
					_getButtonDownMethod = AccessTools.Method(_vrControlsType, "GetButtonDown", (Type[])null, (Type[])null);
				}
			}
			_reflectionCached = true;
			UIVRPatch.LogInfo("VR controls reflection cached");
		}
		catch (Exception arg)
		{
			UIVRPatch.LogError($"Error caching reflection: {arg}");
		}
	}

	private static IEnumerator WaitForVRInput()
	{
		while (true)
		{
			yield return null;
			try
			{
				if (IsVRMenuButtonPressed())
				{
					UIVRPatch.LogInfo("VR button pressed - closing EpicLoot UI");
					CloseEpicLootUI();
					break;
				}
			}
			catch (Exception e)
			{
				UIVRPatch.LogError($"Error in VR input check: {e}");
			}
		}
	}

	private static bool IsVRMenuButtonPressed()
	{
		if (!_reflectionCached || _vrControlsType == null || _instanceProperty == null || _getButtonDownMethod == null)
		{
			return false;
		}
		try
		{
			object value = _instanceProperty.GetValue(null);
			if (value != null)
			{
				bool flag = (bool)_getButtonDownMethod.Invoke(value, new object[1] { "JoyMenu" });
				bool flag2 = (bool)_getButtonDownMethod.Invoke(value, new object[1] { "Inventory" });
				return flag || flag2;
			}
		}
		catch (Exception arg)
		{
			UIVRPatch.LogError($"Error checking VR menu button: {arg}");
		}
		return false;
	}

	private static void CloseEpicLootUI()
	{
		try
		{
			Type type = AccessTools.TypeByName("EpicLoot_UnityLib.EnchantingTableUI");
			MethodInfo methodInfo = AccessTools.Method(type, "Hide", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				methodInfo.Invoke(null, null);
				UIVRPatch.LogInfo("Successfully called EpicLoot Hide() method");
			}
			else
			{
				UIVRPatch.LogError("Could not find EpicLoot Hide method");
			}
		}
		catch (Exception arg)
		{
			UIVRPatch.LogError($"Error closing EpicLoot UI: {arg}");
		}
		Cleanup();
	}

	private static void Cleanup()
	{
		if (_inputCoroutine != null)
		{
			((MonoBehaviour)UIVRPatch.Instance).StopCoroutine(_inputCoroutine);
			_inputCoroutine = null;
		}
	}
}
public static class EpicLootVRUI
{
	public static bool IsEpicLootLoaded()
	{
		try
		{
			Type type = AccessTools.TypeByName("EpicLoot_UnityLib.EnchantingTableUI");
			return type != null;
		}
		catch
		{
			return false;
		}
	}

	public static void PatchEpicLootUI(Harmony harmony)
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		try
		{
			Type type = AccessTools.TypeByName("EpicLoot_UnityLib.EnchantingTableUI");
			if (!(type == null))
			{
				MethodInfo methodInfo = AccessTools.Method(type, "Show", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(EpicLootVRUI), "OnUIShown", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					UIVRPatch.LogInfo("Successfully patched EpicLoot EnchantingTableUI for VR");
				}
			}
		}
		catch (Exception arg)
		{
			UIVRPatch.LogError($"Error patching EpicLoot: {arg}");
		}
	}

	public static void OnUIShown()
	{
		UIVRPatch.LogInfo("=== EPICLOOT UI SHOWN ===");
		((MonoBehaviour)UIVRPatch.Instance).StartCoroutine(ForceVHVRProcessing());
	}

	private static IEnumerator ForceVHVRProcessing()
	{
		yield return null;
		try
		{
			Canvas epicLootCanvas = null;
			Canvas[] allCanvases = Resources.FindObjectsOfTypeAll<Canvas>();
			Canvas[] array = allCanvases;
			foreach (Canvas canvas in array)
			{
				if ((Object)(object)canvas != (Object)null && ((Object)canvas).name.Contains("EnchantingUI"))
				{
					epicLootCanvas = canvas;
					break;
				}
			}
			if ((Object)(object)epicLootCanvas == (Object)null)
			{
				UIVRPatch.LogWarning("EpicLoot canvas not found");
				yield break;
			}
			UIVRPatch.LogInfo("Found EpicLoot canvas: '" + ((Object)epicLootCanvas).name + "'");
			object vrguiInstance = GetVHVRVRGUI();
			if (vrguiInstance == null)
			{
				UIVRPatch.LogError("Could not get VHVR VRGUI instance");
				yield break;
			}
			Type vrguiType = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI");
			AddCanvasToVHVR(epicLootCanvas, vrguiInstance, vrguiType);
			ManuallyProcessCanvasForVHVR(epicLootCanvas, vrguiInstance, vrguiType);
			UIVRPatch.LogInfo("EpicLoot UI should now be visible in VR");
			EpicLootVRControls.Initialize(epicLootCanvas);
		}
		catch (Exception e)
		{
			UIVRPatch.LogError($"Error: {e}");
		}
	}

	private static object GetVHVRVRGUI()
	{
		try
		{
			Type type = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI");
			if (type == null)
			{
				return null;
			}
			Object[] array = Resources.FindObjectsOfTypeAll(type);
			return (array.Length != 0) ? array[0] : null;
		}
		catch (Exception arg)
		{
			UIVRPatch.LogError($"Error getting VHVR VRGUI: {arg}");
			return null;
		}
	}

	private static void AddCanvasToVHVR(Canvas epicLootCanvas, object vrguiInstance, Type vrguiType)
	{
		try
		{
			FieldInfo fieldInfo = AccessTools.Field(vrguiType, "_guiCanvases");
			if (!(fieldInfo == null) && fieldInfo.GetValue(vrguiInstance) is IList list && !list.Contains(epicLootCanvas))
			{
				list.Add(epicLootCanvas);
				UIVRPatch.LogInfo("Added EpicLoot canvas to VHVR _guiCanvases list");
			}
		}
		catch (Exception arg)
		{
			UIVRPatch.LogWarning($"Error adding to VHVR list: {arg}");
		}
	}

	private static void ManuallyProcessCanvasForVHVR(Canvas epicLootCanvas, object vrguiInstance, Type vrguiType)
	{
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			FieldInfo fieldInfo = AccessTools.Field(vrguiType, "_guiCamera");
			if (fieldInfo == null)
			{
				UIVRPatch.LogError("VHVR _guiCamera field not found");
				return;
			}
			object? value = fieldInfo.GetValue(vrguiInstance);
			Camera val = (Camera)((value is Camera) ? value : null);
			if ((Object)(object)val == (Object)null)
			{
				UIVRPatch.LogError("VHVR _guiCamera is null");
				return;
			}
			UIVRPatch.LogInfo("Using VHVR GUI camera: " + ((Object)val).name);
			epicLootCanvas.worldCamera = val;
			epicLootCanvas.renderMode = (RenderMode)2;
			FieldInfo fieldInfo2 = AccessTools.Field(vrguiType, "GUI_DIMENSIONS");
			if (fieldInfo2 != null)
			{
				Vector2 val2 = (Vector2)fieldInfo2.GetValue(null);
				RectTransform component = ((Component)epicLootCanvas).GetComponent<RectTransform>();
				if ((Object)(object)component != (Object)null)
				{
					component.SetSizeWithCurrentAnchors((Axis)0, val2.x);
					component.SetSizeWithCurrentAnchors((Axis)1, val2.y);
					UIVRPatch.LogInfo($"Set canvas size to: {val2}");
				}
			}
			UIVRPatch.LogInfo("Manually processed EpicLoot canvas for VHVR");
		}
		catch (Exception arg)
		{
			UIVRPatch.LogError($"Error manually processing canvas: {arg}");
		}
	}
}
[BepInPlugin("EpicLootVRFix", "Epic Loot VR Fix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class UIVRPatch : BaseUnityPlugin
{
	public static ConfigEntry<bool> EnableVRFix;

	public static ConfigEntry<bool> DebugMode;

	public static UIVRPatch Instance;

	private Harmony _harmony;

	public ManualLogSource Logger => ((BaseUnityPlugin)this).Logger;

	private void Awake()
	{
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Expected O, but got Unknown
		Instance = this;
		EnableVRFix = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable VR Fix", true, "Enable VR UI fixes");
		DebugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Debug Mode", false, "Enable debug logging (for testing only)");
		if (EnableVRFix.Value)
		{
			_harmony = new Harmony("EpicLootVRFix");
			((MonoBehaviour)this).StartCoroutine(WaitForEpicLootAndPatch());
		}
	}

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

	private IEnumerator WaitForEpicLootAndPatch()
	{
		while (!EpicLootVRUI.IsEpicLootLoaded())
		{
			yield return (object)new WaitForSeconds(2f);
		}
		LogInfo("EpicLoot detected! Applying VR fixes...");
		EpicLootVRUI.PatchEpicLootUI(_harmony);
	}

	public static void LogInfo(string message)
	{
		if (DebugMode.Value)
		{
			Instance.Logger.LogInfo((object)message);
		}
	}

	public static void LogWarning(string message)
	{
		if (DebugMode.Value)
		{
			Instance.Logger.LogWarning((object)message);
		}
	}

	public static void LogError(string message)
	{
		Instance.Logger.LogError((object)message);
	}
}