Decompiled source of Spectators v1.2.0

BepInEx/plugins/Spectators.dll

Decompiled a year ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using UnityEngine;
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("Spectators")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Spectators")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("74e3a6a4-b4c9-421a-a540-09717f967437")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Spectators
{
	[BepInPlugin("00Spectators00", "Spectators", "1.0.0")]
	public class SpectatorsModBase : BaseUnityPlugin
	{
		private const string modGUID = "00Spectators00";

		private const string modName = "Spectators";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("00Spectators00");

		private static SpectatorsModBase instance;

		private static ManualLogSource mls;

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			harmony.PatchAll();
			mls = Logger.CreateLogSource(" 00Spectators00");
			mls.LogInfo((object)"-- Spectators -- The eyes are watching...");
		}
	}
}
namespace Spectators.Patches
{
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDManagerPatch
	{
		private static TextMeshProUGUI specBox;

		public static int lastPlayerID = -100;

		public static List<string> specList = new List<string>();

		public const string key1 = "iswatching69";

		public const string key2 = "stoppedwatching69";

		public static string headerColor = "#e36d19";

		public static string specColor = "#3d52a1";

		public static HUDManager hud = HUDManager.Instance;

		public static ManualLogSource mls = Logger.CreateLogSource(" SpectatorDebug");

		[HarmonyPostfix]
		[HarmonyPatch("SetSpectatingTextToPlayer")]
		private static void patchSetSpectatingTextToPlayer(ref PlayerControllerB playerScript)
		{
			string playerUsername = hud.playersManager.localPlayerController.playerUsername;
			int num = -10;
			for (int i = 0; i < hud.playersManager.allPlayerScripts.Length; i++)
			{
				if (hud.playersManager.allPlayerScripts[i].playerUsername == playerScript.playerUsername)
				{
					num = i;
				}
			}
			if (num != lastPlayerID)
			{
				if (lastPlayerID != -100)
				{
					mls.LogInfo((object)("Sending Spec Stop to :" + playerScript.playerUsername));
					hud.AddTextToChatOnServer(playerUsername + "stoppedwatching69", lastPlayerID);
				}
				lastPlayerID = num;
				mls.LogInfo((object)("Sending Spec Start to :" + playerScript.playerUsername));
				hud.AddTextToChatOnServer(playerUsername + "iswatching69", num);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("AddChatMessage")]
		private static bool patchSpectatorsClientChat(ref string chatMessage, ref string nameOfUserWhoTyped)
		{
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			int length = chatMessage.Length;
			if (chatMessage.Contains("iswatching69") || chatMessage.Contains("stoppedwatching69"))
			{
				if (nameOfUserWhoTyped != hud.playersManager.localPlayerController.playerUsername)
				{
					hud.lastChatMessage = chatMessage;
					return false;
				}
				if (chatMessage.Contains("iswatching69"))
				{
					specList.Add(chatMessage.Substring(0, length - "iswatching69".Length));
					mls.LogInfo((object)("Adding Spectator: " + chatMessage.Substring(0, length - "iswatching69".Length)));
					hud.lastChatMessage = chatMessage;
					return false;
				}
				specList.Remove(chatMessage.Substring(0, length - "stoppedwatching69".Length));
				mls.LogInfo((object)("Removing Spectator: " + chatMessage.Substring(0, length - "stoppedwatching69".Length)));
				hud.lastChatMessage = chatMessage;
				return false;
			}
			if (chatMessage.Contains("/specColor") || chatMessage.Contains("/headerColor"))
			{
				if (Enumerable.Contains(chatMessage, '#'))
				{
					if (chatMessage.Contains("/specColor"))
					{
						string text = chatMessage.Substring(11, length - 11);
						specColor = text;
					}
					else
					{
						string text2 = chatMessage.Substring(13, length - 13);
						headerColor = text2;
					}
				}
				hud.lastChatMessage = chatMessage;
				return false;
			}
			if (chatMessage == "/kill512")
			{
				hud.localPlayer.KillPlayer(default(Vector3), true, (CauseOfDeath)0, 0);
				hud.lastChatMessage = chatMessage;
				return false;
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void modifyWeightText()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)specBox == (Object)null)
			{
				return;
			}
			Color color = default(Color);
			ColorUtility.TryParseHtmlString(specColor, ref color);
			((Graphic)specBox).color = color;
			((TMP_Text)specBox).text = "<color=" + headerColor + ">Spectators:</color>\n";
			if (GameNetworkManager.Instance.localPlayerController.isPlayerDead)
			{
				specList.RemoveRange(0, specList.Count);
				((TMP_Text)specBox).text = "";
				return;
			}
			for (int i = 0; i < specList.Count; i++)
			{
				string text = ((TMP_Text)specBox).text;
				((TMP_Text)specBox).text = string.Format(text + "<size=6f><color=" + specColor + "> {0}</color></size>\n", nameSpacer(specList[i]));
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("HideHUD")]
		private static void clearSpecEndRound()
		{
			mls.LogInfo((object)"Look out sweepers!");
			specList.RemoveRange(0, specList.Count);
		}

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void createSpecElem(ref HUDManager __instance)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("SpectatorList");
			val.AddComponent<RectTransform>();
			TextMeshProUGUI val2 = val.AddComponent<TextMeshProUGUI>();
			RectTransform rectTransform = ((TMP_Text)val2).rectTransform;
			((Transform)rectTransform).SetParent(((Component)__instance.PTTIcon).transform, false);
			rectTransform.anchoredPosition = new Vector2(460f, -90f);
			((TMP_Text)val2).font = ((TMP_Text)__instance.controlTipLines[0]).font;
			((TMP_Text)val2).fontSize = 7f;
			((Behaviour)val2).enabled = true;
			((Graphic)val2).color = Color.white;
			((TMP_Text)val2).text = "Spectators: \n";
			((TMP_Text)val2).overflowMode = (TextOverflowModes)0;
			specBox = val2;
			mls.LogInfo((object)"Spectator UI Element Loaded...");
		}

		private static string nameSpacer(string name)
		{
			string text = name;
			int num = 18;
			if (name.Length < num)
			{
				for (int i = 0; i < num - name.Length; i++)
				{
					text += " ";
				}
			}
			return text;
		}
	}
}