Decompiled source of EpicLootVRFix v1.0.1

EpicLootVRFix.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
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
{
	[CompilerGenerated]
	private sealed class <WaitForVRInput>d__7 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		private Exception <e>5__1;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <WaitForVRInput>d__7(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<e>5__1 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				break;
			case 1:
				<>1__state = -1;
				try
				{
					if (IsVRMenuButtonPressed())
					{
						UIVRPatch.LogInfo("VR button pressed - closing EpicLoot UI");
						CloseEpicLootUI();
						return false;
					}
				}
				catch (Exception ex)
				{
					<e>5__1 = ex;
					UIVRPatch.LogError($"Error in VR input check: {<e>5__1}");
				}
				break;
			}
			<>2__current = null;
			<>1__state = 1;
			return true;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	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}");
		}
	}

	[IteratorStateMachine(typeof(<WaitForVRInput>d__7))]
	private static IEnumerator WaitForVRInput()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <WaitForVRInput>d__7(0);
	}

	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
{
	[CompilerGenerated]
	private sealed class <ForceVHVRProcessing>d__3 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		private Canvas <epicLootCanvas>5__1;

		private Canvas[] <allCanvases>5__2;

		private object <vrguiInstance>5__3;

		private Type <vrguiType>5__4;

		private Canvas[] <>s__5;

		private int <>s__6;

		private Canvas <canvas>5__7;

		private Exception <e>5__8;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <ForceVHVRProcessing>d__3(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<epicLootCanvas>5__1 = null;
			<allCanvases>5__2 = null;
			<vrguiInstance>5__3 = null;
			<vrguiType>5__4 = null;
			<>s__5 = null;
			<canvas>5__7 = null;
			<e>5__8 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<>2__current = null;
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				try
				{
					<epicLootCanvas>5__1 = null;
					<allCanvases>5__2 = Resources.FindObjectsOfTypeAll<Canvas>();
					<>s__5 = <allCanvases>5__2;
					for (<>s__6 = 0; <>s__6 < <>s__5.Length; <>s__6++)
					{
						<canvas>5__7 = <>s__5[<>s__6];
						if ((Object)(object)<canvas>5__7 != (Object)null && ((Object)<canvas>5__7).name.Contains("EnchantingUI"))
						{
							<epicLootCanvas>5__1 = <canvas>5__7;
							break;
						}
						<canvas>5__7 = null;
					}
					<>s__5 = null;
					if ((Object)(object)<epicLootCanvas>5__1 == (Object)null)
					{
						UIVRPatch.LogWarning("EpicLoot canvas not found");
						return false;
					}
					UIVRPatch.LogInfo("Found EpicLoot canvas: '" + ((Object)<epicLootCanvas>5__1).name + "'");
					<vrguiInstance>5__3 = GetVHVRVRGUI();
					if (<vrguiInstance>5__3 == null)
					{
						UIVRPatch.LogError("Could not get VHVR VRGUI instance");
						return false;
					}
					<vrguiType>5__4 = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI");
					AddCanvasToVHVR(<epicLootCanvas>5__1, <vrguiInstance>5__3, <vrguiType>5__4);
					ManuallyProcessCanvasForVHVR(<epicLootCanvas>5__1, <vrguiInstance>5__3, <vrguiType>5__4);
					UIVRPatch.LogInfo("EpicLoot UI should now be visible in VR");
					EpicLootVRControls.Initialize(<epicLootCanvas>5__1);
					<epicLootCanvas>5__1 = null;
					<allCanvases>5__2 = null;
					<vrguiInstance>5__3 = null;
					<vrguiType>5__4 = null;
				}
				catch (Exception ex)
				{
					<e>5__8 = ex;
					UIVRPatch.LogError($"Error: {<e>5__8}");
				}
				return false;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	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());
	}

	[IteratorStateMachine(typeof(<ForceVHVRProcessing>d__3))]
	private static IEnumerator ForceVHVRProcessing()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <ForceVHVRProcessing>d__3(0);
	}

	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.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class UIVRPatch : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <WaitForEpicLootAndPatch>d__8 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public UIVRPatch <>4__this;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <WaitForEpicLootAndPatch>d__8(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if (!EpicLootVRUI.IsEpicLootLoaded())
			{
				<>2__current = (object)new WaitForSeconds(2f);
				<>1__state = 1;
				return true;
			}
			LogInfo("EpicLoot detected! Applying VR fixes...");
			EpicLootVRUI.PatchEpicLootUI(<>4__this._harmony);
			return false;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	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");
			_harmony.PatchAll();
			((MonoBehaviour)this).StartCoroutine(WaitForEpicLootAndPatch());
		}
	}

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

	[IteratorStateMachine(typeof(<WaitForEpicLootAndPatch>d__8))]
	private IEnumerator WaitForEpicLootAndPatch()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <WaitForEpicLootAndPatch>d__8(0)
		{
			<>4__this = this
		};
	}

	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);
	}
}
namespace EpicLootVRFix;

[HarmonyPatch]
public static class EpicLootVRTooltipFix
{
	private static MethodBase TargetMethod()
	{
		return AccessTools.Method("EpicLoot.PatchOnHoverFix:AddScrollbar", (Type[])null, (Type[])null);
	}

	private static void Postfix(GameObject tooltipObject)
	{
		try
		{
			if (!UIVRPatch.EnableVRFix.Value || (Object)(object)tooltipObject == (Object)null)
			{
				return;
			}
			if (UIVRPatch.DebugMode.Value)
			{
				UIVRPatch.LogInfo("AddScrollbar patch triggered");
			}
			Transform val = tooltipObject.transform.Find("Canvas");
			if ((Object)(object)val == (Object)null)
			{
				if (UIVRPatch.DebugMode.Value)
				{
					UIVRPatch.LogInfo("Canvas container not found");
				}
				return;
			}
			if (UIVRPatch.DebugMode.Value)
			{
				UIVRPatch.LogInfo("Canvas container found");
			}
			Transform val2 = val.Find("Scroll View");
			if ((Object)(object)val2 == (Object)null)
			{
				if (UIVRPatch.DebugMode.Value)
				{
					UIVRPatch.LogInfo("Scroll View not found under Canvas");
				}
				return;
			}
			if (UIVRPatch.DebugMode.Value)
			{
				UIVRPatch.LogInfo("Scroll View found");
			}
			val2.SetParent(tooltipObject.transform, true);
			if (UIVRPatch.DebugMode.Value)
			{
				UIVRPatch.LogInfo("Scroll View moved to tooltip root");
			}
			int layer = LayerMask.NameToLayer("UI");
			SetLayerRecursive(((Component)val2).gameObject, layer);
			if (UIVRPatch.DebugMode.Value)
			{
				UIVRPatch.LogInfo("Scroll View layer set to UI");
			}
			Object.Destroy((Object)(object)((Component)val).gameObject);
			if (UIVRPatch.DebugMode.Value)
			{
				UIVRPatch.LogInfo("Canvas container destroyed");
			}
		}
		catch (Exception ex)
		{
			UIVRPatch.LogError("EpicLootVRTooltipFix error: " + ex);
		}
	}

	private static void SetLayerRecursive(GameObject obj, int layer)
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		obj.layer = layer;
		foreach (Transform item in obj.transform)
		{
			Transform val = item;
			SetLayerRecursive(((Component)val).gameObject, layer);
		}
	}
}