public override void OnGUI() { if (labelStyle == null) { labelStyle = new GUIStyle(UnityEngine.GUI.skin.label); labelStyle.alignment = TextAnchor.UpperLeft; labelStyle.richText = true; labelStyle.normal.textColor = textColor; labelStyle.fontSize = fontSize; } if (expandedText == null) { DataNode emptyNode = new DataNode("empty", null); ExpressionParser <string> parser = BaseParser.GetParser <string>(); expandedText = parser.ExecuteExpression("null", text, emptyNode); } GUILayout.Label(expandedText, labelStyle, GUILayout.ExpandWidth(true)); }
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); }
/// <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); }
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); }