From f00bfa252638305ebb8bff8a375bee9f700561c1 Mon Sep 17 00:00:00 2001 From: EntenKoeniq <81123713+EntenKoeniq@users.noreply.github.com> Date: Thu, 2 Dec 2021 23:05:22 +0100 Subject: [PATCH] CommandAttribute changed to Command. Usage and ArgsLength added to Command --- Server/Server.cs | 46 +++++++++++++++++++++++++++++++++++------- Server/ServerScript.cs | 17 +++++++++------- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/Server/Server.cs b/Server/Server.cs index 344f7b3..8c1617b 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -728,19 +728,40 @@ namespace CoopServer if (MainResource != null) { - if (packet.Message.StartsWith("/")) + if (packet.Message.StartsWith('/')) { string[] cmdArgs = packet.Message.Split(" "); string cmdName = cmdArgs[0].Remove(0, 1); if (Commands.Any(x => x.Key.Name == cmdName)) { + string[] argsWithoutCmd = cmdArgs.Skip(1).ToArray(); + CommandContext ctx = new() { - Client = Clients.First(x => x.Player.Username == packet.Username), - Args = cmdArgs.Skip(1).ToArray() + Client = Clients.FirstOrDefault(x => x.Player.Username == packet.Username), + Args = argsWithoutCmd }; KeyValuePair> command = Commands.First(x => x.Key.Name == cmdName); + + if (command.Key.Usage != null && command.Key.ArgsLength != argsWithoutCmd.Length) + { + NetConnection userConnection = Util.GetConnectionByUsername(packet.Username); + if (userConnection == default) + { + return; + } + + outgoingMessage = MainNetServer.CreateMessage(); + new ChatMessagePacket() + { + Username = "Server", + Message = command.Key.Usage + }.PacketToNetOutGoingMessage(outgoingMessage); + MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, 0); + return; + } + command.Value.Invoke(ctx); } else @@ -807,9 +828,20 @@ namespace CoopServer } #endregion + public static void RegisterCommand(string name, string usage, short argsLength, Action callback) + { + Command command = new(name) { Usage = usage, ArgsLength = argsLength }; + + if (Commands.ContainsKey(command)) + { + throw new Exception("Command \"" + command.Name + "\" was already been registered!"); + } + + Commands.Add(command, callback); + } public static void RegisterCommand(string name, Action callback) { - Command command = new() { Name = name }; + Command command = new(name); if (Commands.ContainsKey(command)) { @@ -821,13 +853,13 @@ namespace CoopServer public static void RegisterCommands() { - IEnumerable commands = typeof(T).GetMethods().Where(method => method.GetCustomAttributes(typeof(CommandAttribute), false).Any()); + IEnumerable commands = typeof(T).GetMethods().Where(method => method.GetCustomAttributes(typeof(Command), false).Any()); foreach (MethodInfo method in commands) { - CommandAttribute attribute = method.GetCustomAttribute(true); + Command attribute = method.GetCustomAttribute(true); - RegisterCommand(attribute.Name, (Action)Delegate.CreateDelegate(typeof(Action), method)); + RegisterCommand(attribute.Name, attribute.Usage, attribute.ArgsLength, (Action)Delegate.CreateDelegate(typeof(Action), method)); } } } diff --git a/Server/ServerScript.cs b/Server/ServerScript.cs index f3fa27b..d39a4ff 100644 --- a/Server/ServerScript.cs +++ b/Server/ServerScript.cs @@ -298,6 +298,10 @@ namespace CoopServer } } + public static void RegisterCommand(string name, string usage, short argsLength, Action callback) + { + Server.RegisterCommand(name, usage, argsLength, callback); + } public static void RegisterCommand(string name, Action callback) { Server.RegisterCommand(name, callback); @@ -310,20 +314,19 @@ namespace CoopServer #endregion } - public class Command - { - public string Name { get; set; } - } - [AttributeUsage(AttributeTargets.Method)] - public class CommandAttribute : Attribute + public class Command : Attribute { /// /// Sets name of the command /// public string Name { get; set; } - public CommandAttribute(string name) + public string Usage { get; set; } + + public short ArgsLength { get; set; } + + public Command(string name) { Name = name; }