Decompiled source of HealthDisplayWithFont v0.4.0

Mods/HealthDisplayWithFont.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 HarmonyLib;
using HealthDisplayWithFont;
using Il2CppRUMBLE.Managers;
using Il2CppRUMBLE.Players;
using Il2CppRUMBLE.Players.Subsystems;
using Il2CppRUMBLE.Utilities;
using Il2CppSystem.Collections.Generic;
using Il2CppTMPro;
using MelonLoader;
using RumbleModdingAPI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(HealthDisplayWithFontClass), "HealthDisplayWithFont", "0.4.0", "ninjaguardian", "https://thunderstore.io/c/rumble/p/ninjaguardian/HealthDisplayWithFont")]
[assembly: MelonGame("Buckethead Entertainment", "RUMBLE")]
[assembly: MelonColor(255, 0, 160, 230)]
[assembly: MelonAuthorColor(255, 0, 160, 230)]
[assembly: MelonPlatformDomain(/*Could not decode attribute arguments.*/)]
[assembly: VerifyLoaderVersion("0.7.0", true)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("ninjaguardian (github)")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © 2025 ninjaguardian (github), This work is dedicated to the public domain under CC0 1.0.")]
[assembly: AssemblyDescription("Shows health above healthbar with a chosen font")]
[assembly: AssemblyFileVersion("0.4.0.0")]
[assembly: AssemblyInformationalVersion("0.4.0")]
[assembly: AssemblyProduct("HealthDisplayWithFont")]
[assembly: AssemblyTitle("HealthDisplayWithFont")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/ninjaguardian/HealthDisplayWithFont")]
[assembly: AssemblyVersion("0.4.0.0")]
namespace HealthDisplayWithFont;

public static class HealthDisplayWithFontModInfo
{
	public const string ModName = "HealthDisplayWithFont";

	public const string ModVer = "0.4.0";

	public const string MLVersion = "0.7.0";
}
public class HealthDisplayWithFontClass : MelonMod
{
	[HarmonyPatch(typeof(PlayerHealth), "Initialize")]
	private class PlayerHealthInitPatch
	{
		private static void Postfix(PlayerHealth __instance, PlayerController controller)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			AddHealthbarText(GetHealthbar(((Component)__instance).transform, controller.controllerType), controller);
		}
	}

	[HarmonyPatch(typeof(PlayerHealth), "SetHealthBarPercentage")]
	private class SetHealthBarPercentagePatch
	{
		private static void Postfix(PlayerHealth __instance, float currentHealth)
		{
			Transform healthbarText = GetHealthbarText(((Component)__instance).transform, ((PlayerControllerSubsystem)__instance).parentController);
			TextMeshPro val = ((healthbarText != null) ? ((Component)healthbarText).GetComponent<TextMeshPro>() : null);
			if ((Object)(object)val != (Object)null)
			{
				((TMP_Text)val).text = currentHealth.ToString();
			}
		}
	}

	[HarmonyPatch(typeof(PlayerNameTag), "SetPlayerNameTagActive")]
	private class SetPlayerNameTagActivePatch
	{
		private static void Prefix(PlayerNameTag __instance, bool state)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Invalid comparison between Unknown and I4
			if (fadeActive.Contains(__instance))
			{
				return;
			}
			PlayerController parentController = ((PlayerControllerSubsystem)__instance).parentController;
			if (parentController == null || (int)parentController.controllerType != 2 || (Object)(object)((Component)__instance).transform.parent != (Object)(object)((Component)parentController).transform)
			{
				return;
			}
			Transform healthbarText = GetHealthbarText(parentController);
			if (healthbarText != null)
			{
				GameObject gameObject = ((Component)healthbarText).gameObject;
				if (gameObject != null)
				{
					gameObject.SetActive(!state);
				}
			}
		}
	}

	[HarmonyPatch(typeof(PlayerNameTag), "FadePlayerNameTag")]
	private class FadePlayerNameTagPatch
	{
		private static void Prefix(PlayerNameTag __instance, bool on)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Invalid comparison between Unknown and I4
			PlayerController parentController = ((PlayerControllerSubsystem)__instance).parentController;
			if (parentController == null || (int)parentController.controllerType != 2 || (Object)(object)((Component)__instance).transform.parent != (Object)(object)((Component)parentController).transform)
			{
				return;
			}
			Transform healthbar = GetHealthbarText(parentController);
			Transform obj = healthbar;
			if ((Object)(object)((obj != null) ? ((Component)obj).gameObject : null) == (Object)null)
			{
				return;
			}
			if (!on && !((Component)healthbar).gameObject.activeSelf)
			{
				((Component)healthbar).gameObject.SetActive(true);
				((TMP_Text)((Component)healthbar).GetComponent<TextMeshPro>()).alpha = 0f;
			}
			if (!fadeActive.Add(__instance))
			{
				return;
			}
			MelonCoroutines.Start(FadeText(((Component)healthbar).GetComponent<TextMeshPro>(), on ? 0f : 1f, __instance.playerNameFadeOutDuration, on ? ((Action)delegate
			{
				Transform obj2 = healthbar;
				if (obj2 != null)
				{
					GameObject gameObject = ((Component)obj2).gameObject;
					if (gameObject != null)
					{
						gameObject.SetActive(false);
					}
				}
				fadeActive.Remove(__instance);
			}) : ((Action)delegate
			{
				fadeActive.Remove(__instance);
			})));
		}
	}

	private static readonly Dictionary<TMP_FontAsset, TMP_FontAsset> fontToShaderFont = new Dictionary<TMP_FontAsset, TMP_FontAsset>();

	private static readonly HashSet<PlayerNameTag> fadeActive = new HashSet<PlayerNameTag>();

	private static Func<bool, TMP_FontAsset> GetFont;

	private static Func<string, bool, TMP_FontAsset> FontFromName;

	private static Material HealthBarMaterial;

	private static void AddHealthbarText(Transform UIBAR, PlayerController controller)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Expected O, but got Unknown
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Invalid comparison between Unknown and I4
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: 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_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Invalid comparison between Unknown and I4
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)UIBAR == (Object)null)
		{
			return;
		}
		GameObject val = new GameObject("HealthText");
		val.transform.SetParent(UIBAR, false);
		TextMeshPro val2 = val.AddComponent<TextMeshPro>();
		if ((int)controller.controllerType == 1)
		{
			val.transform.localPosition = new Vector3(-1.01f, 0.01f, 0f);
			val.transform.localRotation = Quaternion.Euler(63f, 270f, 0f);
			val.transform.localScale = new Vector3(0.015f, 0.007f, 0.015f);
			((TMP_Text)val2).fontSize = 36f;
		}
		else
		{
			Transform obj = ((Component)controller).transform.Find("NameTag");
			if (obj != null)
			{
				GameObject gameObject = ((Component)obj).gameObject;
				if (((gameObject != null) ? new bool?(gameObject.activeSelf) : null) == true)
				{
					val.SetActive(false);
				}
			}
			val.transform.localPosition = new Vector3(0f, -0.05f, 0f);
			val.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
			val.transform.localScale = new Vector3(-0.1f, 0.1f, 0.1f);
			val.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0f);
			((TMP_Text)val2).fontSize = 30f;
		}
		((TMP_Text)val2).text = controller.assignedPlayer.Data.HealthPoints.ToString();
		((TMP_Text)val2).alignment = (TextAlignmentOptions)514;
		if (GetFont == null)
		{
			return;
		}
		if ((int)controller.controllerType == 1)
		{
			((TMP_Text)val2).font = GetFont(arg: true);
			return;
		}
		TMP_FontAsset key = GetFont(arg: true);
		if (fontToShaderFont.TryGetValue(key, out var value))
		{
			((TMP_Text)val2).font = value;
			return;
		}
		TMP_FontAsset val4 = (((TMP_Text)val2).font = GetFont(arg: false));
		HealthBarMaterial.mainTexture = (Texture)(object)val4.atlasTexture;
		((TMP_Text)val2).fontMaterial = HealthBarMaterial;
		((TMP_Asset)val4).material = HealthBarMaterial;
		fontToShaderFont[key] = val4;
	}

	private static Transform GetHealthbarText(PlayerController controller)
	{
		object uI;
		if (controller == null)
		{
			uI = null;
		}
		else
		{
			Transform transform = ((Component)controller).transform;
			uI = ((transform != null) ? transform.Find("UI") : null);
		}
		return GetHealthbarText((Transform)uI, controller);
	}

	private static Transform GetHealthbarText(Transform UI, PlayerController controller)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		Transform healthbar = GetHealthbar(UI, (controller != null) ? new ControllerType?(controller.controllerType) : null);
		if (healthbar == null)
		{
			return null;
		}
		return healthbar.Find("HealthText");
	}

	private static Transform GetHealthbar(Transform UI, ControllerType? controllerType)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)UI == (Object)null)
		{
			return null;
		}
		if (!controllerType.HasValue)
		{
			return null;
		}
		if (controllerType == (ControllerType?)1)
		{
			Transform child = UI.GetChild(0);
			Transform val = ((child != null) ? child.GetChild(1) : null);
			if (((val != null) ? ((Object)val).name : null) != "Local UI Bar")
			{
				MelonLogger.Warning("Could not get Local Healthbar via GetChild");
				val = UI.Find("LocalUI/Local UI Bar");
				if ((Object)(object)val == (Object)null)
				{
					MelonLogger.Error("Could not get Local Healthbar via Find");
					return null;
				}
			}
			return val;
		}
		if (controllerType == (ControllerType?)2)
		{
			Transform val2 = UI.GetChild(1);
			if (((val2 != null) ? ((Object)val2).name : null) != "RemoteUI")
			{
				MelonLogger.Warning("Could not get RemoteUI via GetChild");
				val2 = UI.Find("RemoteUI");
				if ((Object)(object)val2 == (Object)null)
				{
					MelonLogger.Error("Could not get RemoteUI via Find");
					return null;
				}
			}
			return val2;
		}
		MelonLogger.Warning($"Unknown controller type: {controllerType}");
		return null;
	}

	private static IEnumerator FadeText(TextMeshPro text, float endAlpha, float PlayerNameFadeOutDuration, Action onEnd = null)
	{
		if ((Object)(object)text == (Object)null)
		{
			onEnd?.Invoke();
			yield break;
		}
		float startAlpha = ((TMP_Text)text).alpha;
		if (PlayerNameFadeOutDuration <= 0f || Mathf.Approximately(startAlpha, endAlpha))
		{
			((TMP_Text)text).alpha = endAlpha;
			onEnd?.Invoke();
			yield break;
		}
		float elapsed = 0f;
		while (elapsed < PlayerNameFadeOutDuration)
		{
			elapsed += Time.deltaTime;
			((TMP_Text)text).alpha = Mathf.Lerp(startAlpha, endAlpha, Mathf.Clamp01(elapsed / PlayerNameFadeOutDuration));
			yield return null;
		}
		((TMP_Text)text).alpha = endAlpha;
		onEnd?.Invoke();
	}

	public override void OnInitializeMelon()
	{
		HealthBarMaterial = Calls.LoadAssetFromStream<Material>((MelonMod)(object)this, "HealthDisplayWithFont.healthbartextshader", "healthbartext");
		((Object)HealthBarMaterial).hideFlags = (HideFlags)61;
		((Object)HealthBarMaterial.shader).hideFlags = (HideFlags)61;
		Type type = ((object)MelonBase.FindMelon("Fontifier", "ninjaguardian"))?.GetType();
		if ((object)type != null && type != null)
		{
			(GetFont, FontFromName) = ((Func<bool, TMP_FontAsset>, Func<string, bool, TMP_FontAsset>))type.GetMethod("RegisterModCopy", BindingFlags.Static | BindingFlags.Public)?.Invoke(null, new object[2]
			{
				((MelonBase)this).Info.Name,
				new EventHandler<EventArgs>(FontChanged)
			});
		}
	}

	private static void FontChanged(object sender, EventArgs args)
	{
		//IL_025a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Invalid comparison between Unknown and I4
		TMP_FontAsset val = (TMP_FontAsset)FontFromName(((dynamic)args).Value, true);
		TMP_FontAsset val2;
		if (fontToShaderFont.TryGetValue(val, out var value))
		{
			val2 = value;
		}
		else
		{
			val2 = (TMP_FontAsset)FontFromName(((dynamic)args).Value, false);
			HealthBarMaterial.mainTexture = (Texture)(object)val2.atlasTexture;
			((TMP_Asset)val2).material = HealthBarMaterial;
			fontToShaderFont[val] = val2;
		}
		Enumerator<Player> enumerator = Singleton<PlayerManager>.instance.AllPlayers.GetEnumerator();
		while (enumerator.MoveNext())
		{
			Player current = enumerator.Current;
			Transform healthbarText = GetHealthbarText(current.Controller);
			TextMeshPro val3 = ((healthbarText != null) ? ((Component)healthbarText).GetComponent<TextMeshPro>() : null);
			if (val3 != null && (Object)(object)val3 != (Object)null)
			{
				if ((int)current.Controller.controllerType == 1)
				{
					((TMP_Text)val3).font = val;
					continue;
				}
				((TMP_Text)val3).font = val2;
				((TMP_Text)val3).fontMaterial = HealthBarMaterial;
			}
		}
	}
}