Decompiled source of MissionUtils v1.0.2

MissionUtils.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
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 System.Threading;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MissionUtils;
using Photon.Realtime;
using REPOLib.Modules;
using TMPro;
using UnityEngine;

[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("PencilFoxStudios")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1ee1170b4cacb4e68e06430715016d76cfbd487d")]
[assembly: AssemblyProduct("MissionUtils")]
[assembly: AssemblyTitle("MissionUtils")]
[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;
		}
	}
}
[HarmonyPatch(typeof(SpectateNameUI))]
public class SpectateNameUIPatch
{
	private static string playerName = "";

	[HarmonyPatch("SetName")]
	[HarmonyPrefix]
	private static void SetName_Prefix(SpectateNameUI __instance, ref string name)
	{
		playerName = name;
		string text = ((TMP_Text)MissionUI.instance.Text).text;
		if (MissionUtilsConfigActivator.PencilConfig.DeadPlayersCanSeeMissions && !string.IsNullOrEmpty(text))
		{
			name = name + "<br><size=12>" + text + "</size>";
		}
		((TMP_Text)__instance.Text).text = name;
	}

	[HarmonyPatch("Update")]
	[HarmonyPostfix]
	private static void Update_Postfix(SpectateNameUI __instance)
	{
		if (MissionUtilsConfigActivator.PencilConfig.DeadPlayersCanSeeMissions && (Object)(object)MissionUI.instance?.Text != (Object)null && !string.IsNullOrEmpty(((TMP_Text)MissionUI.instance.Text).text))
		{
			((TMP_Text)__instance.Text).text = playerName + "<br><size=12>" + ((TMP_Text)MissionUI.instance.Text).text + "</size>";
		}
	}
}
namespace MissionUtils
{
	public class MissionQueue
	{
		private static Queue<MissionUtils.MissionOptions> _queue = new Queue<MissionUtils.MissionOptions>();

		public static void Enqueue(MissionUtils.MissionOptions mission)
		{
			_queue.Enqueue(mission);
		}

		public static bool IsEmpty()
		{
			return _queue.Count == 0;
		}

		public static MissionUtils.MissionOptions Dequeue()
		{
			return _queue.Dequeue();
		}

