Decompiled source of EpicMMOVRFix v1.2.1

plugins/EpicMMOVRFix.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using EpicMMO;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[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("EpicMMOVRFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0dabd6c3efcb096dedd47c43c821e9ce11fc19a6")]
[assembly: AssemblyProduct("EpicMMOVRFix")]
[assembly: AssemblyTitle("EpicMMOVRFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("epicmmovrfix", "EpicMMO VR UI Fix", "1.2.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class EpicMMOVRUIPatch : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <WaitForEpicMMO>d__43 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public EpicMMOVRUIPatch <>4__this;

		private int <tries>5__1;

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

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

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

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

		private bool MoveNext()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				if (!ConfigEnableMod.Value)
				{
					return false;
				}
				<tries>5__1 = 0;
				break;
			case 1:
				<>1__state = -1;
				<tries>5__1++;
				if (EpicMMOVRUI.IsEpicMMOSystemLoaded())
				{
					<>4__this._epicLoaded = true;
					<>4__this.ApplyPatches();
					return false;
				}
				break;
			}
			if (!<>4__this._epicLoaded && <tries>5__1 < 60)
			{
				<>2__current = (object)new WaitForSeconds(1f);
				<>1__state = 1;
				return true;
			}
			((BaseUnityPlugin)<>4__this).Logger.LogWarning((object)"EpicMMOSystem not detected");
			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 EpicMMOVRUIPatch _instance;

	private Harmony _harmony;

	private bool _epicLoaded;

	public static EpicMMOVRUIPatch Instance => _instance;

	public static ConfigEntry<bool> ConfigEnableMod { get; private set; }

	public static ConfigEntry<bool> ConfigEnableLogs { get; private set; }

	public static ConfigEntry<bool> ConfigEnableInventoryXPBar { get; private set; }

	public static ConfigEntry<float> ConfigInventoryXPBarPosX { get; private set; }

	public static ConfigEntry<float> ConfigInventoryXPBarPosY { get; private set; }

	public static ConfigEntry<float> ConfigInventoryXPBarScale { get; private set; }

	public static ConfigEntry<float> ConfigHudXPBarPosX { get; private set; }

	public static ConfigEntry<float> ConfigHudXPBarPosY { get; private set; }

	public static ConfigEntry<float> ConfigHudXPBarScale { get; private set; }

	private void Awake()
	{
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Expected O, but got Unknown
		_instance = this;
		ConfigEnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable EpicMMO VR UI Fix");
		ConfigEnableLogs = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableLogs", false, "Enable debug logging");
		ConfigEnableInventoryXPBar = ((BaseUnityPlugin)this).Config.Bind<bool>("XP Bar", "EnableInventoryXPBar", true, "Move EpicMMO XP bar into inventory while inventory is open");
		ConfigInventoryXPBarPosX = ((BaseUnityPlugin)this).Config.Bind<float>("XP Bar", "InventoryXPBarPosX", 500f, "Inventory XP bar X position");
		ConfigInventoryXPBarPosY = ((BaseUnityPlugin)this).Config.Bind<float>("XP Bar", "InventoryXPBarPosY", -750f, "Inventory XP bar Y position");
		ConfigInventoryXPBarScale = ((BaseUnityPlugin)this).Config.Bind<float>("XP Bar", "InventoryXPBarScale", 0.9f, "Inventory XP bar scale");
		ConfigHudXPBarPosX = ((BaseUnityPlugin)this).Config.Bind<float>("XP Bar", "HudXPBarPosX", -550f, "HUD XP bar X position");
		ConfigHudXPBarPosY = ((BaseUnityPlugin)this).Config.Bind<float>("XP Bar", "HudXPBarPosY", -950f, "HUD XP bar Y position");
		ConfigHudXPBarScale = ((BaseUnityPlugin)this).Config.Bind<float>("XP Bar", "HudXPBarScale", 0.5f, "HUD XP bar scale");
		try
		{
			_harmony = new Harmony("epicmmovrfix");
			ConfigEnableInventoryXPBar.SettingChanged += ConfigChanged;
			ConfigInventoryXPBarPosX.SettingChanged += ConfigChanged;
			ConfigInventoryXPBarPosY.SettingChanged += ConfigChanged;
			ConfigInventoryXPBarScale.SettingChanged += ConfigChanged;
			ConfigHudXPBarPosX.SettingChanged += ConfigChanged;
			ConfigHudXPBarPosY.SettingChanged += ConfigChanged;
			ConfigHudXPBarScale.SettingChanged += ConfigChanged;
			StartCoroutine(WaitForEpicMMO());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"EpicMMO VR UI Fix loaded");
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"Load failed: {arg}");
		}
	}

	private void ConfigChanged(object sender, EventArgs e)
	{
		EpicMMOVRXPBar.ApplyCurrentMode();
	}

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

	private void ApplyPatches()
	{
		try
		{
			EpicMMOVRUI.PatchEpicMMOSystemUI(_harmony);
			EpicMMOVRHUD.Initialize(_harmony);
			EpicMMOVRXPBar.Initialize(_harmony);
			EpicMMOVRCrit.Initialize(_harmony);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Patches applied");
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"Patch error: {arg}");
		}
	}

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

	public void StartCoroutine(IEnumerator routine)
	{
		((MonoBehaviour)this).StartCoroutine(routine);
	}

	public static void LogInfo(string msg)
	{
		if (ConfigEnableLogs.Value)
		{
			EpicMMOVRUIPatch instance = _instance;
			if (instance != null)
			{
				((BaseUnityPlugin)instance).Logger.LogInfo((object)msg);
			}
		}
	}

	public static void LogWarning(string msg)
	{
		if (ConfigEnableLogs.Value)
		{
			EpicMMOVRUIPatch instance = _instance;
			if (instance != null)
			{
				((BaseUnityPlugin)instance).Logger.LogWarning((object)msg);
			}
		}
	}

	public static void LogError(string msg)
	{
		EpicMMOVRUIPatch instance = _instance;
		if (instance != null)
		{
			((BaseUnityPlugin)instance).Logger.LogError((object)msg);
		}
	}
}
namespace EpicMMO;

