Decompiled source of Warden v1.0.0

Warden.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
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.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Windows.Speech;
using Warden.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Warden")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Warden")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c5bce3a8-bcd5-4a1b-9655-c31cc033627e")]
[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 Warden
{
	[BepInPlugin("Warden", "Warden", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static PluginLogger logger = new PluginLogger();

		private static Plugin Instance;

		private static bool LoggingEnabled;

		public static KeywordRecognizer SpeechRecognizer;

		private void Awake()
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			Instance = this;
			logger.LogInfo("Plugin is awake!");
			BindConfig();
			try
			{
				logger.LogInfo("Initialized Phrase Recongition System!");
				string[] array = Variables.InverseTeleporterCalls.Union(Variables.TeleporterCalls).Union(Variables.DisableHazardCalls).Union(Variables.OpenSecureDoorCalls)
					.Union(Variables.CloseSecureDoorCalls)
					.Union(Variables.OpenShipDoorCalls)
					.Union(Variables.CloseShipDoorCalls)
					.Union(Variables.DistractCalls)
					.ToArray();
				SpeechRecognizer = new KeywordRecognizer(array, (ConfidenceLevel)2);
				((PhraseRecognizer)SpeechRecognizer).OnPhraseRecognized += new PhraseRecognizedDelegate(OnRecognizeCommand);
				((PhraseRecognizer)SpeechRecognizer).Start();
			}
			catch (Exception arg)
			{
				logger.LogError($"Unable to initialize phrase recognition: {arg}");
			}
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "Warden");
		}

		private void BindConfig()
		{
			string value = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Inverse Teleporter Calls", "start inverse, inverse start, activate inverse, inverse activate, inverse", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.InverseTeleporterCalls = (from cmd in value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value2 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Teleporter Calls", "teleport, tp, start teleporter, teleporter start, activate teleporter, teleporter activate", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.TeleporterCalls = (from cmd in value2.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value3 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Disable Hazard Calls", "let me pass, disable this, deactivate hazard, hazard deactivate", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.DisableHazardCalls = (from cmd in value3.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value4 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Open Secure Door Calls", "open the door, open door, door open, open sesame, door enable, enable door, activate door, door activate", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.OpenSecureDoorCalls = (from cmd in value4.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value5 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Close Secure Door Calls", "close the door, close door, door close, close sesame, deactivate door, door deactivate", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.CloseSecureDoorCalls = (from cmd in value5.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value6 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Open Ship Door Calls", "ship door open, open ship door, activate ship door, ship door activate, let me in, let me out", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.OpenShipDoorCalls = (from cmd in value6.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value7 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Close Ship Door Calls", "ship door close, close ship door, deactivate ship door, ship door deactivate", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.CloseShipDoorCalls = (from cmd in value7.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			string value8 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Distract Calls", "distract", "Separate each voice command by using commas. Spaces don't affect the commands.").Value;
			Variables.DistractCalls = (from cmd in value8.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select cmd.Trim()).ToArray();
			LoggingEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Logging", true, (ConfigDescription)null).Value;
			logger.EnableLogging(LoggingEnabled);
		}

		private void OnRecognizeCommand(PhraseRecognizedEventArgs e)
		{
			//IL_0302: Unknown result type (might be due to invalid IL or missing references)
			//IL_0320: Unknown result type (might be due to invalid IL or missing references)
			List<(HashSet<string>, Func<bool>, Action, string)> list = new List<(HashSet<string>, Func<bool>, Action, string)>
			{
				(new HashSet<string>(Variables.InverseTeleporterCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead && StartOfRound.Instance.shipHasLanded, delegate
				{
					WardenHandler.StartInverseTeleporter();
				}, "Inverse Teleporter"),
				(new HashSet<string>(Variables.TeleporterCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead, delegate
				{
					WardenHandler.TeleportPlayer(StartOfRound.Instance.localPlayerController.playerClientId);
				}, "Teleporter"),
				(new HashSet<string>(Variables.DisableHazardCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead && StartOfRound.Instance.localPlayerController.isInsideFactory, delegate
				{
					WardenHandler.DisableHazard();
				}, "Disable Hazard"),
				(new HashSet<string>(Variables.OpenSecureDoorCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead && StartOfRound.Instance.localPlayerController.isInsideFactory, delegate
				{
					WardenHandler.OpenSecureDoor();
				}, "Open Secure Door"),
				(new HashSet<string>(Variables.CloseSecureDoorCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead && StartOfRound.Instance.localPlayerController.isInsideFactory, delegate
				{
					WardenHandler.CloseSecureDoor();
				}, "Close Secure Door"),
				(new HashSet<string>(Variables.OpenShipDoorCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead, delegate
				{
					WardenHandler.OpenShipDoors();
				}, "Open Ship Door"),
				(new HashSet<string>(Variables.CloseShipDoorCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead, delegate
				{
					WardenHandler.CloseShipDoors();
				}, "Close Ship Door"),
				(new HashSet<string>(Variables.DistractCalls), () => !StartOfRound.Instance.localPlayerController.isPlayerDead && (StartOfRound.Instance.shipHasLanded || !StartOfRound.Instance.shipIsLeaving) && !StartOfRound.Instance.localPlayerController.isInsideFactory && ((int)StartOfRound.Instance.currentLevel.currentWeather == 5 || (int)TimeOfDay.Instance.dayMode >= 2), delegate
				{
					WardenHandler.DistractDogs();
				}, "Distract")
			};
			foreach (var (hashSet, func, action, text) in list)
			{
				if (hashSet.Contains(e.text))
				{
					logger.LogInfo(text + " -> Recognized command '" + e.text + "'");
					if (func())
					{
						logger.LogInfo(text + " -> Phrase has been sent to the server");
						action();
					}
					break;
				}
			}
		}
	}
	public class PluginInfo
	{
		public const string PLUGIN_GUID = "Warden";

		public const string PLUGIN_NAME = "Warden";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	public class PluginLogger
	{
		private static ManualLogSource mls = Logger.CreateLogSource("Warden");

		private static bool ShouldLog = true;

		public void EnableLogging(bool enabled)
		{
			ShouldLog = enabled;
		}

		public void LogInfo(object data)
		{
			if (ShouldLog)
			{
				mls.LogInfo(data);
			}
		}

		public void LogWarning(object data)
		{
			if (ShouldLog)
			{
				mls.LogWarning(data);
			}
		}

		public void LogError(object data)
		{
			if (ShouldLog)
			{
				mls.LogError(data);
			}
		}
	}
}
namespace Warden.Core
{
	public class Variables
	{
		public static string[] InverseTeleporterCalls;

		public static string[] TeleporterCalls;

		public static string[] DisableHazardCalls;

		public static string[] OpenSecureDoorCalls;

		public static string[] CloseSecureDoorCalls;

		public static string[] OpenShipDoorCalls;

		public static string[] CloseShipDoorCalls;

		public static string[] DistractCalls;
	}
	public class WardenHandler
	{
		private static void WatchPlayer(ulong clientID)
		{
			Terminal val = Object.FindObjectOfType<Terminal>();
			if (!((Object)(object)val != (Object)null))
			{
				return;
			}
			ManualCameraRenderer mapScreen = StartOfRound.Instance.mapScreen;
			PlayerControllerB[] array = Object.FindObjectsOfType<PlayerControllerB>();
			int targetTransformIndex = mapScreen.targetTransformIndex;
			int num = targetTransformIndex;
			PlayerControllerB[] array2 = array;
			foreach (PlayerControllerB val2 in array2)
			{
				if (val2.playerClientId == clientID)
				{
					if (num != (int)clientID)
					{
						num = (int)clientID;
					}
					PlayerControllerB component = ((Component)mapScreen.radarTargets[num].transform).GetComponent<PlayerControllerB>();
					if ((!((Object)(object)component != (Object)null) || component.isPlayerDead || component.isPlayerControlled || !((Object)(object)component.redirectToEnemy == (Object)null)) && num != targetTransformIndex)
					{
						mapScreen.SwitchRadarTargetAndSync(num);
						val.LoadNewNode(val.terminalNodes.specialNodes[20]);
					}
					break;
				}
			}
		}

		public static void StartInverseTeleporter()
		{
			Plugin.logger.LogInfo("Activating Inverse Teleporter...");
			ShipTeleporter[] array = Object.FindObjectsOfType<ShipTeleporter>();
			if (array.Length == 0 || array == null)
			{
				Plugin.logger.LogError("Why would you activate a non-existent inverse teleporter? Where would the activation spot be? Inside you?");
				return;
			}
			ShipTeleporter val = null;
			ShipTeleporter[] array2 = array;
			foreach (ShipTeleporter val2 in array2)
			{
				if ((Object)(object)val2 != (Object)null && val2.isInverseTeleporter)
				{
					val = val2;
					break;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				val.PressTeleportButtonOnLocalClient();
			}
		}

		public static void TeleportPlayer(ulong clientID)
		{
			Plugin.logger.LogInfo("Teleporting player...");
			((MonoBehaviour)StartOfRound.Instance.localPlayerController).StartCoroutine(TeleportCoroutine(clientID));
		}

		private static IEnumerator TeleportCoroutine(ulong clientID)
		{
			WatchPlayer(clientID);
			yield return (object)new WaitForSeconds(0.25f);
			ShipTeleporter[] teleporters = Object.FindObjectsOfType<ShipTeleporter>();
			if (teleporters.Length == 0 || teleporters == null)
			{
				Plugin.logger.LogError("Why would you activate a non-existent teleporter? Where would the activation spot be? Inside you?");
				yield break;
			}
			ShipTeleporter normal = null;
			ShipTeleporter[] array = teleporters;
			foreach (ShipTeleporter teleporter in array)
			{
				if ((Object)(object)teleporter != (Object)null && !teleporter.isInverseTeleporter)
				{
					normal = teleporter;
					break;
				}
			}
			if ((Object)(object)normal != (Object)null)
			{
				normal.PressTeleportButtonOnLocalClient();
			}
		}

		public static void OpenShipDoors()
		{
			if (StartOfRound.Instance.shipDoorsEnabled)
			{
				GameObject val = GameObject.Find("StartButton");
				InteractTrigger val2 = ((val != null) ? val.GetComponentInChildren<InteractTrigger>() : null);
				if ((Object)(object)val2 != (Object)null)
				{
					((UnityEvent<PlayerControllerB>)(object)val2.onInteract)?.Invoke(StartOfRound.Instance.localPlayerController);
				}
			}
		}

		public static void CloseShipDoors()
		{
			if (StartOfRound.Instance.shipDoorsEnabled)
			{
				GameObject val = GameObject.Find("StopButton");
				InteractTrigger val2 = ((val != null) ? val.GetComponentInChildren<InteractTrigger>() : null);
				if ((Object)(object)val2 != (Object)null)
				{
					((UnityEvent<PlayerControllerB>)(object)val2.onInteract)?.Invoke(StartOfRound.Instance.localPlayerController);
				}
			}
		}

		public static void DistractDogs()
		{
			Terminal val = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val != (Object)null)
			{
				val.orderedItemsFromTerminal.Add(1);
			}
		}

		public static void DisableHazard()
		{
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			TerminalAccessibleObject[] array = Object.FindObjectsOfType<TerminalAccessibleObject>();
			List<TerminalAccessibleObject> list = new List<TerminalAccessibleObject>();
			TerminalAccessibleObject[] array2 = array;
			foreach (TerminalAccessibleObject val in array2)
			{
				if ((Object)(object)((Component)val).gameObject.GetComponent<Turret>() != (Object)null || (Object)(object)((Component)val).gameObject.GetComponent<Landmine>() != (Object)null)
				{
					list.Add(val);
				}
			}
			if (list.Count == 0)
			{
				Plugin.logger.LogInfo("There are no hazards");
				return;
			}
			TerminalAccessibleObject val2 = null;
			float num = float.MaxValue;
			Vector3 position = ((Component)StartOfRound.Instance.localPlayerController).transform.position;
			foreach (TerminalAccessibleObject item in list)
			{
				float num2 = Vector3.Distance(((Component)item).transform.position, position);
				if (num2 < num)
				{
					val2 = item;
					num = num2;
				}
			}
			if ((Object)(object)val2 != (Object)null)
			{
				val2.CallFunctionFromTerminal();
			}
		}

		public static void OpenSecureDoor()
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: 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)
			TerminalAccessibleObject[] source = Object.FindObjectsOfType<TerminalAccessibleObject>();
			List<TerminalAccessibleObject> list = source.Where((TerminalAccessibleObject x) => x.isBigDoor).ToList();
			if (list.Count == 0)
			{
				Plugin.logger.LogInfo("There are no secure doors");
				return;
			}
			TerminalAccessibleObject val = null;
			float num = float.MaxValue;
			Vector3 position = ((Component)StartOfRound.Instance.localPlayerController).transform.position;
			foreach (TerminalAccessibleObject item in list)
			{
				float num2 = Vector3.Distance(((Component)item).transform.position, position);
				if (num2 < num)
				{
					val = item;
					num = num2;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				val.SetDoorLocalClient(true);
			}
		}

		public static void CloseSecureDoor()
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: 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)
			TerminalAccessibleObject[] source = Object.FindObjectsOfType<TerminalAccessibleObject>();
			List<TerminalAccessibleObject> list = source.Where((TerminalAccessibleObject x) => x.isBigDoor).ToList();
			if (list.Count == 0)
			{
				Plugin.logger.LogInfo("There are no secure doors");
				return;
			}
			TerminalAccessibleObject val = null;
			float num = float.MaxValue;
			Vector3 position = ((Component)StartOfRound.Instance.localPlayerController).transform.position;
			foreach (TerminalAccessibleObject item in list)
			{
				float num2 = Vector3.Distance(((Component)item).transform.position, position);
				if (num2 < num)
				{
					val = item;
					num = num2;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				val.SetDoorLocalClient(false);
			}
		}
	}
}