		public static void QueueListener(CancellationToken token)
		{
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				while (!token.IsCancellationRequested)
				{
					MissionUtils.Logger.LogDebug((object)("MissionQueue: Checking for missions to display... -->" + _queue.Count));
					if (IsEmpty())
					{
						Thread.Sleep(1000);
						continue;
					}
					MissionUI instance = MissionUI.instance;
					if ((Object)(object)instance == (Object)null)
					{
						MissionUtils.Logger.LogWarning((object)"MissionUI instance is null, waiting...");
						Thread.Sleep(100);
						break;
					}
					MissionUtils.MissionOptions missionOptions = Dequeue();
					if (missionOptions != null)
					{
						instance.messageTimer = 0f;
						MissionUIPatch.MissionText(instance, missionOptions.msg, missionOptions.color1, missionOptions.color2, missionOptions.time);
					}
					Thread.Sleep(MissionUtilsConfigActivator.PencilConfig.TimeBetweenMissionDisplays * 1000);
				}
			}
			catch (Exception arg)
			{
				MissionUtils.Logger.LogError((object)$"Error in ListenLoop: {arg}");
			}
		}
	}
	[HarmonyPatch(typeof(MissionUI))]
	public class MissionUIPatch
	{
		public static string _previousMissionText = "";

		public static bool isCurrentlyDisplaying = false;

		[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(typeof(MissionUI), "MissionText")]
		public static void MissionText(MissionUI __instance, string message, Color colorMain, Color colorFlash, float desiredTime = 3f)
		{
			throw new NotImplementedException("It's a stub");
		}

		[HarmonyPatch(typeof(MissionUI))]
		[HarmonyPatch("MissionText")]
		[HarmonyPrefix]
		private static bool MissionText_Prefix(MissionUI __instance, string message, Color colorMain, Color colorFlash, float time = 3f)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			if (_previousMissionText == message)
			{
				MissionUtils.Logger.LogWarning((object)("MissionUIPatch: Skipping duplicate mission message: " + message));
				return false;
			}
			_previousMissionText = message;
			MissionUtils.MissionOptions missionOptions = MissionUtils.MissionOptions.Create(message, colorMain, colorFlash, time);
			bool flag = missionOptions.msg.StartsWith("%broadcast%") && SemiFunc.IsMultiplayer() && SemiFunc.IsMasterClient();
			MissionUtils.Logger.LogInfo((object)$"Broadcast: {missionOptions.msg} ({flag})");
			missionOptions.msg = message.Replace("%broadcast%", "");
			if (flag)
			{
				MissionUtils.Logger.LogInfo((object)("Broadcasting mission: " + missionOptions.msg));
				PencilNetwork.SendMission(missionOptions);
				return false;
			}
			MissionUtils.Logger.LogInfo((object)("Displaying Mission: " + missionOptions.msg));
			MissionQueue.Enqueue(missionOptions);
			return false;
		}
	}
	[BepInPlugin("PencilFoxStudios.MissionUtils", "MissionUtils", "1.0.0")]
	public class MissionUtils : BaseUnityPlugin
	{
		public class MissionOptions
		{
			public Color color1 { get; set; }

			public Color color2 { get; set; }

			public string? msg { get; set; }

			public float time { get; set; }

			public static byte[] Serialize(object customObject)
			{
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Unknown result type (might be due to invalid IL or missing references)
				//IL_004a: 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_006e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0080: Unknown result type (might be due to invalid IL or missing references)
				MissionOptions missionOptions = (MissionOptions)customObject;
				using MemoryStream memoryStream = new MemoryStream();
				using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
				{
					binaryWriter.Write(missionOptions.msg);
					binaryWriter.Write(missionOptions.color1.r);
					binaryWriter.Write(missionOptions.color1.g);
					binaryWriter.Write(missionOptions.color1.b);
					binaryWriter.Write(missionOptions.color2.r);
					binaryWriter.Write(missionOptions.color2.g);
					binaryWriter.Write(missionOptions.color2.b);
					binaryWriter.Write(missionOptions.time);
				}
				return memoryStream.ToArray();
			}

			public static object Deserialize(byte[] serializedCustomObject)
			{
				//IL_0036: Unknown result type (might be due to invalid IL or missing references)
				//IL_0054: Unknown result type (might be due to invalid IL or missing references)
				using MemoryStream input = new MemoryStream(serializedCustomObject);
				using BinaryReader binaryReader = new BinaryReader(input);
				MissionOptions missionOptions = new MissionOptions();
				missionOptions.msg = binaryReader.ReadString();
				missionOptions.color1 = new Color(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
				missionOptions.color2 = new Color(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
				missionOptions.time = binaryReader.ReadSingle();
				return missionOptions;
			}

			public static MissionOptions Create(string message, Color color1, Color color2, float time)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: Unknown result type (might be due to invalid IL or missing references)
				return new MissionOptions
				{
					msg = message,
					color1 = color1,
					color2 = color2,
					time = time
				};
			}
		}

		private CancellationTokenSource cts = new CancellationTokenSource();

		internal static MissionUtils Instance { get; private set; }

		public static ManualLogSource Logger => Instance._logger;

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

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			MissionUtilsConfigActivator.Initialize(new MissionUtilsConfigActivator(((BaseUnityPlugin)this).Config));
			Logger.LogInfo((object)$"Loading {((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version}...");
			Logger.LogInfo((object)("Config file path: " + ((BaseUnityPlugin)this).Config.ConfigFilePath));
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Patch();
			Logger.LogInfo((object)$"Patching {((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version}...");
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
			Task.Run(delegate
			{
				MissionQueue.QueueListener(cts.Token);
			});
		}

		private void Start()
		{
			PencilNetwork.InitNetworking();
		}

		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();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
		}
	}
	internal class MissionUtilsConfigActivator
	{
		private readonly ConfigEntry<int> timeBetweenMissionDisplays;

		private readonly ConfigEntry<bool> deadPlayersCanSeeMissions;

		public static MissionUtilsConfigActivator PencilConfig { get; private set; }

		public int TimeBetweenMissionDisplays => timeBetweenMissionDisplays.Value;

		public bool DeadPlayersCanSeeMissions => deadPlayersCanSeeMissions.Value;

		public MissionUtilsConfigActivator(ConfigFile cfg)
		{
			timeBetweenMissionDisplays = cfg.Bind<int>("MissionUtils", "Time Between Mission Displays", 2, "How long to wait between displaying missions in the queue?\nThe default is 2 seconds.\n");
			deadPlayersCanSeeMissions = cfg.Bind<bool>("MissionUtils", "Dead Players Can See Missions", false, "Should dead players be able to see missions?\nThe default is false.\n");
		}

		public static void Initialize(MissionUtilsConfigActivator config)
		{
			PencilConfig = config;
		}
	}
	internal class PencilNetwork
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static Action<EventData> <0>__HandleBroadcastedMissionEvent;

			public static SerializeMethod <1>__Serialize;

			public static DeserializeMethod <2>__Deserialize;
		}

		public static NetworkedEvent? NewBroadcastedMissionEvent;

		public static void InitNetworking()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			NewBroadcastedMissionEvent = new NetworkedEvent("NewBroadcastedMissionEvent", (Action<EventData>)HandleBroadcastedMissionEvent);
			Type typeFromHandle = typeof(MissionUtils.MissionOptions);
			object obj = <>O.<1>__Serialize;
			if (obj == null)
			{
				SerializeMethod val = MissionUtils.MissionOptions.Serialize;
				<>O.<1>__Serialize = val;
				obj = (object)val;
			}
			object obj2 = <>O.<2>__Deserialize;
			if (obj2 == null)
			{
				DeserializeMethod val2 = MissionUtils.MissionOptions.Deserialize;
				<>O.<2>__Deserialize = val2;
				obj2 = (object)val2;
			}
			PhotonPeer.RegisterType(typeFromHandle, (byte)100, (SerializeMethod)obj, (DeserializeMethod)obj2);
		}

		private static void HandleBroadcastedMissionEvent(EventData eventData)
		{
			MissionUtils.MissionOptions missionOptions = (MissionUtils.MissionOptions)eventData.CustomData;
			MissionQueue.Enqueue(missionOptions);
			MissionUtils.Logger.LogInfo((object)("Received mission: " + missionOptions.msg));
		}

		public static void SendMission(MissionUtils.MissionOptions options)
		{
			//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_0022: 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_0031: Expected O, but got Unknown
			if (SemiFunc.IsMultiplayer() && SemiFunc.IsMasterClient())
			{
				NewBroadcastedMissionEvent.RaiseEvent((object)options, new RaiseEventOptions
				{
					Receivers = (ReceiverGroup)1
				}, SendOptions.SendReliable);
				MissionUtils.Logger.LogInfo((object)("NETACTION: Sent mission: " + options.msg));
			}
			else
			{
				MissionUtils.Logger.LogError((object)"Cannot send mission, not in multiplayer or not master client.");
			}
		}
	}
	[HarmonyPatch(typeof(SemiFunc))]
	public class SemiFuncPatch
	{
		[HarmonyPatch(typeof(SemiFunc))]
		[HarmonyPatch("HUDSpectateSetName")]
		[HarmonyPrefix]
		private static void HUDSpectateSetName_Prefix(ref string name)
		{
		}
	}
}