Decompiled source of CommandHandler v1.0.0

CommandHandler.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CommandHandler")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CommandHandler")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5c941518-e9b2-4b62-8e48-214a42ca67db")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace CommandHandler;

[BepInPlugin("steven4547466.CommandHandler", "Command Handler", "1.0.0")]
public class CommandHandler : BaseUnityPlugin
{
	[HarmonyPatch(typeof(HUDManager), "SubmitChat_performed")]
	private static class SubmitChatPatch
	{
		private static bool HandleMessage(HUDManager manager)
		{
			string text = manager.chatTextField.text;
			if (!Utility.IsNullOrWhiteSpace(text) && text.StartsWith(CommandPrefix.Value))
			{
				string[] array = text.Split(new char[1] { ' ' });
				string text2 = array[0].Substring(CommandPrefix.Value.Length);
				if (TryGetCommandHandler(text2, out var handler))
				{
					string[] obj = array.Skip(1).ToArray();
					try
					{
						handler(obj);
					}
					catch (Exception ex)
					{
						((BaseUnityPlugin)Singleton).Logger.LogError((object)("Error handling command: " + text2));
						((BaseUnityPlugin)Singleton).Logger.LogError((object)ex);
					}
				}
				manager.localPlayer.isTypingChat = false;
				manager.chatTextField.text = "";
				EventSystem.current.SetSelectedGameObject((GameObject)null);
				((Behaviour)manager.typingIndicator).enabled = false;
				return true;
			}
			return false;
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			List<CodeInstruction> newInstructions = new List<CodeInstruction>(instructions);
			Label label = generator.DefineLabel();
			newInstructions[newInstructions.Count - 1].labels.Add(label);
			int index = newInstructions.FindIndex((CodeInstruction i) => i.opcode == OpCodes.Ldfld && (FieldInfo)i.operand == AccessTools.Field(typeof(PlayerControllerB), "isPlayerDead")) - 2;
			newInstructions.InsertRange(index, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[3]
			{
				CodeInstructionExtensions.MoveLabelsFrom(new CodeInstruction(OpCodes.Ldarg_0, (object)null), newInstructions[index]),
				new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(SubmitChatPatch), "HandleMessage", (Type[])null, (Type[])null)),
				new CodeInstruction(OpCodes.Brtrue, (object)label)
			});
			for (int z = 0; z < newInstructions.Count; z++)
			{
				yield return newInstructions[z];
			}
		}
	}

	internal static ConfigEntry<string> CommandPrefix;

	internal static Dictionary<string, Action<string[]>> CommandHandlers = new Dictionary<string, Action<string[]>>();

	internal static Dictionary<string, List<string>> CommandAliases = new Dictionary<string, List<string>>();

	private static Harmony Harmony { get; set; }

	internal static CommandHandler Singleton { get; private set; }

	private void Awake()
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected O, but got Unknown
		Singleton = this;
		CommandPrefix = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Prefix", "/", "Command prefix");
		Harmony = new Harmony($"steven4547466.CommandHandler-{DateTime.Now.Ticks}");
		Harmony.PatchAll();
	}

	public static bool RegisterCommand(string command, Action<string[]> handler)
	{
		if (CommandHandlers.ContainsKey(command))
		{
			return false;
		}
		CommandHandlers.Add(command, handler);
		return true;
	}

	public static bool RegisterCommand(string command, List<string> aliases, Action<string[]> handler)
	{
		if (GetCommandHandler(command) != null)
		{
			return false;
		}
		foreach (string alias in aliases)
		{
			if (GetCommandHandler(alias) != null)
			{
				return false;
			}
		}
		CommandHandlers.Add(command, handler);
		CommandAliases.Add(command, aliases);
		return true;
	}

	public static bool UnregisterCommand(string command)
	{
		CommandAliases.Remove(command);
		return CommandHandlers.Remove(command);
	}

	public static Action<string[]> GetCommandHandler(string command)
	{
		if (CommandHandlers.TryGetValue(command, out var value))
		{
			return value;
		}
		foreach (KeyValuePair<string, List<string>> commandAlias in CommandAliases)
		{
			if (commandAlias.Value.Contains(command))
			{
				return CommandHandlers[commandAlias.Key];
			}
		}
		return null;
	}

	public static bool TryGetCommandHandler(string command, out Action<string[]> handler)
	{
		handler = GetCommandHandler(command);
		return handler != null;
	}
}