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;
using UnityEngine.UI;
using XPortal;
[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("XPortalVRFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("XPortalVRFix")]
[assembly: AssemblyTitle("XPortalVRFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("XPortalVRFix", "XPortal VR Fix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class XPortalVRPatch : BaseUnityPlugin
{
[CompilerGenerated]
private sealed class <WaitForXPortalAndPatch>d__9 : IEnumerator<object>, IDisposable, IEnumerator
{
private int <>1__state;
private object <>2__current;
public XPortalVRPatch <>4__this;
private int <maxAttempts>5__2;
private int <attempts>5__3;
object IEnumerator<object>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <WaitForXPortalAndPatch>d__9(int <>1__state)
{
this.<>1__state = <>1__state;
}
[DebuggerHidden]
void IDisposable.Dispose()
{
<>1__state = -2;
}
private bool MoveNext()
{
//IL_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Expected O, but got Unknown
int num = <>1__state;
XPortalVRPatch xPortalVRPatch = <>4__this;
switch (num)
{
default:
return false;
case 0:
<>1__state = -1;
if (!EnableVRFix.Value)
{
LogInfo("XPortal VR Fix is disabled in configuration");
return false;
}
<maxAttempts>5__2 = 60;
<attempts>5__3 = 0;
break;
case 1:
<>1__state = -1;
<attempts>5__3++;
if (XPortalVRUI.IsXPortalLoaded())
{
xPortalVRPatch._xportalLoaded = true;
xPortalVRPatch.ApplyPatches();
if (DebugMode.Value)
{
LogInfo("XPortal detected, patches applied!");
}
return false;
}
if (<attempts>5__3 % 10 == 0 && DebugMode.Value)
{
LogInfo($"Waiting for XPortal... ({<attempts>5__3}s)");
}
break;
}
if (<attempts>5__3 < <maxAttempts>5__2 && !xPortalVRPatch._xportalLoaded)
{
<>2__current = (object)new WaitForSeconds(1f);
<>1__state = 1;
return true;
}
if (!xPortalVRPatch._xportalLoaded && DebugMode.Value)
{
LogWarning("XPortal not detected after waiting. VR UI fix will not be active.");
}
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 XPortalVRPatch Instance;
private Harmony _harmony;
private bool _xportalLoaded;
public ManualLogSource Logger => ((BaseUnityPlugin)this).Logger;
private void Awake()
{
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: Expected O, but got Unknown
Instance = this;
EnableVRFix = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable VR Fix", true, "Enable VR UI fixes for XPortal");
DebugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Debug Mode", false, "Enable debug logging (for testing only)");
if (EnableVRFix.Value)
{
_harmony = new Harmony("XPortalVRFix");
((MonoBehaviour)this).StartCoroutine(WaitForXPortalAndPatch());
}
}
private void OnDestroy()
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
[IteratorStateMachine(typeof(<WaitForXPortalAndPatch>d__9))]
private IEnumerator WaitForXPortalAndPatch()
{
//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
return new <WaitForXPortalAndPatch>d__9(0)
{
<>4__this = this
};
}
private void ApplyPatches()
{
try
{
XPortalVRUI.PatchXPortalUI(_harmony);
if (DebugMode.Value)
{
LogInfo("XPortal VR UI patches applied successfully");
}
}
catch (Exception arg)
{
LogError($"Error applying patches: {arg}");
}
}
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 XPortal;
public static class XPortalVRControls
{
[CompilerGenerated]
private sealed class <WaitForVRInput>d__7 : IEnumerator<object>, IDisposable, IEnumerator
{
private int <>1__state;
private object <>2__current;
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()
{
<>1__state = -2;
}
private bool MoveNext()
{
int num = <>1__state;
if (num != 0)
{
if (num != 1)
{
return false;
}
<>1__state = -1;
try
{
if (IsVRMenuButtonPressed())
{
XPortalVRPatch.LogInfo("VR button pressed - closing XPortal UI");
CloseXPortalUI();
return false;
}
}
catch (Exception arg)
{
XPortalVRPatch.LogError($"Error in VR input check: {arg}");
}
}
else
{
<>1__state = -1;
}
<>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)XPortalVRPatch.Instance).StopCoroutine(_inputCoroutine);
_inputCoroutine = null;
}
CacheReflection();
_inputCoroutine = ((MonoBehaviour)XPortalVRPatch.Instance).StartCoroutine(WaitForVRInput());
XPortalVRPatch.LogInfo("XPortal 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;
XPortalVRPatch.LogInfo("VR controls reflection cached");
}
catch (Exception arg)
{
XPortalVRPatch.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)
{
XPortalVRPatch.LogError($"Error checking VR menu button: {arg}");
}
return false;
}
private static void CloseXPortalUI()
{
try
{
MethodInfo methodInfo = AccessTools.Method(AccessTools.TypeByName("XPortal.UI.PortalConfigurationPanel"), "Hide", (Type[])null, (Type[])null);
if (methodInfo != null)
{
methodInfo.Invoke(null, null);
XPortalVRPatch.LogInfo("Successfully called XPortal Hide() method");
}
else
{
XPortalVRPatch.LogError("Could not find XPortal Hide method");
}
}
catch (Exception arg)
{
XPortalVRPatch.LogError($"Error closing XPortal UI: {arg}");
}
Cleanup();
}
private static void Cleanup()
{
if (_inputCoroutine != null)
{
((MonoBehaviour)XPortalVRPatch.Instance).StopCoroutine(_inputCoroutine);
_inputCoroutine = null;
}
}
}
public static class XPortalVRUI
{
[CompilerGenerated]
private sealed class <CreateVRUICanvas>d__4 : IEnumerator<object>, IDisposable, IEnumerator
{
private int <>1__state;
private object <>2__current;
object IEnumerator<object>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <CreateVRUICanvas>d__4(int <>1__state)
{
this.<>1__state = <>1__state;
}
[DebuggerHidden]
void IDisposable.Dispose()
{
<>1__state = -2;
}
private bool MoveNext()
{
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
//IL_0049: Expected O, but got Unknown
//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
//IL_0100: Unknown result type (might be due to invalid IL or missing references)
switch (<>1__state)
{
default:
return false;
case 0:
<>1__state = -1;
<>2__current = null;
<>1__state = 1;
return true;
case 1:
<>1__state = -1;
<>2__current = (object)new WaitForSeconds(0.1f);
<>1__state = 2;
return true;
case 2:
<>1__state = -1;
try
{
GameObject val = GameObject.Find("XPortal_MainPanel");
if ((Object)(object)val == (Object)null)
{
XPortalVRPatch.LogError("XPortal_MainPanel not found");
return false;
}
XPortalVRPatch.LogInfo("Found XPortal_MainPanel: '" + ((Object)val).name + "'");
if ((Object)(object)xportalVrCanvas == (Object)null)
{
CreateEpicLootStyleCanvas();
}
object vHVRVRGUI = GetVHVRVRGUI();
if (vHVRVRGUI != null)
{
SetupCanvasForVR(xportalVrCanvas, vHVRVRGUI);
}
val.transform.SetParent(((Component)xportalVrCanvas).transform, false);
RectTransform component = val.GetComponent<RectTransform>();
if ((Object)(object)component != (Object)null)
{
component.anchoredPosition = Vector2.zero;
((Transform)component).localPosition = Vector3.zero;
((Transform)component).localScale = Vector3.one;
XPortalVRPatch.LogInfo("Centered XPortal panel in VR canvas");
}
if (vHVRVRGUI != null)
{
AddCanvasToVHVR(xportalVrCanvas, vHVRVRGUI);
XPortalVRPatch.LogInfo("XPortal VR canvas added to VHVR - should be centered like EpicLoot");
}
}
catch (Exception arg)
{
XPortalVRPatch.LogError($"Error creating VR UI: {arg}");
}
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();
}
}
private static Canvas xportalVrCanvas;
public static bool IsXPortalLoaded()
{
try
{
return AccessTools.TypeByName("XPortal.UI.PortalConfigurationPanel") != null;
}
catch
{
return false;
}
}
public static void PatchXPortalUI(Harmony harmony)
{
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
//IL_004d: Expected O, but got Unknown
try
{
Type type = AccessTools.TypeByName("XPortal.UI.PortalConfigurationPanel");
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(XPortalVRUI), "OnUIShown", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
XPortalVRPatch.LogInfo("Successfully patched XPortal Show method for VR");
}
}
}
catch (Exception arg)
{
XPortalVRPatch.LogError($"Error patching XPortal: {arg}");
}
}
public static void OnUIShown()
{
XPortalVRPatch.LogInfo("=== XPORTAL UI SHOWN ===");
((MonoBehaviour)XPortalVRPatch.Instance).StartCoroutine(CreateVRUICanvas());
}
[IteratorStateMachine(typeof(<CreateVRUICanvas>d__4))]
private static IEnumerator CreateVRUICanvas()
{
//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
return new <CreateVRUICanvas>d__4(0);
}
private static void CreateEpicLootStyleCanvas()
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
//IL_0063: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_0085: Unknown result type (might be due to invalid IL or missing references)
//IL_009a: Unknown result type (might be due to invalid IL or missing references)
//IL_00af: Unknown result type (might be due to invalid IL or missing references)
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
GameObject val = new GameObject("XPortal_VRCanvas");
xportalVrCanvas = val.AddComponent<Canvas>();
xportalVrCanvas.renderMode = (RenderMode)2;
((Component)xportalVrCanvas).gameObject.layer = 5;
CanvasScaler obj = val.AddComponent<CanvasScaler>();
obj.uiScaleMode = (ScaleMode)1;
obj.referenceResolution = new Vector2(1920f, 1080f);
obj.screenMatchMode = (ScreenMatchMode)0;
obj.matchWidthOrHeight = 0.5f;
val.AddComponent<GraphicRaycaster>();
RectTransform component = val.GetComponent<RectTransform>();
component.sizeDelta = new Vector2(1920f, 1080f);
component.anchoredPosition = Vector2.zero;
component.anchorMin = new Vector2(0f, 0f);
component.anchorMax = new Vector2(0f, 0f);
component.pivot = new Vector2(0.5f, 0.5f);
XPortalVRPatch.LogInfo("Created EpicLoot-style VR canvas for XPortal");
}
private static void SetupCanvasForVR(Canvas canvas, object vrguiInstance)
{
//IL_0068: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_0092: Unknown result type (might be due to invalid IL or missing references)
//IL_0096: Unknown result type (might be due to invalid IL or missing references)
//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
try
{
Type type = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI");
FieldInfo fieldInfo = AccessTools.Field(type, "_guiCamera");
if (fieldInfo != null)
{
object? value = fieldInfo.GetValue(vrguiInstance);
Camera val = (Camera)((value is Camera) ? value : null);
if ((Object)(object)val != (Object)null)
{
canvas.worldCamera = val;
XPortalVRPatch.LogInfo("Set VR camera: " + ((Object)val).name);
}
}
RectTransform component = ((Component)canvas).GetComponent<RectTransform>();
((Transform)component).position = new Vector3(960f, 540f, 0f);
FieldInfo fieldInfo2 = AccessTools.Field(type, "GUI_DIMENSIONS");
if (fieldInfo2 != null)
{
Vector2 val2 = (Vector2)fieldInfo2.GetValue(null);
component.SetSizeWithCurrentAnchors((Axis)0, val2.x);
component.SetSizeWithCurrentAnchors((Axis)1, val2.y);
XPortalVRPatch.LogInfo($"Set canvas size to VHVR dimensions: {val2}");
}
XPortalVRPatch.LogInfo("Set up XPortal canvas with EpicLoot properties for VR");
}
catch (Exception arg)
{
XPortalVRPatch.LogError($"Error setting up canvas for VR: {arg}");
}
}
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)
{
XPortalVRPatch.LogError($"Error getting VHVR VRGUI: {arg}");
return null;
}
}
private static void AddCanvasToVHVR(Canvas canvas, object vrguiInstance)
{
try
{
if (AccessTools.Field(AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI"), "_guiCanvases")?.GetValue(vrguiInstance) is IList list && !list.Contains(canvas))
{
list.Add(canvas);
XPortalVRPatch.LogInfo("Added XPortal VR canvas to VHVR _guiCanvases list");
}
}
catch (Exception arg)
{
XPortalVRPatch.LogWarning($"Error adding to VHVR list: {arg}");
}
}
}