示例#1
0
        /// <summary>
        /// Parses a value from a child config node.
        /// </summary>
        /// <typeparam name="T">The type to convert to.</typeparam>
        /// <param name="configNode">The ConfigNode to read from</param>
        /// <param name="key">The key to examine.</param>
        /// <param name="allowExpression">Whether the read value can be an expression.</param>
        /// <returns>The parsed value</returns>
        public static T ParseNode <T>(ConfigNode configNode, string key, bool allowExpression = false)
        {
            T value;

            if (typeof(T) == typeof(Orbit))
            {
                // Get the orbit node
                ConfigNode orbitNode = configNode.GetNode(key);

                // Get our child values
                DataNode oldNode = currentDataNode;
                try
                {
                    currentDataNode = oldNode.GetChild(key);
                    if (currentDataNode == null)
                    {
                        currentDataNode = new DataNode(key, oldNode, oldNode.Factory);
                    }

                    foreach (string orbitKey in new string[] { "SMA", "ECC", "INC", "LPE", "LAN", "MNA", "EPH", "REF" })
                    {
                        object orbitVal;
                        if (orbitKey == "REF")
                        {
                            ParseValue <int>(orbitNode, orbitKey, x => orbitVal = x, oldNode.Factory, 1);
                        }
                        else
                        {
                            ParseValue <double>(orbitNode, orbitKey, x => orbitVal = x, oldNode.Factory, 0.0);
                        }
                    }
                }
                finally
                {
                    currentDataNode = oldNode;
                }

                // Get the orbit parser
                ExpressionParser <T> parser = BaseParser.GetParser <T>();
                if (parser == null)
                {
                    throw new Exception("Couldn't instantiate orbit parser!");
                }

                // Parse the special expression
                string expression = "CreateOrbit([@" + key + "/SMA, @" + key + "/ECC, @" + key +
                                    "/INC, @" + key + "/LPE, @" + key + "/LAN, @" + key + "/MNA, @" + key +
                                    "/EPH ], @" + key + "/REF)";
                if (initialLoad)
                {
                    value = parser.ParseExpression(key, expression, currentDataNode);
                }
                else
                {
                    value = parser.ExecuteExpression(key, expression, currentDataNode);
                }
            }
            else
            {
                throw new Exception("Unhandled type for child node parsing: " + typeof(T));
            }

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

            // Get expression
            valid &= ConfigNodeUtil.ParseValue <string>(configNode, "expression", ref expression, this, x => ExpressionParser.ParseExpression(x) == 0.0 || true);

            return(valid);
        }
 public override bool RequirementMet(ConfiguredContract contract)
 {
     return(ExpressionParser.ExecuteExpression(expression) != 0.0);
 }
示例#4
0
        private static T ParseSingleValue <T>(string key, string stringValue, bool allowExpression)
        {
            ExpressionParser <T> parser = BaseParser.GetParser <T>();
            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 != 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(ProtoCrewMember))
            {
                value = (T)(object)ParseProtoCrewMemberValue(stringValue);
            }
            else if (typeof(T) == typeof(Kerbal))
            {
                value = (T)(object)new Kerbal(ParseProtoCrewMemberValue(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(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);
            }
            // 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);
        }