Beispiel #1
0
            public static IScalar TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                switch (parsedParams.Identifier)
                {
                case SCALE:
                {
                    parsedParams.RequireCount(module, 2, 3);
                    IScalar input  = Scalars.Require(module, parsedParams[0]);
                    double  scale  = Statics.Parse(module, parsedParams[1]);
                    double  offset = (parsedParams.Count > 2) ? Statics.Parse(module, parsedParams[2]) : 0.0;
                    return(Of(input, scale, offset));
                }

                case OFFSET:
                {
                    parsedParams.RequireCount(module, 2);
                    IScalar input  = Scalars.Require(module, parsedParams[0]);
                    double  offset = Statics.Parse(module, parsedParams[1]);
                    return(Offset(input, offset));
                }

                default:
                    return(null);
                }
            }
Beispiel #2
0
            /// <summary>
            /// Try to get a "crew effect" matcher from a ParsedParameters. The expected format is:
            ///
            /// hasCrewEffect(effectName, slot)
            /// hasCrewEffect(effectName, slot, minLevel)
            ///
            /// ...where effectName is the name of the effect to check for (e.g. "ScienceSkill" or whatever;
            /// these are generally the names of ExperienceEffect classes in the Experience.Effects namespace),
            /// slot is the crew slot to evaluate (-1 for "any"), and minLevel is the minimum experience level
            /// needed for the ExperienceEffect to apply ("any level" if omitted).
            /// </summary>
            public static IToggle TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (!TYPE_NAME.Equals(parsedParams.Identifier))
                {
                    return(null);
                }
                if (module == null)
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 2, 3);
                string effectName = parsedParams[0];

                if (!Kerbals.Effects.Contains(effectName))
                {
                    StringBuilder builder = new StringBuilder();
                    foreach (string validName in Kerbals.Effects)
                    {
                        builder.Append(" ").Append(validName);
                    }
                    throw new ArgumentException(
                              "Invalid effect name '" + effectName + "'. Valid values are:"
                              + builder.ToString());
                }
                int slot     = (int)Statics.Parse(module, parsedParams[1]);
                int minLevel = (parsedParams.Count > 2) ? (int)Statics.Parse(module, parsedParams[2]) : -1;

                return(new CrewEffectMatch(module, effectName, slot, minLevel));
            }
Beispiel #3
0
            public static IToggle TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (parsedParams.Identifier != TYPE_NAME)
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 2);
                IScalar input     = Scalars.Require(module, parsedParams[0]);
                double  threshold = Statics.Parse(module, parsedParams[1]);

                return(new LessThanOrEqual(input, threshold));
            }
            /// <summary>
            /// Try to get a dim color source from a ParsedParameters. The expected format is:
            ///
            /// dim(origin, multiplier)
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }

                string tag = FindTag(parsedParams.Identifier);

                if (tag == null)
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 2);

                IColorSource origin;

                try
                {
                    origin = FindPrivate(module, parsedParams[0]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, tag + "() source has invalid origin", e);
                }

                float multiplier;

                try
                {
                    multiplier = (float)Statics.Parse(module, parsedParams[1]);
                }
                catch (ArgumentException e)
                {
                    throw new ColorSourceException(module, tag + "(): Invalid multiplier value '" + parsedParams[1] + "': " + e.Message, e);
                }
                if (multiplier < 0)
                {
                    throw new ColorSourceException(module, tag + "(): Invalid multiplier value '" + parsedParams[1] + "' (can't be negative)");
                }
                if (multiplier == 1)
                {
                    return(origin);
                }

                return(new DimColorSource(tag, origin, multiplier));
            }
