Use CurrentCulture, and use whitespace instead of comma as separator

This commit is contained in:
Sinai 2021-07-27 22:17:26 +10:00
parent b7e275f02c
commit dabf92a1a5
4 changed files with 65 additions and 67 deletions

View File

@ -13,7 +13,7 @@ namespace UnityExplorer
/// </summary> /// </summary>
public static bool ContainsIgnoreCase(this string _this, string s) public static bool ContainsIgnoreCase(this string _this, string s)
{ {
return ParseUtility.en_US.CompareInfo.IndexOf(_this, s, CompareOptions.IgnoreCase) >= 0; return CultureInfo.CurrentCulture.CompareInfo.IndexOf(_this, s, CompareOptions.IgnoreCase) >= 0;
} }
/// <summary> /// <summary>

View File

@ -10,8 +10,6 @@ namespace UnityExplorer
{ {
public static class ParseUtility public static class ParseUtility
{ {
public static CultureInfo en_US = new CultureInfo("en-US");
private static readonly HashSet<Type> nonPrimitiveTypes = new HashSet<Type> private static readonly HashSet<Type> nonPrimitiveTypes = new HashSet<Type>
{ {
typeof(string), typeof(string),
@ -19,20 +17,18 @@ namespace UnityExplorer
typeof(DateTime), typeof(DateTime),
}; };
public const string NUMBER_FORMAT = "0.####"; // Helper for formatting float/double/decimal numbers to maximum of 4 decimal points.
// And also for formatting a sequence of those numbers, ie a Vector3, Color etc
public static readonly string NumberFormatString = $"0{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}####";
private static readonly Dictionary<int, string> numSequenceStrings = new Dictionary<int, string>(); private static readonly Dictionary<int, string> numSequenceStrings = new Dictionary<int, string>();
// Helper for formatting float/double/decimal numbers to maximum of 4 decimal points.
public static string FormatDecimalSequence(params object[] numbers) public static string FormatDecimalSequence(params object[] numbers)
{ {
if (numbers.Length <= 0) if (numbers.Length <= 0)
return null; return null;
int count = numbers.Length; return string.Format(CultureInfo.CurrentCulture, GetSequenceFormatString(numbers.Length), numbers);
var formatString = GetSequenceFormatString(count);
return string.Format(en_US, formatString, numbers);
} }
public static string GetSequenceFormatString(int count) public static string GetSequenceFormatString(int count)
@ -46,19 +42,19 @@ namespace UnityExplorer
string[] strings = new string[count]; string[] strings = new string[count];
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
strings[i] = $"{{{i}:{NUMBER_FORMAT}}}"; strings[i] = $"{{{i}:{NumberFormatString}}}";
string s = string.Join(", ", strings); string ret = string.Join(" ", strings);
numSequenceStrings.Add(count, ret);
numSequenceStrings.Add(count, s); return ret;
return s;
} }
// Main parsing API
public static bool CanParse(Type type) public static bool CanParse(Type type)
{ {
if (string.IsNullOrEmpty(type.FullName)) return !string.IsNullOrEmpty(type?.FullName)
return false; && (type.IsPrimitive || type.IsEnum || nonPrimitiveTypes.Contains(type) || customTypes.ContainsKey(type.FullName));
return type.IsPrimitive || type.IsEnum || nonPrimitiveTypes.Contains(type) || customTypes.ContainsKey(type.FullName);
} }
public static bool TryParse(string input, Type type, out object obj, out Exception parseException) public static bool TryParse(string input, Type type, out object obj, out Exception parseException)
@ -143,7 +139,7 @@ namespace UnityExplorer
else if (formattedTypes.Contains(type)) else if (formattedTypes.Contains(type))
{ {
return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(string), typeof(IFormatProvider) }) return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(string), typeof(IFormatProvider) })
.Invoke(obj, new object[] { NUMBER_FORMAT, en_US }) .Invoke(obj, new object[] { NumberFormatString, CultureInfo.CurrentCulture })
as string; as string;
} }
else else
@ -166,9 +162,7 @@ namespace UnityExplorer
try try
{ {
if (type.IsEnum) if (type.IsEnum)
{
typeInputExamples.Add(type.AssemblyQualifiedName, Enum.GetNames(type).First()); typeInputExamples.Add(type.AssemblyQualifiedName, Enum.GetNames(type).First());
}
else else
{ {
var instance = Activator.CreateInstance(type); var instance = Activator.CreateInstance(type);
@ -222,10 +216,10 @@ namespace UnityExplorer
{ {
Vector2 vector = default; Vector2 vector = default;
var split = input.Split(','); var split = input.Split(' ');
vector.x = float.Parse(split[0].Trim(), en_US); vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
vector.y = float.Parse(split[1].Trim(), en_US); vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
return vector; return vector;
} }
@ -244,11 +238,11 @@ namespace UnityExplorer
{ {
Vector3 vector = default; Vector3 vector = default;
var split = input.Split(','); var split = input.Split(' ');
vector.x = float.Parse(split[0].Trim(), en_US); vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
vector.y = float.Parse(split[1].Trim(), en_US); vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
vector.z = float.Parse(split[2].Trim(), en_US); vector.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
return vector; return vector;
} }
@ -267,12 +261,12 @@ namespace UnityExplorer
{ {
Vector4 vector = default; Vector4 vector = default;
var split = input.Split(','); var split = input.Split(' ');
vector.x = float.Parse(split[0].Trim(), en_US); vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
vector.y = float.Parse(split[1].Trim(), en_US); vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
vector.z = float.Parse(split[2].Trim(), en_US); vector.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
vector.w = float.Parse(split[3].Trim(), en_US); vector.w = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
return vector; return vector;
} }
@ -291,22 +285,22 @@ namespace UnityExplorer
{ {
Vector3 vector = default; Vector3 vector = default;
var split = input.Split(','); var split = input.Split(' ');
if (split.Length == 4) if (split.Length == 4)
{ {
Quaternion quat = default; Quaternion quat = default;
quat.x = float.Parse(split[0].Trim(), en_US); quat.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
quat.y = float.Parse(split[1].Trim(), en_US); quat.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
quat.z = float.Parse(split[2].Trim(), en_US); quat.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
quat.w = float.Parse(split[3].Trim(), en_US); quat.w = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
return quat; return quat;
} }
else else
{ {
vector.x = float.Parse(split[0].Trim(), en_US); vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
vector.y = float.Parse(split[1].Trim(), en_US); vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
vector.z = float.Parse(split[2].Trim(), en_US); vector.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
return Quaternion.Euler(vector); return Quaternion.Euler(vector);
} }
} }
@ -327,12 +321,12 @@ namespace UnityExplorer
{ {
Rect rect = default; Rect rect = default;
var split = input.Split(','); var split = input.Split(' ');
rect.x = float.Parse(split[0].Trim(), en_US); rect.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
rect.y = float.Parse(split[1].Trim(), en_US); rect.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
rect.width = float.Parse(split[2].Trim(), en_US); rect.width = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
rect.height = float.Parse(split[3].Trim(), en_US); rect.height = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
return rect; return rect;
} }
@ -351,13 +345,13 @@ namespace UnityExplorer
{ {
Color color = default; Color color = default;
var split = input.Split(','); var split = input.Split(' ');
color.r = float.Parse(split[0].Trim(), en_US); color.r = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
color.g = float.Parse(split[1].Trim(), en_US); color.g = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
color.b = float.Parse(split[2].Trim(), en_US); color.b = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
if (split.Length > 3) if (split.Length > 3)
color.a = float.Parse(split[3].Trim(), en_US); color.a = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
else else
color.a = 1; color.a = 1;
@ -378,13 +372,13 @@ namespace UnityExplorer
{ {
Color32 color = default; Color32 color = default;
var split = input.Split(','); var split = input.Split(' ');
color.r = byte.Parse(split[0].Trim(), en_US); color.r = byte.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
color.g = byte.Parse(split[1].Trim(), en_US); color.g = byte.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
color.b = byte.Parse(split[2].Trim(), en_US); color.b = byte.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
if (split.Length > 3) if (split.Length > 3)
color.a = byte.Parse(split[3].Trim(), en_US); color.a = byte.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
else else
color.a = 255; color.a = 255;
@ -397,7 +391,7 @@ namespace UnityExplorer
return null; return null;
// ints, this is fine // ints, this is fine
return $"{color.r}, {color.g}, {color.b}, {color.a}"; return $"{color.r} {color.g} {color.b} {color.a}";
} }
// Layermask (Int32) // Layermask (Int32)

View File

@ -209,6 +209,10 @@ namespace UnityExplorer.UI.Panels
{ {
ExplorerCore.LogWarning("Invalid or corrupt panel save data! Restoring to default."); ExplorerCore.LogWarning("Invalid or corrupt panel save data! Restoring to default.");
SetTransformDefaults(); SetTransformDefaults();
UIManager.Initializing = false;
DoSaveToConfigElement();
ConfigManager.InternalHandler.SaveConfig();
UIManager.Initializing = true;
} }
} }
@ -335,7 +339,7 @@ namespace UnityExplorer.UI.Panels
if (!rect) if (!rect)
throw new ArgumentNullException("rect"); throw new ArgumentNullException("rect");
return string.Format(ParseUtility.en_US, "{0},{1},{2},{3}", new object[] return string.Format(CultureInfo.CurrentCulture, "{0} {1} {2} {3}", new object[]
{ {
rect.anchorMin.x, rect.anchorMin.x,
rect.anchorMin.y, rect.anchorMin.y,
@ -349,16 +353,16 @@ namespace UnityExplorer.UI.Panels
if (string.IsNullOrEmpty(stringAnchors)) if (string.IsNullOrEmpty(stringAnchors))
throw new ArgumentNullException("stringAnchors"); throw new ArgumentNullException("stringAnchors");
var split = stringAnchors.Split(','); var split = stringAnchors.Split(' ');
if (split.Length != 4) if (split.Length != 4)
throw new Exception($"stringAnchors split is unexpected length: {split.Length}"); throw new Exception($"stringAnchors split is unexpected length: {split.Length}");
Vector4 anchors; Vector4 anchors;
anchors.x = float.Parse(split[0], ParseUtility.en_US); anchors.x = float.Parse(split[0], CultureInfo.CurrentCulture);
anchors.y = float.Parse(split[1], ParseUtility.en_US); anchors.y = float.Parse(split[1], CultureInfo.CurrentCulture);
anchors.z = float.Parse(split[2], ParseUtility.en_US); anchors.z = float.Parse(split[2], CultureInfo.CurrentCulture);
anchors.w = float.Parse(split[3], ParseUtility.en_US); anchors.w = float.Parse(split[3], CultureInfo.CurrentCulture);
panel.anchorMin = new Vector2(anchors.x, anchors.y); panel.anchorMin = new Vector2(anchors.x, anchors.y);
panel.anchorMax = new Vector2(anchors.z, anchors.w); panel.anchorMax = new Vector2(anchors.z, anchors.w);
@ -369,7 +373,7 @@ namespace UnityExplorer.UI.Panels
if (!rect) if (!rect)
throw new ArgumentNullException("rect"); throw new ArgumentNullException("rect");
return string.Format(ParseUtility.en_US, "{0},{1}", new object[] return string.Format(CultureInfo.CurrentCulture, "{0} {1}", new object[]
{ {
rect.localPosition.x, rect.localPosition.y rect.localPosition.x, rect.localPosition.y
}); });
@ -377,14 +381,14 @@ namespace UnityExplorer.UI.Panels
internal static void SetPositionFromString(this RectTransform rect, string stringPosition) internal static void SetPositionFromString(this RectTransform rect, string stringPosition)
{ {
var split = stringPosition.Split(','); var split = stringPosition.Split(' ');
if (split.Length != 2) if (split.Length != 2)
throw new Exception($"stringPosition split is unexpected length: {split.Length}"); throw new Exception($"stringPosition split is unexpected length: {split.Length}");
Vector3 vector = rect.localPosition; Vector3 vector = rect.localPosition;
vector.x = float.Parse(split[0], ParseUtility.en_US); vector.x = float.Parse(split[0], CultureInfo.CurrentCulture);
vector.y = float.Parse(split[1], ParseUtility.en_US); vector.y = float.Parse(split[1], CultureInfo.CurrentCulture);
rect.localPosition = vector; rect.localPosition = vector;
} }
} }

View File

@ -36,7 +36,7 @@ namespace UnityExplorer.UI
Bottom Bottom
} }
public static bool Initializing { get; private set; } = true; public static bool Initializing { get; internal set; } = true;
private static readonly Dictionary<Panels, UIPanel> UIPanels = new Dictionary<Panels, UIPanel>(); private static readonly Dictionary<Panels, UIPanel> UIPanels = new Dictionary<Panels, UIPanel>();