Decompiled source of CommandAPI v1.0.0

plugins/CommandAPI.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PurrNet;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("CommandAPI")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CommandAPI")]
[assembly: AssemblyTitle("CommandAPI")]
[assembly: AssemblyVersion("1.0.0.0")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace CommandAPI
{
	public class CommandInfo
	{
		public string Name { get; }

		public Action<string[]> Handler { get; }

		public string Description { get; }

		public Parameter[] Parameters { get; }

		public string Category { get; }

		public CommandInfo(string name, string category, Action<string[]> handler, string description, Parameter[] parameters)
		{
			Name = name;
			Category = (string.IsNullOrEmpty(category?.Trim()) ? "General" : category.Trim());
			Handler = handler;
			Description = description;
			Parameters = parameters ?? Array.Empty<Parameter>();
		}

		public string GetUsageString()
		{
			StringBuilder stringBuilder = new StringBuilder("/" + Name);
			Parameter[] parameters = Parameters;
			foreach (Parameter parameter in parameters)
			{
				stringBuilder.Append(" [" + parameter.Type.ToString().ToLower() + " " + parameter.Name + "]");
			}
			return stringBuilder.ToString();
		}

		public bool ValidateArguments(string[] args, out string error)
		{
			error = null;
			if (args.Length < Parameters.Count((Parameter p) => !p.IsOptional))
			{
				error = "Not enough arguments. Usage: " + GetUsageString();
				return false;
			}
			return true;
		}
	}
	public static class CommandRegistry
	{
		private static Dictionary<string, CommandInfo> commands = new Dictionary<string, CommandInfo>();

		public static bool Register(string name, Action<string[]> handler, string description, Parameter[] parameters)
		{
			return Register(name, "General", handler, description, parameters);
		}

		public static bool Register(string name, string category, Action<string[]> handler, string description, Parameter[] parameters)
		{
			commands[name.ToLower()] = new CommandInfo(name, category, handler, description, parameters);
			return true;
		}

		public static bool TryGetCommand(string name, out CommandInfo info)
		{
			return commands.TryGetValue(name.ToLower(), out info);
		}

		public static IEnumerable<CommandInfo> GetAllCommands()
		{
			return commands.Values;
		}

		public static IEnumerable<CommandInfo> FindMatches(string prefix)
		{
			return from c in commands.Values
				where c.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
				orderby c.Name
				select c;
		}
	}
	public class Parameter
	{
		public string Name { get; }

		public ParameterType Type { get; }

		public bool IsOptional { get; }

		public object MinValue { get; }

		public object MaxValue { get; }

		public Parameter(string name, ParameterType type, bool isOptional = false, object minValue = null, object maxValue = null)
		{
			Name = name;
			Type = type;
			IsOptional = isOptional;
			MinValue = minValue;
			MaxValue = maxValue;
		}
	}
	public enum ParameterType
	{
		String,
		Int,
		Float,
		Bool
	}
	[BepInPlugin("com.on-together-mods.commandapi", "Command API", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"CommandAPI loaded");
			Harmony val = new Harmony("com.on-together-mods.commandapi");
			Type type = AccessTools.TypeByName("TextChannelManager");
			val.Patch((MethodBase)AccessTools.Method(type, "OnEnterPressed", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "OnEnterPressed_Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static bool OnEnterPressed_Prefix()
		{
			UIManager i = MonoSingleton<UIManager>.I;
			TMP_InputField val = ((i != null) ? i.MessageInput : null);
			if ((Object)(object)val == (Object)null)
			{
				return true;
			}
			string text = val.text;
			if (string.IsNullOrEmpty(text) || !text.StartsWith("/"))
			{
				return true;
			}
			string[] array = text.Substring(1).Split(' ', StringSplitOptions.RemoveEmptyEntries);
			if (array.Length == 0)
			{
				return true;
			}
			string text2 = array[0].ToLower();
			string[] array2 = array.Skip(1).ToArray();
			if (CommandRegistry.TryGetCommand(text2, out var info))
			{
				val.text = "";
				if (info.ValidateArguments(array2, out var error))
				{
					try
					{
						info.Handler(array2);
						Utilities.ResetPostMessage();
					}
					catch (Exception ex)
					{
						Utilities.Notify("Error executing /" + text2 + ": " + ex.Message);
						Utilities.ResetPostMessage();
					}
				}
				else
				{
					Utilities.Notify("Invalid arguments: " + error);
					Utilities.ResetPostMessage();
				}
				return false;
			}
			if (text2 == "help")
			{
				val.text = "";
				Utilities.ResetPostMessage();
				return false;
			}
			return true;
		}
	}
	public static class Utilities
	{
		public static float UpdateDelayer = 0f;

		private static string _ifTeleportString = "";

		public static void Notify(string notification)
		{
			TextChannelManager i = NetworkSingleton<TextChannelManager>.I;
			if (!((Object)(object)i == (Object)null))
			{
				i.AddNotification(notification);
			}
		}

		public static void EmitUpdate()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)MonoSingleton<MainSceneManager>.I == (Object)null)
			{
				return;
			}
			try
			{
				if (UpdateDelayer <= 0f)
				{
					NetworkSingleton<TextChannelManager>.I.MainCustomizationController.UpdatePlayerInfo(MonoSingleton<DataManager>.I.PlayerData.GetPlayerIdInfo(), default(RPCInfo));
					string name = MonoSingleton<DataManager>.I.PlayerData.Name;
					NetworkSingleton<TextChannelManager>.I.UserName = name;
					((TMP_Text)MonoSingleton<UIManager>.I.PlayerText).text = name;
					if (!string.IsNullOrEmpty(_ifTeleportString))
					{
						FieldRef<UIManager, TextMeshProUGUI> obj = AccessTools.FieldRefAccess<UIManager, TextMeshProUGUI>("_playerText");
						UIManager i = MonoSingleton<UIManager>.I;
						((TMP_Text)obj.Invoke(i)).text = name + _ifTeleportString;
					}
				}
			}
			catch (NullReferenceException)
			{
				UpdateDelayer = 2.5f;
				Debug.Log((object)"StatusManager exception, delaying");
			}
		}

		public static void ResetPostMessage(bool skipText = false)
		{
			MusicManager i = NetworkSingleton<MusicManager>.I;
			bool flag = i != null && i.IsActive;
			MonoSingleton<TaskManager>.I.SetLockState((LockState)(flag ? 5 : 0));
			EventSystem.current.SetSelectedGameObject((GameObject)null);
			if (!skipText)
			{
				MonoSingleton<UIManager>.I.MessageInput.text = "";
			}
		}

		public static void SetTeleportSuffix(string suffix)
		{
			_ifTeleportString = suffix;
		}

		public static string GetTeleportSuffix()
		{
			return _ifTeleportString;
		}
	}
}