コード例 #1
0
        /// <summary>
        /// Parse an IToggle from the specified text. Throws ArgumentException if there's a problem.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static IToggle Require(PartModule module, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("must supply a value");
            }
            text = text.Trim();

            // Check for constants.
            if (text == "true")
            {
                return(Constant.TRUE);
            }
            if (text == "false")
            {
                return(Constant.FALSE);
            }

            // Perhaps it's an inverted toggle?
            if (text.StartsWith(NOT_OPERATOR))
            {
                return(Inverter.of(Require(module, text.Substring(NOT_OPERATOR.Length))));
            }

            // Maybe it's an identifier for a toggle.
            IToggle found = Identifiers.FindFirst <IToggle>(module.part, text);

            if (found != null)
            {
                return(found);
            }

            // Could it be a named-field reference?
            Identifiers.IFieldEvaluator field = Identifiers.FindKSPField(module.part, text);
            if (field != null)
            {
                if (!typeof(bool).IsAssignableFrom(field.FieldType))
                {
                    throw new ArgumentException("Can't use " + text + " as a boolean field (it's of type " + field.FieldType.Name + ")");
                }
                return(new NamedField(field));
            }

            // Perhaps it's a parameterized expression?
            ParsedParameters parsedParams = ParsedParameters.TryParse(text);

            if (parsedParams != null)
            {
                for (int i = 0; i < PARSEABLE_TOGGLES.Length; ++i)
                {
                    IToggle parsed = PARSEABLE_TOGGLES[i](module, parsedParams);
                    if (parsed != null)
                    {
                        return(parsed);
                    }
                }
            }

            // Nope, not parseable.
            throw new ArgumentException("Invalid toggle syntax \"" + text + "\"");
        }
コード例 #2
0
ファイル: Scalars.cs プロジェクト: yadenisyur/IndicatorLights
        /// <summary>
        /// Parse an IScalar from the specified text. Throws ArgumentException if there's a problem.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static IScalar Require(PartModule module, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("must supply a value");
            }
            text = text.Trim();

            // Perhaps it's an inverted scalar?
            if (text.StartsWith(INVERT_OPERATOR))
            {
                return(LinearTransform.Invert(Require(module, text.Substring(INVERT_OPERATOR.Length))));
            }

            // Maybe it's an identifier for a scalar.
            IScalar found = Identifiers.FindFirst <IScalar>(module.part, text);

            if (found != null)
            {
                return(found);
            }

            // Could it be a named-field reference?
            Identifiers.IFieldEvaluator field = Identifiers.FindKSPField(module.part, text);
            if (field != null)
            {
                if (NamedSingleField.Is(field))
                {
                    return(new NamedSingleField(field));
                }
                if (NamedDoubleField.Is(field))
                {
                    return(new NamedDoubleField(field));
                }
                if (NamedIntegerField.Is(field))
                {
                    return(new NamedIntegerField(field));
                }
                if (NamedShortField.Is(field))
                {
                    return(new NamedShortField(field));
                }
                if (NamedLongField.Is(field))
                {
                    return(new NamedLongField(field));
                }

                throw new ArgumentException("Can't use " + text + " as a scalar field (it's of type " + field.FieldType.Name + ")");
            }

            // Perhaps it's a parameterized expression?
            ParsedParameters parsedParams = ParsedParameters.TryParse(text);

            if (parsedParams != null)
            {
                for (int i = 0; i < PARSEABLE_SCALARS.Length; ++i)
                {
                    IScalar parsed = PARSEABLE_SCALARS[i](module, parsedParams);
                    if (parsed != null)
                    {
                        return(parsed);
                    }
                }
            }

            // Last chance:  it could be a static value.
            return(Constant.Of(Statics.Parse(module, text)));
        }