Esempio n. 1
0
        /// <summary>
        /// Bind the trigger to a command instance and initialize his parameters. Returns false whenever an error occurs during the initialization
        /// </summary>
        public bool BindToCommand(CommandBase command)
        {
            BoundCommand = command;

            if (command is SubCommandContainer) // SubCommandContainer has no params
            {
                return(true);
            }

            var definedParam  = new List <IParameter>();
            var paramToDefine = new List <IParameterDefinition>(BoundCommand.Parameters);

            // command has only a string parameter -> then we can assign the entire string to this parameter
            if (paramToDefine.Count == 1 && paramToDefine[0].ValueType == typeof(string) && !paramToDefine[0].IsOptional)
            {
                var param = paramToDefine[0].CreateParameter();

                param.SetValue(Args.NextWords(), this);

                definedParam.Add(param);
                paramToDefine.Remove(paramToDefine[0]);
            }

            if (BoundCommand.Parameters.Count == 0)
            {
                return(true);
            }

            var word        = Args.NextWord();
            var definedOnly = false;

            while (!string.IsNullOrEmpty(word) && definedParam.Count < BoundCommand.Parameters.Count)
            {
                if (word.StartsWith("\"") && word.EndsWith("\""))
                {
                    word = word.Remove(word.Length - 1, 1).Remove(0, 1);
                }

                var parsed = false;
                if (word.StartsWith("-")) // becareful it can be the minus sign
                {
                    string name         = null;
                    string value        = null;
                    var    matchIsNamed = m_regexIsNamed.Match(word);
                    if (matchIsNamed.Success)
                    {
                        name  = matchIsNamed.Groups[1].Value;
                        value = matchIsNamed.Groups[2].Value;

                        if (value.StartsWith("\"") && value.EndsWith("\""))
                        {
                            value = value.Remove(value.Length - 1, 1).Remove(0, 1);
                        }
                    }
                    else
                    {
                        var matchVar = m_regexVar.Match(word);
                        if (matchVar.Success)
                        {
                            name        = matchVar.Groups[1].Value;
                            value       = string.Empty;
                            definedOnly = true;
                        }
                    }

                    if (!string.IsNullOrEmpty(name)) // if one of both regex success
                    {
                        var definition =
                            paramToDefine.SingleOrDefault(entry => CompareParameterName(entry, name, CommandBase.IgnoreCommandCase));

                        if (definition != null)
                        {
                            var parameter = definition.CreateParameter();

                            try
                            {
                                // parameters defined like "-life" imply being true
                                // writting "-life" is similar to "-life=true"
                                if (definedOnly && definition.ValueType == typeof(bool))
                                {
                                    value = "true";
                                }

                                parameter.SetValue(value, this);
                            }
                            catch (ConverterException ex)
                            {
                                ReplyError(ex.Message);
                                return(false);
                            }
                            catch (Exception ex)
                            {
                                ReplyError("Cannot parse : {0} as {1} (error-index:{2})", word, definition.ValueType, RegisterException(ex));
                                return(false);
                            }

                            definedParam.Add(parameter);
                            paramToDefine.Remove(definition);
                            parsed = true;
                        }
                    }
                }


                if (!parsed)
                {
                    IParameterDefinition definition = paramToDefine.First();

                    IParameter parameter = definition.CreateParameter();

                    try
                    {
                        parameter.SetValue(word, this);
                    }
                    catch (ConverterException ex)
                    {
                        ReplyError(ex.Message);
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        ReplyError("Cannot parse : {0} as {1} (error-index:{2})", word, definition.ValueType, RegisterException(ex));
                        return(false);
                    }

                    definedParam.Add(parameter);
                    paramToDefine.Remove(definition);
                }

                word = Args.NextWord();
            }


            foreach (var unusedDefinition in paramToDefine)
            {
                if (!unusedDefinition.IsOptional)
                {
                    ReplyError("{0} is not defined", unusedDefinition.Name);
                    return(false);
                }

                var parameter = unusedDefinition.CreateParameter();

                parameter.SetDefaultValue(this);
                definedParam.Add(parameter);
            }

            CommandsParametersByName      = definedParam.ToDictionary(entry => entry.Definition.Name);
            CommandsParametersByShortName = definedParam.ToDictionary(entry =>
                                                                      !string.IsNullOrEmpty(entry.Definition.ShortName) ?
                                                                      entry.Definition.ShortName : entry.Definition.Name);
            return(true);
        }
        public bool BindToCommand(CommandBase command)
        {
            this.BoundCommand = command;
            bool result;

            if (command is SubCommandContainer)
            {
                result = true;
            }
            else
            {
                List <IParameter>           list  = new List <IParameter>();
                List <IParameterDefinition> list2 = new List <IParameterDefinition>(this.BoundCommand.Parameters);
                if (list2.Count == 1 && list2[0].ValueType == typeof(string) && !list2[0].IsOptional)
                {
                    IParameter parameter = list2[0].CreateParameter();
                    parameter.SetValue(this.Args.NextWords(), this);
                    list.Add(parameter);
                    list2.Remove(list2[0]);
                }
                if (this.BoundCommand.Parameters.Count == 0)
                {
                    result = true;
                }
                else
                {
                    string text = this.Args.NextWord();
                    bool   flag = false;
                    while (!string.IsNullOrEmpty(text) && list.Count < this.BoundCommand.Parameters.Count)
                    {
                        if (text.StartsWith("\"") && text.EndsWith("\""))
                        {
                            text = text.Remove(text.Length - 1, 1).Remove(0, 1);
                        }
                        bool flag2 = false;
                        if (text.StartsWith("-"))
                        {
                            string name  = null;
                            string text2 = null;
                            Match  match = this.m_regexIsNamed.Match(text);
                            if (match.Success)
                            {
                                name  = match.Groups[1].Value;
                                text2 = match.Groups[2].Value;
                                if (text2.StartsWith("\"") && text2.EndsWith("\""))
                                {
                                    text2 = text2.Remove(text2.Length - 1, 1).Remove(0, 1);
                                }
                            }
                            else
                            {
                                Match match2 = this.m_regexVar.Match(text);
                                if (match2.Success)
                                {
                                    name  = match2.Groups[1].Value;
                                    text2 = string.Empty;
                                    flag  = true;
                                }
                            }
                            if (!string.IsNullOrEmpty(name))
                            {
                                IParameterDefinition parameterDefinition = list2.SingleOrDefault((IParameterDefinition entry) => TriggerBase.CompareParameterName(entry, name, CommandBase.IgnoreCommandCase));
                                if (parameterDefinition != null)
                                {
                                    IParameter parameter2 = parameterDefinition.CreateParameter();
                                    try
                                    {
                                        if (flag && parameterDefinition.ValueType == typeof(bool))
                                        {
                                            text2 = "true";
                                        }
                                        parameter2.SetValue(text2, this);
                                    }
                                    catch (ConverterException ex)
                                    {
                                        this.ReplyError(ex.Message);
                                        result = false;
                                        return(result);
                                    }
                                    catch (Exception ex2)
                                    {
                                        this.ReplyError("Cannot parse : {0} as {1} (error-index:{2})", new object[]
                                        {
                                            text,
                                            parameterDefinition.ValueType,
                                            this.RegisterException(ex2)
                                        });
                                        result = false;
                                        return(result);
                                    }
                                    list.Add(parameter2);
                                    list2.Remove(parameterDefinition);
                                    flag2 = true;
                                }
                            }
                        }
                        if (!flag2)
                        {
                            IParameterDefinition parameterDefinition = list2.First <IParameterDefinition>();
                            IParameter           parameter2          = parameterDefinition.CreateParameter();
                            try
                            {
                                parameter2.SetValue(text, this);
                            }
                            catch (ConverterException ex)
                            {
                                this.ReplyError(ex.Message);
                                result = false;
                                return(result);
                            }
                            catch (Exception ex2)
                            {
                                this.ReplyError("Cannot parse : {0} as {1} (error-index:{2})", new object[]
                                {
                                    text,
                                    parameterDefinition.ValueType,
                                    this.RegisterException(ex2)
                                });
                                result = false;
                                return(result);
                            }
                            list.Add(parameter2);
                            list2.Remove(parameterDefinition);
                        }
                        text = this.Args.NextWord();
                    }
                    foreach (IParameterDefinition current in list2)
                    {
                        if (!current.IsOptional)
                        {
                            this.ReplyError("{0} is not defined", new object[]
                            {
                                current.Name
                            });
                            result = false;
                            return(result);
                        }
                        IParameter parameter2 = current.CreateParameter();
                        parameter2.SetDefaultValue(this);
                        list.Add(parameter2);
                    }
                    this.CommandsParametersByName      = list.ToDictionary((IParameter entry) => entry.Definition.Name);
                    this.CommandsParametersByShortName = list.ToDictionary((IParameter entry) => (!string.IsNullOrEmpty(entry.Definition.ShortName)) ? entry.Definition.ShortName : entry.Definition.Name);
                    result = true;
                }
            }
            return(result);
        }