Exemplo n.º 1
0
        public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
        {
            switch (CommandRights)
            {
            case Admin:
                if (!info.IsAdmin.Value)
                {
                    throw new CommandException("Command must be invoked by an admin!");
                }
                break;

            case Public:
                if (info.TextMessage.Target != MessageTarget.Server && !info.IsAdmin.Value)
                {
                    throw new CommandException("Command must be used in public mode!");
                }
                break;

            case Private:
                if (info.TextMessage.Target != MessageTarget.Private && !info.IsAdmin.Value)
                {
                    throw new CommandException("Command must be used in a private session!");
                }
                break;
            }
            return(base.Execute(info, arguments, returnTypes));
        }
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
 {
     // Make arguments lazy, we only want to execute them once
     arguments = arguments.Select(c => new LazyCommand(c));
     foreach (FunctionCommand f in Functions)
     {
         bool fits = false;
         try
         {
             // Find out if this overload works
             int i;
             f.FitArguments(info, arguments, returnTypes, out i);
             fits = true;
         }
         catch (CommandException)
         {
             // Do nothing, just move on to the next function
         }
         if (fits)
         {
             // Call this overload
             return(f.Execute(info, arguments, returnTypes));
         }
     }
     throw new CommandException("No matching function could be found");
 }
Exemplo n.º 3
0
        public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
        {
            string result;

            if (!arguments.Any())
            {
                if (returnTypes.Contains(CommandResultType.Command))
                {
                    return(new CommandCommandResult(this));
                }
                result = string.Empty;
            }
            else
            {
                var comResult = arguments.First().Execute(info, Enumerable.Empty <ICommand>(), new CommandResultType[] { CommandResultType.String });
                result = ((StringCommandResult)comResult).Content;
            }

            var commandResults = XCommandSystem.FilterList(commands, result);

            if (commandResults.Skip(1).Any())
            {
                throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall);
            }

            var argSubList = arguments.Skip(1).ToArray();

            return(commandResults.First().Value.Execute(info, argSubList, returnTypes));
        }
Exemplo n.º 4
0
        public ICommandResult Execute(ExecutionInformation info, string command, IEnumerable <CommandResultType> returnTypes)
        {
            var ast = CommandParser.ParseCommandRequest(command);
            var cmd = AstToCommandResult(ast);

            return(cmd.Execute(info, Enumerable.Empty <ICommand>(), returnTypes));
        }
Exemplo n.º 5
0
        public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
        {
            switch (CommandRights)
            {
            case Admin:
                if (!info.IsAdmin)
                {
                    throw new CommandException("Command must be invoked by an admin!", CommandExceptionReason.MissingRights);
                }
                break;

            case Public:
                if (!info.ApiCall && info.TextMessage.Target != TextMessageTargetMode.Server && !info.IsAdmin)                 // TODO: Apicall special ??
                {
                    throw new CommandException("Command must be used in public mode!", CommandExceptionReason.MissingRights);
                }
                break;

            case Private:
                if (!info.ApiCall && info.TextMessage.Target != TextMessageTargetMode.Private && !info.IsAdmin)                 // TODO: Apicall special ??
                {
                    throw new CommandException("Command must be used in a private session!", CommandExceptionReason.MissingRights);
                }
                break;
            }
            return(base.Execute(info, arguments, returnTypes));
        }
Exemplo n.º 6
0
        public static object Execute(ExecutionInformation info, string command, IReadOnlyList <Type> returnTypes)
        {
            var ast = CommandParser.ParseCommandRequest(command);
            var cmd = AstToCommandResult(ast);

            return(cmd.Execute(info, Array.Empty <ICommand>(), returnTypes));
        }
Exemplo n.º 7
0
        public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            string result;

            if (arguments.Count == 0)
            {
                if (returnTypes.Contains(CommandResultType.Command))
                {
                    return(new CommandCommandResult(this));
                }
                result = string.Empty;
            }
            else
            {
                var comResult = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String });
                result = ((StringCommandResult)comResult).Content;
            }

            var commandResults = XCommandSystem.FilterList(commands, result).ToArray();

            if (commandResults.Length > 1)
            {
                throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall);
            }
            if (commandResults.Length == 0)
            {
                throw new CommandException("No matching command", CommandExceptionReason.AmbiguousCall);
            }


            var argSubList = arguments.TrySegment(1);

            return(commandResults[0].Value.Execute(info, argSubList, returnTypes));
        }
