Decompiled source of AmalgamationTracker v1.0.2

BepInEx/plugins/AmalgamationTracker.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Pigeon.Movement;
using Sparroh.UI;
using TMPro;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Sparroh")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("AmalgamationTracker")]
[assembly: AssemblyTitle("AmalgamationTracker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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;
		}
	}
}
public class BossTimerHUD
{
	private float finalTime;

	private bool hasFinalTime;

	private HudHandle hud;

	private bool isTiming;

	private float startTime;

	public static BossTimerHUD Instance { get; private set; }

	public bool IsActive
	{
		get
		{
			if (HudHandle.IsValid(hud))
			{
				return hud.IsActive;
			}
			return false;
		}
	}

	public Vector2 GetSize
	{
		get
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			if (!HudHandle.IsValid(hud))
			{
				return Vector2.zero;
			}
			return hud.Size;
		}
	}

	public BossTimerHUD()
	{
		Instance = this;
	}

	public void OnConfigChanged()
	{
		if (!ConfigManager.EnableBossTimerHUD.Value && HudHandle.IsValid(hud))
		{
			DestroyHud();
		}
		UpdateHudVisibility();
	}

	public void UpdateHudVisibility()
	{
		if (HudHandle.IsValid(hud))
		{
			hud.SetActive(ConfigManager.EnableBossTimerHUD.Value);
		}
	}

	private void CreateTimerHUD()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		if (hud != null && !hud.IsAlive)
		{
			hud = null;
		}
		if (!HudHandle.IsValid(hud))
		{
			hud = HudBuilder.Create("AmalgamationTimerHUD").ParentToReticle(true).Anchor(ConfigManager.Anchors.XValue, ConfigManager.Anchors.YValue)
				.Pivot(new Vector2(0.5f, 0.5f))
				.Size(350f, 28f, true)
				.AddText("TimerText", 20f, (TextAlignmentOptions)514)
				.Build();
			if (HudHandle.IsValid(hud))
			{
				hud.EnableReposition("sparroh.amalgamationtracker", "Amalgamation Timer", ConfigManager.Anchors);
				UpdateHudVisibility();
			}
		}
	}

	private void DestroyHud()
	{
		if (hud != null)
		{
			if (hud.IsAlive)
			{
				hud.Destroy();
			}
			hud = null;
		}
	}

	public void StartTimer()
	{
		isTiming = true;
		startTime = Time.realtimeSinceStartup;
		finalTime = 0f;
		hasFinalTime = false;
		SparrohPlugin.Logger.LogInfo((object)"Amalgamation timer started");
	}

	public void StopTimer()
	{
		if (isTiming)
		{
			finalTime = Time.realtimeSinceStartup - startTime;
			isTiming = false;
			hasFinalTime = true;
			SparrohPlugin.Logger.LogInfo((object)("Amalgamation timer stopped: " + FormatTime(finalTime)));
		}
	}

	public void ResetTimer()
	{
		isTiming = false;
		startTime = 0f;
		finalTime = 0f;
		hasFinalTime = false;
		SparrohPlugin.Logger.LogInfo((object)"Amalgamation timer reset");
	}

	public void Update()
	{
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!ConfigManager.EnableBossTimerHUD.Value)
			{
				return;
			}
			if (!HudHandle.IsValid(hud))
			{
				CreateTimerHUD();
			}
			else if (hud.Primary != null && !((Object)(object)Player.LocalPlayer == (Object)null))
			{
				if (hasFinalTime)
				{
					hud.Primary.SetRich("Amalgamation", FormatTime(finalTime), ConfigManager.ValueColor.Value, (string)null);
				}
				else if (isTiming)
				{
					hud.Primary.SetRich("Amalgamation", FormatTime(Time.realtimeSinceStartup - startTime), ConfigManager.ValueColor.Value, (string)null);
				}
				else
				{
					hud.Primary.Text = "Amalgamation: Waiting...";
				}
			}
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Error in BossTimerHUD.Update(): " + ex.Message));
		}
	}

	private string FormatTime(float timeInSeconds)
	{
		int num = (int)(timeInSeconds / 60f);
		int num2 = (int)(timeInSeconds % 60f);
		int num3 = (int)(timeInSeconds % 1f * 1000f);
		return $"{num:D2}:{num2:D2}.{num3:D3}";
	}

	public void OnDestroy()
	{
		try
		{
			DestroyHud();
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Error in BossTimerHUD.OnDestroy(): " + ex.Message));
		}
	}
}
[HarmonyPatch]
public static class BossTimerPatches
{
	[HarmonyPatch(typeof(MissionManager), "OnMissionStarted_Client")]
	[HarmonyPostfix]
	private static void OnMissionStarted_Client_Postfix()
	{
		if (BossTimerHUD.Instance != null)
		{
			BossTimerHUD.Instance.ResetTimer();
		}
	}

	[HarmonyPatch(typeof(AmalgamationObjective), "OnAmalgamationSpawned_ClientRpc")]
	[HarmonyPostfix]
	private static void OnAmalgamationSpawned_ClientRpc_Postfix(AmalgamationObjective __instance, NetworkBehaviourReference brainRef)
	{
		if (BossTimerHUD.Instance != null)
		{
			BossTimerHUD.Instance.StartTimer();
		}
	}

