Decompiled source of TerminalShortcuts v1.0.1

TheLore-TerminalShortcuts/TerminalShortcuts/TerminalShortcuts.dll

Decompiled a year ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TerminalApi.Events;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("TerminalShortcuts")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TerminalShortcuts")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("88b527ea-2bb6-4562-aac3-afca95458430")]
[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 TerminalShortcuts;

[BepInPlugin("TheLore.TerminalShortcuts", "Terminal Shortcuts", "1.0.0.0")]
public class ModManager : BaseUnityPlugin
{
	public static ModManager instance;

	private const string guid = "TheLore.TerminalShortcuts";

	private const string name = "Terminal Shortcuts";

	private const string version = "1.0.0.0";

	private readonly Harmony harmony = new Harmony("TheLore.TerminalShortcuts");

	public ManualLogSource logSource;

	private ShortcutManager manager;

	private void Awake()
	{
		if ((Object)(object)instance == (Object)null)
		{
			instance = this;
		}
		logSource = Logger.CreateLogSource("TheLore.TerminalShortcuts");
		manager = new ShortcutManager();
		manager.Setup();
		harmony.PatchAll();
	}
}
[BepInDependency(/*Could not decode attribute arguments.*/)]
[HarmonyPatch(typeof(Terminal))]
public class ShortcutManager
{
	public static ShortcutManager instance;

	private static string writtenTextTemp = null;

	public static Terminal t;

	private static string lastUsedBooster;

	private static bool shortcutsEnabled = true;

	private static List<string> cyclesDeactivateCode = new List<string>();

	private static List<float> cyclesDeactivateTimer = new List<float>();

	private static float timeBetweenInterval = 5f;

	public void Setup()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Expected O, but got Unknown
		if (instance == null)
		{
			instance = this;
		}
		Events.TerminalTextChanged += new TerminalTextChangedEventHandler(textChanged);
		Events.TerminalStarted += new TerminalEventHandler(started);
	}

	private void started(object sender, TerminalEventArgs e)
	{
		t = e.Terminal;
	}

	[HarmonyPatch("Update")]
	[HarmonyPostfix]
	private static void update()
	{
		if (!shortcutsEnabled)
		{
			return;
		}
		for (int i = 0; i < cyclesDeactivateCode.Count; i++)
		{
			string textToRepleceWritten = cyclesDeactivateCode[i];
			if (cyclesDeactivateTimer[i] <= 0f)
			{
				cyclesDeactivateTimer[i] = timeBetweenInterval;
				sendText(textToRepleceWritten, writeLastText: true);
			}
			else
			{
				cyclesDeactivateTimer[i] -= Time.deltaTime;
			}
		}
	}

	private static void createDeactivateCycles(string code)
	{
		cyclesDeactivateCode.Add(code);
		cyclesDeactivateTimer.Add(timeBetweenInterval);
	}

	private static void stopAllCycles()
	{
		cyclesDeactivateCode.Clear();
		cyclesDeactivateTimer.Clear();
	}

	public void keySwitch()
	{
		if ((Object)(object)t != (Object)null)
		{
			sendText("switch", writeLastText: true);
		}
	}

	private void textChanged(object sender, TerminalTextChangedEventArgs e)
	{
		if (!shortcutsEnabled)
		{
			return;
		}
		t = ((TerminalEventArgs)e).Terminal;
		if (e.CurrentInputText.Contains("\\"))
		{
			removeLastWrittenLetter(e.CurrentInputText);
			keySwitch();
		}
		string[] array = new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
		for (int i = 0; i < array.Length; i++)
		{
			string value = array[i];
			if (e.CurrentInputText.Equals(value) && StartOfRound.Instance.allPlayerScripts.Length >= i + 1)
			{
				string name = StartOfRound.Instance.mapScreen.radarTargets[i].name;
				sendText("switch " + name, writeLastText: false);
				break;
			}
		}
	}

	private void removeLastWrittenLetter(string writtenText)
	{
		string text = writtenText;
		text = writtenText.Substring(0, writtenText.Length - 1);
		string text2 = t.screenText.text.Substring(0, t.screenText.text.Length - t.textAdded);
		t.screenText.text = text2 + text;
		t.textAdded = text.Length;
	}

	public static void replaceWrittenText(string textToRepleceWritten, bool writeLastText)
	{
		if (writeLastText)
		{
			writtenTextTemp = t.screenText.text.Substring(t.screenText.text.Length - t.textAdded);
		}
		else
		{
			writtenTextTemp = null;
		}
		string text = t.screenText.text.Substring(0, t.screenText.text.Length - t.textAdded);
		t.screenText.text = text + textToRepleceWritten;
		t.textAdded = textToRepleceWritten.Length;
	}

	[HarmonyPatch("OnSubmit")]
	[HarmonyPrefix]
	private static void preTextSent()
	{
		if ((Object)(object)t == (Object)null)
		{
			return;
		}
		string text = t.screenText.text.Substring(t.screenText.text.Length - t.textAdded);
		if (text.Equals("ts on"))
		{
			shortcutsEnabled = true;
		}
		else if (text.Equals("ts off"))
		{
			shortcutsEnabled = false;
			stopAllCycles();
		}
		if (!shortcutsEnabled)
		{
			return;
		}
		if (text.StartsWith("t "))
		{
			string text2 = text.Substring(1);
			replaceWrittenText("transmit " + text2, writeLastText: false);
		}
		else if (text.StartsWith("ping "))
		{
			lastUsedBooster = text.Substring(4);
		}
		else if (text.Equals("p"))
		{
			replaceWrittenText("ping " + lastUsedBooster, writeLastText: false);
		}
		else if (text.Equals("f"))
		{
			replaceWrittenText("flash " + lastUsedBooster, writeLastText: false);
		}
		else if (text.Equals("vb"))
		{
			replaceWrittenText("switch " + lastUsedBooster, writeLastText: false);
		}
		else if (text.Length == 4)
		{
			string text3 = text.Substring(2);
			string code = text.Substring(0, 2);
			if (text3.Equals(" r"))
			{
				createDeactivateCycles(code);
			}
		}
		else if (text.Equals("s"))
		{
			stopAllCycles();
		}
	}

	[HarmonyPatch("OnSubmit")]
	[HarmonyPostfix]
	private static void postTextSent()
	{
		if (!((Object)(object)t == (Object)null) && shortcutsEnabled && writtenTextTemp != null)
		{
			string text = t.screenText.text.Substring(0, t.screenText.text.Length - t.textAdded);
			t.screenText.text = text + writtenTextTemp;
			t.textAdded = writtenTextTemp.Length;
			writtenTextTemp = null;
		}
	}

	public static void sendText(string textToRepleceWritten, bool writeLastText)
	{
		replaceWrittenText(textToRepleceWritten, writeLastText);
		t.OnSubmit();
	}
}