Exemplo n.º 8
0
 public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
 {
     // Make arguments lazy, we only want to execute them once
     arguments = arguments.Select(c => new LazyCommand(c)).ToArray();
     foreach (var f in Functions)
     {
         bool fits = false;
         try
         {
             // Find out if this overload works
             f.FitArguments(info, arguments, returnTypes, out var _);
             fits = true;
         }
         catch (CommandException)
         {
             // Do nothing, just move on to the next function
         }
         if (fits)
         {
             // Call this overload
             return(f.Execute(info, arguments, returnTypes));
         }
     }
     throw new CommandException("No matching function could be found", CommandExceptionReason.FunctionNotFound);
 }
Exemplo n.º 9
0
        public static async Task <ICmdResult> Execute(ExecutionInformation info, string command)
        {
            var ast = CommandParser.ParseCommandRequest(command);
            var cmd = AstToCommandResult(ast);

            return(new ICmdResult(await cmd.Execute(info, Array.Empty <ICommand>())));
        }
Exemplo n.º 10
0
        public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            var merged = new ICommand[internArguments.Count + arguments.Count];

            internArguments.CopyTo(0, merged, 0);
            arguments.CopyTo(0, merged, internArguments.Count);
            return(internCommand.Execute(info, merged, returnTypes));
        }
Exemplo n.º 11
0
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
 {
     if (!info.HasRights(requiredRights))
     {
         throw new CommandException($"You cannot execute \"{InvokeName}\". You are missing the \"{RequiredRight}\" right.!",
                                    CommandExceptionReason.MissingRights);
     }
     return(base.Execute(info, arguments, returnTypes));
 }
Exemplo n.º 12
0
        public override ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            if (!info.HasRights(requiredRights))
            {
                throw new CommandException(string.Format(strings.error_missing_right, InvokeName, RequiredRight),
                                           CommandExceptionReason.MissingRights);
            }

            return(base.Execute(info, arguments, returnTypes));
        }
Exemplo n.º 13
0
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
 {
     if (result == null)
     {
         result = innerCommand.Execute(info, arguments, returnTypes);
         return result;
     }
     // Check if we can return that type
     if (!returnTypes.Contains(result.ResultType))
         throw new CommandException("The cached result can't be returned");
     return result;
 }
Exemplo n.º 14
0
        public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
        {
            if (!arguments.Any())
                return base.Execute(info, arguments, returnTypes);

            var result = arguments.First().Execute(info, Enumerable.Empty<ICommand>(), new CommandResultType[] { CommandResultType.Command, CommandResultType.String });
            if (result.ResultType == CommandResultType.String)
                // Use cached result so we don't execute the first argument twice
                return base.Execute(info, new ICommand[] { new StringCommand(((StringCommandResult)result).Content) }
                                    .Concat(arguments.Skip(1)), returnTypes);

            return ((CommandCommandResult)result).Command.Execute(info, arguments.Skip(1), returnTypes);
        }
Exemplo n.º 15
0
 public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
 {
     if (result == null)
     {
         result = innerCommand.Execute(info, arguments, returnTypes);
         return(result);
     }
     // Check if we can return that type
     if (!returnTypes.Contains(result.ResultType))
     {
         throw new CommandException("The cached result can't be returned", CommandExceptionReason.NoReturnMatch);
     }
     return(result);
 }
Exemplo n.º 16
0
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
 {
     if (result == null)
     {
         result = innerCommand.Execute(info, arguments, returnTypes);
         return(result);
     }
     // Check if we can return that type
     if (!returnTypes.Contains(result.ResultType))
     {
         throw new CommandException("The cached result can't be returned");
     }
     return(result);
 }