	[HarmonyPatch(typeof(AmalgamationObjective), "OnBrainKilled")]
	[HarmonyPostfix]
	private static void OnBrainKilled_Postfix(EnemyBrain brain)
	{
		if (BossTimerHUD.Instance != null)
		{
			BossTimerHUD.Instance.StopTimer();
		}
	}
}
public static class ConfigManager
{
	private const float DebounceSeconds = 0.25f;

	private static ConfigFile config;

	private static ManualLogSource logger;

	private static FileSystemWatcher configWatcher;

	private static volatile bool pendingRefresh;

	private static volatile bool reloadPending;

	private static float lastReloadTime;

	public static ConfigEntry<bool> EnableBossTimerHUD { get; private set; }

	public static HudAnchors Anchors { get; private set; }

	public static ConfigColor ValueColor { get; private set; }

	public static void Initialize(ConfigFile configFile, ManualLogSource log)
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		config = configFile;
		logger = log;
		EnableBossTimerHUD = config.Bind<bool>("General", "Enable Amalgamation Timer", true, "Enables the Amalgamation timer HUD display.");
		Anchors = HudAnchors.Bind(config, "Timer", 0.8229749f, 0.9050629f, "HUD Positioning");
		ValueColor = ConfigColor.Bind(config, "Colors", "Timer Color", UIColors.Amber, "Rich-text value color for the Amalgamation timer (hex RRGGBB or #RRGGBB).");
		EnableBossTimerHUD.SettingChanged += OnSettingChanged;
		try
		{
			SetupFileWatcher();
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error setting up config file watcher: " + ex.Message));
		}
	}

	public static void Tick()
	{
		if (!reloadPending || Time.unscaledTime - lastReloadTime < 0.25f)
		{
			return;
		}
		reloadPending = false;
		lastReloadTime = Time.unscaledTime;
		try
		{
			config.Reload();
			pendingRefresh = true;
			logger.LogInfo((object)"Config reloaded from disk.");
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error reloading config: " + ex.Message));
		}
	}

	public static bool ConsumePendingRefresh()
	{
		if (!pendingRefresh)
		{
			return false;
		}
		pendingRefresh = false;
		return true;
	}

	public static void Dispose()
	{
		if (EnableBossTimerHUD != null)
		{
			EnableBossTimerHUD.SettingChanged -= OnSettingChanged;
		}
		if (configWatcher != null)
		{
			configWatcher.EnableRaisingEvents = false;
			configWatcher.Changed -= OnConfigFileChanged;
			configWatcher.Created -= OnConfigFileChanged;
			configWatcher.Renamed -= OnConfigFileChanged;
			configWatcher.Dispose();
			configWatcher = null;
		}
	}

	private static void SetupFileWatcher()
	{
		configWatcher = new FileSystemWatcher(Paths.ConfigPath, "sparroh.amalgamationtracker.cfg");
		configWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
		configWatcher.Changed += OnConfigFileChanged;
		configWatcher.Created += OnConfigFileChanged;
		configWatcher.Renamed += OnConfigFileChanged;
		configWatcher.EnableRaisingEvents = true;
	}

	private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
	{
		reloadPending = true;
	}

	private static void OnSettingChanged(object sender, EventArgs e)
	{
		pendingRefresh = true;
	}
}
[BepInPlugin("sparroh.amalgamationtracker", "AmalgamationTracker", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[MycoMod(/*Could not decode attribute arguments.*/)]
public class SparrohPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "sparroh.amalgamationtracker";

	public const string PluginName = "AmalgamationTracker";

	public const string PluginVersion = "1.0.2";

	internal static ManualLogSource Logger;

	private BossTimerHUD bossTimerHUD;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		Logger = ((BaseUnityPlugin)this).Logger;
		try
		{
			ConfigManager.Initialize(((BaseUnityPlugin)this).Config, Logger);
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Failed to initialize config: " + ex.Message));
			return;
		}
		try
		{
			harmony = new Harmony("sparroh.amalgamationtracker");
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Failed to create Harmony instance: " + ex2.Message));
			return;
		}
		try
		{
			bossTimerHUD = new BossTimerHUD();
		}
		catch (Exception ex3)
		{
			Logger.LogError((object)("Failed to initialize BossTimerHUD: " + ex3.Message));
		}
		try
		{
			harmony.PatchAll();
		}
		catch (Exception ex4)
		{
			Logger.LogError((object)("Failed to apply Harmony patches: " + ex4.Message));
		}
		Logger.LogInfo((object)"AmalgamationTracker loaded successfully.");
	}

	private void Update()
	{
		try
		{
			ConfigManager.Tick();
			if (ConfigManager.ConsumePendingRefresh() && bossTimerHUD != null)
			{
				bossTimerHUD.OnConfigChanged();
			}
			if (bossTimerHUD != null)
			{
				bossTimerHUD.Update();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in BossTimerHUD.Update(): " + ex.Message));
		}
	}

	private void OnDestroy()
	{
		try
		{
			if (bossTimerHUD != null)
			{
				bossTimerHUD.OnDestroy();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in BossTimerHUD.OnDestroy(): " + ex.Message));
		}
		try
		{
			ConfigManager.Dispose();
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Error disposing config: " + ex2.Message));
		}
		try
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch (Exception ex3)
		{
			Logger.LogError((object)("Error unpatching Harmony: " + ex3.Message));
		}
	}
}
namespace AmalgamationTracker
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "AmalgamationTracker";

		public const string PLUGIN_NAME = "AmalgamationTracker";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}