Decompiled source of TeamVitals v1.0.2

TeamVitals.dll

Decompiled 4 days 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.Logging;
using HarmonyLib;
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("TeamVitals")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+5cf153d318f4f64f1cd91f34a1f7a25416aa59a9")]
[assembly: AssemblyProduct("TeamVitals")]
[assembly: AssemblyTitle("TeamVitals")]
[assembly: AssemblyVersion("1.0.2.0")]
namespace TeamVitals;

[BepInPlugin("t0y9n.peak.teamvitals", "TeamVitals", "1.0.2")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string Guid = "t0y9n.peak.teamvitals";

	internal const string Version = "1.0.2";

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		if (!((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Show a status bar above other players' name tags. Read once at start; restart the game after changing it.").Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"TeamVitals 1.0.2 loaded. Enabled = false; nothing is patched.");
			return;
		}
		try
		{
			new Harmony("t0y9n.peak.teamvitals").CreateClassProcessor(typeof(NamePatch)).Patch();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"TeamVitals 1.0.2 loaded. Patched the player name tags.");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("TeamVitals: the player name tags were not patched; no bars are shown. " + ex.Message));
		}
	}
}
[HarmonyPatch(typeof(UIPlayerNames), "UpdateName")]
internal static class NamePatch
{
	private static bool errorLogged;

	private static void Postfix(UIPlayerNames __instance, int index)
	{
		VitalsBar vitalsBar = null;
		try
		{
			if (__instance.playerNameText == null || index < 0 || index >= __instance.playerNameText.Length)
			{
				return;
			}
			PlayerName val = __instance.playerNameText[index];
			if ((Object)(object)val == (Object)null || !((Component)val).gameObject.activeSelf)
			{
				return;
			}
			vitalsBar = ((Component)val).GetComponent<VitalsBar>();
			if ((Object)(object)vitalsBar != (Object)null && vitalsBar.Broken)
			{
				return;
			}
			Character val2 = (((Object)(object)val.characterInteractable != (Object)null) ? val.characterInteractable.character : null);
			if ((Object)(object)val2 == (Object)null || val2.refs == null || (Object)(object)val2.refs.afflictions == (Object)null || (Object)(object)val2.data == (Object)null)
			{
				if ((Object)(object)vitalsBar != (Object)null)
				{
					vitalsBar.Hide();
				}
				return;
			}
			if ((Object)(object)vitalsBar == (Object)null)
			{
				vitalsBar = VitalsBar.Create(val);
			}
			if ((Object)(object)vitalsBar != (Object)null)
			{
				vitalsBar.Show(val2);
			}
		}
		catch (Exception ex)
		{
			if ((Object)(object)vitalsBar != (Object)null)
			{
				vitalsBar.Broken = true;
				vitalsBar.Hide();
			}
			if (!errorLogged)
			{
				errorLogged = true;
				Plugin.Log.LogError((object)("TeamVitals: updating a bar failed; that bar is hidden and further errors are not logged. " + ex));
			}
		}
	}
}
internal sealed class VitalsBar : MonoBehaviour
{
	private sealed class Part
	{
		internal STATUSTYPE Type;

		internal bool IsPetrify;

		internal RectTransform Segment;

		internal Image Icon;
	}

	private const float Width = 110f;

	private const float Height = 10f;

	private const float Border = 1.5f;

	private const float IconSize = 14f;

	private const float Gap = 3f;

	private static bool unavailable;

	private static bool partsLogged;

	private const int WaitsBeforeWarning = 600;

	private static int waits;

	internal bool Broken;

	private readonly List<Part> parts = new List<Part>();

	private readonly List<GameObject> made = new List<GameObject>();

	private RectTransform root;

	private RectTransform health;

	private RectTransform icons;

	private RectTransform text;

	private float minWidth;

	private float[] values = new float[0];

