public static LocalTargetInfo LocalTargetInfoFromNode(XmlNode node, string label, LocalTargetInfo defaultValue) { LoadIDsWantedBank loadIDs = Scribe.loader.crossRefs.loadIDs; if (node != null && Scribe.EnterNode(label)) { try { string innerText = node.InnerText; LocalTargetInfo result; if (innerText.Length != 0 && innerText[0] == '(') { loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), "thing"); result = new LocalTargetInfo(IntVec3.FromString(innerText)); return(result); } loadIDs.RegisterLoadIDReadFromXml(innerText, typeof(Thing), "thing"); result = LocalTargetInfo.Invalid; return(result); } finally { Scribe.ExitNode(); } } loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), label + "/thing"); return(defaultValue); }
public static TargetInfo TargetInfoFromNode(XmlNode node, string label, TargetInfo defaultValue) { LoadIDsWantedBank loadIDs = Scribe.loader.crossRefs.loadIDs; if (node != null && Scribe.EnterNode(label)) { try { string innerText = node.InnerText; TargetInfo result; if (innerText.Length != 0 && innerText[0] == '(') { string str; string targetLoadID; ScribeExtractor.ExtractCellAndMapPairFromTargetInfo(innerText, out str, out targetLoadID); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(targetLoadID, typeof(Map), "map"); result = new TargetInfo(IntVec3.FromString(str), null, true); return(result); } loadIDs.RegisterLoadIDReadFromXml(innerText, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), "map"); result = TargetInfo.Invalid; return(result); } finally { Scribe.ExitNode(); } } loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), label + "/thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), label + "/map"); return(defaultValue); }
public static TargetInfo TargetInfoFromNode(XmlNode node, string label, TargetInfo defaultValue) { LoadIDsWantedBank loadIDs = Scribe.loader.crossRefs.loadIDs; if (node != null && Scribe.EnterNode(label)) { try { string innerText = node.InnerText; if (innerText.Length != 0 && innerText[0] == '(') { ExtractCellAndMapPairFromTargetInfo(innerText, out var cell, out var map); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(map, typeof(Map), "map"); return(new TargetInfo(IntVec3.FromString(cell), null, allowNullMap: true)); } loadIDs.RegisterLoadIDReadFromXml(innerText, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), "map"); return(TargetInfo.Invalid); } finally { Scribe.ExitNode(); } } loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), label + "/thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), label + "/map"); return(defaultValue); }
public static GlobalTargetInfo GlobalTargetInfoFromNode(XmlNode node, string label, GlobalTargetInfo defaultValue) { LoadIDsWantedBank loadIDs = Scribe.loader.crossRefs.loadIDs; if (node != null && Scribe.EnterNode(label)) { try { string innerText = node.InnerText; if (innerText.Length != 0 && innerText[0] == '(') { string str; string targetLoadID; ScribeExtractor.ExtractCellAndMapPairFromTargetInfo(innerText, out str, out targetLoadID); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(targetLoadID, typeof(Map), "map"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(WorldObject), "worldObject"); return(new GlobalTargetInfo(IntVec3.FromString(str), null, true)); } int tile; if (int.TryParse(innerText, out tile)) { loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), "map"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(WorldObject), "worldObject"); return(new GlobalTargetInfo(tile)); } if (innerText.Length != 0 && innerText[0] == '@') { loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), "map"); loadIDs.RegisterLoadIDReadFromXml(innerText.Substring(1), typeof(WorldObject), "worldObject"); return(GlobalTargetInfo.Invalid); } loadIDs.RegisterLoadIDReadFromXml(innerText, typeof(Thing), "thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), "map"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(WorldObject), "worldObject"); return(GlobalTargetInfo.Invalid); } finally { Scribe.ExitNode(); } } loadIDs.RegisterLoadIDReadFromXml(null, typeof(Thing), label + "/thing"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(Map), label + "/map"); loadIDs.RegisterLoadIDReadFromXml(null, typeof(WorldObject), label + "/worldObject"); return(defaultValue); }
public static object FromString(string str, Type itemType) { try { itemType = (Nullable.GetUnderlyingType(itemType) ?? itemType); if (itemType == typeof(string)) { str = str.Replace("\\n", "\n"); return(str); } if (itemType == typeof(int)) { return(ParseIntPermissive(str)); } if (itemType == typeof(float)) { return(float.Parse(str, CultureInfo.InvariantCulture)); } if (itemType == typeof(bool)) { return(bool.Parse(str)); } if (itemType == typeof(long)) { return(long.Parse(str, CultureInfo.InvariantCulture)); } if (itemType == typeof(double)) { return(double.Parse(str, CultureInfo.InvariantCulture)); } if (itemType == typeof(sbyte)) { return(sbyte.Parse(str, CultureInfo.InvariantCulture)); } if (itemType.IsEnum) { try { object obj = BackCompatibility.BackCompatibleEnum(itemType, str); if (obj != null) { return(obj); } return(Enum.Parse(itemType, str)); } catch (ArgumentException innerException) { string str2 = "'" + str + "' is not a valid value for " + itemType + ". Valid values are: \n"; str2 += GenText.StringFromEnumerable(Enum.GetValues(itemType)); ArgumentException ex = new ArgumentException(str2, innerException); throw ex; } } if (itemType == typeof(Type)) { if (str == "null" || str == "Null") { return(null); } Type typeInAnyAssembly = GenTypes.GetTypeInAnyAssembly(str); if (typeInAnyAssembly == null) { Log.Error("Could not find a type named " + str); } return(typeInAnyAssembly); } if (itemType == typeof(Action)) { string[] array = str.Split('.'); string methodName = array[array.Length - 1]; string empty = string.Empty; empty = ((array.Length != 3) ? array[0] : (array[0] + "." + array[1])); Type typeInAnyAssembly2 = GenTypes.GetTypeInAnyAssembly(empty); MethodInfo method = typeInAnyAssembly2.GetMethods().First((MethodInfo m) => m.Name == methodName); return((Action)Delegate.CreateDelegate(typeof(Action), method)); } if (itemType == typeof(Vector3)) { return(FromStringVector3(str)); } if (itemType == typeof(Vector2)) { return(FromStringVector2(str)); } if (itemType == typeof(Rect)) { return(FromStringRect(str)); } if (itemType == typeof(Color)) { str = str.TrimStart('(', 'R', 'G', 'B', 'A'); str = str.TrimEnd(')'); string[] array2 = str.Split(','); float num = (float)FromString(array2[0], typeof(float)); float num2 = (float)FromString(array2[1], typeof(float)); float num3 = (float)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)FromString(array2[3], typeof(float)); } Color color = default(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)); } return(color); } if (itemType == typeof(PublishedFileId_t)) { return(new PublishedFileId_t(ulong.Parse(str))); } if (itemType == typeof(IntVec2)) { return(IntVec2.FromString(str)); } if (itemType == typeof(IntVec3)) { return(IntVec3.FromString(str)); } if (itemType == typeof(Rot4)) { return(Rot4.FromString(str)); } if (itemType == typeof(CellRect)) { return(CellRect.FromString(str)); } if (itemType != typeof(CurvePoint)) { if (itemType == typeof(NameTriple)) { NameTriple nameTriple = NameTriple.FromString(str); nameTriple.ResolveMissingPieces(); } else { if (itemType == typeof(FloatRange)) { return(FloatRange.FromString(str)); } if (itemType == typeof(IntRange)) { return(IntRange.FromString(str)); } if (itemType == typeof(QualityRange)) { return(QualityRange.FromString(str)); } if (itemType == typeof(ColorInt)) { str = str.TrimStart('(', 'R', 'G', 'B', 'A'); str = str.TrimEnd(')'); string[] array3 = str.Split(','); ColorInt colorInt = new ColorInt(255, 255, 255, 255); colorInt.r = (int)FromString(array3[0], typeof(int)); colorInt.g = (int)FromString(array3[1], typeof(int)); colorInt.b = (int)FromString(array3[2], typeof(int)); if (array3.Length == 4) { colorInt.a = (int)FromString(array3[3], typeof(int)); } else { colorInt.a = 255; } return(colorInt); } } throw new ArgumentException("Trying to parse to unknown data type " + itemType.Name + ". Content is '" + str + "'."); } return(CurvePoint.FromString(str)); } catch (Exception innerException2) { ArgumentException ex2 = new ArgumentException("Exception parsing " + itemType + " from \"" + str + "\"", innerException2); throw ex2; } }
public static IntVec3 ParseIntVec3(string str) { return(IntVec3.FromString(str)); }
public static object FromString(string str, Type itemType) { object result; try { itemType = (Nullable.GetUnderlyingType(itemType) ?? itemType); 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) { string text = string.Concat(new object[] { "'", str, "' is not a valid value for ", itemType, ". Valid values are: \n" }); text += GenText.StringFromEnumerable(Enum.GetValues(itemType)); ArgumentException ex = new ArgumentException(text, innerException); throw ex; } } if (itemType == typeof(Type)) { if (str == "null" || str == "Null") { result = null; } else { Type typeInAnyAssembly = GenTypes.GetTypeInAnyAssembly(str); if (typeInAnyAssembly == null) { Log.Error("Could not find a type named " + str); } result = typeInAnyAssembly; } } else if (itemType == typeof(Action)) { string[] array = str.Split(new char[] { '.' }); string methodName = array[array.Length - 1]; string typeName = string.Empty; if (array.Length == 3) { typeName = array[0] + "." + array[1]; } else { typeName = array[0]; } Type typeInAnyAssembly2 = GenTypes.GetTypeInAnyAssembly(typeName); MethodInfo method = typeInAnyAssembly2.GetMethods().First((MethodInfo m) => m.Name == methodName); result = (Action)Delegate.CreateDelegate(typeof(Action), method); } else if (itemType == typeof(Vector3)) { result = ParseHelper.FromStringVector3(str); } else if (itemType == typeof(Vector2)) { result = ParseHelper.FromStringVector2(str); } else if (itemType == typeof(Rect)) { result = ParseHelper.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)ParseHelper.FromString(array2[0], typeof(float)); float num2 = (float)ParseHelper.FromString(array2[1], typeof(float)); float num3 = (float)ParseHelper.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)ParseHelper.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 if (itemType == typeof(CellRect)) { result = CellRect.FromString(str); } else { if (itemType != typeof(CurvePoint)) { 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)ParseHelper.FromString(array3[0], typeof(int)); colorInt.g = (int)ParseHelper.FromString(array3[1], typeof(int)); colorInt.b = (int)ParseHelper.FromString(array3[2], typeof(int)); if (array3.Length == 4) { colorInt.a = (int)ParseHelper.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 = CurvePoint.FromString(str); } } } catch (Exception innerException2) { ArgumentException ex2 = new ArgumentException(string.Concat(new object[] { "Exception parsing ", itemType, " from \"", str, "\"" }), innerException2); throw ex2; } return(result); }