public static class EpicMMOVRCrit
{
	private static Type _vrDamageTextType;

	private static MethodInfo _poolMethod;

	private static MethodInfo _createTextMethod;

	public static void Initialize(Harmony harmony)
	{
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Expected O, but got Unknown
		try
		{
			Type type = AccessTools.TypeByName("EpicMMOSystem.CritDmgVFX");
			if (type == null)
			{
				EpicMMOVRUIPatch.LogWarning("Crit patch: CritDmgVFX not found");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "CriticalVFX", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				EpicMMOVRUIPatch.LogWarning("Crit patch: CriticalVFX method not found");
				return;
			}
			_vrDamageTextType = AccessTools.TypeByName("ValheimVRMod.Scripts.VRDamageTexts");
			if (_vrDamageTextType != null)
			{
				_poolMethod = AccessTools.Method(_vrDamageTextType, "Pool", (Type[])null, (Type[])null);
				_createTextMethod = AccessTools.Method(_vrDamageTextType, "CreateText", new Type[5]
				{
					typeof(string),
					typeof(Vector3),
					typeof(Color),
					typeof(bool),
					typeof(float)
				}, (Type[])null);
			}
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(EpicMMOVRCrit), "OnCriticalVFXPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			EpicMMOVRUIPatch.LogInfo("EpicMMO Crit VR patch loaded");
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Crit patch init failed: {arg}");
		}
	}

