Decompiled source of BuildingHealthDisplay v0.7.0

plugins/BuildingHealthDisplay.dll

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

[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.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.7.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("0.7.0.0")]
namespace BuildingHealthDisplay;

[BepInPlugin("cjayride.BuildingHealthDisplay", "Building Health Display", "0.7.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Hud), "UpdateCrosshair")]
	private static class UpdateCrosshair_Patch
	{
		private static void Postfix(Hud __instance, Player player)
		{
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_029e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0415: Unknown result type (might be due to invalid IL or missing references)
			//IL_0424: Unknown result type (might be due to invalid IL or missing references)
			//IL_042e: 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)
			if (!modEnabled.Value)
			{
				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) || component2 == null || !component2.IsValid())
			{
				return;
			}
			((Component)((Component)__instance.m_pieceHealthBar).transform.parent.Find("bkg")).gameObject.SetActive(showHealthBar.Value);
			((Component)((Component)__instance.m_pieceHealthBar).transform.parent.Find("darken")).gameObject.SetActive(showHealthBar.Value);
			((Component)__instance.m_pieceHealthBar).gameObject.SetActive(showHealthBar.Value);
			float healthPercentage = component.GetHealthPercentage();
			if (customHealthColors.Value)
			{
				__instance.m_pieceHealthBar.SetValue(component.GetHealthPercentage());
				if ((double)healthPercentage < 0.5)
				{
					__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 val = ((Transform)__instance.m_pieceHealthRoot).Find("_HealthText");
				if ((Object)(object)val == (Object)null)
				{
					val = Object.Instantiate<TMP_Text>(__instance.m_healthText, ((Component)__instance.m_pieceHealthRoot).transform).transform;
					((Transform)((Component)val).GetComponent<RectTransform>()).localEulerAngles = new Vector3(0f, 0f, -90f);
				}
				((Object)val).name = "_HealthText";
				((Component)val).GetComponent<TMP_Text>().text = string.Format(healthText.Value, Mathf.RoundToInt(component2.GetZDO().GetFloat("health", component.m_health)), Mathf.RoundToInt(component.m_health), Mathf.RoundToInt(healthPercentage * 100f));
				((Component)val).GetComponent<TMP_Text>().fontSize = healthTextSize.Value;
				((Component)val).GetComponent<TMP_Text>().maxVisibleCharacters = ((Component)val).GetComponent<TMP_Text>().text.Length;
				((Component)val).GetComponent<RectTransform>().anchoredPosition = new Vector2(healthTextPosition.Value.y, healthTextPosition.Value.x);
			}
			float value = Traverse.Create((object)component).Method("GetSupport", Array.Empty<object>()).GetValue<float>();
			float value2 = Traverse.Create((object)component).Method("GetMaxSupport", Array.Empty<object>()).GetValue<float>();
			if (showIntegrityText.Value && value2 >= value)
			{
				Transform val2 = ((Transform)__instance.m_pieceHealthRoot).Find("_IntegrityText");
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = Object.Instantiate<TMP_Text>(__instance.m_healthText, ((Component)__instance.m_pieceHealthRoot).transform).transform;
					((Transform)((Component)val2).GetComponent<RectTransform>()).localEulerAngles = new Vector3(0f, 0f, -90f);
				}
				((Object)val2).name = "_IntegrityText";
				((Component)val2).GetComponent<TMP_Text>().text = string.Format(integrityText.Value, Mathf.RoundToInt(value), Mathf.RoundToInt(value2), Mathf.RoundToInt(value / value2 * 100f));
				((Component)val2).GetComponent<TMP_Text>().fontSize = integrityTextSize.Value;
				((Component)val2).GetComponent<TMP_Text>().maxVisibleCharacters = ((Component)val2).GetComponent<TMP_Text>().text.Length;
				((Component)val2).GetComponent<RectTransform>().anchoredPosition = new Vector2(integrityTextPosition.Value.y, integrityTextPosition.Value.x);
			}
		}
	}

	[HarmonyPatch(typeof(WearNTear), "Highlight")]
	private static class WearNTear_Highlight_Patch
	{
		private static void Postfix(WearNTear __instance)
		{
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: 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_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || !customIntegrityColors.Value)
			{
				return;
			}
			float value = Traverse.Create((object)__instance).Method("GetSupport", Array.Empty<object>()).GetValue<float>();
			float value2 = Traverse.Create((object)__instance).Method("GetMaxSupport", Array.Empty<object>()).GetValue<float>();
			if (value < 0f || value2 < value)
			{
				return;
			}
			Color val = ((!(value / value2 >= 0.5f)) ? Color.Lerp(lowIntegrityColor.Value, midIntegrityColor.Value, value / value2 * 2f) : Color.Lerp(midIntegrityColor.Value, highIntegrityColor.Value, (value / value2 - 0.5f) * 2f));
			foreach (Renderer item in Traverse.Create((object)__instance).Method("GetHighlightRenderers", Array.Empty<object>()).GetValue<List<Renderer>>())
			{
				Material[] materials = item.materials;
				foreach (Material val2 in materials)
				{
					val2.SetColor("_EmissionColor", val * 0.4f);
					val2.color = val;
				}
			}
		}
	}

	private static readonly bool isDebug = true;

	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;

	public static int itemSize = 70;

	private void Awake()
	{
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0243: Unknown result type (might be due to invalid IL or missing references)
		//IL_0271: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b3: 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()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchAll((string)null);
		}
	}
}