	internal static VitalsBar Create(PlayerName name)
	{
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		if (unavailable)
		{
			return null;
		}
		StaminaBar val = (((Object)(object)GUIManager.instance != (Object)null) ? GUIManager.instance.bar : null);
		if ((Object)(object)name.text == (Object)null || (Object)(object)val == (Object)null || val.afflictions == null)
		{
			if (++waits == 600)
			{
				Plugin.Log.LogWarning((object)("TeamVitals: the base status bar or name text is still not ready after " + 600 + " tries; no bars yet."));
			}
			return null;
		}
		if ((Object)(object)val.fullBar == (Object)null || val.fullBar.sizeDelta.x <= 0f || val.afflictions.Length == 0)
		{
			GiveUp("the base status bar has no size or no affliction parts");
			return null;
		}
		VitalsBar vitalsBar = ((Component)name).gameObject.AddComponent<VitalsBar>();
		try
		{
			vitalsBar.Build(name, val);
			if (vitalsBar.parts.Count > 0)
			{
				return vitalsBar;
			}
			GiveUp("none of the base status bar's affliction parts could be read");
		}
		catch (Exception ex)
		{
			GiveUp("building a bar failed: " + ex.Message);
		}
		vitalsBar.Discard();
		return null;
	}

	private static void GiveUp(string reason)
	{
		unavailable = true;
		Plugin.Log.LogWarning((object)("TeamVitals: " + reason + "; no new bars are made."));
	}

	private void Discard()
	{
		foreach (GameObject item in made)
		{
			if ((Object)(object)item != (Object)null)
			{
				Object.Destroy((Object)(object)item);
			}
		}
		Object.Destroy((Object)(object)this);
	}

