Esempio n. 1
0
 private IEnumerator RunAction()
 {
     RemoveCommands();
     while (CommandList.Any(x => !x.Completed))
     {
         var currentCommand = CommandList.FirstOrDefault(x => !x.Completed);
         yield return(StartCoroutine(HandleCommands(currentCommand)));
     }
 }
Esempio n. 2
0
 private void Update()
 {
     if (CanAction && CommandList.Any(x => !x.Completed))
     {
         if (!health.isDead)
         {
             StartCoroutine(RunAction());
         }
     }
 }
Esempio n. 3
0
    private void RemoveCommands()
    {
        if (CommandList.Any(x => x.Completed))
        {
            CommandList.RemoveAt(0);
        }

        if (!CommandList.Any())
        {
            CommandList.Clear();
            CanAction = true;
        }
    }
Esempio n. 4
0
        /// <summary>
        /// This method will execute a command if there is one
        /// </summary>
        /// <param name="context">The current discord <see cref="ICommandContext"/></param>
        /// <returns>The <see cref="ICommandResult"/> containing what the status of the execution is </returns>
        public async Task <ICommandResult> ExecuteAsync(SocketCommandContext context)
        {
            string[] param = context.Message.Content.Split(' ');
            param = param.TakeLast(param.Length - 1).ToArray();
            string command = context.Message.Content.Split(' ').First().Remove(0, 1).ToLower();
            char   prefix  = context.Message.Content.Split(' ').First().ToCharArray().First();

            CommandModuleBase.Context = context;
            CommandModuleBase.HasExecutePermission = currentSettings.HasPermissionMethod(context);
            //check if the command exists
            if (!CommandList.Any(x => x.CommandName.ToLower() == command))
            {
                return new CommandResult()
                       {
                           Result    = CommandStatus.NotFound,
                           IsSuccess = false
                       }
            }
            ;

            //Command exists

            //if more than one command with the same name exists then try to execute both or find one that matches the params
            var commandobj = CommandList.Where(x => x.CommandName.ToLower() == command);

            foreach (var item in commandobj)
            {
                if (!item.Prefixes.Contains(prefix))          //prefix doesnt match, check the parent class
                {
                    if (item.parent.attribute.prefix == '\0') //if theres no prefix
                    {
                        return new CommandResult()
                               {
                                   Result = CommandStatus.NotFound, IsSuccess = false
                               }
                    }
                    ;
                    else //there is a prefix
                    {
                        if (item.parent.Prefix == prefix)
                        {
                            commandobj = commandobj.Where(x => x.parent.Prefix == prefix);
                        }
                        else
                        {
                            return new CommandResult()
                                   {
                                       Result = CommandStatus.NotFound, IsSuccess = false
                                   }
                        };
                    }
                }
                else //prefix match for method
                {
                    if (item.parent.attribute.OverwritesPrefix)
                    {
                        if (item.parent.Prefix == prefix)
                        {
                            commandobj = commandobj.Where(x => x.parent.Prefix == prefix);
                        }
                        else
                        {
                            return new CommandResult()
                                   {
                                       Result = CommandStatus.NotFound, IsSuccess = false
                                   }
                        };
                    }
                    else
                    {
                        commandobj = commandobj.Where(x => x.Prefixes.Contains(prefix));
                    }
                }
            }

            if (commandobj.Any(x => x.Paramaters.Length == 0) && param.Length == 0)
            {
                try
                {
                    var cmd = commandobj.First(x => x.Paramaters.Length == 0);

                    var d = (Func <Task>)Delegate.CreateDelegate(typeof(Func <Task>), cmd.parent.ClassInstance, cmd.Method);
                    await d();

                    //cmd.Method.Invoke(cmd.parent.ClassInstance, null);
                    return(new CommandResult()
                    {
                        Result = CommandStatus.Success, IsSuccess = true
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return(new CommandResult()
                    {
                        Exception = ex, Result = CommandStatus.Error, IsSuccess = false
                    });
                }
            }

            //1 find if param counts match
            foreach (var cmd in commandobj)
            {
                if (cmd.Paramaters.Length == 0 && param.Length == 0)
                {
                    try
                    {
                        var d = (Func <Task>)Delegate.CreateDelegate(typeof(Func <Task>), cmd.parent.ClassInstance, cmd.Method);
                        await d();

                        //cmd.Method.Invoke(cmd.parent.ClassInstance, null);
                        return(new CommandResult()
                        {
                            Result = CommandStatus.Success, IsSuccess = true
                        });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        return(new CommandResult()
                        {
                            Exception = ex, Result = CommandStatus.Error, IsSuccess = false
                        });
                    }
                }
                else if (cmd.Paramaters.Length == 0 && param.Length > 0)
                {
                    return new CommandResult()
                           {
                               Result = CommandStatus.InvalidParams, IsSuccess = false
                           }
                }
                ;

                if (cmd.Paramaters.Last().GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0)
                {
                    List <object> parsedparams = new List <object>();

                    bool check = true;
                    for (int i = 0; i != cmd.Paramaters.Length; i++)
                    {
                        var dp = cmd.Paramaters[i];
                        if (dp.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0)
                        {
                            string[] arr = param.Skip(i).Where(x => x != "").ToArray();

                            if (arr.Length == 0)
                            {
                                ArrayList al     = new ArrayList();
                                Type      destyp = dp.ParameterType.GetElementType();
                                parsedparams.Add(al.ToArray(destyp));
                            }
                            else
                            {
                                ArrayList al     = new ArrayList();
                                Type      destyp = dp.ParameterType.GetElementType();
                                foreach (var item in arr)
                                {
                                    if (TypeDescriptor.GetConverter(destyp).IsValid(item))
                                    {
                                        //we can
                                        var pparam = TypeDescriptor.GetConverter(destyp).ConvertFromString(item);
                                        al.Add(pparam);
                                    }
                                    else
                                    {
                                        check = false;
                                    }
                                }
                                if (check)
                                {
                                    parsedparams.Add(al.ToArray(destyp));
                                }
                            }
                        }
                        else
                        {
                            if (param.Length < cmd.Paramaters.Length - 1)
                            {
                                return new CommandResult()
                                       {
                                           Result = CommandStatus.InvalidParams, IsSuccess = false
                                       }
                            }
                            ;
                            var p = param[i];
                            if (TypeDescriptor.GetConverter(dp.ParameterType).IsValid(p))
                            {
                                //we can
                                var pparam = TypeDescriptor.GetConverter(dp.ParameterType).ConvertFromString(p);

                                parsedparams.Add(pparam);
                            }
                            else
                            {
                                check = false;
                            }
                        }
                    }
                    //if it worked
                    if (check)
                    {
                        //invoke the method
                        try
                        {
                            Task s = (Task)cmd.Method.Invoke(cmd.parent.ClassInstance, parsedparams.ToArray());
                            if (s.Exception == null)
                            {
                                return new CommandResult()
                                       {
                                           Result = CommandStatus.Success, IsSuccess = true
                                       }
                            }
                            ;
                            else
                            {
                                return new CommandResult()
                                       {
                                           Exception = s.Exception.InnerException, Result = CommandStatus.Error, IsSuccess = false
                                       }
                            };
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            return(new CommandResult()
                            {
                                Exception = ex, Result = CommandStatus.Error, IsSuccess = false
                            });
                        }
                    }
                    else
                    {
                        return new CommandResult()
                               {
                                   Result = CommandStatus.InvalidParams, IsSuccess = false
                               }
                    };
                }
                if (cmd.Paramaters.Length == param.Length)
                {
                    List <object> parsedparams = new List <object>();
                    bool          check        = true;

                    //right command lengths met or params object in it now we gotta parse the params
                    for (int i = 0; i != param.Length; i++)
                    {
                        //PrimitiveParsers.Get(unparsedparam)
                        var p  = param[i];
                        var dp = cmd.Paramaters[i];
                        //check if we can parse the value
                        if (TypeDescriptor.GetConverter(dp.ParameterType).IsValid(p))
                        {
                            //we can
                            var pparam = TypeDescriptor.GetConverter(dp.ParameterType).ConvertFromString(p);
                            parsedparams.Add(pparam);
                        }
                        else
                        {
                            check = false;
                        }
                    }
                    //if we parsed all the params correctly
                    if (check)
                    {
                        //invoke the method
                        try
                        {
                            Task s = (Task)cmd.Method.Invoke(cmd.parent.ClassInstance, parsedparams.ToArray());
                            if (s.Exception == null)
                            {
                                return new CommandResult()
                                       {
                                           Result = CommandStatus.Success, IsSuccess = true
                                       }
                            }
                            ;
                            else
                            {
                                return new CommandResult()
                                       {
                                           Exception = s.Exception.InnerException, Result = CommandStatus.Error, IsSuccess = false
                                       }
                            };
                        }
                        catch (TargetInvocationException ex)
                        {
                            Console.WriteLine(ex);
                            return(new CommandResult()
                            {
                                Exception = ex, Result = CommandStatus.Error, IsSuccess = false
                            });
                        }
                    }
                    else
                    {
                        return new CommandResult()
                               {
                                   Result = CommandStatus.InvalidParams, IsSuccess = false
                               }
                    };
                }
                else
                {
                    return new CommandResult()
                           {
                               Result = CommandStatus.NotEnoughParams, IsSuccess = false
                           }
                };
            }
            return(new CommandResult()
            {
                Result = CommandStatus.Unknown, IsSuccess = false
            });
        }
Esempio n. 5
0
 // returns true if the current command is used as a breakpoint (during the execute state)
 public bool IsBreakPoint() => CommandList.Any(command => command.Address == Core.PipelineStatus[EPipeline.Execute] && command.Breakpoint);
Esempio n. 6
0
 /// <summary>
 /// Returns a boolean indicating if the command already exists in the factory
 /// </summary>
 public bool Contains(FFmpegCommand command)
 {
     return(CommandList.Any(c => c.Id == command.Id));
 }