Beispiel #1
0
        public override CommandOverload[] GetOverloads()
        {
            CommandOverload[] overloads = new CommandOverload[_sources.Length];
            for (int i = 0; i < overloads.Length; i++)
            {
                ConfigFunctionInfo source = _sources[i];
                overloads[i] = new CommandOverload(
                    typeof(void),
                    source.GetParameters().Select(x => new CommandOverload.Parameter(x.Name, x.Type, Array.Empty <Attribute>())).ToArray(),
                    source.Desc,
                    new CommandOverload.ExampleInfo(null, null, null),
                    x =>
                {
                    var args = new List <object>(x);
                    args.RemoveAt(0);     // Remove command metadata.

                    object result = source.Action.DynamicInvoke(args.ToArray());
                    bool success  = result is bool?(bool)result: true;
                    if (result != null)
                    {
                        args.Insert(0, success);                     // Effectively replace metadata with a success if a success boolean was returned.
                    }
                    return(TaskResult(null, source.Message.DynamicInvoke(args.ToArray()) as string));
                });
            }
            return(overloads);
        }
 private void WriteCommandOverloads(CommandOverload[] overloads, List <string> enumValues, BinaryStream stream)
 {
     stream.WriteUVarInt((uint)overloads.Length);
     for (int i = 0; i < overloads.Length; ++i)
     {
         CommandOverload         overload   = overloads[i];
         List <CommandParameter> parameters = overload.Parameters;
         this.WriteCommandParameters(parameters, enumValues, stream);
     }
 }
Beispiel #3
0
        public override BaseHelpFormatter WithCommand(Command command)
        {
            _embed.WithTitle("Command description");

            StringBuilder sb = new StringBuilder();

            bool countOverloads = false;

            if (command.Overloads.Count > 1)
            {
                countOverloads = true;
            }

            for (int i = 0; i < command.Overloads.Count; i++)
            {
                CommandOverload commandOverload = command.Overloads[i];

                if (countOverloads)
                {
                    sb.AppendLine($"**__Variant {i + 1}__**");
                }

                sb.AppendLine($"```\nsk!{command.Name} {string.Join(' ', commandOverload.Arguments.Select(x => $"[{ x.Name}]").ToList())}```{command.Description}");
                sb.AppendLine();

                if (command.Aliases?.Count != 0)
                {
                    sb.AppendLine("**Aliases:**");
                    foreach (string alias in command.Aliases)
                    {
                        sb.AppendLine(alias);
                    }

                    sb.AppendLine();
                }

                sb.AppendLine("**Arguments:**");
                foreach (var c in commandOverload.Arguments)
                {
                    sb.AppendLine($"`{c.Name}`: {c.Description}");
                }
                sb.AppendLine();

                //if (command.ExecutionChecks?.Count != 0)
                //{
                //    sb.AppendLine("**Execution checks:**");
                //    sb.AppendLine(string.Join(' ', command.ExecutionChecks.Select(x => x.ToString().Split('.').Last())));
                //}
                //sb.AppendLine();
            }
            _embed.WithDescription(sb.ToString());

            return(this);
        }
Beispiel #4
0
        internal CommandOverload Build()
        {
            var ovl = new CommandOverload()
            {
                Arguments = this.Arguments,
                Priority  = this.Priority,
                Callable  = this.Callable
            };

            return(ovl);
        }
        internal CommandOverload Build()
        {
            var ovl = new CommandOverload()
            {
                Arguments        = this.Arguments,
                Priority         = this.Priority,
                Callable         = this.Callable,
                InvocationTarget = this.InvocationTarget
            };

            return(ovl);
        }
