Esempio n. 1
0
        public object HandleCommand(Player player, string commandName, string commandOverload, dynamic commandInputJson)
        {
            Log.Debug($"HandleCommand {commandName}");

            try
            {
                Command command = null;
                if (Commands.ContainsKey(commandName))
                {
                    command = Commands[commandName];
                }
                else
                {
                    command = Commands.Values.FirstOrDefault(cmd => cmd.Versions.Any(version => version.Aliases != null && version.Aliases.Any(s => s == commandName)));
                }

                if (command == null)
                {
                    Log.Warn($"Found no command handler for {commandName}");
                    return(null);
                }

                Overload overload = command.Versions.First().Overloads[commandOverload];

                int requiredPermission = command.Versions.First().CommandPermission;
                if (player.CommandPermission < requiredPermission)
                {
                    Log.Debug($"Insufficient permissions. Require {requiredPermission} but player had {player.CommandPermission}");
                    return(null);
                }

                MethodInfo method = overload.Method;

                List <string> strings = new List <string>();
                if (commandInputJson != null)
                {
                    foreach (ParameterInfo parameter in method.GetParameters())
                    {
                        if (typeof(Player).IsAssignableFrom(parameter.ParameterType))
                        {
                            continue;
                        }

                        if (HasProperty(commandInputJson, parameter.Name))
                        {
                            Log.Debug($"Parameter: {commandInputJson[ToCamelCase(parameter.Name)].ToString()}");
                            strings.Add(commandInputJson[ToCamelCase(parameter.Name)].ToString());
                        }
                    }
                }

                ExecuteCommand(method, player, strings.ToArray(), out object retVal);
                return(retVal);
            }
            catch (Exception e)
            {
                Log.Error("Handle JSON command", e);
            }

            return(null);
        }
Esempio n. 2
0
        public static CommandSet GenerateCommandSet(MethodInfo[] methods)
        {
            CommandSet commands = new CommandSet();

            foreach (MethodInfo method in methods)
            {
                CommandAttribute commandAttribute = Attribute.GetCustomAttribute(method, typeof(CommandAttribute), false) as CommandAttribute;
                if (commandAttribute == null)
                {
                    continue;
                }

                AuthorizeAttribute authorizeAttribute = Attribute.GetCustomAttribute(method, typeof(AuthorizeAttribute), false) as AuthorizeAttribute ?? new AuthorizeAttribute();

                if (string.IsNullOrEmpty(commandAttribute.Name))
                {
                    commandAttribute.Name = method.Name;
                }

                var overload = new Overload
                {
                    Description = commandAttribute.Description ?? "Bullshit",
                    Method      = method,
                    Input       = new Input(),
                };

                string    commandName      = commandAttribute.Name.ToLowerInvariant();
                var       split            = commandName.Split(' ');
                Parameter subCommmandParam = null;
                if (split.Length > 1)
                {
                    subCommmandParam            = new Parameter();
                    subCommmandParam.Name       = "subcommand";
                    subCommmandParam.Type       = "stringenum";
                    subCommmandParam.EnumType   = "SubCommand" + commandName.Replace(" ", "-");
                    subCommmandParam.EnumValues = new[] { split[1] };
                    commandName = split[0];
                }
                if (commands.ContainsKey(commandName))
                {
                    Command command = commands[commandName];
                    command.Versions.First().Overloads.Add(commandAttribute.Overload ?? Guid.NewGuid().ToString(), overload);
                }
                else
                {
                    commands.Add(commandName, new Command
                    {
                        Name     = commandName,
                        Versions = new[]
                        {
                            new Version
                            {
                                Permission        = authorizeAttribute.Permission.ToString().ToLowerInvariant(),
                                CommandPermission = authorizeAttribute.Permission,
                                ErrorMessage      = authorizeAttribute.ErrorMessage,
                                Aliases           = commandAttribute.Aliases ?? new string[0],
                                Description       = commandAttribute.Description ?? "",
                                Overloads         = new Dictionary <string, Overload>
                                {
                                    {
                                        "default", overload
                                    },
                                }
                            },
                        }
                    });
                }


                List <Parameter> inputParams = new List <Parameter>();
                if (subCommmandParam != null)
                {
                    inputParams.Add(subCommmandParam);
                }

                var  parameters   = method.GetParameters();
                bool isFirstParam = true;
                foreach (var parameter in parameters)
                {
                    if (isFirstParam && typeof(Player).IsAssignableFrom(parameter.ParameterType))
                    {
                        continue;
                    }
                    isFirstParam = false;

                    Parameter param = new Parameter();
                    param.Name     = ToCamelCase(parameter.Name);
                    param.Type     = GetParameterType(parameter);
                    param.Optional = parameter.IsOptional;
                    if (param.Type.Equals("bool"))
                    {
                        param.Type       = "stringenum";
                        param.EnumType   = "bool";
                        param.EnumValues = new string[] { "false", "true" };
                    }
                    else if (param.Type.Equals("stringenum"))
                    {
                        if (parameter.ParameterType.IsEnum)
                        {
                            param.EnumValues = parameter.ParameterType.GetEnumNames().Select(s => s.ToLowerInvariant()).ToArray();

                            string typeName = parameter.ParameterType.Name;
                            typeName       = typeName.Replace("Enum", "");
                            typeName       = typeName.ToLowerInvariant()[0] + typeName.Substring(1);
                            param.EnumType = typeName;
                        }
                        else
                        {
                            param.EnumValues = null;

                            string typeName = parameter.ParameterType.Name;
                            typeName       = typeName.Replace("Enum", "");
                            typeName       = typeName.ToLowerInvariant()[0] + typeName.Substring(1);
                            param.EnumType = typeName;

                            if (parameter.ParameterType == typeof(ItemTypeEnum))
                            {
                                param.EnumValues = new string[] { };
                                param.EnumType   = "Item";
                            }
                            if (parameter.ParameterType == typeof(BlockTypeEnum))
                            {
                                param.EnumValues = new string[] { };
                                param.EnumType   = "Block";
                            }
                            if (parameter.ParameterType == typeof(EntityTypeEnum))
                            {
                                param.EnumValues = new string[] { };
                                param.EnumType   = "EntityType";
                            }
                            if (parameter.ParameterType == typeof(CommandNameEnum))
                            {
                                param.EnumValues = new string[] { };
                                param.EnumType   = "CommandName";
                            }
                            if (parameter.ParameterType == typeof(EnchantEnum))
                            {
                                param.EnumValues = new string[] { "enchant_test" };
                                param.EnumType   = "Enchant";
                            }
                            if (parameter.ParameterType == typeof(EffectEnum))
                            {
                                param.EnumValues = new string[] { "effect_test" };
                                param.EnumType   = "Effect";
                            }
                        }
                    }
                    inputParams.Add(param);
                }

                if (inputParams.Count == 0)
                {
                    overload.Input.Parameters = null;
                }
                else
                {
                    overload.Input.Parameters = inputParams.ToArray();
                }
            }

            return(commands);
        }