	private static void OnCriticalVFXPostfix(Vector3 position, float damage)
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (_vrDamageTextType == null || _poolMethod == null || _createTextMethod == null)
			{
				return;
			}
			object obj = _poolMethod.Invoke(null, null);
			if (obj != null)
			{
				string text = "Crit " + Mathf.RoundToInt(damage);
				_createTextMethod.Invoke(obj, new object[5]
				{
					text,
					position,
					Color.cyan,
					false,
					1.5f
				});
				TryAddOutline(obj);
				if (EpicMMOVRUIPatch.ConfigEnableLogs.Value)
				{
					EpicMMOVRUIPatch.LogInfo("VR Crit spawned: " + text);
				}
			}
		}
		catch (Exception ex)
		{
			EpicMMOVRUIPatch.LogWarning("Crit spawn failed: " + ex.Message);
		}
	}

	private static void TryAddOutline(object pooled)
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			MonoBehaviour val = (MonoBehaviour)((pooled is MonoBehaviour) ? pooled : null);
			if ((Object)(object)val == (Object)null || (Object)(object)((Component)val).gameObject == (Object)null)
			{
				return;
			}
			Text component = ((Component)val).GetComponent<Text>();
			if (!((Object)(object)component == (Object)null))
			{
				Outline val2 = ((Component)component).GetComponent<Outline>();
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = ((Component)component).gameObject.AddComponent<Outline>();
				}
				((Shadow)val2).effectColor = Color.black;
				((Shadow)val2).effectDistance = new Vector2(2f, -2f);
			}
		}
		catch (Exception ex)
		{
			EpicMMOVRUIPatch.LogWarning("Outline failed: " + ex.Message);
		}
	}
}
public static class EpicMMOVRHUD
{
	private static FieldInfo _characterLevelField;

	private static Func<object, Character, object> _getEnemyHudFast;

	private static Func<object, object> _getNameComponentFast;

	private static Func<object, string> _getTextFast;

	private static Action<object, string> _setTextFast;

	private static MethodInfo _containsMethod;

	private static MethodInfo _getLevelMethod;

	private static PropertyInfo _levelSystemInstance;

	private static MethodInfo _getPlayerLevelMethod;