Beispiel #5
0
            public static IToggle TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (parsedParams.Identifier != TYPE_NAME)
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 3);
                IScalar input   = Scalars.Require(module, parsedParams[0]);
                double  minimum = Statics.Parse(module, parsedParams[1]);
                double  maximum = Statics.Parse(module, parsedParams[2]);

                if (minimum > maximum)
                {
                    return(Constant.FALSE);
                }
                return(new Between(input, minimum, maximum));
            }
            /// <summary>
            /// Try to get a lerp color source from a ParsedParameters. The expected format is:
            ///
            /// lerp(input, source1, source2)
            /// lerp(input, source1, value1, source2, value2)
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (parsedParams.Identifier != TYPE_NAME)
                {
                    return(null);
                }
                if ((parsedParams.Count != 3) && (parsedParams.Count != 5))
                {
                    throw new ColorSourceException(
                              module,
                              TYPE_NAME + "() source specified " + parsedParams.Count + " parameters (3 or 5 required)");
                }
                IScalar input = Scalars.Require(module, parsedParams[0]);

                if (parsedParams.Count == 3)
                {
                    return(new LerpColorSource(
                               input,
                               Find(module, parsedParams[1]),
                               0,
                               Find(module, parsedParams[2]),
                               1,
                               parsedParams.ToString()));
                }
                else
                {
                    return(new LerpColorSource(
                               input,
                               Find(module, parsedParams[1]),
                               Statics.Parse(module, parsedParams[2]),
                               Find(module, parsedParams[3]),
                               Statics.Parse(module, parsedParams[4]),
                               parsedParams.ToString()));
                }
            }
