예제 #1
0
        public void ThreadProc()
        {
            var sender = new ConsoleCommandSender
            {
                GameServer = GameServer
            };

            // continue until we're told to stop
            while (!CancellationToken.IsCancellationRequested)
            {
                // read the input from the console and attempt to parse it as command
                // this will find the command in the gameserver and execute if it exist

                var input = Console.ReadLine();
                if (input == null)
                {
                    continue;
                }
                // TODO this needs to take quotes into account
                var arguments = Util.SplitCommandString(input);

                Dictionary <string, Action <CommandContext, List <string> > > commands;
                lock (GameServer)
                {
                    commands = GameServer.Commands;

                    // continue if the command exists
                    if (arguments.Count == 0 || !commands.ContainsKey(arguments[0]))
                    {
                        continue;
                    }

                    // invoke the command
                    commands[arguments[0]].Invoke(new CommandContext
                    {
                        Sender     = sender,
                        GameServer = GameServer
                    }, arguments.Skip(1).ToList());
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Executes a command on the current <see cref="GameServer"/> instance,
        /// Called by ConsoleThread on native and by JS in WebAssembly
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns>Whether the command existed</returns>
        public static bool ExecuteCommand(string cmd, GameServer server = null, ICommandSender sender = null)
        {
            server = server ?? _gameServer;

            if (sender == null)
            {
                sender = new ConsoleCommandSender
                {
                    GameServer = server
                };
            }

            var arguments = Util.SplitCommandString(cmd);

            Dictionary <Command, Action <CommandContext, List <string> > > commands;

            lock (server)
            {
                commands = server.Commands;

                // check if the command exists
                if (arguments.Count == 0 || !commands.Any(x => x.Key.Name == arguments[0]))
                {
                    return(false);
                }

                // invoke the command
                var command = commands.First(x => x.Key.Name == arguments[0]);
                var context = new CommandContext {
                    Sender = sender, GameServer = _gameServer
                };

                command.Value.Invoke(context, arguments.Skip(1).ToList());

                return(true);
            }
        }