Decompiled source of DamageOverlay v1.0.2

DamageOverlay.dll

Decompiled 6 months ago
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("DamageOverlay")]
[assembly: AssemblyDescription("https://valheim.thunderstore.io/package/blacks7ar/DamageOverlay/")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("blacks7ar")]
[assembly: AssemblyProduct("DamageOverlay")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5740D0B6-1440-4AD5-A446-075883B23676")]
[assembly: AssemblyFileVersion("1.0.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
namespace DamageOverlay;

[BepInPlugin("blacks7ar.DamageOverlay", "DamageOverlay", "1.0.2")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch]
	public static class Patches
	{
		private static float _durationTimer;

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Hud), "Awake")]
		public static void Awake_Prefix(Hud __instance)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: 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_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			__instance.m_damageScreen.sprite = _damageOverlay;
			Color color = ((Graphic)__instance.m_damageScreen).color;
			((Color)(ref color))..ctor(color.r, color.g, color.b, 0f);
			((Graphic)__instance.m_damageScreen).color = color;
			GameObject gameObject;
			(gameObject = ((Component)__instance.m_damageScreen).gameObject).SetActive(true);
			_newDamageScreenObject = new GameObject("extraDamageScreen");
			_newDamageScreenObject.transform.SetParent(gameObject.transform);
			_newDamageScreenObject.transform.localPosition = new Vector3(0f, 0f, 0f);
			_newDamageScreenObject.transform.localScale = ((Component)__instance.m_damageScreen).gameObject.transform.localScale;
			_newDamageScreenObject.layer = 5;
			RectTransform obj = _newDamageScreenObject.AddComponent<RectTransform>();
			((Transform)obj).localPosition = new Vector3(0f, 0f, 0f);
			obj.anchorMin = new Vector2(0f, 0f);
			obj.anchorMax = new Vector2(1f, 1f);
			obj.pivot = new Vector2(0.5f, 0.5f);
			((Transform)obj).localScale = new Vector3(1f, 1f, 1f);
			_newDamageScreenObject.AddComponent<CanvasRenderer>().cullTransparentMesh = false;
			_newDamageScreen = _newDamageScreenObject.AddComponent<Image>();
			((Graphic)_newDamageScreen).color = new Color(255f, 0f, 0f, 0f);
			((Component)_newDamageScreen).gameObject.SetActive(true);
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Hud), "DamageFlash")]
		public static void DamageFlash_Prefix(Hud __instance)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			_durationTimer = 0f;
			Color color = ((Graphic)__instance.m_damageScreen).color;
			Color color2 = ((Graphic)_newDamageScreen).color;
			((Color)(ref color))..ctor(color.r, color.g, color.b, 1f);
			((Graphic)__instance.m_damageScreen).color = color;
			((Color)(ref color2))..ctor(color2.r, color2.g, color2.b, 0.1f);
			((Graphic)_newDamageScreen).color = color2;
			((Component)__instance.m_damageScreen).gameObject.SetActive(true);
			((Component)_newDamageScreen).gameObject.SetActive(true);
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Hud), "UpdateDamageFlash")]
		public static void UpdateDamageFlash_Prefix(Hud __instance, float dt)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: 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_00a9: 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_00d3: 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)
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				return;
			}
			Color color = ((Graphic)__instance.m_damageScreen).color;
			Color color2 = ((Graphic)_newDamageScreen).color;
			if (!(((Graphic)__instance.m_damageScreen).color.a > 0f))
			{
				return;
			}
			if ((double)((Character)Player.m_localPlayer).GetHealthPercentage() < 0.2)
			{
				((Color)(ref color))..ctor(color.r, color.g, color.b, 1f);
				((Graphic)__instance.m_damageScreen).color = color;
				((Component)__instance.m_damageScreen).gameObject.SetActive(true);
			}
			_durationTimer += dt;
			if (_durationTimer > 10f)
			{
				color.a = Mathf.MoveTowards(color.a, 0f, dt * 0.05f);
				((Graphic)__instance.m_damageScreen).color = color;
				color2.a = Mathf.MoveTowards(color2.a, 0f, dt * 4f);
				((Graphic)_newDamageScreen).color = color2;
				if (((Graphic)_newDamageScreen).color.a <= 0f)
				{
					((Component)_newDamageScreen).gameObject.SetActive(false);
				}
			}
		}
	}

	private const string modGUID = "blacks7ar.DamageOverlay";

	public const string modName = "DamageOverlay";

	public const string modAuthor = "blacks7ar";

	public const string modVersion = "1.0.2";

	public const string modLink = "https://valheim.thunderstore.io/package/blacks7ar/DamageOverlay/";

	private static readonly Harmony _harmony = new Harmony("blacks7ar.DamageOverlay");

	private static AssetBundle _overlayBundle;

	private static Sprite _damageOverlay;

	private static GameObject _newDamageScreenObject;

	private static Image _newDamageScreen;

	public void Awake()
	{
		_overlayBundle = GetAssetBundleFromResources("overlaybundle");
		_damageOverlay = _overlayBundle.LoadAsset<Sprite>("Assets/CustomAssets/damageoverlay.png");
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		_harmony.PatchAll(executingAssembly);
	}

	private static AssetBundle GetAssetBundleFromResources(string filename)
	{
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string name = executingAssembly.GetManifestResourceNames().Single((string x) => x.EndsWith(filename));
		using Stream stream = executingAssembly.GetManifestResourceStream(name);
		return AssetBundle.LoadFromStream(stream);
	}
}