public static T ValueFromNode <T>(XmlNode subNode, T defaultValue) { if (subNode == null) { return(defaultValue); } T result; try { try { result = (T)((object)Helper_Parsing.FromString(subNode.InnerText, typeof(T))); return(result); } catch (Exception ex) { //Log.Error(string.Concat(new object[] //{ // "Exception parsing node ", // subNode.OuterXml, // " into a ", // typeof(T), // ":\n", // ex.ToString() //})); } result = default(T); } catch (Exception arg) { //Log.Error("Exception loading XML: " + arg); result = defaultValue; } return(result); }
public static object FromString(string str, Type itemType) { object result = ""; try { if (itemType == typeof(string)) { str = str.Replace("\\n", "\n"); result = str; } else if (itemType == typeof(int)) { result = int.Parse(str, CultureInfo.InvariantCulture); } else if (itemType == typeof(float)) { result = float.Parse(str, CultureInfo.InvariantCulture); } else if (itemType == typeof(bool)) { result = bool.Parse(str); } else if (itemType == typeof(long)) { result = long.Parse(str, CultureInfo.InvariantCulture); } else if (itemType == typeof(double)) { result = double.Parse(str, CultureInfo.InvariantCulture); } else if (itemType == typeof(sbyte)) { result = sbyte.Parse(str, CultureInfo.InvariantCulture); } else { if (itemType.IsEnum) { try { result = Enum.Parse(itemType, str); return(result); } catch (ArgumentException innerException) { throw innerException; } } else if (itemType == typeof(Vector3)) { result = Helper_Parsing.FromStringVector3(str); } else if (itemType == typeof(Vector2)) { result = Helper_Parsing.FromStringVector2(str); } else if (itemType == typeof(Rect)) { result = Helper_Parsing.FromStringRect(str); } else if (itemType == typeof(Color)) { str = str.TrimStart(new char[] { '(', 'R', 'G', 'B', 'A' }); str = str.TrimEnd(new char[] { ')' }); string[] array2 = str.Split(new char[] { ',' }); float num = (float)Helper_Parsing.FromString(array2[0], typeof(float)); float num2 = (float)Helper_Parsing.FromString(array2[1], typeof(float)); float num3 = (float)Helper_Parsing.FromString(array2[2], typeof(float)); bool flag = num > 1f || num3 > 1f || num2 > 1f; float num4 = (float)((!flag) ? 1 : 255); if (array2.Length == 4) { num4 = (float)Helper_Parsing.FromString(array2[3], typeof(float)); } Color color; //if (!flag) //{ color.r = num; color.g = num2; color.b = num3; color.a = num4; //} //else //{ // color = GenColor.FromBytes(Mathf.RoundToInt(num), Mathf.RoundToInt(num2), Mathf.RoundToInt(num3), Mathf.RoundToInt(num4)); //} result = color; } //else if (itemType == typeof(PublishedFileId_t)) //{ // result = new PublishedFileId_t(ulong.Parse(str)); //} else if (itemType == typeof(IntVec2)) { result = IntVec2.FromString(str); } else if (itemType == typeof(IntVec3)) { result = IntVec3.FromString(str); } else if (itemType == typeof(Rot4)) { result = Rot4.FromString(str); } else { // Nothing compatible found? Handle as if it is a string. str = str.Replace("\\n", "\n"); result = str; } //else //{ //if (itemType != typeof(CellRect)) //{ // if (itemType == typeof(NameTriple)) // { // NameTriple nameTriple = NameTriple.FromString(str); // nameTriple.ResolveMissingPieces(null); // } // else // { // if (itemType == typeof(FloatRange)) // { // result = FloatRange.FromString(str); // return result; // } // if (itemType == typeof(IntRange)) // { // result = IntRange.FromString(str); // return result; // } // if (itemType == typeof(QualityRange)) // { // result = QualityRange.FromString(str); // return result; // } // if (itemType == typeof(ColorInt)) // { // str = str.TrimStart(new char[] // { // '(', // 'R', // 'G', // 'B', // 'A' // }); // str = str.TrimEnd(new char[] // { // ')' // }); // string[] array3 = str.Split(new char[] // { // ',' // }); // ColorInt colorInt = new ColorInt(255, 255, 255, 255); // colorInt.r = (int)Helper_Parsing.FromString(array3[0], typeof(int)); // colorInt.g = (int)Helper_Parsing.FromString(array3[1], typeof(int)); // colorInt.b = (int)Helper_Parsing.FromString(array3[2], typeof(int)); // if (array3.Length == 4) // { // colorInt.a = (int)Helper_Parsing.FromString(array3[3], typeof(int)); // } // else // { // colorInt.a = 255; // } // result = colorInt; // return result; // } // } // throw new ArgumentException(string.Concat(new string[] // { // "Trying to parse to unknown data type ", // itemType.Name, // ". Content is '", // str, // "'." // })); //} // result = CellRect.FromString(str); //} } } catch (Exception innerException2) { ArgumentException ex2 = new ArgumentException(string.Concat(new object[] { "Exception parsing ", itemType, " from \"", str, "\"" }), innerException2); throw ex2; } return(result); }
public static void LookList <T>(ref List <T> list, bool saveDestroyedThings, string label, LookMode lookMode = LookMode.Undefined, params object[] ctorArgs) { if (lookMode == LookMode.Undefined) { if (Helper_Parsing.HandlesType(typeof(T))) { lookMode = LookMode.Value; } else { lookMode = LookMode.Deep; } } if (Scribe.mode == LoadSaveMode.Saving) { if (list == null && lookMode == LookMode.Reference) { //Log.Warning(string.Concat(new object[] //{ // "Saving null list \"", // label, // "\" with look mode ", // lookMode, // ". This will cause bugs because null lists are not registered during loading so CrossRefResolver will break." //})); } Scribe.EnterNode(label); if (list == null) { Scribe.WriteAttribute("IsNull", "True"); } else { foreach (T current in list) { if (lookMode == LookMode.Value) { T t = current; Scribe_Values.LookValue <T>(ref t, "li", default(T), true); } else if (lookMode == LookMode.Deep) { T t2 = current; Scribe_Deep.LookDeep <T>(ref t2, "li", ctorArgs); } } } Scribe.ExitNode(); } else if (Scribe.mode == LoadSaveMode.LoadingVars) { if (Scribe.curParent == null) { //Log.Error("XmlHandling.curParent is null. I'm not sure why."); list = null; return; } XmlNode xmlNode = Scribe.curParent[label]; if (xmlNode == null) { list = null; return; } XmlAttribute xmlAttribute = xmlNode.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToLower() == "true") { list = null; return; } if (lookMode == LookMode.Value) { list = new List <T>(xmlNode.ChildNodes.Count); foreach (XmlNode subNode in xmlNode.ChildNodes) { T item = ScribeExtractor.ValueFromNode <T>(subNode, default(T)); list.Add(item); } } else if (lookMode == LookMode.Deep) { list = new List <T>(xmlNode.ChildNodes.Count); foreach (XmlNode subNode2 in xmlNode.ChildNodes) { T item2 = ScribeExtractor.SaveableFromNode <T>(subNode2, ctorArgs); list.Add(item2); } } } }