Decompiled source of EpicMMOVRFix v1.0.0

plugins/EpicMMOVRFix.dll

Decompiled 2 months 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 EpicMMO;
using HarmonyLib;
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("EpicMMOVRFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EpicMMOVRFix")]
[assembly: AssemblyTitle("EpicMMOVRFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("epicmmovrfix", "EpicMMO VR UI Fix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class EpicMMOVRUIPatch : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <WaitForEpicMMOSystem>d__14 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public EpicMMOVRUIPatch <>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 <WaitForEpicMMOSystem>d__14(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

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

		private bool MoveNext()
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			int num = <>1__state;
			EpicMMOVRUIPatch epicMMOVRUIPatch = <>4__this;
			switch (num)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				if (!ConfigEnableMod.Value)
				{
					((BaseUnityPlugin)epicMMOVRUIPatch).Logger.LogInfo((object)"EpicMMO VR UI 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 (EpicMMOVRUI.IsEpicMMOSystemLoaded())
				{
					epicMMOVRUIPatch._epicMMOLoaded = true;
					epicMMOVRUIPatch.ApplyPatches();
					if (ConfigEnableLogs.Value)
					{
						((BaseUnityPlugin)epicMMOVRUIPatch).Logger.LogInfo((object)"EpicMMOSystem detected, patches applied!");
					}
					return false;
				}
				if (<attempts>5__3 % 10 == 0 && ConfigEnableLogs.Value)
				{
					((BaseUnityPlugin)epicMMOVRUIPatch).Logger.LogInfo((object)$"Waiting for EpicMMOSystem... ({<attempts>5__3}s)");
				}
				break;
			}
			if (<attempts>5__3 < <maxAttempts>5__2 && !epicMMOVRUIPatch._epicMMOLoaded)
			{
				<>2__current = (object)new WaitForSeconds(1f);
				<>1__state = 1;
				return true;
			}
			if (!epicMMOVRUIPatch._epicMMOLoaded && ConfigEnableLogs.Value)
			{
				((BaseUnityPlugin)epicMMOVRUIPatch).Logger.LogWarning((object)"EpicMMOSystem 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();
		}
	}

	private static EpicMMOVRUIPatch _instance;

	private Harmony _harmony;

	private bool _epicMMOLoaded;

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

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

	public static EpicMMOVRUIPatch Instance => _instance;

	private void Awake()
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		_instance = this;
		ConfigEnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable the EpicMMO VR UI Fix mod");
		ConfigEnableLogs = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableLogs", false, "Enable debug logging for the EpicMMO VR UI Fix mod");
		try
		{
			_harmony = new Harmony("epicmmovrfix");
			StartCoroutine(WaitForEpicMMOSystem());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"EpicMMO VR UI Fix loaded successfully!");
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to load EpicMMO VR UI Fix: {arg}");
		}
	}

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

	private void ApplyPatches()
	{
		try
		{
			EpicMMOVRUI.PatchEpicMMOSystemUI(_harmony);
			EpicMMOVRHUD.Initialize(_harmony);
			if (ConfigEnableLogs.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"EpicMMOSystem VR UI and HUD patches applied successfully");
			}
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"Error applying patches: {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 message)
	{
		if (ConfigEnableLogs.Value)
		{
			EpicMMOVRUIPatch instance = _instance;
			if (instance != null)
			{
				((BaseUnityPlugin)instance).Logger.LogInfo((object)message);
			}
		}
	}

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

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

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

public static class EpicMMOVRHUD
{
	private static bool _isInitialized = false;

	private static Harmony _harmony;

	private static Dictionary<string, string> _nameCache = new Dictionary<string, string>();

	private static Dictionary<string, int> _monsterLevelCache = new Dictionary<string, int>();

	private static Type _epicMMOSystemType;

	private static Type _dataMonstersType;

	private static MethodInfo _containsMethod;

	private static MethodInfo _getLevelMethod;

	private static bool? _enabledLevelControl;

	private static bool? _mobLvlPerStar;

	private static string _mobLVLChars;

	private static int _maxLevelExp;

	private static int _minLevelExp;

	private static FieldInfo _characterLevelField;

	public static void Initialize(Harmony harmony)
	{
		if (!_isInitialized)
		{
			_harmony = harmony;
			_characterLevelField = typeof(Character).GetField("m_level", BindingFlags.Instance | BindingFlags.NonPublic);
			_epicMMOSystemType = AccessTools.TypeByName("EpicMMOSystem.EpicMMOSystem");
			_dataMonstersType = AccessTools.TypeByName("EpicMMOSystem.DataMonsters");
			if (_dataMonstersType != null)
			{
				_containsMethod = AccessTools.Method(_dataMonstersType, "contains", (Type[])null, (Type[])null);
				_getLevelMethod = AccessTools.Method(_dataMonstersType, "getLevel", (Type[])null, (Type[])null);
			}
			CacheConfigValues();
			ApplyVHVRPatches();
			_isInitialized = true;
		}
	}

	private static void CacheConfigValues()
	{
		try
		{
			if (_epicMMOSystemType != null)
			{
				Traverse val = Traverse.Create(_epicMMOSystemType).Field("enabledLevelControl");
				Traverse val2 = Traverse.Create(_epicMMOSystemType).Field("mobLvlPerStar");
				Traverse obj = Traverse.Create(_epicMMOSystemType).Field("MobLVLChars");
				Traverse val3 = Traverse.Create(_epicMMOSystemType).Field("maxLevelExp");
				Traverse val4 = Traverse.Create(_epicMMOSystemType).Field("minLevelExp");
				_enabledLevelControl = (val.GetValue() as bool?).GetValueOrDefault(true);
				_mobLvlPerStar = (val2.GetValue() as bool?).GetValueOrDefault();
				_mobLVLChars = (obj.GetValue() as string) ?? "[@]";
				_maxLevelExp = (val3.GetValue() as int?).GetValueOrDefault(5);
				_minLevelExp = (val4.GetValue() as int?).GetValueOrDefault(5);
			}
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Error caching config values: {arg}");
			_enabledLevelControl = true;
			_mobLvlPerStar = false;
			_mobLVLChars = "[@]";
			_maxLevelExp = 5;
			_minLevelExp = 5;
		}
	}

	private static void ApplyVHVRPatches()
	{
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Expected O, but got Unknown
		try
		{
			Type type = AccessTools.TypeByName("ValheimVRMod.VRCore.UI.EnemyHudManager");
			if (!(type == null))
			{
				MethodInfo methodInfo = AccessTools.Method(type, "UpdateName", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(EpicMMOVRHUD), "OnUpdateNamePrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogError($"Error applying VHVR patches: {arg}");
		}
	}

	private static void OnUpdateNamePrefix(object __instance, Character c, ref string name)
	{
		if (!EpicMMOVRUIPatch.ConfigEnableMod.Value || (Object)(object)c == (Object)null || _enabledLevelControl == false)
		{
			return;
		}
		try
		{
			int num = ((!(_characterLevelField != null)) ? 1 : ((int)_characterLevelField.GetValue(c)));
			string key = $"{((Object)((Component)c).gameObject).name}|{name}|{num}";
			if (_nameCache.TryGetValue(key, out var value))
			{
				name = value;
				return;
			}
			string modifiedNameWithLevel = GetModifiedNameWithLevel(c, name, num);
			if (modifiedNameWithLevel != name)
			{
				_nameCache[key] = modifiedNameWithLevel;
				name = modifiedNameWithLevel;
			}
		}
		catch
		{
		}
	}

	private static string GetModifiedNameWithLevel(Character c, string originalName, int characterLevel)
	{
		if ((Object)(object)c == (Object)null)
		{
			return originalName;
		}
		try
		{
			if (_containsMethod == null || _getLevelMethod == null)
			{
				return originalName;
			}
			if (!(bool)_containsMethod.Invoke(null, new object[1] { ((Object)((Component)c).gameObject).name }))
			{
				return originalName;
			}
			int num = (int)_getLevelMethod.Invoke(null, new object[1] { ((Object)((Component)c).gameObject).name });
			if (_mobLvlPerStar.GetValueOrDefault())
			{
				num += characterLevel - 1;
			}
			string newValue = num.ToString();
			if (num == 0)
			{
				newValue = "???";
			}
			string levelColor = GetLevelColor(num);
			string text = _mobLVLChars ?? "[@]";
			text = text.Replace("@", newValue);
			return originalName + " <color=" + levelColor + ">" + text + "</color>";
		}
		catch
		{
			return originalName;
		}
	}

	private static string GetLevelColor(int monsterLevel)
	{
		int num = 1;
		try
		{
			Type type = AccessTools.TypeByName("EpicMMOSystem.LevelSystem");
			if (type != null)
			{
				PropertyInfo propertyInfo = AccessTools.Property(type, "Instance");
				MethodInfo methodInfo = AccessTools.Method(type, "getLevel", (Type[])null, (Type[])null);
				if (propertyInfo != null && methodInfo != null)
				{
					object value = propertyInfo.GetValue(null);
					if (value != null)
					{
						num = (int)methodInfo.Invoke(value, null);
					}
				}
			}
		}
		catch
		{
		}
		int num2 = num + _maxLevelExp;
		int num3 = num - _minLevelExp;
		if (monsterLevel > num2)
		{
			return "red";
		}
		if (monsterLevel < num3)
		{
			return "#2FFFDC";
		}
		return "white";
	}
}
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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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
		{
			return AccessTools.TypeByName("EpicMMOSystem.MyUI") != null;
		}
		catch
		{
			return false;
		}
	}

	public static void PatchEpicMMOSystemUI(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("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
		{
			object? obj = AccessTools.Field(AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI"), "_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
		{
			if (AccessTools.Field(AccessTools.TypeByName("ValheimVRMod.VRCore.UI.VRGUI"), "_guiCanvases")?.GetValue(vrguiInstance) is IList list && !list.Contains(canvas))
			{
				list.Add(canvas);
			}
		}
		catch (Exception arg)
		{
			EpicMMOVRUIPatch.LogWarning($"Error adding to VHVR: {arg}");
		}
	}
}