Beispiel #7
0
            public static IScalar TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                switch (parsedParams.Identifier)
                {
                case RANGE:
                {
                    parsedParams.RequireCount(module, 3);
                    IScalar input   = Scalars.Require(module, parsedParams[0]);
                    double  minimum = Statics.Parse(module, parsedParams[1]);
                    double  maximum = Statics.Parse(module, parsedParams[2]);
                    return(Between(input, minimum, maximum));
                }

                case GREATER_THAN:
                {
                    parsedParams.RequireCount(module, 2);
                    IScalar input   = Scalars.Require(module, parsedParams[0]);
                    double  minimum = Statics.Parse(module, parsedParams[1]);
                    return(AtLeast(input, minimum));
                }

                case LESS_THAN:
                {
                    parsedParams.RequireCount(module, 2);
                    IScalar input   = Scalars.Require(module, parsedParams[0]);
                    double  maximum = Statics.Parse(module, parsedParams[1]);
                    return(AtMost(input, maximum));
                }

                default:
                    return(null);
                }
            }
            /// <summary>
            /// Try to get a random color source from a ParsedParameters. The expected format is:
            ///
            /// random(onSource, offSource, periodMillis, bias, seed)
            /// random(onSource, offSource, periodMillis, bias)
            /// random(onSource, offSource, periodMillis)
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (!TYPE_NAME.Equals(parsedParams.Identifier))
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 3, 5);

                IColorSource onSource;

                try
                {
                    onSource = FindPrivate(module, parsedParams[0]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid 'on' parameter", e);
                }


                IColorSource offSource;

                try
                {
                    offSource = FindPrivate(module, parsedParams[1]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid 'off' parameter", e);
                }

                long periodMillis;

                try
                {
                    periodMillis = (long)Statics.Parse(module, parsedParams[2]);
                }
                catch (ArgumentException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid 'period' milliseconds value '" + parsedParams[2] + "': " + e.Message, e);
                }
                if (periodMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): 'period' milliseconds must be positive");
                }

                double bias = 0;

                if (parsedParams.Count > 3)
                {
                    try
                    {
                        bias = Statics.Parse(module, parsedParams[3]);
                    }
                    catch (ArgumentException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid bias value '" + parsedParams[3] + "': " + e.Message, e);
                    }
                    if ((bias < -1) || (bias > 1))
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid bias value '" + parsedParams[3] + "' (must be between -1 and 1)");
                    }
                }

                int seed = 0;

                if (parsedParams.Count > 4)
                {
                    try
                    {
                        seed = (int)Statics.Parse(module, parsedParams[4]);
                    }
                    catch (ArgumentException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid seed value '" + parsedParams[4] + "': " + e.Message, e);
                    }
                }
                if (module.vessel != null)
                {
                    seed ^= module.vessel.id.GetHashCode();
                }
                if (module.part != null)
                {
                    seed ^= module.part.flightID.GetHashCode();
                    seed ^= IndexOf(module).GetHashCode();
                }

                return(new RandomColorSource(onSource, offSource, periodMillis, bias, seed));
            }
            /// <summary>
            /// Try to get a pulsate color source from a ParsedParameters. The expected format is:
            ///
            /// pulsate(origin, cycleMillis, multiplier)
            /// pulsate(origin, cycleMillis, multiplier1, multiplier2)
            /// pulsate(origin, cycleMillis, multiplier1, multiplier2, phase)
            ///
            /// ...where multiplier is a float in the range 0 - 1.
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (!TYPE_NAME.Equals(parsedParams.Identifier))
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 3, 5);

                IColorSource origin;

                try
                {
                    origin = FindPrivate(module, parsedParams[0]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid origin", e);
                }

                long cycleMillis;

                try
                {
                    cycleMillis = (long)Statics.Parse(module, parsedParams[1]);
                }
                catch (ArgumentException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid cycle milliseconds value '" + parsedParams[1] + "': " + e.Message, e);
                }
                if (cycleMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): cycle milliseconds must be positive");
                }

                float multiplier1;

                try
                {
                    multiplier1 = (float)Statics.Parse(module, parsedParams[2]);
                }
                catch (ArgumentException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[2] + "': " + e.Message, e);
                }
                if ((multiplier1 < 0) || (multiplier1 > 1))
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[2] + "' (must be in range 0 - 1)");
                }

                float multiplier2 = 1f;

                if (parsedParams.Count > 3)
                {
                    try
                    {
                        multiplier2 = (float)Statics.Parse(module, parsedParams[3]);
                    }
                    catch (ArgumentException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[3] + "': " + e.Message, e);
                    }
                    if ((multiplier2 < 0) || (multiplier2 > 1))
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[3] + "' (must be in range 0 - 1)");
                    }
                }

                float phase = 0;

                if (parsedParams.Count > 4)
                {
                    try
                    {
                        phase = (float)Statics.Parse(module, parsedParams[4]);
                    }
                    catch (ArgumentException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid phase value '" + parsedParams[4] + "': " + e.Message, e);
                    }
                }

                if ((multiplier1 >= 1) && (multiplier2 >= 1))
                {
                    return(origin);
                }
                if ((multiplier1 <= 0) && (multiplier2 <= 0))
                {
                    return(BLACK);
                }

                return(new PulsateColorSource(origin, cycleMillis, multiplier1, multiplier2, phase));
            }
            /// <summary>
            /// Try to get a blink color source from a ParsedParameters. The expected format is:
            ///
            /// blink(onSource, onMillis, offSource, offMillis)
            /// blink(onSource, onMillis, offSource, offMillis, phase)
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (!TYPE_NAME.Equals(parsedParams.Identifier))
                {
                    return(null);
                }
                parsedParams.RequireCount(module, 4, 5);

                IColorSource onSource;

                try
                {
                    onSource = FindPrivate(module, parsedParams[0]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid 'on' parameter", e);
                }


                long onMillis;

                try
                {
                    onMillis = (long)Statics.Parse(module, parsedParams[1]);
                }
                catch (ArgumentException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid 'on' milliseconds value '" + parsedParams[1] + "': " + e.Message, e);
                }
                if (onMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): 'on' milliseconds must be positive");
                }

                IColorSource offSource;

                try
                {
                    offSource = FindPrivate(module, parsedParams[2]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid 'off' parameter", e);
                }

                long offMillis;

                try
                {
                    offMillis = (long)Statics.Parse(module, parsedParams[3]);
                }
                catch (ArgumentException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid 'off' milliseconds value '" + parsedParams[3] + "': " + e.Message, e);
                }
                if (offMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): 'off' milliseconds must be positive");
                }

                float phase = 0F;

                if (parsedParams.Count > 4)
                {
                    try
                    {
                        phase = (float)Statics.Parse(module, parsedParams[4]);
                    }
                    catch (ArgumentException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid phase value '" + parsedParams[4] + "': " + e.Message, e);
                    }
                }

                return(new BlinkColorSource(onSource, onMillis, offSource, offMillis, phase));
            }
Beispiel #11
0
        /// <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)));
        }