public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            // Get type
            string contractType = null;

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "contractType", ref contractType, this);
            if (valid)
            {
                if (ContractType.contractTypes.Keys.Contains(contractType))
                {
                    ccType = contractType;
                }
                else
                {
                    ccType = null;

                    // Search for the correct type
                    var classes =
                        from assembly in AppDomain.CurrentDomain.GetAssemblies()
                        from type in assembly.GetTypes()
                        where type.IsSubclassOf(typeof(Contract)) && type.Name.Equals(contractType)
                        select type;

                    if (classes.Count() < 1)
                    {
                        valid = false;
                        LoggingUtil.LogError(this.GetType(), "contractType '" + contractType +
                                             "' must either be a Contract sub-class or ContractConfigurator contract type");
                    }
                    else
                    {
                        contractClass = classes.First();
                    }
                }
            }

            valid &= ConfigNodeUtil.ParseValue <uint>(configNode, "minCount", ref minCount, this, 1);
            valid &= ConfigNodeUtil.ParseValue <uint>(configNode, "maxCount", ref maxCount, this, UInt32.MaxValue);

            // Get cooldown
            string cooldownStr = null;

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "cooldownDuration", ref cooldownStr, this, "");
            if (cooldownStr != null)
            {
                cooldown = cooldownStr != "" ? DurationUtil.ParseDuration(cooldownStr) : 0.0;
            }

            return(valid);
        }
예제 #2
0
        public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            // Get duration
            string durationStr = null;

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "duration", ref durationStr, this, "");
            if (durationStr != null)
            {
                duration = durationStr != "" ? DurationUtil.ParseDuration(durationStr) : 0.0;
            }

            return(valid);
        }
예제 #3
0
        public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            // Get duration
            string durationStr = null;

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "duration", ref durationStr, this, "");
            if (durationStr != null)
            {
                duration = durationStr != "" ? DurationUtil.ParseDuration(durationStr) : 0.0;
            }

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "define", ref define, this, (string)null);
            valid &= ConfigNodeUtil.ParseValue <List <string> >(configNode, "vessel", ref vesselList, this, new List <string>());

            return(valid);
        }
        public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "minAltitude", ref minAltitude, this, 0.0, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "maxAltitude", ref maxAltitude, this, double.MaxValue, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "minApA", ref minApoapsis, this, 0.0, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "maxApA", ref maxApoapsis, this, double.MaxValue, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "minPeA", ref minPeriapsis, this, 0.0, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "maxPeA", ref maxPeriapsis, this, double.MaxValue, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "minEccentricity", ref minEccentricity, this, 0.0, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "maxEccentricity", ref maxEccentricity, this, double.MaxValue, x => Validation.GE(x, 0.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "minInclination", ref minInclination, this, 0.0, x => Validation.Between(x, 0.0, 180.0));
            valid &= ConfigNodeUtil.ParseValue <double>(configNode, "maxInclination", ref maxInclination, this, 180.0, x => Validation.Between(x, 0.0, 180.0));

            // Get minPeriod
            string minPeriodStr = null;

            valid    &= ConfigNodeUtil.ParseValue <string>(configNode, "minPeriod", ref minPeriodStr, this, (string)null);
            minPeriod = minPeriodStr != null?DurationUtil.ParseDuration(minPeriodStr) : 0.0;

            // Get maxPeriod
            string maxPeriodStr = null;

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "maxPeriod", ref maxPeriodStr, this, (string)null);
            if (maxPeriodStr != null)
            {
                maxPeriod = DurationUtil.ParseDuration(maxPeriodStr);
            }
            maxPeriod = maxPeriodStr != null?DurationUtil.ParseDuration(maxPeriodStr) : double.MaxValue;

            // Validate target body
            valid &= ValidateTargetBody(configNode);

            // Validation minimum and groupings
            valid &= ConfigNodeUtil.AtLeastOne(configNode, new string[] { "minAltitude", "maxAltitude", "minApA", "maxApA", "minPeA", "maxPeA",
                                                                          "minEccentricity", "maxEccentricity", "minInclination", "maxInclination", "minPeriod", "maxPeriod" }, this);
            valid &= ConfigNodeUtil.MutuallyExclusive(configNode, new string[] { "minAltitude", "maxAltitude" },
                                                      new string[] { "minApA", "maxApA", "minPeA", "maxPeA" }, this);

            return(valid);
        }
예제 #5
0
        public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            // Get duration
            string durationStr = null;

            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "duration", ref durationStr, this, "");
            if (durationStr != null)
            {
                duration = durationStr != "" ? DurationUtil.ParseDuration(durationStr) : 0.0;
            }
            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "preWaitText", ref preWaitText, this);
            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "waitingText", ref waitingText, this);
            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "completionText", ref completionText, this);
            valid &= ConfigNodeUtil.ValidateExcludedValue(configNode, "title", this);

            return(valid);
        }