Exemplo n.º 17
0
        public string ExecuteCommand(ExecutionInformation info, string command)
        {
            var result = Execute(info, command);

            if (result.ResultType == CommandResultType.String)
            {
                return(result.ToString());
            }
            if (result.ResultType == CommandResultType.Empty)
            {
                return(null);
            }
            throw new CommandException("Expected a string or nothing as result", CommandExceptionReason.NoReturnMatch);
        }
Exemplo n.º 18
0
        public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
        {
            if (!arguments.Any())
            {
                return(base.Execute(info, arguments, returnTypes));
            }

            var result = arguments.First().Execute(info, Enumerable.Empty <ICommand>(), new CommandResultType[] { CommandResultType.Command, CommandResultType.String });

            if (result.ResultType == CommandResultType.String)
            {
                // Use cached result so we don't execute the first argument twice
                return(base.Execute(info, new ICommand[] { new StringCommand(((StringCommandResult)result).Content) }
                                    .Concat(arguments.Skip(1)), returnTypes));
            }

            return(((CommandCommandResult)result).Command.Execute(info, arguments.Skip(1), returnTypes));
        }
Exemplo n.º 19
0
        public override ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            if (arguments.Count == 0)
            {
                return(base.Execute(info, arguments, returnTypes));
            }

            var result = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.Command, CommandResultType.String });

            if (result.ResultType == CommandResultType.String)
            {
                // Use cached result so we don't execute the first argument twice
                var passArgs = new ICommand[arguments.Count];
                passArgs[0] = new StringCommand(((StringCommandResult)result).Content);
                arguments.CopyTo(1, passArgs, 1);
                return(base.Execute(info, passArgs, returnTypes));
            }
            return(((CommandCommandResult)result).Command.Execute(info, arguments.TrySegment(1), returnTypes));
        }
Exemplo n.º 20
0
		public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
		{
			string result;
			if (!arguments.Any())
			{
				if (returnTypes.Contains(CommandResultType.Command))
					return new CommandCommandResult(this);
				result = string.Empty;
			}
			else
			{
				var comResult = arguments.First().Execute(info, Enumerable.Empty<ICommand>(), new CommandResultType[] { CommandResultType.String });
				result = ((StringCommandResult)comResult).Content;
			}

			var commandResults = XCommandSystem.FilterList(commands, result);
			if (commandResults.Skip(1).Any())
				throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)));

			var argSubList = arguments.Skip(1).ToArray();
			return commandResults.First().Value.Execute(info, argSubList, returnTypes);
		}
Exemplo n.º 21
0
 public static string ExecuteCommand(ExecutionInformation info, IReadOnlyList <ICommand> arguments)
 => CastResult(Execute(info, arguments, ReturnStringOrNothing));
Exemplo n.º 22
0
 public static string ExecuteCommand(ExecutionInformation info, string command)
 => CastResult(Execute(info, command, ReturnStringOrNothing));
Exemplo n.º 23
0
 public static object Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <Type> returnTypes)
 => info.GetModule <CommandManager>().RootGroup.Execute(info, arguments, returnTypes);
Exemplo n.º 24
0
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
 {
     return internCommand.Execute(info, internArguments.Concat(arguments), returnTypes);
 }
Exemplo n.º 25
0
 public ICommandResult Execute(ExecutionInformation info, string command)
 {
     return(Execute(info, command, new[] { CommandResultType.String, CommandResultType.Empty }));
 }
