Пример #1
0
        // Reads a line in the script as a command.
        protected virtual bool PerformCommand(string commandName, CommandParam parameters)
        {
            List <string> matchingFormats = new List <string>();
            CommandParam  newParams       = null;

            // Search for the correct command.
            for (int i = 0; i < commands.Count; i++)
            {
                ScriptCommand command = commands[i];
                if (command.HasName(commandName))
                {
                    if (command.HasParameters(parameters, out newParams))
                    {
                        // Run the command.
                        command.Action(newParams);
                        return(true);
                    }
                    else
                    {
                        // Preemptively append the possible overloads to the error message.
                        for (int j = 0; j < command.ParameterOverloads.Count; j++)
                        {
                            matchingFormats.Add(command.Name + " " +
                                                CommandParamParser.ToString(command.ParameterOverloads[j]));
                        }
                    }
                }
            }

            // Throw an error because the command was not found.
            if (matchingFormats.Count > 0)
            {
                Console.WriteLine(CommandParamParser.ToString(parameters));
                string msg = "No matching overload found for the command " + commandName + "\n";
                msg += "Possible overloads include:\n";
                for (int i = 0; i < matchingFormats.Count; i++)
                {
                    msg += "  * " + matchingFormats[i];
                    if (i + 1 < matchingFormats.Count)
                    {
                        msg += "\n";
                    }
                }
                ThrowCommandParseError(msg);
            }
            else
            {
                ThrowCommandParseError(commandName + " is not a valid command");
            }

            return(false);
        }