예제 #6
0
        public static T ParseSingleValue <T>(string key, string stringValue, bool allowExpression)
        {
            ExpressionParser <T> parser;
            T value;

            // Handle nullable
            if (typeof(T).Name == "Nullable`1")
            {
                if (typeof(T).GetGenericArguments()[0].IsEnum)
                {
                    value = (T)Enum.Parse(typeof(T).GetGenericArguments()[0], stringValue);
                }
                else
                {
                    value = (T)Convert.ChangeType(stringValue, typeof(T).GetGenericArguments()[0]);
                }
            }
            else if (allowExpression && (parser = BaseParser.GetParser <T>()) != null)
            {
                if (initialLoad)
                {
                    value = parser.ParseExpression(key, stringValue, currentDataNode);
                }
                else
                {
                    value = parser.ExecuteExpression(key, stringValue, currentDataNode);
                }
            }
            // Enum parsing logic
            else if (typeof(T).IsEnum)
            {
                value = (T)Enum.Parse(typeof(T), stringValue);
            }
            else if (typeof(T) == typeof(AvailablePart))
            {
                value = (T)(object)ParsePartValue(stringValue);
            }
            else if (typeof(T) == typeof(ContractGroup))
            {
                if (!ContractGroup.contractGroups.ContainsKey(stringValue))
                {
                    throw new ArgumentException("No contract group with name '" + stringValue + "'");
                }
                value = (T)(object)ContractGroup.contractGroups[stringValue];
            }
            else if (typeof(T) == typeof(CelestialBody))
            {
                value = (T)(object)ParseCelestialBodyValue(stringValue);
            }
            else if (typeof(T) == typeof(PartResourceDefinition))
            {
                value = (T)(object)ParseResourceValue(stringValue);
            }
            else if (typeof(T) == typeof(Resource))
            {
                value = (T)(object)new Resource(ParseResourceValue(stringValue));
            }
            else if (typeof(T) == typeof(Agent))
            {
                value = (T)(object)ParseAgentValue(stringValue);
            }
            else if (typeof(T) == typeof(Duration))
            {
                value = (T)(object)new Duration(DurationUtil.ParseDuration(stringValue));
            }
            else if (typeof(T) == typeof(ProtoCrewMember))
            {
                value = (T)(object)ParseProtoCrewMemberValue(stringValue);
            }
            else if (typeof(T) == typeof(Kerbal))
            {
                value = (T)(object)new Kerbal(stringValue);
            }
            else if (typeof(T) == typeof(Guid))
            {
                value = (T)(object)new Guid(stringValue);
            }
            else if (typeof(T) == typeof(Vessel))
            {
                value = (T)(object)ParseVesselValue(stringValue);
            }
            else if (typeof(T) == typeof(VesselIdentifier))
            {
                value = (T)(object)new VesselIdentifier(stringValue);
            }
            else if (typeof(T) == typeof(Vector3))
            {
                string[] vals = stringValue.Split(new char[] { ',' });
                float    x    = (float)Convert.ChangeType(vals[0], typeof(float));
                float    y    = (float)Convert.ChangeType(vals[1], typeof(float));
                float    z    = (float)Convert.ChangeType(vals[2], typeof(float));
                value = (T)(object)new Vector3(x, y, z);
            }
            else if (typeof(T) == typeof(Vector3d))
            {
                string[] vals = stringValue.Split(new char[] { ',' });
                double   x    = (double)Convert.ChangeType(vals[0], typeof(double));
                double   y    = (double)Convert.ChangeType(vals[1], typeof(double));
                double   z    = (double)Convert.ChangeType(vals[2], typeof(double));
                value = (T)(object)new Vector3d(x, y, z);
            }
            else if (typeof(T) == typeof(Type))
            {
                value = (T)(object)ParseTypeValue(stringValue);
            }
            else if (typeof(T) == typeof(ScienceSubject))
            {
                value = (T)(object)(ResearchAndDevelopment.Instance != null ? ResearchAndDevelopment.GetSubjectByID(stringValue) : null);
            }
            else if (typeof(T) == typeof(ScienceExperiment))
            {
                value = (T)(object)(ResearchAndDevelopment.Instance != null ? ResearchAndDevelopment.GetExperiment(stringValue) : null);
            }
            else if (typeof(T) == typeof(Color))
            {
                if ((stringValue.Length != 7 && stringValue.Length != 9) || stringValue[0] != '#')
                {
                    throw new ArgumentException("Invalid color code '" + stringValue + "': Must be # followed by 6 or 8 hex digits (ARGB or RGB).");
                }
                stringValue = stringValue.Replace("#", "");
                int a = 255;
                if (stringValue.Length == 8)
                {
                    a           = byte.Parse(stringValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
                    stringValue = stringValue.Substring(2, 6);
                }
                int r = byte.Parse(stringValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
                int g = byte.Parse(stringValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
                int b = byte.Parse(stringValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

                value = (T)(object)(new Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f));
            }
            else if (typeof(T) == typeof(Biome))
            {
                string[]      biomeData = stringValue.Split(new char[] { ';' });
                CelestialBody cb        = ParseCelestialBodyValue(biomeData[0]);
                value = (T)(object)(new Biome(cb, biomeData[1]));
            }
            // Do newline conversions
            else if (typeof(T) == typeof(string))
            {
                value = (T)(object)stringValue.Replace("\\n", "\n");
            }
            // Try a basic type
            else
            {
                value = (T)Convert.ChangeType(stringValue, typeof(T));
            }

            return(value);
        }
예제 #7
0
 public override string ToString()
 {
     return(DurationUtil.StringValue(Value));
 }
예제 #8
0
 public Duration(string durationStr)
 {
     Value = DurationUtil.ParseDuration(durationStr);
 }
예제 #9
0
 public override string ToString()
 {
     return("Duration[" + DurationUtil.StringValue(Value) + "]");
 }