private bool ExecuteCommand(MethodInfo method, Player player, string[] args, CommandAttribute commandAttribute) { var parameters = method.GetParameters(); var addLenght = 0; int requiredParameters = 0; if (parameters.Length > 0 && parameters[0].ParameterType == typeof (Player)) { addLenght = 1; requiredParameters = -1; } bool hasRequiredParameters = true; bool hasStringArray = false; foreach (var param in parameters) { if (!param.IsOptional) requiredParameters++; if (param.ParameterType == typeof (string[])) hasStringArray = true; } if (args.Length < requiredParameters && !hasStringArray) hasRequiredParameters = false; if (!hasRequiredParameters || args.Length > (parameters.Length - addLenght) && !hasStringArray) { player.SendChat("Invalid command usage!", ChatColor.Red); player.SendChat(commandAttribute.Usage, ChatColor.Red); return true; } var objectArgs = new object[parameters.Length]; bool stringarrayfound = false; int stringarrayposition = 0; List<string> stringarrayvalues = new List<string>(); int length = args.Length + addLenght; for (var k = 0; k < length; k++) { var parameter = parameters[k]; var i = k - addLenght; if (k == 0 && addLenght == 1) { if (parameter.ParameterType == typeof (Player)) { objectArgs[k] = player; continue; } ConsoleFunctions.WriteWarningLine("Command method " + method.Name + " missing Player as first argument."); return false; } if (parameter.ParameterType == typeof (string[])) { stringarrayfound = true; stringarrayposition = k; stringarrayvalues.Add(args[i]); objectArgs[stringarrayposition] = stringarrayvalues.ToArray(); break; } if (parameter.ParameterType == typeof (string)) { objectArgs[k] = args[i]; continue; } if (parameter.ParameterType == typeof (byte)) { byte value; if (!byte.TryParse(args[i], out value)) return false; objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof (short)) { short value; if (!short.TryParse(args[i], out value)) return false; objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof (int)) { int value; if (!int.TryParse(args[i], out value)) return false; objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof (bool)) { bool value; if (!bool.TryParse(args[i], out value)) return false; objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof (float)) { float value; if (!float.TryParse(args[i], out value)) return false; objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof (double)) { double value; if (!double.TryParse(args[i], out value)) return false; objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(Player)) { Player value = Globals.LevelManager.GetAllPlayers().FirstOrDefault(p => p.Username.ToLower().Equals(args[i].ToLower())); if (value == null) { player.SendChat(String.Format("Player \"{0}\" is not found!", args[i]), ChatColor.Red); return true; } objectArgs[k] = value; continue; } return false; } if (stringarrayfound) { for (int k = stringarrayposition + 1; k <= args.Length; k++) { var i = k - addLenght; stringarrayvalues.Add(args[i]); objectArgs[stringarrayposition] = stringarrayvalues.ToArray(); } } var pluginInstance = _plugins.FirstOrDefault(plugin => plugin.GetType() == method.DeclaringType); if (pluginInstance == null) { ConsoleFunctions.WriteDebugLine("Plugin instance is null!"); return false; } if (method.IsStatic) { method.Invoke(null, objectArgs); } else { if (method.DeclaringType == null) return false; method.Invoke(pluginInstance, objectArgs); } return true; }
private bool ExecuteCommand(MethodInfo method, Player player, string[] args, CommandAttribute commandAttribute) { var parameters = method.GetParameters(); var addLenght = 0; int requiredParameters = 0; if (parameters.Length > 0 && parameters[0].ParameterType == typeof(Player)) { addLenght = 1; requiredParameters = -1; } bool hasRequiredParameters = true; bool hasStringArray = false; foreach (var param in parameters) { if (!param.IsOptional) { requiredParameters++; } if (param.ParameterType == typeof(string[])) { hasStringArray = true; } } if (args.Length < requiredParameters && !hasStringArray) { hasRequiredParameters = false; } if (!hasRequiredParameters || args.Length > (parameters.Length - addLenght) && !hasStringArray) { player.SendChat("Invalid command usage!", ChatColor.Red); player.SendChat(commandAttribute.Usage, ChatColor.Red); return(true); } var objectArgs = new object[parameters.Length]; bool stringarrayfound = false; int stringarrayposition = 0; List <string> stringarrayvalues = new List <string>(); int length = args.Length + addLenght; for (var k = 0; k < length; k++) { var parameter = parameters[k]; var i = k - addLenght; if (k == 0 && addLenght == 1) { if (parameter.ParameterType == typeof(Player)) { objectArgs[k] = player; continue; } ConsoleFunctions.WriteWarningLine("Command method " + method.Name + " missing Player as first argument."); return(false); } if (parameter.ParameterType == typeof(string[])) { stringarrayfound = true; stringarrayposition = k; stringarrayvalues.Add(args[i]); objectArgs[stringarrayposition] = stringarrayvalues.ToArray(); break; } if (parameter.ParameterType == typeof(string)) { objectArgs[k] = args[i]; continue; } if (parameter.ParameterType == typeof(byte)) { byte value; if (!byte.TryParse(args[i], out value)) { return(false); } objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(short)) { short value; if (!short.TryParse(args[i], out value)) { return(false); } objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(int)) { int value; if (!int.TryParse(args[i], out value)) { return(false); } objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(bool)) { bool value; if (!bool.TryParse(args[i], out value)) { return(false); } objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(float)) { float value; if (!float.TryParse(args[i], out value)) { return(false); } objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(double)) { double value; if (!double.TryParse(args[i], out value)) { return(false); } objectArgs[k] = value; continue; } if (parameter.ParameterType == typeof(Player)) { Player value = Globals.LevelManager.GetAllPlayers().FirstOrDefault(p => p.Username.ToLower().Equals(args[i].ToLower())); if (value == null) { player.SendChat(String.Format("Player \"{0}\" is not found!", args[i]), ChatColor.Red); return(true); } objectArgs[k] = value; continue; } return(false); } if (stringarrayfound) { for (int k = stringarrayposition + 1; k <= args.Length; k++) { var i = k - addLenght; stringarrayvalues.Add(args[i]); objectArgs[stringarrayposition] = stringarrayvalues.ToArray(); } } var pluginInstance = _plugins.FirstOrDefault(plugin => plugin.GetType() == method.DeclaringType); if (pluginInstance == null) { ConsoleFunctions.WriteDebugLine("Plugin instance is null!"); return(false); } if (method.IsStatic) { method.Invoke(null, objectArgs); } else { if (method.DeclaringType == null) { return(false); } method.Invoke(pluginInstance, objectArgs); } return(true); }