	private void Build(PlayerName name, StaminaBar source)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: 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_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0205: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: Unknown result type (might be due to invalid IL or missing references)
		//IL_0253: Unknown result type (might be due to invalid IL or missing references)
		//IL_0274: Unknown result type (might be due to invalid IL or missing references)
		//IL_0336: Unknown result type (might be due to invalid IL or missing references)
		//IL_033b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0372: Unknown result type (might be due to invalid IL or missing references)
		//IL_0377: Unknown result type (might be due to invalid IL or missing references)
		//IL_039a: Unknown result type (might be due to invalid IL or missing references)
		//IL_032d: Unknown result type (might be due to invalid IL or missing references)
		//IL_031f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0479: Unknown result type (might be due to invalid IL or missing references)
		//IL_047e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0407: Unknown result type (might be due to invalid IL or missing references)
		//IL_0413: Unknown result type (might be due to invalid IL or missing references)
		text = ((TMP_Text)name.text).rectTransform;
		minWidth = 110f * source.minAfflictionWidth / source.fullBar.sizeDelta.x;
		List<string> list = new List<string>();
		Image backing = source.backing;
		if ((Object)(object)backing == (Object)null || (Object)(object)backing.sprite == (Object)null)
		{
			list.Add("backing sprite");
		}
		float num;
		if ((Object)(object)backing != (Object)null)
		{
			Rect rect = ((Graphic)backing).rectTransform.rect;
			if (((Rect)(ref rect)).height > 0f)
			{
				rect = ((Graphic)backing).rectTransform.rect;
				num = ((Rect)(ref rect)).height;
				goto IL_00a2;
			}
		}
		num = 10f;
		goto IL_00a2;
		IL_00a2:
		float num2 = num / 10f;
		Image val = FillOf((Transform)(object)source.staminaBar);
		if ((Object)(object)val == (Object)null)
		{
			list.Add("stamina color");
		}
		Color color = (((Object)(object)val != (Object)null) ? ((Graphic)val).color : Color.white);
		color.a = 1f;
		Image val2 = FindImage(((Component)source).transform, "OutlineMask/Outline/OutlineImage");
		if ((Object)(object)val2 == (Object)null)
		{
			list.Add("outline color");
		}
		Color color2 = (Color)(((Object)(object)val2 != (Object)null) ? ((Graphic)val2).color : new Color(0f, 0f, 0f, 0.85f));
		color2.a = 1f;
		Color defaultBackingColor = source.defaultBackingColor;
		defaultBackingColor.a = 1f;
		root = NewRect("TeamVitals", ((Component)this).transform, new Vector2(110f, 10f));
		CopyShape(NewImage("Rim", (Transform)(object)root, new Vector2(113f, 13f), color2), backing, num2);
		Image obj = NewImage("Back", (Transform)(object)root, new Vector2(110f, 10f), defaultBackingColor);
		CopyShape(obj, backing, num2);
		((Component)obj).gameObject.AddComponent<Mask>().showMaskGraphic = true;
		RectTransform rectTransform = ((Graphic)obj).rectTransform;
		Image val3 = NewImage("Health", (Transform)(object)rectTransform, new Vector2(110f, 10f), color);
		CopyShape(val3, val, num2);
		health = ((Graphic)val3).rectTransform;
		health.pivot = new Vector2(0f, 0.5f);
		health.anchoredPosition = new Vector2(-55f, 0f);
		icons = NewRect("TeamVitals icons", ((Component)this).transform, new Vector2(110f, 14f));
		for (int i = 0; i < source.afflictions.Length; i++)
		{
			BarAffliction val4 = source.afflictions[i];
			if ((Object)(object)val4 == (Object)null)
			{
				list.Add("part " + i);
				continue;
			}
			Image val5 = FillOf((Transform)(object)val4.rtf) ?? FillOf(((Component)val4).transform);
			if ((Object)(object)val5 == (Object)null)
			{
				list.Add(((object)Unsafe.As<STATUSTYPE, STATUSTYPE>(ref val4.afflictionType)/*cast due to .constrained prefix*/).ToString() + " color");
			}
			Color color3 = (((Object)(object)val5 != (Object)null) ? ((Graphic)val5).color : (((Object)(object)val4.icon != (Object)null) ? ((Graphic)val4.icon).color : Color.gray));
			color3.a = 1f;
			Image obj2 = NewImage("Segment " + ((object)Unsafe.As<STATUSTYPE, STATUSTYPE>(ref val4.afflictionType)/*cast due to .constrained prefix*/).ToString(), (Transform)(object)rectTransform, new Vector2(0f, 10f), color3);
			CopyTiles(obj2, val5, num2);
			RectTransform rectTransform2 = ((Graphic)obj2).rectTransform;
			rectTransform2.pivot = new Vector2(1f, 0.5f);
			((Component)rectTransform2).gameObject.SetActive(false);
			Image val6 = null;
			if ((Object)(object)val4.icon != (Object)null && (Object)(object)val4.icon.sprite != (Object)null)
			{
				val6 = NewImage("Icon " + ((object)Unsafe.As<STATUSTYPE, STATUSTYPE>(ref val4.afflictionType)/*cast due to .constrained prefix*/).ToString(), (Transform)(object)icons, new Vector2(14f, 14f), ((Graphic)val4.icon).color);
				val6.sprite = val4.icon.sprite;
				val6.preserveAspect = true;
				((Component)val6).gameObject.SetActive(false);
			}
			else
			{
				list.Add(((object)Unsafe.As<STATUSTYPE, STATUSTYPE>(ref val4.afflictionType)/*cast due to .constrained prefix*/).ToString() + " icon");
			}
			parts.Add(new Part
			{
				Type = val4.afflictionType,
				IsPetrify = val4.isPetrify,
				Segment = rectTransform2,
				Icon = val6
			});
		}
		values = new float[parts.Count];
		if (list.Count > 0 && !partsLogged)
		{
			partsLogged = true;
			Plugin.Log.LogWarning((object)("TeamVitals: some base status bar parts are incomplete (" + string.Join(", ", list) + "); they are shown without them."));
		}
	}

	internal void Hide()
	{
		if ((Object)(object)root != (Object)null)
		{
			((Component)root).gameObject.SetActive(false);
		}
		if ((Object)(object)icons != (Object)null)
		{
			((Component)icons).gameObject.SetActive(false);
		}
	}

	internal void Show(Character character)
	{
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0241: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
		for (int i = 0; i < parts.Count; i++)
		{
			values[i] = (parts[i].IsPetrify ? ((float)character.data.petrifyAmount / 100f) : character.refs.afflictions.GetCurrentStatus(parts[i].Type));
		}
		float statusSum = character.refs.afflictions.statusSum;
		Transform transform = ((Component)this).transform;
		RectTransform obj = text;
		Rect rect = text.rect;
		float x = ((Rect)(ref rect)).center.x;
		rect = text.rect;
		Vector3 val = transform.InverseTransformPoint(((Transform)obj).TransformPoint(new Vector3(x, ((Rect)(ref rect)).yMax, 0f)));
		((Transform)root).localPosition = val + new Vector3(0f, 9.5f, 0f);
		((Transform)icons).localPosition = val + new Vector3(0f, 26f, 0f);
		float num = 55f;
		float num2 = 0f;
		int num3 = 0;
		for (int j = 0; j < parts.Count; j++)
		{
			Part part = parts[j];
			bool flag = values[j] > 0.01f;
			bool flag2 = flag && num2 < 110f;
			((Component)part.Segment).gameObject.SetActive(flag2);
			if (flag2)
			{
				float num4 = Mathf.Min(Mathf.Max(110f * values[j], minWidth), 110f - num2);
				part.Segment.sizeDelta = new Vector2(num4, 10f);
				part.Segment.anchoredPosition = new Vector2(num - num2, 0f);
				num2 += num4;
			}
			if ((Object)(object)part.Icon != (Object)null)
			{
				((Component)part.Icon).gameObject.SetActive(flag);
				if (flag)
				{
					num3++;
				}
			}
		}
		health.sizeDelta = new Vector2(Mathf.Clamp(110f * (1f - statusSum), 0f, 110f - num2), 10f);
		float num5 = (0f - ((float)num3 * 14f + (float)Mathf.Max(0, num3 - 1) * 3f)) / 2f + 7f;
		foreach (Part part2 in parts)
		{
			if (!((Object)(object)part2.Icon == (Object)null) && ((Component)part2.Icon).gameObject.activeSelf)
			{
				((Graphic)part2.Icon).rectTransform.anchoredPosition = new Vector2(num5, 0f);
				num5 += 17f;
			}
		}
		((Component)root).gameObject.SetActive(true);
		((Component)icons).gameObject.SetActive(true);
	}

	private static void CopyShape(Image target, Image source, float cornerScale)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Invalid comparison between Unknown and I4
		if (!((Object)(object)source == (Object)null) && !((Object)(object)source.sprite == (Object)null) && (int)source.type == 1)
		{
			target.sprite = source.sprite;
			target.type = (Type)1;
			target.pixelsPerUnitMultiplier = source.pixelsPerUnitMultiplier * cornerScale;
		}
	}

	private static void CopyTiles(Image target, Image source, float scale)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Invalid comparison between Unknown and I4
		if (!((Object)(object)source == (Object)null) && !((Object)(object)source.sprite == (Object)null) && (int)source.type == 2)
		{
			target.sprite = source.sprite;
			target.type = (Type)2;
			target.pixelsPerUnitMultiplier = source.pixelsPerUnitMultiplier * scale;
		}
	}

	private static Image FillOf(Transform part)
	{
		if ((Object)(object)part == (Object)null)
		{
			return null;
		}
		Image val = FindImage(part, "Fill");
		if (!((Object)(object)val != (Object)null))
		{
			return ((Component)part).GetComponent<Image>();
		}
		return val;
	}

	private static Image FindImage(Transform parent, string path)
	{
		Transform val = (((Object)(object)parent != (Object)null) ? parent.Find(path) : null);
		if (!((Object)(object)val != (Object)null))
		{
			return null;
		}
		return ((Component)val).GetComponent<Image>();
	}

	private RectTransform NewRect(string name, Transform parent, Vector2 size)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: 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_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		GameObject val = new GameObject(name, new Type[1] { typeof(RectTransform) });
		made.Add(val);
		RectTransform val2 = (RectTransform)val.transform;
		((Transform)val2).SetParent(parent, false);
		Vector2 val3 = default(Vector2);
		((Vector2)(ref val3))..ctor(0.5f, 0.5f);
		val2.anchorMax = val3;
		val2.anchorMin = val3;
		val2.sizeDelta = size;
		return val2;
	}

	private Image NewImage(string name, Transform parent, Vector2 size, Color color)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		Image obj = ((Component)NewRect(name, parent, size)).gameObject.AddComponent<Image>();
		((Graphic)obj).color = color;
		((Graphic)obj).raycastTarget = false;
		return obj;
	}
}