Decompiled source of DynamicHealthIcon v1.0.0

DynamicHealthicon.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("PhotonRealtime")]
[assembly: IgnoresAccessChecksTo("PhotonUnityNetworking")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: AssemblyCompany("Catburger")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("R.E.P.O mod to make the health icon dynamic")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+39f4c10aab2cdbf643757623324359e2b334bd6e")]
[assembly: AssemblyProduct("Dynamic health icon")]
[assembly: AssemblyTitle("DynamicHealthicon")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DynamicHealthIcon
{
	[BepInPlugin("DynamicHealthicon", "Dynamic health icon", "1.0.0")]
	public class DynamicHealthIcon : BaseUnityPlugin
	{
		public Image? plusTexture;

		public string defaultPath = "Resources";

		public string overridesPath = "Overrides";

		public readonly Dictionary<int, (Sprite sprite, Color color)> sprites = new Dictionary<int, (Sprite, Color)>();

		private Color? _dividerColor;

		internal static DynamicHealthIcon Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			//IL_002c: Expected O, but got Unknown
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			Instance = this;
			if (Harmony == null)
			{
				Harmony val = new Harmony("at.kibbyc.DynamicHealthicon");
				Harmony val2 = val;
				Harmony = val;
			}
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			if (directoryName == null)
			{
				Logger.LogFatal((object)"The current path is null");
				return;
			}
			LoadTextures(Path.Join((ReadOnlySpan<char>)directoryName, (ReadOnlySpan<char>)defaultPath));
			LoadTextures(Path.Join((ReadOnlySpan<char>)directoryName, (ReadOnlySpan<char>)overridesPath));
			Harmony.PatchAll(typeof(DynamicHealthIcon));
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded in {stopwatch.ElapsedMilliseconds}ms!");
		}

		public void LoadTextures(string current)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_008a: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			if (!Directory.Exists(current))
			{
				Directory.CreateDirectory(current);
			}
			string[] files = Directory.GetFiles(current);
			foreach (string text in files)
			{
				if ((text.EndsWith(".png") || text.EndsWith(".jpg") || text.EndsWith(".jpeg")) && int.TryParse(Path.GetFileNameWithoutExtension(text), out var result))
				{
					Texture2D val = new Texture2D(1, 1);
					ImageConversion.LoadImage(val, File.ReadAllBytes(text));
					Sprite item = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
					sprites[result] = (item, GetSpriteColor(val));
				}
			}
		}

		private Color GetSpriteColor(Texture2D texture)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			Color[] pixels = texture.GetPixels();
			float num = 0f;
			float num2 = 0f;
			float num3 = 0f;
			float num4 = 0f;
			Color[] array = pixels;
			foreach (Color val in array)
			{
				if (!((double)val.a < 0.5) && !((double)(val.r + val.g + val.b) < 0.3))
				{
					num += val.r;
					num2 += val.g;
					num3 += val.b;
					num4 += 1f;
				}
			}
			float num5 = default(float);
			float num6 = default(float);
			float num7 = default(float);
			Color.RGBToHSV(new Color(num / num4, num2 / num4, num3 / num4), ref num5, ref num6, ref num7);
			if ((double)num7 < 0.2)
			{
				num7 = 0.2f;
			}
			return Color.HSVToRGB(num5, num6, num7);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HealthUI), "Start")]
		public static void FetchPlusObjectPatch(HealthUI __instance)
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			Transform val = ((Component)__instance).transform.Find("Plus");
			Image val2 = default(Image);
			if (!Object.op_Implicit((Object)(object)val))
			{
				Logger.LogFatal((object)"DynamicHealthIcon couldn't load because the hierarchy of the HealthUI must have changed or something");
			}
			else if (((Component)val).TryGetComponent<Image>(ref val2))
			{
				Instance.plusTexture = val2;
				((Graphic)Instance.plusTexture).color = new Color(1f, 1f, 1f);
			}
			else
			{
				Logger.LogFatal((object)"DynamicHealthIcon couldn't load because the image on the plus object couldnt be found");
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(HealthUI), "Update")]
		private static IEnumerable<CodeInstruction> Patch2(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).SearchForward((Func<CodeInstruction, bool>)((CodeInstruction ci) => CodeInstructionExtensions.Calls(ci, AccessTools.PropertyGetter(typeof(Color), "white")))).Insert((CodeInstruction[])(object)new CodeInstruction[1]
			{
				new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(DynamicHealthIcon), "HealthChanged", (Type[])null, (Type[])null))
			}).SearchForward((Func<CodeInstruction, bool>)((CodeInstruction ci) => ci.opcode == OpCodes.Ldstr))
				.SetInstruction(new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(DynamicHealthIcon), "GetDividerColor", (Type[])null, (Type[])null)))
				.InstructionEnumeration();
		}

		public static string GetDividerColor()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			if (!Instance._dividerColor.HasValue)
			{
				return "/";
			}
			return "<b><color=#" + ColorUtility.ToHtmlStringRGB(Instance._dividerColor.Value) + ">/</color></b>";
		}

		public static void HealthChanged()
		{
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: 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_0113: 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)
			HealthUI instance = HealthUI.instance;
			PlayerHealth playerHealth = instance.playerHealth;
			if (!Object.op_Implicit((Object)(object)playerHealth))
			{
				Logger.LogDebug((object)"playerhealth is null");
				return;
			}
			Image val = Instance.plusTexture;
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)"PlusTexture not initialized");
				return;
			}
			int num = (int)((float)playerHealth.health / (float)playerHealth.maxHealth * 100f);
			int num2 = -1;
			foreach (KeyValuePair<int, (Sprite, Color)> sprite2 in Instance.sprites)
			{
				if (num2 <= sprite2.Key && sprite2.Key < num)
				{
					num2 = sprite2.Key;
				}
			}
			if (num2 == -1)
			{
				Logger.LogError((object)$"No image found for the health you have \"{num}\"");
				return;
			}
			var (sprite, val2) = Instance.sprites[num2];
			val.sprite = sprite;
			((SemiUI)instance).originalTextColor = val2;
			((Graphic)instance.textMaxHealth).color = val2;
			float num3 = default(float);
			float num4 = default(float);
			float num5 = default(float);
			Color.RGBToHSV(val2, ref num3, ref num4, ref num5);
			num4 = Mathf.Clamp(num4 + 0.2f, 0.2f, 1f);
			num5 = Mathf.Clamp(num5 - 0.35f, 0.35f, 1f);
			Instance._dividerColor = Color.HSVToRGB(num3, num4, num5);
			((Graphic)val).SetMaterialDirty();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "DynamicHealthicon";

		public const string PLUGIN_NAME = "Dynamic health icon";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}