Decompiled source of MacosHealthbarsFix v1.0.27

BepInEx/plugins/MetalSafeBars/MetalSafeBars.dll

Decompiled 2 weeks 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.Logging;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MetalSafeBars")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+774384bcf28152a3b9fd91466ebcd2109e836743")]
[assembly: AssemblyProduct("MetalSafeBars")]
[assembly: AssemblyTitle("MetalSafeBars")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MetalSafeBars
{
	public class Class1
	{
	}
	[BepInPlugin("me.metalfix.ror2.healthbars", "MetalSafeBars", "1.0.0")]
	public class MetalSafeBarsPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <ScanLoop>d__13 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public MetalSafeBarsPlugin <>4__this;

			private WaitForSeconds <wait>5__2;

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

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

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

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

			private bool MoveNext()
			{
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Expected O, but got Unknown
				int num = <>1__state;
				MetalSafeBarsPlugin metalSafeBarsPlugin = <>4__this;
				if (num != 0)
				{
					if (num != 1)
					{
						return false;
					}
					<>1__state = -1;
				}
				else
				{
					<>1__state = -1;
					<wait>5__2 = new WaitForSeconds(0.5f);
				}
				try
				{
					metalSafeBarsPlugin.AttachToExistingBars();
					if (Time.time - metalSafeBarsPlugin._lastLogTime >= 10f)
					{
						((BaseUnityPlugin)metalSafeBarsPlugin).Logger.LogInfo((object)$"[MetalSafeBars] Активен, отслежено {metalSafeBarsPlugin._seen.Count} healthbar'ов");
						metalSafeBarsPlugin._lastLogTime = Time.time;
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)metalSafeBarsPlugin).Logger.LogError((object)ex);
				}
				<>2__current = <wait>5__2;
				<>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();
			}
		}

		internal static MetalSafeBarsPlugin Instance;

		private readonly HashSet<GameObject> _seen = new HashSet<GameObject>();

		private float _lastLogTime;

		internal static Sprite WhiteSprite;

		internal static Material UnlitMat;

		internal static Material UiDefaultMat;

		private static Type _healthBarType;

		internal int SeenCount => _seen.Count;

		private void Awake()
		{
			Instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[MetalSafeBars] Loaded");
			EnsureWhiteSprite();
			EnsureMaterials();
			ResolveHealthBarType();
			((MonoBehaviour)this).StartCoroutine(ScanLoop());
		}

		private void ResolveHealthBarType()
		{
			_healthBarType = Type.GetType("RoR2.UI.HealthBar, RoR2");
			if (_healthBarType != null)
			{
				return;
			}
			try
			{
				Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
				for (int i = 0; i < assemblies.Length; i++)
				{
					Type type = assemblies[i].GetType("RoR2.UI.HealthBar");
					if (type != null)
					{
						_healthBarType = type;
						break;
					}
				}
			}
			catch
			{
			}
			if (_healthBarType == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[MetalSafeBars] Не удалось найти тип RoR2.UI.HealthBar — мод активен, но нечему патчиться.");
			}
		}

		private void EnsureMaterials()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			try
			{
				Shader val = Shader.Find("Unlit/Color");
				if (Object.op_Implicit((Object)(object)val))
				{
					UnlitMat = new Material(val);
				}
			}
			catch
			{
			}
			try
			{
				Shader val2 = Shader.Find("UI/Default");
				if (Object.op_Implicit((Object)(object)val2))
				{
					UiDefaultMat = new Material(val2);
				}
			}
			catch
			{
			}
		}

		private static void EnsureWhiteSprite()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			if (!Object.op_Implicit((Object)(object)WhiteSprite))
			{
				Texture2D val = new Texture2D(1, 1, (TextureFormat)4, false);
				val.SetPixel(0, 0, Color.white);
				val.Apply();
				WhiteSprite = Sprite.Create(val, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f), 1f);
			}
		}

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

		private void AttachToExistingBars()
		{
			if (_healthBarType == null)
			{
				return;
			}
			Object[] array = Resources.FindObjectsOfTypeAll(_healthBarType);
			if (array == null)
			{
				return;
			}
			Object[] array2 = array;
			foreach (Object obj in array2)
			{
				Component val = (Component)(object)((obj is Component) ? obj : null);
				if (!Object.op_Implicit((Object)(object)val))
				{
					continue;
				}
				GameObject gameObject = val.gameObject;
				if (!Object.op_Implicit((Object)(object)gameObject) || _seen.Contains(gameObject) || !gameObject.activeInHierarchy)
				{
					continue;
				}
				if (HasComponentTypeInParents(gameObject, "BossHealthBar"))
				{
					_seen.Add(gameObject);
					continue;
				}
				MetalSafeHealthbarFixer metalSafeHealthbarFixer = gameObject.GetComponent<MetalSafeHealthbarFixer>();
				if (!Object.op_Implicit((Object)(object)metalSafeHealthbarFixer))
				{
					metalSafeHealthbarFixer = gameObject.AddComponent<MetalSafeHealthbarFixer>();
				}
				metalSafeHealthbarFixer.Init(gameObject, ((BaseUnityPlugin)this).Logger);
				_seen.Add(gameObject);
			}
		}

		private static bool HasComponentTypeInParents(GameObject go, string typeName)
		{
			Component[] componentsInParent = go.GetComponentsInParent<Component>(true);
			foreach (Component val in componentsInParent)
			{
				if ((Object)(object)val != (Object)null && ((object)val).GetType().Name == typeName)
				{
					return true;
				}
			}
			return false;
		}
	}
	internal class MetalSafeHealthbarFixer : MonoBehaviour
	{
		private GameObject _root;

		private ManualLogSource _log;

		private float _nextScan;

		private int _lastChildrenHash;

		public void Init(GameObject root, ManualLogSource log)
		{
			_root = root;
			_log = log;
			ForcePatch("Init");
		}

		private void OnEnable()
		{
			ForcePatch("OnEnable");
		}

		private void OnTransformChildrenChanged()
		{
			ForcePatch("ChildrenChanged");
		}

		private void LateUpdate()
		{
			if (Time.time >= _nextScan)
			{
				_nextScan = Time.time + 0.33f;
				int num = ChildrenHash();
				if (num != _lastChildrenHash)
				{
					ForcePatch("ChildrenHashChanged");
					_lastChildrenHash = num;
				}
				else
				{
					PatchOnce(full: false);
				}
			}
		}

		private int ChildrenHash()
		{
			if (!Object.op_Implicit((Object)(object)_root))
			{
				return 0;
			}
			int num = 17;
			Image[] componentsInChildren = _root.GetComponentsInChildren<Image>(true);
			foreach (Image val in componentsInChildren)
			{
				if (Object.op_Implicit((Object)(object)val))
				{
					num = num * 31 + ((Object)val).GetInstanceID();
				}
			}
			TMP_Text[] componentsInChildren2 = _root.GetComponentsInChildren<TMP_Text>(true);
			foreach (TMP_Text val2 in componentsInChildren2)
			{
				if (Object.op_Implicit((Object)(object)val2))
				{
					num = num * 31 + ((Object)val2).GetInstanceID();
				}
			}
			return num;
		}

		private void ForcePatch(string reason)
		{
			try
			{
				PatchOnce(full: true);
				_lastChildrenHash = ChildrenHash();
				ManualLogSource log = _log;
				if (log != null)
				{
					log.LogInfo((object)("[MetalSafeBars] Patched HB (" + reason + ")"));
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log2 = _log;
				if (log2 != null)
				{
					log2.LogError((object)ex);
				}
			}
		}

		private void PatchOnce(bool full)
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Invalid comparison between Unknown and I4
			if (!Object.op_Implicit((Object)(object)_root))
			{
				return;
			}
			Image[] componentsInChildren = _root.GetComponentsInChildren<Image>(true);
			foreach (Image val in componentsInChildren)
			{
				if (Object.op_Implicit((Object)(object)val))
				{
					if (!Object.op_Implicit((Object)(object)val.sprite) && Object.op_Implicit((Object)(object)MetalSafeBarsPlugin.WhiteSprite))
					{
						val.sprite = MetalSafeBarsPlugin.WhiteSprite;
					}
					if (Object.op_Implicit((Object)(object)MetalSafeBarsPlugin.UnlitMat))
					{
						((Graphic)val).material = MetalSafeBarsPlugin.UnlitMat;
					}
					else if (Object.op_Implicit((Object)(object)MetalSafeBarsPlugin.UiDefaultMat))
					{
						((Graphic)val).material = MetalSafeBarsPlugin.UiDefaultMat;
					}
					else
					{
						((Graphic)val).material = null;
					}
					((MaskableGraphic)val).maskable = false;
					val.useSpriteMesh = false;
					Color color = ((Graphic)val).color;
					if (color.a <= 0f)
					{
						color.a = 1f;
						((Graphic)val).color = color;
					}
					if (full && (int)val.type == 3)
					{
						val.fillMethod = (FillMethod)0;
						val.fillOrigin = 0;
					}
				}
			}
			TMP_Text[] componentsInChildren2 = _root.GetComponentsInChildren<TMP_Text>(true);
			foreach (TMP_Text val2 in componentsInChildren2)
			{
				if (!Object.op_Implicit((Object)(object)val2))
				{
					continue;
				}
				try
				{
					if (Object.op_Implicit((Object)(object)val2.font))
					{
						val2.fontMaterial = ((TMP_Asset)val2.font).material;
					}
				}
				catch
				{
				}
			}
		}
	}
}