Decompiled source of FusionReadyCheck v1.0.0

Mods/FusionReadyCheck.dll

Decompiled 33 minutes ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib.BoneMenu;
using FusionReadyCheck;
using LabFusion.Data;
using LabFusion.Network;
using LabFusion.Player;
using LabFusion.SDK.Metadata;
using LabFusion.UI.Popups;
using LabFusion.Utilities;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(ReadyCheckMod), "Fusion Ready Check", "1.0.0", "Fusion Ready Check Contributors", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonAdditionalDependencies(new string[] { "LabFusion", "BoneLib" })]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("FusionReadyCheck")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FusionReadyCheck")]
[assembly: AssemblyTitle("FusionReadyCheck")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
namespace FusionReadyCheck
{
	public sealed class ReadyCheckMod : MelonMod
	{
		private const string RoundKey = "FusionReadyCheck.v1.Round";

		private const string VoteKey = "FusionReadyCheck.v1.Vote";

		private const string NotificationTag = "FusionReadyCheck.v1";

		private const double RoundDuration = 45.0;

		private static readonly Color Accent = new Color(0.56f, 1f, 0.49f);

		private FunctionElement? _statusButton;

		private string? _observedRound;

		private double _hostDeadline;

		private double _nextRefresh;

		private double _nextHostAction;

		private double _nextVote;

		public override void OnInitializeMelon()
		{
			//IL_000a: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Expected O, but got Unknown
			Page val = Page.Root.CreatePage("Fusion Ready Check", Accent, 0, true);
			_statusButton = val.CreateFunction("No active ready check", Accent, (Action)ShowStatus);
			val.CreateFunction("Ready", Color.green, (Action)delegate
			{
				Vote(ready: true);
			});
			val.CreateFunction("Need more time", Color.yellow, (Action)delegate
			{
				Vote(ready: false);
			});
			val.CreateFunction("Start 45-second check (host)", Color.white, (Action)StartCheck);
			val.CreateFunction("End check (host)", Color.white, (Action)EndCheck);
			MultiplayerHooking.OnDisconnected += new ServerEvent(Reset);
			LocalPlayer.OnApplyInitialMetadata += ClearLocalMetadata;
		}

		public override void OnDeinitializeMelon()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			MultiplayerHooking.OnDisconnected -= new ServerEvent(Reset);
			LocalPlayer.OnApplyInitialMetadata -= ClearLocalMetadata;
		}

		public override void OnUpdate()
		{
			double realtimeSinceStartupAsDouble = Time.realtimeSinceStartupAsDouble;
			if (realtimeSinceStartupAsDouble < _nextRefresh)
			{
				return;
			}
			_nextRefresh = realtimeSinceStartupAsDouble + 0.25;
			if (_hostDeadline > 0.0 && realtimeSinceStartupAsDouble >= _hostDeadline && NetworkInfo.IsHost)
			{
				EndCheck();
			}
			string text = ActiveRound();
			if (text != _observedRound)
			{
				string observedRound = _observedRound;
				_observedRound = text;
				if (text != null)
				{
					Notify("Ready check started", "Vote in BoneMenu > Fusion Ready Check.");
				}
				else if (observedRound != null && NetworkInfo.HasServer)
				{
					Notify("Ready check ended", Summarize(observedRound));
				}
			}
			string text2 = ((text == null) ? "No active ready check" : Summarize(text));
			if (_statusButton != null && ((Element)_statusButton).ElementName != text2)
			{
				((Element)_statusButton).ElementName = text2;
			}
		}

		private static string? ActiveRound()
		{
			if (!NetworkInfo.HasServer)
			{
				return null;
			}
			object obj;
			if (!NetworkInfo.IsHost)
			{
				PlayerID hostID = PlayerIDManager.GetHostID();
				obj = ((hostID != null) ? hostID.Metadata.Metadata : null);
			}
			else
			{
				obj = LocalPlayer.Metadata.Metadata;
			}
			string text = ((obj != null) ? ((NetworkMetadata)obj).GetMetadata("FusionReadyCheck.v1.Round") : null);
			if (!Guid.TryParseExact(text, "N", out var _))
			{
				return null;
			}
			return text;
		}

		private void StartCheck()
		{
			if (!CanManageCheck())
			{
				return;
			}
			if (ActiveRound() != null)
			{
				ShowStatus();
				return;
			}
			double realtimeSinceStartupAsDouble = Time.realtimeSinceStartupAsDouble;
			if (!(realtimeSinceStartupAsDouble < _nextHostAction))
			{
				_nextHostAction = realtimeSinceStartupAsDouble + 3.0;
				if (LocalPlayer.Metadata.Metadata.TrySetMetadata("FusionReadyCheck.v1.Round", Guid.NewGuid().ToString("N")))
				{
					_hostDeadline = realtimeSinceStartupAsDouble + 45.0;
				}
			}
		}

		private void EndCheck()
		{
			if (CanManageCheck())
			{
				_hostDeadline = 0.0;
				if (ActiveRound() != null)
				{
					LocalPlayer.Metadata.Metadata.TrySetMetadata("FusionReadyCheck.v1.Round", string.Empty);
				}
			}
		}

		private static bool CanManageCheck()
		{
			if (NetworkInfo.IsHost)
			{
				PlayerID localID = PlayerIDManager.LocalID;
				if (localID != null && localID.IsValid)
				{
					return true;
				}
			}
			Notify("Host controls", "The lobby host starts and ends the ready check.");
			return false;
		}

		private void Vote(bool ready)
		{
			string text = ActiveRound();
			if (text != null)
			{
				PlayerID localID = PlayerIDManager.LocalID;
				if (localID != null && localID.IsValid)
				{
					double realtimeSinceStartupAsDouble = Time.realtimeSinceStartupAsDouble;
					if (realtimeSinceStartupAsDouble < _nextVote)
					{
						return;
					}
					NetworkMetadata metadata = LocalPlayer.Metadata.Metadata;
					string text2 = text + (ready ? "/ready" : "/wait");
					if (!(metadata.GetMetadata("FusionReadyCheck.v1.Vote") == text2))
					{
						_nextVote = realtimeSinceStartupAsDouble + 0.4;
						if (metadata.TrySetMetadata("FusionReadyCheck.v1.Vote", text2))
						{
							Notify("Response sent", ready ? "Ready" : "Need more time");
						}
					}
					return;
				}
			}
			Notify("Ready check", "There is no active check.");
		}

		private static string Summarize(string round)
		{
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			string text = round + "/ready";
			string text2 = round + "/wait";
			foreach (PlayerID playerID in PlayerIDManager.PlayerIDs)
			{
				if (playerID.IsValid)
				{
					NetworkMetadata obj = (playerID.IsMe ? LocalPlayer.Metadata.Metadata : playerID.Metadata.Metadata);
					string text3 = ((obj != null) ? obj.GetMetadata("FusionReadyCheck.v1.Vote") : null);
					if (text3 == text)
					{
						num++;
					}
					else if (text3 == text2)
					{
						num2++;
					}
					else
					{
						num3++;
					}
				}
			}
			return $"{num} ready / {num2} wait / {num3} pending";
		}

		private void ShowStatus()
		{
			string text = ActiveRound();
			Notify("Ready check", (text == null) ? "There is no active check." : Summarize(text));
		}

		private void Reset()
		{
			_observedRound = null;
			_hostDeadline = 0.0;
			_nextHostAction = 0.0;
			_nextVote = 0.0;
			ClearLocalMetadata();
			Notifier.Cancel("FusionReadyCheck.v1");
		}

		private static void ClearLocalMetadata()
		{
			NetworkMetadata metadata = LocalPlayer.Metadata.Metadata;
			metadata.ForceRemoveLocalMetadata("FusionReadyCheck.v1.Round");
			metadata.ForceRemoveLocalMetadata("FusionReadyCheck.v1.Vote");
		}

		private static void Notify(string title, string message)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			Notifier.Cancel("FusionReadyCheck.v1");
			Notifier.Send(new Notification
			{
				Title = NotificationText.op_Implicit(title),
				Message = NotificationText.op_Implicit(message),
				Tag = "FusionReadyCheck.v1",
				PopupLength = 3f,
				ShowPopup = RigData.HasPlayer,
				SaveToMenu = false
			});
		}
	}
}