Decompiled source of Death Count v1.0.1

DeathCount.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
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.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Ara")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DeathCount")]
[assembly: AssemblyTitle("DeathCount")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 DeathCount
{
	[BepInPlugin("Ara.DeathCount", "DeathCount", "1.0")]
	public class DeathCount : BaseUnityPlugin
	{
		private int totalDeaths = 0;

		private float deathCooldown = 0f;

		private const float DEATH_COOLDOWN_TIME = 2f;

		private GUIStyle deathCountStyle;

		private GUIStyle debugStyle;

		private Color deathCountColor = new Color(1f, 0.5f, 0f);

		private bool forceDisplay = false;

		private float uiUpdateTimer = 0f;

		private int uiCallCount = 0;

		internal static DeathCount Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

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

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			totalDeaths = 0;
			SceneManager.sceneLoaded += OnSceneLoaded;
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			Logger.LogInfo((object)("Scene loaded: " + ((Scene)(ref scene)).name));
			if (((Scene)(ref scene)).name.StartsWith("Level - ") && !((Scene)(ref scene)).name.Contains("Lobby"))
			{
				Logger.LogInfo((object)("POTENTIAL GAME LEVEL LOADED: " + ((Scene)(ref scene)).name));
				uiCallCount = 0;
				uiUpdateTimer = 0f;
			}
		}

		internal void Patch()
		{
			//IL_001a: 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_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
			Logger.LogInfo((object)"Applied Harmony patches for death tracking");
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogInfo((object)"Removed Harmony patches");
		}

		private void InitializeGUIStyles()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Expected O, but got Unknown
			//IL_00a4: 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)
			if (deathCountStyle == null)
			{
				deathCountStyle = new GUIStyle();
				deathCountStyle.fontSize = 20;
				deathCountStyle.fontStyle = (FontStyle)1;
				deathCountStyle.normal.textColor = deathCountColor;
				deathCountStyle.alignment = (TextAnchor)2;
				deathCountStyle.font = Font.CreateDynamicFontFromOSFont("Arial", 20);
			}
			if (debugStyle == null)
			{
				debugStyle = new GUIStyle();
				debugStyle.fontSize = 12;
				debugStyle.normal.textColor = Color.yellow;
				debugStyle.normal.background = MakeTexture(2, 2, new Color(0f, 0f, 0f, 0.5f));
			}
		}

		private Texture2D MakeTexture(int width, int height, Color color)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			Color[] array = (Color[])(object)new Color[width * height];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = color;
			}
			Texture2D val = new Texture2D(width, height);
			val.SetPixels(array);
			val.Apply();
			return val;
		}

		private void OnGUI()
		{
			//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_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			uiCallCount++;
			InitializeGUIStyles();
			Scene activeScene = SceneManager.GetActiveScene();
			string name = ((Scene)(ref activeScene)).name;
			if ((name.StartsWith("Level - ") && !name.Contains("Lobby")) || forceDisplay)
			{
				float num = Screen.width - 125;
				float num2 = 275f;
				GUI.Label(new Rect(num, num2, 80f, 25f), "DEATHS", deathCountStyle);
				GUI.Label(new Rect(num, num2 + 25f, 80f, 25f), totalDeaths.ToString(), deathCountStyle);
			}
		}

		public void RegisterDeath()
		{
			if (deathCooldown <= 0f)
			{
				totalDeaths++;
				deathCooldown = 2f;
				Logger.LogInfo((object)$"Player died! Total deaths: {totalDeaths}");
			}
		}

		public void DeleteDeath()
		{
			if (totalDeaths > 0 && deathCooldown <= 0f)
			{
				totalDeaths--;
				deathCooldown = 2f;
				Logger.LogInfo((object)$"Death removed! Total deaths: {totalDeaths}");
			}
		}

		private void Update()
		{
			if (deathCooldown > 0f)
			{
				deathCooldown -= Time.deltaTime;
			}
			uiUpdateTimer += Time.deltaTime;
			if (uiUpdateTimer >= 5f)
			{
				Logger.LogInfo((object)$"UI update: {uiCallCount} OnGUI calls in 5 seconds");
				uiUpdateTimer = 0f;
				uiCallCount = 0;
			}
			if (Input.GetKeyDown((KeyCode)291))
			{
				forceDisplay = !forceDisplay;
				Logger.LogInfo((object)$"Force display toggled to: {forceDisplay}");
			}
			if (Input.GetKeyDown((KeyCode)290))
			{
				DeleteDeath();
			}
			if (Input.GetKeyDown((KeyCode)289))
			{
				RegisterDeath();
			}
		}

		private void OnDestroy()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
		}
	}
	[HarmonyPatch(typeof(PlayerAvatar), "PlayerDeath")]
	internal class PlayerDeathPatch
	{
		private static void Prefix()
		{
			DeathCount.Instance.RegisterDeath();
			DeathCount.Logger.LogInfo((object)"Death detected: PlayerDeath called");
		}
	}
}