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); } }
/// <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)); }
/// <summary> /// Parse a static expression thus: /// /// static(fieldname) /// </summary> /// <param name="module"></param> /// <param name="parameters"></param> /// <returns></returns> private static double TryParseField(PartModule module, ParsedParameters parameters) { if (parameters.Identifier != "static") { return(double.NaN); } parameters.RequireCount(module, 1); string fieldName = parameters[0]; FieldInfo field = module.GetType().GetField( fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (object.ReferenceEquals(field, null)) { throw new ArgumentException("Unknown static field '" + fieldName + "' on " + module.ClassName + " of " + module.part.GetTitle()); } if (StaticField.Is(field)) { return((double)field.GetValue(module)); } else { throw new ArgumentOutOfRangeException("Field '" + fieldName + "' on " + module.ClassName + " of " + module.part.GetTitle() + " is not a static field"); } }
/// <summary> /// Try to get an "if" color source from a ParsedParameters. The expected format is: /// /// if(toggle, onSource) /// if(toggle, onSource, offSource) /// </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, 2, 3); IToggle toggle = null; try { toggle = Toggles.Require(module, parsedParams[0]); } catch (ArgumentException e) { throw new ColorSourceException(module, "Invalid toggle specified for " + TYPE_NAME + "(): " + e.Message); } IColorSource onSource = Find(module, parsedParams[1]); IColorSource offSource = (parsedParams.Count > 2) ? Find(module, parsedParams[2]) : BLACK; return(new IfColorSource(toggle, parsedParams[0], onSource, offSource)); }
public static IScalar TryParse(PartModule module, ParsedParameters parsedParams) { if (parsedParams == null) { return(null); } if (parsedParams.Identifier != TYPE_NAME) { return(null); } parsedParams.RequireCount(module, 1); return(Of(Toggles.Require(module, parsedParams[0]), 1, 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); } }
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)); }
/// <summary> /// Try to get a "vessel situation" matcher from a ParsedParameters. The expected format is: /// /// situation(situation1, situation2, ...) /// /// Must have at least one situation. The allowed situation values are the enum constants /// in Vessel.Situations. /// </summary> /// <param name="module"></param> /// <param name="parsedParams"></param> /// <returns></returns> public static IToggle TryParse(PartModule module, ParsedParameters parsedParams) { if (parsedParams == null) { return(null); } if (!TYPE_NAME.Equals(parsedParams.Identifier)) { return(null); } parsedParams.RequireCount(module, 1, -1); Vessel.Situations requiredSituations = ParseSituation(module, parsedParams[0]); for (int i = 1; i < parsedParams.Count; ++i) { requiredSituations |= ParseSituation(module, parsedParams[i]); } return(new VesselSituationMatch(module, requiredSituations)); }
public static IScalar TryParse(PartModule module, ParsedParameters parsedParams) { if (parsedParams == null) { return(null); } if (parsedParams.Identifier != TYPE_NAME) { return(null); } parsedParams.RequireCount(module, 2, -1); IScalar[] inputs = new IScalar[parsedParams.Count]; for (int i = 0; i < inputs.Length; ++i) { inputs[i] = Scalars.Require(module, parsedParams[i]); } return(Of(inputs)); }
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 "logical AND" toggle from a ParsedParameters. The expected format is: /// /// and(toggle1, toggle2, ... toggleN) /// </summary> /// <param name="module"></param> /// <param name="parameters"></param> /// <returns></returns> public static IToggle TryParse(PartModule module, ParsedParameters parsedParams) { if (parsedParams == null) { return(null); } if (!TYPE_NAME.Equals(parsedParams.Identifier)) { return(null); } parsedParams.RequireCount(module, 1, -1); if (parsedParams.Count == 1) { return(Require(module, parsedParams[0])); } IToggle[] inputs = new IToggle[parsedParams.Count]; for (int i = 0; i < inputs.Length; ++i) { inputs[i] = Require(module, parsedParams[i]); } return(new LogicalOr(inputs)); }
/// <summary> /// Try to get a "vessel control level" matcher from a ParsedParameters. The expected format is: /// /// controlLevel(level1, level2, ...) /// /// Must have at least one level. The allowed control level values are the enum constants /// in Vessel.ControlLevel. /// </summary> /// <param name="module"></param> /// <param name="parsedParams"></param> /// <returns></returns> public static IToggle TryParse(PartModule module, ParsedParameters parsedParams) { if (parsedParams == null) { return(null); } if (!TYPE_NAME.Equals(parsedParams.Identifier)) { return(null); } parsedParams.RequireCount(module, 1, -1); HashSet <Vessel.ControlLevel> requiredLevels = new HashSet <Vessel.ControlLevel>(); for (int i = 0; i < parsedParams.Count; ++i) { Vessel.ControlLevel additionalControlLevel = ParseLevel(module, parsedParams[i]); if (!requiredLevels.Add(additionalControlLevel)) { throw new ArgumentException("Duplicate specification of '" + parsedParams[i] + "' for " + TYPE_NAME + "() on " + module.ClassName + " of " + module.part.GetTitle()); } } return(new VesselControlLevelMatch(module, requiredLevels)); }
/// <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)); }