public static Color ParseColor(string str) { str = str.TrimStart(colorTrimStartParameters); str = str.TrimEnd(colorTrimEndParameters); string[] array = str.Split(','); float num = ParseFloat(array[0]); float num2 = ParseFloat(array[1]); float num3 = ParseFloat(array[2]); bool num4 = num > 1f || num3 > 1f || num2 > 1f; float num5 = (!num4) ? 1 : 255; if (array.Length == 4) { num5 = FromString <float>(array[3]); } Color result = default(Color); if (!num4) { result.r = num; result.g = num2; result.b = num3; result.a = num5; return(result); } result = GenColor.FromBytes(Mathf.RoundToInt(num), Mathf.RoundToInt(num2), Mathf.RoundToInt(num3), Mathf.RoundToInt(num5)); return(result); }
public static Color FromHex(string hex) { if (hex.StartsWith("#")) { hex = hex.Substring(1); } Color result; if (hex.Length != 6 && hex.Length != 8) { Log.Error(hex + " is not a valid hex color.", false); result = Color.white; } else { int r = int.Parse(hex.Substring(0, 2), NumberStyles.HexNumber); int g = int.Parse(hex.Substring(2, 2), NumberStyles.HexNumber); int b = int.Parse(hex.Substring(4, 2), NumberStyles.HexNumber); int a = 255; if (hex.Length == 8) { a = int.Parse(hex.Substring(6, 2), NumberStyles.HexNumber); } result = GenColor.FromBytes(r, g, b, a); } return(result); }
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 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); }