Beispiel #6
0
        public override CommandOverload[] GetOverloads()
        {
            CommandOverload[] overloads = new CommandOverload[_sources.Length];
            for (int i = 0; i < overloads.Length; i++)
            {
                ConfigFunctionInfo source = _sources[i];
                overloads[i] = new CommandOverload(
                    typeof(void),
                    source.GetParameters().Select(x => new CommandOverload.Parameter(x.Name, x.Type, Array.Empty <Attribute>())).ToArray(),
                    source.Desc,
                    new CommandOverload.ExampleInfo(null, null, null),
                    x =>
                {
                    var args = new List <object>(x);
                    args.RemoveAt(0);     // Remove command metadata.

                    source.Action.DynamicInvoke(args.ToArray());
                    return(TaskResult(null, source.Message.DynamicInvoke(args.ToArray()) as string));
                });
            }
            return(overloads);
        }
        public override void Encode()
        {
            base.Encode();

            List <CommandEnumCash> enums      = new List <CommandEnumCash>();
            List <string>          enumValues = new List <string>();
            List <string>          postFixes  = new List <string>();

            byte[] result = null;
            using (BinaryStream stream = new BinaryStream())
            {
                foreach (Command command in this.commands.Values)
                {
                    if (command.Name == "help")
                    {
                        continue;
                    }

                    stream.WriteString(command.Name);
                    if (!string.IsNullOrEmpty(command.Description))
                    {
                        if (command.Description[0] == '%')
                        {
                            stream.WriteString(command.Description.Remove(0, 1));
                        }
                        else
                        {
                            stream.WriteString(command.Description);
                        }
                    }
                    else
                    {
                        stream.WriteString(command.Description);
                    }
                    stream.WriteByte((byte)command.Flag);
                    stream.WriteByte((byte)command.CommandPermission);

                    int enumIndex = -1;
                    if (command.Aliases != null && command.Aliases.Length > 0)
                    {
                        List <int> aliases = new List <int>();
                        for (int i = 0; i < command.Aliases.Length; ++i)
                        {
                            enumValues.Add(command.Aliases[i]);
                            aliases.Add(enumValues.Count - 1);
                        }
                        enumValues.Add(command.Name);
                        aliases.Add(enumValues.Count - 1);

                        enums.Add(new CommandEnumCash($"{command.Name}CommandAliases", aliases.ToArray()));
                        enumIndex = enums.Count - 1;
                    }
                    stream.WriteLInt((uint)enumIndex);

                    CommandOverload[] overloads = command.CommandOverloads;
                    stream.WriteUVarInt((uint)overloads.Length);
                    for (int i = 0; i < overloads.Length; ++i)
                    {
                        CommandOverload         overload   = overloads[i];
                        List <CommandParameter> parameters = overload.Parameters;
                        stream.WriteUVarInt((uint)parameters.Count);
                        for (int j = 0; j < parameters.Count; ++j)
                        {
                            CommandParameter parameter = parameters[j];
                            stream.WriteString(parameter.Name);
                            int type = parameter.Type;
                            if (parameter.CommandEnum != null && parameter.CommandEnum.Values.Length > 0)
                            {
                                CommandEnum commandEnum = parameter.CommandEnum;
                                List <int>  realValue   = new List <int>();
                                for (int k = 0; k < commandEnum.Values.Length; ++k)
                                {
                                    string value = commandEnum.Values[k];
                                    enumValues.Add(value);
                                    realValue.Add(enumValues.Count - 1);
                                }
                                enums.Add(new CommandEnumCash(commandEnum.Name, realValue.ToArray()));
                                enumIndex = enums.Count - 1;
                                type      = CommandParameter.ARG_FLAG_ENUM | CommandParameter.ARG_FLAG_VALID | enumIndex;
                            }
                            else if (parameter.Postfix != null && parameter.Postfix.Length > 0)
                            {
                                postFixes.Add(parameter.Postfix);
                                int key = postFixes.Count - 1;
                                type |= CommandParameter.ARG_FLAG_VALID | CommandParameter.ARG_FLAG_POSTFIX | key;
                            }
                            else
                            {
                                type |= CommandParameter.ARG_FLAG_VALID;
                            }
                            stream.WriteLInt((uint)type);
                            stream.WriteBool(parameter.Optional);
                        }
                    }
                }
                result = stream.ToArray();
            }

            this.WriteUVarInt((uint)enumValues.Count);
            for (int i = 0; i < enumValues.Count; ++i)
            {
                this.WriteString(enumValues[i]);
            }

            this.WriteUVarInt((uint)postFixes.Count);
            for (int i = 0; i < postFixes.Count; ++i)
            {
                this.WriteString(postFixes[i]);
            }

            this.WriteUVarInt((uint)enums.Count);
            for (int i = 0; i < enums.Count; ++i)
            {
                CommandEnumCash cash = enums[i];
                this.WriteString(cash.Name);
                this.WriteUVarInt((uint)cash.Index.Length);
                for (int j = 0; j < cash.Index.Length; ++j)
                {
                    if (enumValues.Count < byte.MaxValue)
                    {
                        this.WriteByte((byte)cash.Index[j]);
                    }
                    else if (enumValues.Count < ushort.MaxValue)
                    {
                        this.WriteLShort((ushort)cash.Index[j]);
                    }
                    else
                    {
                        this.WriteLInt((uint)cash.Index[j]);
                    }
                }
            }

            this.WriteUVarInt((uint)this.commands.Count);
            this.WriteBytes(result);
        }