Exemplo n.º 26
0
        public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            // Make arguments lazy, we only want to execute them once
            arguments = arguments.Select(c => new LazyCommand(c)).ToArray();
            object[] parameters = FitArguments(info, arguments, returnTypes, out int availableArguments);

            // Check if we were able to set enough arguments
            if (availableArguments < Math.Min(parameters.Length, RequiredParameters))
            {
                if (returnTypes.Contains(CommandResultType.Command))
                {
                    return(arguments.Any()
                                                ? new CommandCommandResult(new AppliedCommand(this, arguments))
                                                : new CommandCommandResult(this));
                }
                throw new CommandException("Not enough arguments for function " + internCommand.Name, CommandExceptionReason.MissingParameter);
            }

            if (CommandReturn == typeof(ICommandResult))
            {
                return((ICommandResult)ExecuteFunction(parameters));
            }

            bool   executed = false;
            object result   = null;

            // Take first fitting command result
            foreach (var returnType in returnTypes)
            {
                switch (returnType)
                {
                case CommandResultType.Command:
                    // Return a command if we can take more arguments
                    if (CommandParameter.Any(p => p == typeof(string[])) || availableArguments < NormalParameters)
                    {
                        return(new CommandCommandResult(new AppliedCommand(this, arguments)));
                    }
                    break;

                case CommandResultType.Empty:
                    if (!executed)
                    {
                        ExecuteFunction(parameters);
                    }
                    return(new EmptyCommandResult());

                case CommandResultType.String:
                    if (!executed)
                    {
                        result   = ExecuteFunction(parameters);
                        executed = true;
                    }
                    if (!string.IsNullOrEmpty(result?.ToString()))
                    {
                        return(new StringCommandResult(result.ToString()));
                    }
                    break;

                case CommandResultType.Json:
                    if (!executed)
                    {
                        result   = ExecuteFunction(parameters);
                        executed = true;
                    }
                    if (result is JsonObject jsonResult)
                    {
                        return(new JsonCommandResult(jsonResult));
                    }
                    break;
                }
            }
            // Try to return an empty string
            if (returnTypes.Contains(CommandResultType.String) && executed)
            {
                return(new StringCommandResult(""));
            }
            throw new CommandException("Couldn't find a proper command result for function " + internCommand.Name, CommandExceptionReason.NoReturnMatch);
        }
Exemplo n.º 27
0
 /// <summary>Execute this command.</summary>
 /// <param name="info">All global informations for this execution.</param>
 /// <param name="arguments">
 /// The arguments for this command.
 /// They are evaluated lazy which means they will only be evaluated if needed.
 /// </param>
 /// <param name="returnTypes">
 /// The possible return types that should be returned by this execution.
 /// They are ordered by priority so, if possible, the first return type should be picked, then the second and so on.
 /// </param>
 public abstract ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes);
Exemplo n.º 28
0
 public ICommandResult Execute(ExecutionInformation info, string command)
 {
     return(Execute(info, command, ReturnStringOrNothing));
 }
Exemplo n.º 29
0
        /// <summary>
        /// Try to fit the given arguments to the underlying function.
        /// This function will throw an exception if the parameters can't be applied.
        /// The parameters that are extracted from the arguments will be returned if they can be applied successfully.
        /// </summary>
        /// <param name="info">The ExecutionInformation.</param>
        /// <param name="arguments">The arguments that are applied to this function.</param>
        /// <param name="returnTypes">The possible return types.</param>
        /// <param name="availableArguments">How many arguments could be set.</param>
        public object[] FitArguments(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes, out int availableArguments)
        {
            object[] parameters = new object[CommandParameter.Length];
            var argList = new Lazy<List<ICommand>>(() => arguments.ToList());

            // availableArguments: Iterate through arguments
            // p: Iterate through parameters
            availableArguments = 0;
            for (int p = 0; p < parameters.Length; p++)
            {
                var arg = CommandParameter[p];
                if (arg == typeof(ExecutionInformation))
                    parameters[p] = info;
                else if (arg == typeof(IEnumerable<ICommand>))
                    parameters[p] = arguments;
                else if (arg == typeof(IEnumerable<CommandResultType>))
                    parameters[p] = returnTypes;
                // Only add arguments if we still have some
                else if (availableArguments < argList.Value.Count)
                {
                    if (arg.IsArray) // array
                    {
                        var typeArr = arg.GetElementType();
                        var args = Array.CreateInstance(typeArr, argList.Value.Count - availableArguments);
                        try
                        {
                            for (int i = 0; i < args.Length; i++, availableArguments++)
                            {
                                var argResult = ((StringCommandResult)argList.Value[availableArguments].Execute(info, Enumerable.Empty<ICommand>(), new[] { CommandResultType.String })).Content;
                                var convResult = ConvertParam(argResult, typeArr);
                                args.SetValue(convResult, i);
                            }
                        }
                        catch (FormatException ex) { throw new CommandException("Could not convert to " + arg.Name, ex); }
                        catch (OverflowException ex) { throw new CommandException("The number is too big.", ex); }

                        parameters[p] = args;
                    }
                    else // primitive value
                    {
                        var argResult = ((StringCommandResult)argList.Value[availableArguments].Execute(info, Enumerable.Empty<ICommand>(), new[] { CommandResultType.String })).Content;
                        try { parameters[p] = ConvertParam(argResult, arg); }
                        catch (FormatException ex) { throw new CommandException("Could not convert to " + UnwrapType(arg).Name, ex); }
                        catch (OverflowException ex) { throw new CommandException("The number is too big.", ex); }

                        availableArguments++;
                    }
                }
                else
                    parameters[p] = GetDefault(arg);
            }

            // Check if we were able to set enough arguments
            if (availableArguments < Math.Min(parameters.Length, RequiredParameters) && !returnTypes.Contains(CommandResultType.Command))
                throw new CommandException("Not enough arguments for function " + internCommand.Name);

            return parameters;
        }
