Decompiled source of Barrier HP Display v1.0.2

BepInEx/plugins/BarrierHPDisplay/BarrierHPDisplay.dll

Decompiled a day ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace JayBird.BarrierHPDisplay;

[BepInPlugin("jaybird.barrierhpdisplay", "Barrier HP Display", "1.0.2")]
public sealed class BarrierHPDisplay : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		Harmony harmony = new Harmony("jaybird.barrierhpdisplay");
		int num = 0;
		num += PatchIfPresent(harmony, "GetIconText", typeof(ShieldIconTextPatch));
		num += PatchIfPresent(harmony, "GetTooltipString", typeof(ShieldTooltipPatch));
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Barrier HP Display 1.0.2 loaded; display hooks=" + num));
	}

	private int PatchIfPresent(Harmony harmony, string methodName, Type patchType)
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		MethodInfo methodInfo = AccessTools.Method(typeof(StatusEffect), methodName, Type.EmptyTypes, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(patchType, "Postfix", (Type[])null, (Type[])null);
		if (methodInfo == null || methodInfo2 == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not find StatusEffect." + methodName + "; that display hook was skipped."));
			return 0;
		}
		try
		{
			MethodInfo methodInfo3 = methodInfo;
			HarmonyMethod val = new HarmonyMethod(methodInfo2);
			harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			return 1;
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not patch StatusEffect." + methodName + ": " + ex.Message));
			return 0;
		}
	}
}
internal static class ShieldIconTextPatch
{
	internal static void Postfix(StatusEffect __instance, ref string __result)
	{
		if (ShieldValues.TryRead(__instance, out var remaining, out var maximum))
		{
			string text = "HP " + Math.Ceiling(remaining).ToString("0") + "/" + Math.Ceiling(maximum).ToString("0");
			__result = ((!string.IsNullOrEmpty(__result)) ? (__result + " | " + text) : text);
		}
	}
}
internal static class ShieldTooltipPatch
{
	internal static void Postfix(StatusEffect __instance, ref string __result)
	{
		if (ShieldValues.TryRead(__instance, out var remaining, out var maximum))
		{
			string text = "Barrier HP: " + Math.Ceiling(remaining).ToString("0") + " / " + Math.Ceiling(maximum).ToString("0");
			__result = ((!string.IsNullOrEmpty(__result)) ? (__result + "\n" + text) : text);
		}
	}
}
internal static class ShieldValues
{
	internal static bool TryRead(StatusEffect effect, out float remaining, out float maximum)
	{
		remaining = 0f;
		maximum = 0f;
		if (effect == null || ((object)effect).GetType().Name != "SE_Shield")
		{
			return false;
		}
		try
		{
			object obj = ReadField(effect, "m_totalAbsorbDamage");
			object obj2 = ReadField(effect, "m_damage");
			if (obj == null || obj2 == null)
			{
				return false;
			}
			maximum = Convert.ToSingle(obj);
			float num = Convert.ToSingle(obj2);
			if (maximum <= 0f)
			{
				return false;
			}
			remaining = Math.Max(0f, maximum - num);
			return true;
		}
		catch
		{
			return false;
		}
	}

	private static object ReadField(object instance, string name)
	{
		FieldInfo fieldInfo = AccessTools.Field(instance.GetType(), name);
		return (!(fieldInfo == null)) ? fieldInfo.GetValue(instance) : null;
	}
}