예제 #1
0
        public void Validate(ICharacter byCharacter)
        {
            if (IsClient)
            {
                if ((this.Kind & ConsoleCommandKinds.Client) == 0)
                {
                    throw new Exception("Cannot execute server command on Client-side: " + this.Name);
                }

                // can execute
                return;
            }

            // if server
            var isServerCommandForEveryone      = (this.Kind & ConsoleCommandKinds.ServerEveryone) != 0;
            var isServerCommandRequiresOperator = (this.Kind & ConsoleCommandKinds.ServerOperator) != 0;

            if (!isServerCommandForEveryone &&
                !isServerCommandRequiresOperator)
            {
                throw new Exception("Cannot execute client command on Server-side: " + this.Name);
            }

            if (isServerCommandRequiresOperator)
            {
                var isOperator = ConsoleCommandsSystem.ServerIsOperatorOrSystemConsole(byCharacter);
                if (!isOperator)
                {
                    throw new Exception("You need a server operator access to execute this command: " + this.Name);
                }
            }
        }
예제 #2
0
        public void Execute(ICharacter byCharacter, string[] args)
        {
            var sendNotification = Api.IsServer &&
                                   byCharacter is not null;

            try
            {
                var parsedArgs = this.ParseArguments(
                    byCharacter,
                    args,
                    throwExceptionOnUnparsedArgument: true,
                    successfullyParsedArgsCount: out _);
                var resultObj = this.MethodInfo.MethodInfo.Invoke(this.ConsoleCommand, parsedArgs);
                if (resultObj is not null)
                {
                    var result = resultObj.ToString();
                    Api.Logger.Important(
                        $"Console command \"{this.ConsoleCommand.Name}\" completed: {result}",
                        byCharacter);

                    if (sendNotification)
                    {
                        var maxLength = ServerOperatorSystem.ServerIsOperator(byCharacter.Name) ||
                                        ServerModeratorSystem.ServerIsModerator(byCharacter.Name)
                                            ? 30000
                                            : 5000;

                        if (result.Length > maxLength)
                        {
                            result = result.Substring(0, maxLength) + "... (server's response truncated)";
                        }

                        // send notification to this character
                        ConsoleCommandsSystem.ServerOnConsoleCommandResult(
                            byCharacter,
                            this.ConsoleCommand,
                            result);
                    }
                }
            }
            catch (ConsoleCommandWrongArgumentException ex)
            {
                var commandParameter = this.Parameters[ex.Index];
                var message          = new StringBuilder(capacity: 100)
                                       .Append(ex.IsMissing ? "Argument missing" : "Argument wrong/unknown value provided")
                                       .Append(" - argument #")
                                       .Append(ex.Index + 1)
                                       .Append(' ')
                                       .Append(commandParameter.Name).ToString();

                Api.Logger.Warning(message, byCharacter);

                if (sendNotification)
                {
                    // send notification to this character
                    NotificationSystem.ServerSendNotification(
                        byCharacter,
                        "Command cannot be executed: " + this.ConsoleCommand.Name,
                        message,
                        NotificationColor.Bad);
                }
            }
            catch (Exception ex)
            {
                ex = ex.InnerException ?? ex;
                Api.Logger.Warning(ex.Message, byCharacter);

                if (sendNotification)
                {
                    // send notification to this character
                    NotificationSystem.ServerSendNotification(
                        byCharacter,
                        "Command error: " + this.ConsoleCommand.Name,
                        ex.Message,
                        NotificationColor.Bad);
                }
            }
        }
        public void Execute(ICharacter byCharacter, string[] args)
        {
            var sendNotification = Api.IsServer &&
                                   byCharacter != null;

            try
            {
                var parsedArgs = this.ParseArguments(
                    byCharacter,
                    args,
                    throwExceptionOnUnparsedArgument: true,
                    successfullyParsedArgsCount: out _);
                var resultObj = this.MethodInfo.MethodInfo.Invoke(this.ConsoleCommand, parsedArgs);
                if (resultObj != null)
                {
                    var resultStr = resultObj.ToString();
                    Api.Logger.Important(
                        $"Console command \"{this.ConsoleCommand.Name}\" completed: {resultStr}",
                        byCharacter);

                    if (sendNotification &&
                        resultStr.Length < 10000
                        // it will break formatting so let's not include such results
                        && !resultStr.Contains('['))
                    {
                        // send notification to this character
                        ConsoleCommandsSystem.ServerOnConsoleCommandResult(
                            byCharacter,
                            this.ConsoleCommand,
                            resultStr);
                    }
                }
            }
            catch (ConsoleCommandWrongArgumentException ex)
            {
                var commandParameter = this.Parameters[ex.Index];
                var message          = new StringBuilder(capacity: 100)
                                       .Append(ex.IsMissing ? "Argument missing" : "Argument wrong/unknown value provided")
                                       .Append(" - argument #")
                                       .Append(ex.Index + 1)
                                       .Append(' ')
                                       .Append(commandParameter.Name).ToString();

                Api.Logger.Warning(message, byCharacter);

                if (sendNotification)
                {
                    // send notification to this character
                    NotificationSystem.ServerSendNotification(
                        byCharacter,
                        "Command cannot be executed: " + this.ConsoleCommand.Name,
                        message,
                        NotificationColor.Bad);
                }
            }
            catch (Exception ex)
            {
                ex = ex.InnerException ?? ex;
                Api.Logger.Warning(ex.Message, byCharacter);

                if (sendNotification)
                {
                    // send notification to this character
                    NotificationSystem.ServerSendNotification(
                        byCharacter,
                        "Command error: " + this.ConsoleCommand.Name,
                        ex.Message,
                        NotificationColor.Bad);
                }
            }
        }