Exemplo n.º 1
0
        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
                        NotificationSystem.ServerSendNotification(
                            byCharacter,
                            "Command executed: " + this.ConsoleCommand.Name,
                            resultStr.Replace("\n", "[br]"),
                            NotificationColor.Neutral);
                    }
                }
            }
            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);
                }
            }
        }
Exemplo n.º 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);
                }
            }
        }