|
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Runtime.InteropServices;
-
- namespace TechbloxModdingAPI.Commands
- {
- internal static class CustomCommands
- {
- public struct CommandData
- {
- public string Name;
- public string Description;
- public Delegate Action;
- }
-
- private static Dictionary<string, CommandData> _commands = new Dictionary<string, CommandData>();
- public static void Register(string name, Delegate action, string desc)
- {
- _commands.Add(name, new CommandData
- {
- Name = name,
- Description = desc,
- Action = action
- });
- }
-
- public static void Call(string name, params object[] args)
- {
- if (_commands.TryGetValue(name, out var command))
- {
- var paramz = command.Action.Method.GetParameters();
- if (paramz.Length > args.Length)
- throw new CommandParameterMissingException(
- $"This command requires {paramz.Length} arguments, {args.Length} given");
- for (var index = 0; index < paramz.Length; index++)
- {
- args[index] = Convert.ChangeType(args[index], paramz[index].ParameterType);
- }
-
- command.Action.DynamicInvoke(args);
- }
- else
- throw new CommandNotFoundException($"Command {name} does not exist!");
- }
-
- public static void Unregister(string name)
- {
- _commands.Remove(name);
- }
-
- public static bool Exists(string name) => _commands.ContainsKey(name);
-
- public static ReadOnlyDictionary<string, CommandData> GetAllCommandData() =>
- new ReadOnlyDictionary<string, CommandData>(_commands);
- }
- }
|