	public static void Initialize(Harmony harmony)
	{
		//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02db: Expected O, but got Unknown
		try
		{
			_characterLevelField = typeof(Character).GetField("m_level", BindingFlags.Instance | BindingFlags.NonPublic);
			Type type = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.EnemyHudManager");
			Type type2 = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.EnemyHudManager+HudData");
			if (!(type == null) && !(type2 == null))
			{
				MethodInfo methodInfo = AccessTools.Method(type, "UpdateName", (Type[])null, (Type[])null);
				MethodInfo method = AccessTools.Method(type, "getEnemyHud", (Type[])null, (Type[])null);
				FieldInfo fieldInfo = AccessTools.Field(type2, "name");
				ParameterExpression parameterExpression = Expression.Parameter(typeof(object), "instance");
				ParameterExpression parameterExpression2 = Expression.Parameter(typeof(Character), "c");
				UnaryExpression instance = Expression.Convert(parameterExpression, type);
				MethodCallExpression expression = Expression.Call(instance, method, parameterExpression2);
				UnaryExpression body = Expression.Convert(expression, typeof(object));
				_getEnemyHudFast = Expression.Lambda<Func<object, Character, object>>(body, new ParameterExpression[2] { parameterExpression, parameterExpression2 }).Compile();
				ParameterExpression parameterExpression3 = Expression.Parameter(typeof(object), "hud");
				UnaryExpression expression2 = Expression.Convert(parameterExpression3, type2);
				MemberExpression expression3 = Expression.Field(expression2, fieldInfo);
				UnaryExpression body2 = Expression.Convert(expression3, typeof(object));
				_getNameComponentFast = Expression.Lambda<Func<object, object>>(body2, new ParameterExpression[1] { parameterExpression3 }).Compile();
				PropertyInfo property = fieldInfo.FieldType.GetProperty("text");
				ParameterExpression parameterExpression4 = Expression.Parameter(typeof(object), "obj");
				UnaryExpression expression4 = Expression.Convert(parameterExpression4, fieldInfo.FieldType);
				MemberExpression body3 = Expression.Property(expression4, property);
				_getTextFast = Expression.Lambda<Func<object, string>>(body3, new ParameterExpression[1] { parameterExpression4 }).Compile();
				ParameterExpression parameterExpression5 = Expression.Parameter(typeof(object), "obj");
				ParameterExpression parameterExpression6 = Expression.Parameter(typeof(string), "val");
				UnaryExpression expression5 = Expression.Convert(parameterExpression5, fieldInfo.FieldType);
				MemberExpression left = Expression.Property(expression5, property);
				BinaryExpression body4 = Expression.Assign(left, parameterExpression6);
				_setTextFast = Expression.Lambda<Action<object, string>>(body4, new ParameterExpression[2] { parameterExpression5, parameterExpression6 }).Compile();
				Type type3 = AccessTools.TypeByName("EpicMMOSystem.DataMonsters");
				if (type3 != null)
				{
					_containsMethod = AccessTools.Method(type3, "contains", (Type[])null, (Type[])null);
					_getLevelMethod = AccessTools.Method(type3, "getLevel", (Type[])null, (Type[])null);
				}
				Type type4 = AccessTools.TypeByName("EpicMMOSystem.LevelSystem");
				if (type4 != null)
				{
					_levelSystemInstance = AccessTools.Property(type4, "Instance");
					_getPlayerLevelMethod = AccessTools.Method(type4, "getLevel", (Type[])null, (Type[])null);
				}
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(EpicMMOVRHUD), "OnUpdateNamePostfix", (Type[])null)
				{
					priority = 600
				}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"EpicMMO VR HUD init error: {arg}");
		}
	}

	private static void OnUpdateNamePostfix(object __instance, Character c)
	{
		if (!EpicMMOVRUIPatch.ConfigEnableMod.Value || (Object)(object)c == (Object)null)
		{
			return;
		}
		try
		{
			object obj = _getEnemyHudFast(__instance, c);
			if (obj == null)
			{
				return;
			}
			object obj2 = _getNameComponentFast(obj);
			if (obj2 == null)
			{
				return;
			}
			string text = _getTextFast(obj2);
			if (!string.IsNullOrEmpty(text) && (!text.Contains("[") || !text.Contains("]")))
			{
				int characterLevel = ((!(_characterLevelField != null)) ? 1 : ((int)_characterLevelField.GetValue(c)));
				int monsterLevel = GetMonsterLevel(c, characterLevel);
				if (monsterLevel > 0)
				{
					string levelColor = GetLevelColor(monsterLevel);
					string text2 = $"<color={levelColor}>[{monsterLevel}]</color>";
					_setTextFast(obj2, text + " " + text2);
				}
			}
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"EpicMMO VR HUD Error: {arg}");
		}
	}

	private static int GetMonsterLevel(Character c, int characterLevel)
	{
		try
		{
			if (_containsMethod == null || _getLevelMethod == null)
			{
				return 0;
			}
			if (!(bool)_containsMethod.Invoke(null, new object[1] { ((Object)((Component)c).gameObject).name }))
			{
				return 0;
			}
			int num = (int)_getLevelMethod.Invoke(null, new object[1] { ((Object)((Component)c).gameObject).name });
			return num + (characterLevel - 1);
		}
		catch
		{
			return 0;
		}
	}

	private static string GetLevelColor(int monsterLevel)
	{
		int num = 1;
		try
		{
			object obj = _levelSystemInstance?.GetValue(null);
			if (obj != null && _getPlayerLevelMethod != null)
			{
				num = (int)_getPlayerLevelMethod.Invoke(obj, null);
			}
		}
		catch
		{
		}
		int num2 = num + 5;
		int num3 = num - 5;
		if (monsterLevel > num2)
		{
			return "#FF0000";
		}
		if (monsterLevel < num3)
		{
			return "#2FFFDC";
		}
		return "#FFFFFF";
	}
}
public static class EpicMMOVRUI
{
	[CompilerGenerated]
	private sealed class <ProcessEpicMMOUIForVR>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 <ProcessEpicMMOUIForVR>d__4(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>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;
				ProcessEpicMMOCanvas();
				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();
		}
	}

	[CompilerGenerated]
	private sealed class <TryProcessExistingUI>d__3 : 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 <TryProcessExistingUI>d__3(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

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

		private bool MoveNext()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<>2__current = (object)new WaitForSeconds(2f);
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				ProcessEpicMMOCanvas();
				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 IsEpicMMOSystemLoaded()
	{
		try
		{
			Type type = AccessTools.TypeByName("EpicMMOSystem.MyUI");
			return type != null;
		}
		catch
		{
			return false;
		}
	}

	public static void PatchEpicMMOSystemUI(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("EpicMMOSystem.MyUI");
			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(EpicMMOVRUI), "OnEpicMMOUIShown", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				EpicMMOVRUIPatch.Instance.StartCoroutine(TryProcessExistingUI());
			}
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Error patching: {arg}");
		}
	}

	public static void OnEpicMMOUIShown()
	{
		EpicMMOVRUIPatch.Instance.StartCoroutine(ProcessEpicMMOUIForVR());
	}

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

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

	private static void ProcessEpicMMOCanvas()
	{
		try
		{
			Canvas[] array = Resources.FindObjectsOfTypeAll<Canvas>();
			Canvas val = null;
			Canvas[] array2 = array;
			foreach (Canvas val2 in array2)
			{
				if ((Object)(object)val2 != (Object)null && ((Object)val2).name == "Canvas" && (Object)(object)((Component)val2).transform.parent != (Object)null && ((Object)((Component)val2).transform.parent).name.Contains("LevelHud"))
				{
					val = val2;
					break;
				}
			}
			if (!((Object)(object)val == (Object)null))
			{
				object vHVRVRGUI = GetVHVRVRGUI();
				if (vHVRVRGUI != null)
				{
					ProcessCanvasForVR(val, vHVRVRGUI);
				}
			}
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Error: {arg}");
		}
	}

	private static void ProcessCanvasForVR(Canvas canvas, object vrguiInstance)
	{
		try
		{
			((Component)canvas).gameObject.layer = 5;
			canvas.renderMode = (RenderMode)2;
			Camera vHGuiCamera = GetVHGuiCamera(vrguiInstance);
			if ((Object)(object)vHGuiCamera != (Object)null)
			{
				canvas.worldCamera = vHGuiCamera;
			}
			CanvasGroup val = ((Component)canvas).GetComponent<CanvasGroup>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)canvas).gameObject.AddComponent<CanvasGroup>();
			}
			val.blocksRaycasts = true;
			val.interactable = true;
			AddCanvasToVHVR(canvas, vrguiInstance);
			EpicMMOVRUIPatch.LogInfo("EpicMMO canvas processed for VR");
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Error processing canvas: {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)
		{
			EpicMMOVRUIPatch.LogError($"Error getting VRGUI: {arg}");
			return null;
		}
	}

	private static Camera GetVHGuiCamera(object vrguiInstance)
	{
		try
		{
			Type type = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI");
			object? obj = AccessTools.Field(type, "_guiCamera")?.GetValue(vrguiInstance);
			return (Camera)((obj is Camera) ? obj : null);
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Error getting camera: {arg}");
			return null;
		}
	}

	private static void AddCanvasToVHVR(Canvas canvas, object vrguiInstance)
	{
		try
		{
			Type type = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI");
			if (AccessTools.Field(type, "_guiCanvases")?.GetValue(vrguiInstance) is IList list && !list.Contains(canvas))
			{
				list.Add(canvas);
			}
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogWarning($"Error adding to VHVR: {arg}");
		}
	}
}
public static class EpicMMOVRXPBar
{
	[CompilerGenerated]
	private sealed class <DelayedMoveToInventory>d__9 : 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 <DelayedMoveToInventory>d__9(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>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;
				<>2__current = null;
				<>1__state = 2;
				return true;
			case 2:
				<>1__state = -1;
				MoveToInventory();
				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();
		}
	}

	[CompilerGenerated]
	private sealed class <DelayedReacquire>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 <DelayedReacquire>d__7(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>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;
				TryFindXPBar();
				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 GameObject _xpBar;

	private static Transform _originalParent;

	public static void Initialize(Harmony harmony)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Expected O, but got Unknown
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Expected O, but got Unknown
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		harmony.Patch((MethodBase)AccessTools.Method(typeof(InventoryGui), "Show", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(EpicMMOVRXPBar), "OnInventoryShow", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(InventoryGui), "Hide", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(EpicMMOVRXPBar), "OnInventoryHide", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		harmony.Patch((MethodBase)AccessTools.Method(typeof(Game), "SpawnPlayer", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(EpicMMOVRXPBar), "OnPlayerSpawned", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		TryFindXPBar();
	}

	private static void TryFindXPBar()
	{
		if (!((Object)(object)_xpBar != (Object)null))
		{
			_xpBar = GameObject.Find("ExpPanel(Clone)");
			if ((Object)(object)_xpBar != (Object)null)
			{
				_originalParent = _xpBar.transform.parent;
				EpicMMOVRUIPatch.LogInfo("XPBar found and cached");
				ApplyCurrentMode();
			}
		}
	}

	private static void OnInventoryShow()
	{
		TryFindXPBar();
		ApplyCurrentMode();
	}

	private static void OnInventoryHide()
	{
		ApplyCurrentMode();
	}

	private static void OnPlayerSpawned()
	{
		_xpBar = null;
		_originalParent = null;
		EpicMMOVRUIPatch.Instance.StartCoroutine(DelayedReacquire());
	}

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

	public static void ApplyCurrentMode()
	{
		if (!((Object)(object)_xpBar == (Object)null))
		{
			bool flag = (Object)(object)InventoryGui.instance != (Object)null && ((Component)InventoryGui.instance).gameObject.activeInHierarchy;
			if (EpicMMOVRUIPatch.ConfigEnableInventoryXPBar.Value && flag)
			{
				EpicMMOVRUIPatch.Instance.StartCoroutine(DelayedMoveToInventory());
			}
			else
			{
				RestoreToHud();
			}
		}
	}

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

	private static void MoveToInventory()
	{
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_xpBar == (Object)null) && !((Object)(object)InventoryGui.instance == (Object)null))
		{
			Transform val = ((Component)InventoryGui.instance).transform.Find("root/Player");
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)InventoryGui.instance).transform;
			}
			_xpBar.transform.SetParent(val, false);
			_xpBar.SetActive(true);
			RectTransform component = _xpBar.GetComponent<RectTransform>();
			if ((Object)(object)component != (Object)null)
			{
				((Transform)component).localScale = Vector3.one;
				((Transform)component).localRotation = Quaternion.identity;
			}
			ApplyInventoryPosition();
		}
	}

	private static void RestoreToHud()
	{
		if (!((Object)(object)_xpBar == (Object)null) && !((Object)(object)_originalParent == (Object)null))
		{
			_xpBar.transform.SetParent(_originalParent, false);
			_xpBar.SetActive(true);
			ApplyHudPosition();
		}
	}

	public static void ApplyInventoryPosition()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_xpBar == (Object)null))
		{
			RectTransform component = _xpBar.GetComponent<RectTransform>();
			if (!((Object)(object)component == (Object)null))
			{
				component.anchorMin = new Vector2(0.5f, 0.5f);
				component.anchorMax = new Vector2(0.5f, 0.5f);
				component.pivot = new Vector2(0.5f, 0.5f);
				component.anchoredPosition = new Vector2(EpicMMOVRUIPatch.ConfigInventoryXPBarPosX.Value, EpicMMOVRUIPatch.ConfigInventoryXPBarPosY.Value);
				((Transform)component).localScale = Vector3.one * EpicMMOVRUIPatch.ConfigInventoryXPBarScale.Value;
			}
		}
	}

	public static void ApplyHudPosition()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_xpBar == (Object)null))
		{
			RectTransform component = _xpBar.GetComponent<RectTransform>();
			if (!((Object)(object)component == (Object)null))
			{
				component.anchorMin = new Vector2(0.5f, 1f);
				component.anchorMax = new Vector2(0.5f, 1f);
				component.pivot = new Vector2(0.5f, 1f);
				component.anchoredPosition = new Vector2(EpicMMOVRUIPatch.ConfigHudXPBarPosX.Value, EpicMMOVRUIPatch.ConfigHudXPBarPosY.Value);
				((Transform)component).localScale = Vector3.one * EpicMMOVRUIPatch.ConfigHudXPBarScale.Value;
			}
		}
	}
}