コード例 #1
0
        public bool Execute(IRocketPlayer player, string command)
        {
            command = command.TrimStart('/');
            string[] commandParts = Regex.Matches(command, @"[\""](.+?)[\""]|([^ ]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture).Cast <Match>().Select(m => m.Value.Trim('"').Trim()).ToArray();

            if (commandParts.Length != 0)
            {
                name = commandParts[0];
                string[] parameters = commandParts.Skip(1).ToArray();
                if (player == null)
                {
                    player = new ConsolePlayer();
                }
                IRocketCommand rocketCommand = GetCommand(name);
                double         cooldown      = GetCooldown(player, rocketCommand);
                if (rocketCommand != null)
                {
                    if (rocketCommand.AllowedCaller == AllowedCaller.Player && player is ConsolePlayer)
                    {
                        Logger.Log("This command can't be called from console");
                        return(false);
                    }
                    if (rocketCommand.AllowedCaller == AllowedCaller.Console && !(player is ConsolePlayer))
                    {
                        Logger.Log("This command can only be called from console");
                        return(false);
                    }
                    if (cooldown != -1)
                    {
                        Logger.Log("This command is still on cooldown");
                        return(false);
                    }
                    try
                    {
                        bool cancelCommand = false;
                        if (OnExecuteCommand != null)
                        {
                            foreach (var handler in OnExecuteCommand.GetInvocationList().Cast <ExecuteCommand>())
                            {
                                try
                                {
                                    handler(player, rocketCommand, ref cancelCommand);
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogException(ex);
                                }
                            }
                        }
                        if (!cancelCommand)
                        {
                            try
                            {
                                rocketCommand.Execute(player, parameters);
                                if (!player.HasPermission("*"))
                                {
                                    SetCooldown(player, rocketCommand);
                                }
                            }
                            catch (NoPermissionsForCommandException ex)
                            {
                                Logger.LogWarning(ex.Message);
                            }
                            catch (WrongUsageOfCommandException)
                            {
                                //
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError("An error occured while executing " + rocketCommand.Name + " [" + String.Join(", ", parameters) + "]: " + ex.ToString());
                    }
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
        public bool Execute(IRocketPlayer player, string command)
        {
            /*if (Thread.CurrentThread != SDG.Unturned.ThreadUtil.gameThread)
             * {
             *  var cmd = new AsyncCommand();
             *  cmd.caller = player;
             *  cmd.command = command;
             *  var task = new Task<bool>(cmd.Execute);
             *  TaskDispatcher.QueueOnMainThread(task.Start);
             *  return task.Result;
             * }*/
            command = command.TrimStart('/');
            string[] commandParts = Regex.Matches(command, @"[\""](.+?)[\""]|([^ ]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture).Cast <Match>().Select(m => m.Value.Trim('"').Trim()).ToArray();

            if (commandParts.Length != 0)
            {
                IRocketCommand rocketCommand = GetCommand(commandParts[0]);
                if (rocketCommand != null)
                {
                    if (player == null)
                    {
                        player = new ConsolePlayer();
                    }

                    if (rocketCommand.AllowedCaller == AllowedCaller.Player && player is ConsolePlayer)
                    {
                        Logging.Logger.Log("This command can't be called from console");
                        return(false);
                    }
                    if (rocketCommand.AllowedCaller == AllowedCaller.Console && !(player is ConsolePlayer))
                    {
                        Logging.Logger.Log("This command can only be called from console");
                        return(false);
                    }
                    if (GetCooldown(player, rocketCommand) != -1)
                    {
                        Logging.Logger.Log("This command is still on cooldown");
                        return(false);
                    }

                    string[] parameters;
                    if (commandParts.Length == 1)
                    {
                        parameters = Array.Empty <string>();
                    }
                    else
                    {
                        parameters = new string[commandParts.Length - 1];
                        Array.Copy(commandParts, 1, parameters, 0, parameters.Length);
                    }

                    try
                    {
                        bool cancelCommand = false;
                        if (OnExecuteCommand != null)
                        {
                            foreach (var handler in OnExecuteCommand.GetInvocationList())
                            {
                                try
                                {
                                    ((ExecuteCommand)handler)(player, rocketCommand, ref cancelCommand);
                                }
                                catch (Exception ex)
                                {
                                    Logging.Logger.LogException(ex);
                                }
                            }
                        }
                        if (!cancelCommand)
                        {
                            try
                            {
                                rocketCommand.Execute(player, parameters);
                                if (!player.HasPermission("*"))
                                {
                                    SetCooldown(player, rocketCommand);
                                }
                            }
                            catch (NoPermissionsForCommandException ex)
                            {
                                Logging.Logger.LogWarning(ex.Message);
                            }
                            catch (WrongUsageOfCommandException)
                            {
                                //
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.Logger.LogError("An error occured while executing " + rocketCommand.Name + " [" + string.Join(", ", parameters) + "]: " + ex.ToString());
                    }
                    return(true);
                }
            }

            return(false);
        }