Exemplo n.º 30
0
        public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
        {
            // Make arguments lazy, we only want to execute them once
            arguments = arguments.Select(c => new LazyCommand(c));
            int availableArguments;
            object[] parameters = FitArguments(info, arguments, returnTypes, out availableArguments);

            // Check if we were able to set enough arguments
            if (availableArguments < Math.Min(parameters.Length, RequiredParameters))
            {
                if (returnTypes.Contains(CommandResultType.Command))
                {
                    if (!arguments.Any())
                        return new CommandCommandResult(this);
                    return new CommandCommandResult(new AppliedCommand(this, arguments));
                }
                throw new CommandException("Not enough arguments for function " + internCommand.Name);
            }

            if (CommandReturn == typeof(ICommandResult))
                return (ICommandResult)ExecuteFunction(parameters);

            bool executed = false;
            object result = null;
            // Take first fitting command result
            foreach (var returnType in returnTypes)
            {
                switch (returnType)
                {
                case CommandResultType.Command:
                    // Return a command if we can take more arguments
                    if (CommandParameter.Any(p => p == typeof(string[])) || availableArguments < NormalParameters)
                        return new CommandCommandResult(new AppliedCommand(this, arguments));
                    break;
                case CommandResultType.Empty:
                    if (!executed)
                        ExecuteFunction(parameters);
                    return new EmptyCommandResult();
                case CommandResultType.String:
                    if (!executed)
                    {
                        result = ExecuteFunction(parameters);
                        executed = true;
                    }
                    if (result != null && !string.IsNullOrEmpty(result.ToString()))
                        return new StringCommandResult(result.ToString());
                    break;
                }
            }
            // Try to return an empty string
            if (returnTypes.Contains(CommandResultType.String) && executed)
                return new StringCommandResult("");
            throw new CommandException("Couldn't find a proper command result for function " + internCommand.Name);
        }
Exemplo n.º 31
0
		public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
		{
			switch (CommandRights)
			{
			case Admin:
				if (!info.IsAdmin.Value)
					throw new CommandException("Command must be invoked by an admin!");
				break;
			case Public:
				if (info.TextMessage.Target != MessageTarget.Server && !info.IsAdmin.Value)
					throw new CommandException("Command must be used in public mode!");
				break;
			case Private:
				if (info.TextMessage.Target != MessageTarget.Private && !info.IsAdmin.Value)
					throw new CommandException("Command must be used in a private session!");
				break;
			}
			return base.Execute(info, arguments, returnTypes);
		}
Exemplo n.º 32
0
 string CallCommand(string command)
 {
     var info = new ExecutionInformation(null, CreateTextMessage(), new Lazy<bool>(true));
     return bot.CommandManager.CommandSystem.ExecuteCommand(info, command);
 }
Exemplo n.º 33
0
 public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
 {
     return(new StringCommandResult(content));
 }
Exemplo n.º 34
0
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
 {
     return new StringCommandResult(content);
 }
Exemplo n.º 35
0
 public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
 {
     return(internCommand.Execute(info, internArguments.Concat(arguments), returnTypes));
 }
