Decompiled source of BuildingHealthDisplay v0.8.1

BuildingHealthDisplay.dll

Decompiled a week ago
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: AssemblyFileVersion("0.8.0.0")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.8.0.0")]
namespace BuildingHealthDisplay;

[BepInPlugin("cjayride.BuildingHealthDisplay", "Building Health Display", "0.8.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Hud), "UpdateCrosshair")]
	private static class UpdateCrosshair_Patch
	{
		private static void Postfix(Hud __instance, Player player)
		{
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0410: Unknown result type (might be due to invalid IL or missing references)
			//IL_041f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0429: Unknown result type (might be due to invalid IL or missing references)
			//IL_0371: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || (Object)(object)player == (Object)null || (Object)(object)__instance.m_pieceHealthBar == (Object)null || (Object)(object)__instance.m_pieceHealthRoot == (Object)null)
			{
				return;
			}
			Piece hoveringPiece = player.GetHoveringPiece();
			if (!Object.op_Implicit((Object)(object)hoveringPiece))
			{
				return;
			}
			WearNTear component = ((Component)hoveringPiece).GetComponent<WearNTear>();
			ZNetView component2 = ((Component)hoveringPiece).GetComponent<ZNetView>();
			if (!Object.op_Implicit((Object)(object)component) || (Object)(object)component2 == (Object)null || !component2.IsValid())
			{
				return;
			}
			Transform parent = ((Component)__instance.m_pieceHealthBar).transform.parent;
			Transform val = parent.Find("bkg");
			if (Object.op_Implicit((Object)(object)val))
			{
				((Component)val).gameObject.SetActive(showHealthBar.Value);
			}
			Transform val2 = parent.Find("darken");
			if (Object.op_Implicit((Object)(object)val2))
			{
				((Component)val2).gameObject.SetActive(showHealthBar.Value);
			}
			((Component)__instance.m_pieceHealthBar).gameObject.SetActive(showHealthBar.Value);
			float healthPercentage = component.GetHealthPercentage();
			if (customHealthColors.Value)
			{
				__instance.m_pieceHealthBar.SetValue(healthPercentage);
				if (healthPercentage < 0.5f)
				{
					__instance.m_pieceHealthBar.SetColor(Color.Lerp(lowColor.Value, midColor.Value, healthPercentage * 2f));
				}
				else
				{
					__instance.m_pieceHealthBar.SetColor(Color.Lerp(midColor.Value, highColor.Value, (healthPercentage - 0.5f) * 2f));
				}
			}
			if (showHealthText.Value)
			{
				Transform val3 = ((Transform)__instance.m_pieceHealthRoot).Find("_HealthText");
				if ((Object)(object)val3 == (Object)null && Object.op_Implicit((Object)(object)__instance.m_healthText))
				{
					val3 = Object.Instantiate<TMP_Text>(__instance.m_healthText, (Transform)(object)__instance.m_pieceHealthRoot).transform;
					((Transform)((Component)val3).GetComponent<RectTransform>()).localEulerAngles = new Vector3(0f, 0f, -90f);
				}
				if (Object.op_Implicit((Object)(object)val3))
				{
					((Object)val3).name = "_HealthText";
					ZDO zDO = component2.GetZDO();
					float num = ((zDO != null) ? zDO.GetFloat(ZDOVars.s_health, component.m_health) : component.m_health);
					TMP_Text component3 = ((Component)val3).GetComponent<TMP_Text>();
					component3.text = string.Format(healthText.Value, Mathf.RoundToInt(num), Mathf.RoundToInt(component.m_health), Mathf.RoundToInt(healthPercentage * 100f));
					component3.fontSize = healthTextSize.Value;
					component3.maxVisibleCharacters = component3.text.Length;
					((Component)val3).GetComponent<RectTransform>().anchoredPosition = new Vector2(healthTextPosition.Value.y, healthTextPosition.Value.x);
				}
			}
			if (!showIntegrityText.Value)
			{
				return;
			}
			float value = Traverse.Create((object)component).Method("GetSupport", new object[0]).GetValue<float>();
			float value2 = Traverse.Create((object)component).Method("GetMaxSupport", new object[0]).GetValue<float>();
			if (value2 >= value)
			{
				Transform val4 = ((Transform)__instance.m_pieceHealthRoot).Find("_IntegrityText");
				if ((Object)(object)val4 == (Object)null && Object.op_Implicit((Object)(object)__instance.m_healthText))
				{
					val4 = Object.Instantiate<TMP_Text>(__instance.m_healthText, (Transform)(object)__instance.m_pieceHealthRoot).transform;
					((Transform)((Component)val4).GetComponent<RectTransform>()).localEulerAngles = new Vector3(0f, 0f, -90f);
				}
				if (Object.op_Implicit((Object)(object)val4))
				{
					((Object)val4).name = "_IntegrityText";
					TMP_Text component4 = ((Component)val4).GetComponent<TMP_Text>();
					component4.text = string.Format(integrityText.Value, Mathf.RoundToInt(value), Mathf.RoundToInt(value2), Mathf.RoundToInt(value / value2 * 100f));
					component4.fontSize = integrityTextSize.Value;
					component4.maxVisibleCharacters = component4.text.Length;
					((Component)val4).GetComponent<RectTransform>().anchoredPosition = new Vector2(integrityTextPosition.Value.y, integrityTextPosition.Value.x);
				}
			}
		}
	}

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> showHealthBar;

	public static ConfigEntry<bool> showHealthText;

	public static ConfigEntry<bool> showIntegrityText;

	public static ConfigEntry<bool> customHealthColors;

	public static ConfigEntry<bool> customIntegrityColors;

	public static ConfigEntry<string> healthText;

	public static ConfigEntry<string> integrityText;

	public static ConfigEntry<Vector2> healthTextPosition;

	public static ConfigEntry<Vector2> integrityTextPosition;

	public static ConfigEntry<int> healthTextSize;

	public static ConfigEntry<int> integrityTextSize;

	public static ConfigEntry<Color> lowColor;

	public static ConfigEntry<Color> midColor;

	public static ConfigEntry<Color> highColor;

	public static ConfigEntry<Color> lowIntegrityColor;

	public static ConfigEntry<Color> midIntegrityColor;

	public static ConfigEntry<Color> highIntegrityColor;

	private void Awake()
	{
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0242: Unknown result type (might be due to invalid IL or missing references)
		//IL_0270: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ac: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		showHealthBar = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowHealthBar", true, "Show health bar?");
		showHealthText = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowHealthText", true, "Show health text?");
		showIntegrityText = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowIntegrityText", true, "Show integrity text?");
		customHealthColors = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "customHealthColors", true, "Use custom health colors?");
		customIntegrityColors = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "CustomIntegrityColors", true, "Use custom integrity colors?");
		lowColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "LowColor", Color.red, "Color used for low health.");
		midColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "MidColor", Color.yellow, "Color used for mid health.");
		highColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "HighColor", Color.green, "Color used for high health.");
		lowIntegrityColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "LowIntegrityColor", Color.red, "Color used for low integrity.");
		midIntegrityColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "MidIntegrityColor", Color.yellow, "Color used for mid integrity.");
		highIntegrityColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "HighIntegrityColor", Color.green, "Color used for high integrity.");
		healthText = ((BaseUnityPlugin)this).Config.Bind<string>("General", "HealthText", "{0}/{1} ({2}%)", "Health text. {0} is replaced by current health. {1} is replaced by max health. {2} is replaced by percentage health.");
		integrityText = ((BaseUnityPlugin)this).Config.Bind<string>("General", "IntegrityText", "{0}/{1} ({2}%)", "Integrity text. {0} is replaced by current integrity. {1} is replaced by max integrity. {2} is replaced by percentage integrity.");
		healthTextSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "HealthTextSize", 18, "Health text size.");
		integrityTextSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "IntegrityTextSize", 18, "Integrity text size.");
		healthTextPosition = ((BaseUnityPlugin)this).Config.Bind<Vector2>("General", "HealthTextPosition", new Vector2(0f, 40f), "Health text position offset.");
		integrityTextPosition = ((BaseUnityPlugin)this).Config.Bind<Vector2>("General", "IntegrityTextPosition", new Vector2(0f, -40f), "Integrity text position offset.");
		if (modEnabled.Value)
		{
			harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
			harmony.PatchAll();
		}
	}

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