Example #1
0
        public void Run(string[] command)
        {
            if (command.Length != 1 && command.Length != 2)
            {
                Console.WriteLine("Invalid ammount of parameters, use help [command]");
                return;
            }

            if (command.Length == 1)
            {
                Console.WriteLine("Aviable commands: ");
                foreach (var cmd in RegisteredCommands)
                {
                    Console.WriteLine(cmd.Key);
                }

                Help(command);
            }
            else
            {
                BaseCommand b;
                if (RegisteredCommands.TryGetValue(command[1], out b))
                {
                    List <string> l = command.ToList();

                    l.RemoveAt(0);
                    Console.WriteLine(command[1] + ":");
                    b.Help(l.ToArray());
                }
                else
                {
                    Console.WriteLine("Command not found");
                }
            }
        }
Example #2
0
        /// <summary>
        /// Executes Immediate commands and enqueues unsafe commands.
        /// </summary>
        /// <param name="session">session that sent the command</param>
        /// <param name="requestInfo">request that tells us which command to execute</param>
        /// <seealso cref="Whisper.Daemon.Shard.Net.ShardServer.ExecuteQueuedCommand"/>
        protected override void ExecuteCommand(ShardSession session, ShardRequest requestInfo)
        {
            IWhisperCommand <ShardSession, ShardRequest> command;

            if (RegisteredCommands.TryGetValue(requestInfo.Key, out command))
            {
                // best to assume the worst, right?
                CommandThreadSafety safety = CommandThreadSafety.NotThreadSafe;

                if (command is IThreadAwareCommand)
                {
                    safety = (command as IThreadAwareCommand).ThreadSafety;
                }

                if (safety == CommandThreadSafety.Immediate)
                {
                    // execute Immediate commands immediately
                    base.ExecuteCommand(session, requestInfo);
                }
                else
                {
                    // split remaining commands by thread safety. thread-safe commands are executed on a thread owned by the session that received them; thread-unsafe commands are executed on the server update thread
                    CommandClosure closure = () => base.ExecuteCommand(session, requestInfo);

                    if (safety == CommandThreadSafety.ThreadSafe)
                    {
                        session.EnqueueCommand(closure);
                    }
                    else
                    {
                        EnqueueCommand(closure);
                    }
                }
            }
            // no need to log the 'else' case here. unknown packet notifications are handled by the Composer
        }
        /// <summary>
        /// Finds a specified command by its qualified name, then separates arguments.
        /// </summary>
        /// <param name="commandString">Qualified name of the command, optionally with arguments.</param>
        /// <param name="rawArguments">Separated arguments.</param>
        /// <returns>Found command or null if none was found.</returns>
        public Command FindCommand(string commandString, out string rawArguments)
        {
            rawArguments = null;

            var ignoreCase = !Config.CaseSensitive;
            var pos        = 0;
            var next       = commandString.ExtractNextArgument(ref pos);

            if (next == null)
            {
                return(null);
            }

            if (!RegisteredCommands.TryGetValue(next, out var cmd))
            {
                if (!ignoreCase)
                {
                    return(null);
                }

                next = next.ToLowerInvariant();
                var cmdKvp = RegisteredCommands.FirstOrDefault(x => x.Key.ToLowerInvariant() == next);
                if (cmdKvp.Value == null)
                {
                    return(null);
                }

                cmd = cmdKvp.Value;
            }

            if (!(cmd is CommandGroup))
            {
                rawArguments = commandString.Substring(pos).Trim();
                return(cmd);
            }

            while (cmd is CommandGroup)
            {
                var cm2    = cmd as CommandGroup;
                var oldPos = pos;
                next = commandString.ExtractNextArgument(ref pos);
                if (next == null)
                {
                    break;
                }

                if (ignoreCase)
                {
                    next = next.ToLowerInvariant();
                    cmd  = cm2.Children.FirstOrDefault(x => x.Name.ToLowerInvariant() == next || x.Aliases?.Any(xx => xx.ToLowerInvariant() == next) == true);
                }
                else
                {
                    cmd = cm2.Children.FirstOrDefault(x => x.Name == next || x.Aliases?.Contains(next) == true);
                }

                if (cmd == null)
                {
                    cmd = cm2;
                    pos = oldPos;
                    break;
                }
            }

            rawArguments = commandString.Substring(pos).Trim();
            return(cmd);
        }