Decompiled source of Attempt Counter v1.0.0

plugins/AttemptCounter.dll

Decompiled a day ago
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using AttemptCounter.UI;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("AttemptCounter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AttemptCounter")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("efdbf538-57f7-4339-be40-9a09bea20df9")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AttemptCounter
{
	[BepInPlugin("nachariah.whiteknuckle.attemptcounter", "AttemptCounter", "1.0.0")]
	public class AttemptCounterPlugin : BaseUnityPlugin
	{
		public const string pluginGuid = "nachariah.whiteknuckle.attemptcounter";

		public static ConfigEntry<bool> TextOnRight;

		private static Dictionary<string, int> sessionAttempts = new Dictionary<string, int>();

		private static Dictionary<string, int> lifetimeAttempts = new Dictionary<string, int>();

		private const string PlayerPrefsPrefix = "WK_Attempts_";

		private void Awake()
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			TextOnRight = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "TextOnRight", false, "If true, moves the attempt counter to the bottom-right instead of bottom-left.");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[AttemptCounter] Loaded");
			Harmony val = new Harmony("whiteknuckle.maxroaches");
			val.PatchAll();
		}

		public static void Increment(string gamemode)
		{
			if (!sessionAttempts.TryGetValue(gamemode, out var value))
			{
				value = 0;
			}
			value++;
			sessionAttempts[gamemode] = value;
			if (!lifetimeAttempts.TryGetValue(gamemode, out var value2))
			{
				value2 = PlayerPrefs.GetInt("WK_Attempts_" + gamemode, 0);
			}
			value2++;
			lifetimeAttempts[gamemode] = value2;
			PlayerPrefs.SetInt("WK_Attempts_" + gamemode, value2);
			PlayerPrefs.Save();
		}

		public static int GetSession(string gamemode)
		{
			if (!sessionAttempts.TryGetValue(gamemode, out var value))
			{
				return 0;
			}
			return value;
		}

		public static int GetLifetime(string gamemode)
		{
			if (!lifetimeAttempts.TryGetValue(gamemode, out var value))
			{
				value = PlayerPrefs.GetInt("WK_Attempts_" + gamemode, 0);
				lifetimeAttempts[gamemode] = value;
			}
			return value;
		}
	}
}
namespace AttemptCounter.UI
{
	public class AttemptUI : MonoBehaviour
	{
		private static AttemptUI instance;

		private TextMeshProUGUI mainText;

		private TextMeshProUGUI sessionText;

		private TMP_FontAsset ticketingFont;

		public static void Show(int session, int lifetime, string gamemode)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			if ((Object)(object)instance == (Object)null)
			{
				GameObject val = new GameObject("AttemptUI");
				instance = val.AddComponent<AttemptUI>();
				instance.CreateUI();
			}
			((MonoBehaviour)instance).StopAllCoroutines();
			((TMP_Text)instance.mainText).text = gamemode + " Attempts\n" + $"Session: {session}\n" + $"Total: {lifetime}";
			((TMP_Text)instance.sessionText).text = $"{session}";
			((MonoBehaviour)instance).StartCoroutine(instance.FadeTransition());
		}

		private void CreateUI()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Canvas");
			val.transform.SetParent(((Component)this).transform, false);
			Canvas val2 = val.AddComponent<Canvas>();
			val2.renderMode = (RenderMode)0;
			val2.sortingOrder = 999;
			val.AddComponent<CanvasScaler>();
			ticketingFont = FindTicketingFont();
			mainText = CreateText(val.transform, 22f, new Vector2(18f, 18f));
			sessionText = CreateText(val.transform, 44f, new Vector2(18f, 18f));
			((TMP_Text)mainText).alpha = 0f;
			((TMP_Text)sessionText).alpha = 0f;
		}

		private TextMeshProUGUI CreateText(Transform parent, float size, Vector2 pos)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: 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_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("TMP_Text");
			val.transform.SetParent(parent, false);
			TextMeshProUGUI val2 = val.AddComponent<TextMeshProUGUI>();
			((TMP_Text)val2).fontSize = size;
			((Graphic)val2).color = Color.white;
			((TMP_Text)val2).enableWordWrapping = false;
			((Graphic)val2).raycastTarget = false;
			if ((Object)(object)ticketingFont != (Object)null)
			{
				((TMP_Text)val2).font = ticketingFont;
			}
			bool value = AttemptCounterPlugin.TextOnRight.Value;
			((TMP_Text)val2).alignment = (TextAlignmentOptions)(value ? 1028 : 1025);
			RectTransform rectTransform = ((TMP_Text)val2).rectTransform;
			if (value)
			{
				rectTransform.anchorMin = new Vector2(1f, 0f);
				rectTransform.anchorMax = new Vector2(1f, 0f);
				rectTransform.pivot = new Vector2(1f, 0f);
				rectTransform.anchoredPosition = new Vector2(-24f, 24f);
			}
			else
			{
				rectTransform.anchorMin = Vector2.zero;
				rectTransform.anchorMax = Vector2.zero;
				rectTransform.pivot = Vector2.zero;
				rectTransform.anchoredPosition = new Vector2(24f, 24f);
			}
			rectTransform.sizeDelta = new Vector2(520f, 140f);
			return val2;
		}

		private TMP_FontAsset FindTicketingFont()
		{
			TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
			foreach (TMP_FontAsset val in array)
			{
				if (((Object)val).name == "Ticketing SDF")
				{
					return val;
				}
			}
			Debug.LogWarning((object)"[AttemptUI] Ticketing SDF font not found");
			return null;
		}

		private IEnumerator FadeTransition()
		{
			((TMP_Text)mainText).alpha = 0.4f;
			((TMP_Text)sessionText).alpha = 0f;
			yield return (object)new WaitForSeconds(2f);
			float t = 0f;
			while (t < 1f)
			{
				t += Time.deltaTime;
				float lerp = t / 1f;
				((TMP_Text)mainText).alpha = Mathf.Lerp(0.4f, 0f, lerp);
				((TMP_Text)sessionText).alpha = Mathf.Lerp(0f, 0.25f, lerp);
				yield return null;
			}
			((TMP_Text)mainText).alpha = 0f;
			((TMP_Text)sessionText).alpha = 0.25f;
		}
	}
}
namespace AttemptCounter.Patches
{
	[HarmonyPatch(typeof(CL_GameManager), "Start")]
	public static class CL_GameManager_Start_Patch
	{
		private static void Postfix()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			if (!(((Scene)(ref activeScene)).name != "Game-Main"))
			{
				M_Gamemode currentGamemode = CL_GameManager.GetCurrentGamemode();
				if (!((Object)(object)currentGamemode == (Object)null))
				{
					string gamemodeName = currentGamemode.GetGamemodeName(true);
					AttemptCounterPlugin.Increment(gamemodeName);
					AttemptUI.Show(AttemptCounterPlugin.GetSession(gamemodeName), AttemptCounterPlugin.GetLifetime(gamemodeName), gamemodeName);
				}
			}
		}
	}
}