private Command GetCommand(CommandSet commands, string commandName) { Command command; 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))); } return(command); }
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; } if (string.IsNullOrEmpty(commandAttribute.Name)) { commandAttribute.Name = method.Name; } var overload = new Overload { Description = commandAttribute.Description ?? "", Aliases = commandAttribute.Aliases ?? new string[0], IsHidden = commandAttribute.IsHidden, UseFullDescription = commandAttribute.UseFullDescription, Method = method, }; string commandName = commandAttribute.Name.ToLowerInvariant(); string uuid = commandAttribute.Overload ?? Guid.NewGuid().ToString(); if (commands.ContainsKey(commandName)) { Command command = commands[commandName]; command.Overloads.Add(uuid, overload); } else { commands.Add(commandName, new Command { Name = commandName, Overloads = new Dictionary <string, Overload> { { uuid, overload } } }); } } return(commands); }
private 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; } // TODO: Do we need the authorize attribute with the new permission system? 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 MiNET.Plugins.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(OpenPlayer).IsAssignableFrom(parameter.ParameterType)) { continue; } isFirstParam = false; if (PluginManager.Services.TryResolve(parameter.ParameterType, out _)) { //This is a depencency injected param continue; } 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"; } else if (parameter.ParameterType == typeof(BlockTypeEnum)) { param.EnumValues = new string[] { }; param.EnumType = "Block"; } else if (parameter.ParameterType == typeof(EntityTypeEnum)) { param.EnumValues = new string[] { }; param.EnumType = "EntityType"; } else if (parameter.ParameterType == typeof(CommandNameEnum)) { param.EnumValues = new string[] { }; param.EnumType = "CommandName"; } else if (parameter.ParameterType == typeof(EnchantEnum)) { param.EnumValues = new string[] { "enchant_test" }; param.EnumType = "Enchant"; } else if (parameter.ParameterType == typeof(EffectEnum)) { param.EnumValues = new string[] { "effect_test" }; param.EnumType = "Effect"; } else if (typeof(CustomEnum).IsAssignableFrom(parameter.ParameterType)) { CustomEnum c = parameter.ParameterType.GetConstructor(Type.EmptyTypes).Invoke(null) as CustomEnum; if (c != null) { param.EnumValues = c.GetValues(); param.EnumType = c.EnumType; //param.EnumType = c. } } } } inputParams.Add(param); } if (inputParams.Count == 0) { overload.Input.Parameters = null; } else { overload.Input.Parameters = inputParams.ToArray(); } } return(commands); }
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, Method = method, Input = new Input(), Output = new Output() { FormatStrings = new[] { new FormatString() { Format = "{0}" }, }, Parameters = new[] { new Parameter { Name = "result", Type = "string" }, } } }; string commandName = commandAttribute.Name.ToLowerInvariant(); 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(), Aliases = commandAttribute.Aliases, Description = commandAttribute.Description, Overloads = new Dictionary <string, Overload> { { "default", overload }, } }, } }); } var parameters = method.GetParameters(); bool isFirstParam = true; List <Parameter> inputParams = new List <Parameter>(); 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("stringenum")) { if (parameter.ParameterType.IsEnum) { param.EnumValues = parameter.ParameterType.GetEnumNames().Select(s => s.ToLowerInvariant()).ToArray(); } else { string typeName = parameter.ParameterType.Name; typeName = typeName.Replace("Enum", ""); typeName = typeName.ToLowerInvariant()[0] + typeName.Substring(1); param.EnumType = typeName; } } inputParams.Add(param); } if (inputParams.Count == 0) { overload.Input.Parameters = null; } else { overload.Input.Parameters = inputParams.ToArray(); } // Output objects if (method.ReturnType != typeof(void)) { var properties = method.ReturnType.GetProperties(BindingFlags.Public | BindingFlags.Instance); List <Parameter> outputParams = new List <Parameter>(); foreach (PropertyInfo property in properties) { if (property.Name.Equals("StatusCode")) { continue; } if (property.Name.Equals("SuccessCount")) { continue; } Parameter param = new Parameter(); param.Name = ToCamelCase(property.Name); param.Type = GetPropertyType(property); outputParams.Add(param); } overload.Output.Parameters = outputParams.ToArray(); } if (commandAttribute.OutputFormatStrings != null) { overload.Output.FormatStrings = new FormatString[commandAttribute.OutputFormatStrings.Length]; int i = 0; foreach (var formatString in commandAttribute.OutputFormatStrings) { overload.Output.FormatStrings[i] = new FormatString() { Format = commandAttribute.OutputFormatStrings[i] }; i++; } } } return(commands); }