Exemplo n.º 36
0
 public ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments)
 {
     return(Execute(info, arguments, ReturnStringOrNothing));
 }
Exemplo n.º 37
0
 public ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
 {
     return(RootCommand.Execute(info, arguments, returnTypes));
 }
Exemplo n.º 38
0
        /// <summary>
        /// Try to fit the given arguments to the underlying function.
        /// This function will throw an exception if the parameters can't be applied.
        /// The parameters that are extracted from the arguments will be returned if they can be applied successfully.
        /// </summary>
        /// <param name="info">The ExecutionInformation.</param>
        /// <param name="arguments">The arguments that are applied to this function.</param>
        /// <param name="returnTypes">The possible return types.</param>
        /// <param name="availableArguments">How many arguments could be set.</param>
        public object[] FitArguments(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes, out int availableArguments)
        {
            var parameters = new object[CommandParameter.Length];

            // availableArguments: Iterate through arguments
            // p: Iterate through parameters
            availableArguments = 0;
            for (int p = 0; p < parameters.Length; p++)
            {
                var arg = CommandParameter[p];
                if (arg == typeof(ExecutionInformation))
                {
                    parameters[p] = info;
                }
                else if (arg == typeof(IReadOnlyList <ICommand>))
                {
                    parameters[p] = arguments;
                }
                else if (arg == typeof(IReadOnlyList <CommandResultType>))
                {
                    parameters[p] = returnTypes;
                }
                // Only add arguments if we still have some
                else if (availableArguments < arguments.Count)
                {
                    if (arg.IsArray)                     // array
                    {
                        var typeArr = arg.GetElementType();
                        var args    = Array.CreateInstance(typeArr, arguments.Count - availableArguments);
                        try
                        {
                            for (int i = 0; i < args.Length; i++, availableArguments++)
                            {
                                var argResult  = ((StringCommandResult)arguments[availableArguments].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String })).Content;
                                var convResult = ConvertParam(argResult, typeArr);
                                args.SetValue(convResult, i);
                            }
                        }
                        catch (FormatException ex) { throw new CommandException("Could not convert to " + arg.Name, ex, CommandExceptionReason.CommandError); }
                        catch (OverflowException ex) { throw new CommandException("The number is too big.", ex, CommandExceptionReason.CommandError); }

                        parameters[p] = args;
                    }
                    else                     // primitive value
                    {
                        var argResult = ((StringCommandResult)arguments[availableArguments].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String })).Content;
                        try { parameters[p] = ConvertParam(argResult, arg); }
                        catch (FormatException ex) { throw new CommandException("Could not convert to " + UnwrapType(arg).Name, ex, CommandExceptionReason.CommandError); }
                        catch (OverflowException ex) { throw new CommandException("The number is too big.", ex, CommandExceptionReason.CommandError); }

                        availableArguments++;
                    }
                }
                else
                {
                    parameters[p] = GetDefault(arg);
                }
            }

            // Check if we were able to set enough arguments
            if (availableArguments < Math.Min(parameters.Length, RequiredParameters) && !returnTypes.Contains(CommandResultType.Command))
            {
                throw new CommandException("Not enough arguments for function " + internCommand.Name, CommandExceptionReason.MissingParameter);
            }

            return(parameters);
        }
		public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes)
		{
			// Make arguments lazy, we only want to execute them once
			arguments = arguments.Select(c => new LazyCommand(c));
			foreach (FunctionCommand f in Functions)
			{
				bool fits = false;
				try
				{
					// Find out if this overload works
					int i;
					f.FitArguments(info, arguments, returnTypes, out i);
					fits = true;
				}
				catch (CommandException)
				{
					// Do nothing, just move on to the next function
				}
				if (fits)
				{
					// Call this overload
					return f.Execute(info, arguments, returnTypes);
				}
			}
			throw new CommandException("No matching function could be found");
		}
Exemplo n.º 40
0
 public ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes)
 {
     return(RootCommand.Execute(info, arguments, returnTypes));
 }
Exemplo n.º 41
0
 public ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments)
 {
     return(Execute(info, arguments, new[] { CommandResultType.String, CommandResultType.Empty }));
 }