Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit dabf92a

Browse files
committed
Use CurrentCulture, and use whitespace instead of comma as separator
1 parent b7e275f commit dabf92a

File tree

4 files changed

+65
-67
lines changed

4 files changed

+65
-67
lines changed

src/Core/Utility/MiscUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class MiscUtility
1313
/// </summary>
1414
public static bool ContainsIgnoreCase(this string _this, string s)
1515
{
16-
return ParseUtility.en_US.CompareInfo.IndexOf(_this, s, CompareOptions.IgnoreCase) >= 0;
16+
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(_this, s, CompareOptions.IgnoreCase) >= 0;
1717
}
1818

1919
/// <summary>

src/Core/Utility/ParseUtility.cs

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,25 @@ namespace UnityExplorer
1010
{
1111
public static class ParseUtility
1212
{
13-
public static CultureInfo en_US = new CultureInfo("en-US");
14-
1513
private static readonly HashSet<Type> nonPrimitiveTypes = new HashSet<Type>
1614
{
1715
typeof(string),
1816
typeof(decimal),
1917
typeof(DateTime),
2018
};
2119

22-
public const string NUMBER_FORMAT = "0.####";
20+
// Helper for formatting float/double/decimal numbers to maximum of 4 decimal points.
21+
// And also for formatting a sequence of those numbers, ie a Vector3, Color etc
2322

23+
public static readonly string NumberFormatString = $"0{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}####";
2424
private static readonly Dictionary<int, string> numSequenceStrings = new Dictionary<int, string>();
2525

26-
// Helper for formatting float/double/decimal numbers to maximum of 4 decimal points.
2726
public static string FormatDecimalSequence(params object[] numbers)
2827
{
2928
if (numbers.Length <= 0)
3029
return null;
3130

32-
int count = numbers.Length;
33-
var formatString = GetSequenceFormatString(count);
34-
35-
return string.Format(en_US, formatString, numbers);
31+
return string.Format(CultureInfo.CurrentCulture, GetSequenceFormatString(numbers.Length), numbers);
3632
}
3733

3834
public static string GetSequenceFormatString(int count)
@@ -46,19 +42,19 @@ public static string GetSequenceFormatString(int count)
4642
string[] strings = new string[count];
4743

4844
for (int i = 0; i < count; i++)
49-
strings[i] = $"{{{i}:{NUMBER_FORMAT}}}";
50-
51-
string s = string.Join(", ", strings);
45+
strings[i] = $"{{{i}:{NumberFormatString}}}";
5246

53-
numSequenceStrings.Add(count, s);
54-
return s;
47+
string ret = string.Join(" ", strings);
48+
numSequenceStrings.Add(count, ret);
49+
return ret;
5550
}
5651

52+
// Main parsing API
53+
5754
public static bool CanParse(Type type)
5855
{
59-
if (string.IsNullOrEmpty(type.FullName))
60-
return false;
61-
return type.IsPrimitive || type.IsEnum || nonPrimitiveTypes.Contains(type) || customTypes.ContainsKey(type.FullName);
56+
return !string.IsNullOrEmpty(type?.FullName)
57+
&& (type.IsPrimitive || type.IsEnum || nonPrimitiveTypes.Contains(type) || customTypes.ContainsKey(type.FullName));
6258
}
6359

6460
public static bool TryParse(string input, Type type, out object obj, out Exception parseException)
@@ -143,7 +139,7 @@ public static string ToStringForInput(object obj, Type type)
143139
else if (formattedTypes.Contains(type))
144140
{
145141
return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(string), typeof(IFormatProvider) })
146-
.Invoke(obj, new object[] { NUMBER_FORMAT, en_US })
142+
.Invoke(obj, new object[] { NumberFormatString, CultureInfo.CurrentCulture })
147143
as string;
148144
}
149145
else
@@ -166,9 +162,7 @@ public static string GetExampleInput(Type type)
166162
try
167163
{
168164
if (type.IsEnum)
169-
{
170165
typeInputExamples.Add(type.AssemblyQualifiedName, Enum.GetNames(type).First());
171-
}
172166
else
173167
{
174168
var instance = Activator.CreateInstance(type);
@@ -222,10 +216,10 @@ public static object TryParseVector2(string input)
222216
{
223217
Vector2 vector = default;
224218

225-
var split = input.Split(',');
219+
var split = input.Split(' ');
226220

227-
vector.x = float.Parse(split[0].Trim(), en_US);
228-
vector.y = float.Parse(split[1].Trim(), en_US);
221+
vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
222+
vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
229223

230224
return vector;
231225
}
@@ -244,11 +238,11 @@ public static object TryParseVector3(string input)
244238
{
245239
Vector3 vector = default;
246240

247-
var split = input.Split(',');
241+
var split = input.Split(' ');
248242

249-
vector.x = float.Parse(split[0].Trim(), en_US);
250-
vector.y = float.Parse(split[1].Trim(), en_US);
251-
vector.z = float.Parse(split[2].Trim(), en_US);
243+
vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
244+
vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
245+
vector.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
252246

253247
return vector;
254248
}
@@ -267,12 +261,12 @@ public static object TryParseVector4(string input)
267261
{
268262
Vector4 vector = default;
269263

270-
var split = input.Split(',');
264+
var split = input.Split(' ');
271265

272-
vector.x = float.Parse(split[0].Trim(), en_US);
273-
vector.y = float.Parse(split[1].Trim(), en_US);
274-
vector.z = float.Parse(split[2].Trim(), en_US);
275-
vector.w = float.Parse(split[3].Trim(), en_US);
266+
vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
267+
vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
268+
vector.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
269+
vector.w = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
276270

277271
return vector;
278272
}
@@ -291,22 +285,22 @@ public static object TryParseQuaternion(string input)
291285
{
292286
Vector3 vector = default;
293287

294-
var split = input.Split(',');
288+
var split = input.Split(' ');
295289

296290
if (split.Length == 4)
297291
{
298292
Quaternion quat = default;
299-
quat.x = float.Parse(split[0].Trim(), en_US);
300-
quat.y = float.Parse(split[1].Trim(), en_US);
301-
quat.z = float.Parse(split[2].Trim(), en_US);
302-
quat.w = float.Parse(split[3].Trim(), en_US);
293+
quat.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
294+
quat.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
295+
quat.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
296+
quat.w = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
303297
return quat;
304298
}
305299
else
306300
{
307-
vector.x = float.Parse(split[0].Trim(), en_US);
308-
vector.y = float.Parse(split[1].Trim(), en_US);
309-
vector.z = float.Parse(split[2].Trim(), en_US);
301+
vector.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
302+
vector.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
303+
vector.z = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
310304
return Quaternion.Euler(vector);
311305
}
312306
}
@@ -327,12 +321,12 @@ public static object TryParseRect(string input)
327321
{
328322
Rect rect = default;
329323

330-
var split = input.Split(',');
324+
var split = input.Split(' ');
331325

332-
rect.x = float.Parse(split[0].Trim(), en_US);
333-
rect.y = float.Parse(split[1].Trim(), en_US);
334-
rect.width = float.Parse(split[2].Trim(), en_US);
335-
rect.height = float.Parse(split[3].Trim(), en_US);
326+
rect.x = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
327+
rect.y = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
328+
rect.width = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
329+
rect.height = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
336330

337331
return rect;
338332
}
@@ -351,13 +345,13 @@ public static object TryParseColor(string input)
351345
{
352346
Color color = default;
353347

354-
var split = input.Split(',');
348+
var split = input.Split(' ');
355349

356-
color.r = float.Parse(split[0].Trim(), en_US);
357-
color.g = float.Parse(split[1].Trim(), en_US);
358-
color.b = float.Parse(split[2].Trim(), en_US);
350+
color.r = float.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
351+
color.g = float.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
352+
color.b = float.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
359353
if (split.Length > 3)
360-
color.a = float.Parse(split[3].Trim(), en_US);
354+
color.a = float.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
361355
else
362356
color.a = 1;
363357

@@ -378,13 +372,13 @@ public static object TryParseColor32(string input)
378372
{
379373
Color32 color = default;
380374

381-
var split = input.Split(',');
375+
var split = input.Split(' ');
382376

383-
color.r = byte.Parse(split[0].Trim(), en_US);
384-
color.g = byte.Parse(split[1].Trim(), en_US);
385-
color.b = byte.Parse(split[2].Trim(), en_US);
377+
color.r = byte.Parse(split[0].Trim(), CultureInfo.CurrentCulture);
378+
color.g = byte.Parse(split[1].Trim(), CultureInfo.CurrentCulture);
379+
color.b = byte.Parse(split[2].Trim(), CultureInfo.CurrentCulture);
386380
if (split.Length > 3)
387-
color.a = byte.Parse(split[3].Trim(), en_US);
381+
color.a = byte.Parse(split[3].Trim(), CultureInfo.CurrentCulture);
388382
else
389383
color.a = 255;
390384

@@ -397,7 +391,7 @@ public static string Color32ToString(object obj)
397391
return null;
398392

399393
// ints, this is fine
400-
return $"{color.r}, {color.g}, {color.b}, {color.a}";
394+
return $"{color.r} {color.g} {color.b} {color.a}";
401395
}
402396

403397
// Layermask (Int32)

src/UI/Panels/UIPanel.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ public virtual void ApplySaveData(string data)
209209
{
210210
ExplorerCore.LogWarning("Invalid or corrupt panel save data! Restoring to default.");
211211
SetTransformDefaults();
212+
UIManager.Initializing = false;
213+
DoSaveToConfigElement();
214+
ConfigManager.InternalHandler.SaveConfig();
215+
UIManager.Initializing = true;
212216
}
213217
}
214218

@@ -335,7 +339,7 @@ internal static string RectAnchorsToString(this RectTransform rect)
335339
if (!rect)
336340
throw new ArgumentNullException("rect");
337341

338-
return string.Format(ParseUtility.en_US, "{0},{1},{2},{3}", new object[]
342+
return string.Format(CultureInfo.CurrentCulture, "{0} {1} {2} {3}", new object[]
339343
{
340344
rect.anchorMin.x,
341345
rect.anchorMin.y,
@@ -349,16 +353,16 @@ internal static void SetAnchorsFromString(this RectTransform panel, string strin
349353
if (string.IsNullOrEmpty(stringAnchors))
350354
throw new ArgumentNullException("stringAnchors");
351355

352-
var split = stringAnchors.Split(',');
356+
var split = stringAnchors.Split(' ');
353357

354358
if (split.Length != 4)
355359
throw new Exception($"stringAnchors split is unexpected length: {split.Length}");
356360

357361
Vector4 anchors;
358-
anchors.x = float.Parse(split[0], ParseUtility.en_US);
359-
anchors.y = float.Parse(split[1], ParseUtility.en_US);
360-
anchors.z = float.Parse(split[2], ParseUtility.en_US);
361-
anchors.w = float.Parse(split[3], ParseUtility.en_US);
362+
anchors.x = float.Parse(split[0], CultureInfo.CurrentCulture);
363+
anchors.y = float.Parse(split[1], CultureInfo.CurrentCulture);
364+
anchors.z = float.Parse(split[2], CultureInfo.CurrentCulture);
365+
anchors.w = float.Parse(split[3], CultureInfo.CurrentCulture);
362366

363367
panel.anchorMin = new Vector2(anchors.x, anchors.y);
364368
panel.anchorMax = new Vector2(anchors.z, anchors.w);
@@ -369,22 +373,22 @@ internal static string RectPositionToString(this RectTransform rect)
369373
if (!rect)
370374
throw new ArgumentNullException("rect");
371375

372-
return string.Format(ParseUtility.en_US, "{0},{1}", new object[]
376+
return string.Format(CultureInfo.CurrentCulture, "{0} {1}", new object[]
373377
{
374378
rect.localPosition.x, rect.localPosition.y
375379
});
376380
}
377381

378382
internal static void SetPositionFromString(this RectTransform rect, string stringPosition)
379383
{
380-
var split = stringPosition.Split(',');
384+
var split = stringPosition.Split(' ');
381385

382386
if (split.Length != 2)
383387
throw new Exception($"stringPosition split is unexpected length: {split.Length}");
384388

385389
Vector3 vector = rect.localPosition;
386-
vector.x = float.Parse(split[0], ParseUtility.en_US);
387-
vector.y = float.Parse(split[1], ParseUtility.en_US);
390+
vector.x = float.Parse(split[0], CultureInfo.CurrentCulture);
391+
vector.y = float.Parse(split[1], CultureInfo.CurrentCulture);
388392
rect.localPosition = vector;
389393
}
390394
}

src/UI/UIManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public enum VerticalAnchor
3636
Bottom
3737
}
3838

39-
public static bool Initializing { get; private set; } = true;
39+
public static bool Initializing { get; internal set; } = true;
4040

4141
private static readonly Dictionary<Panels, UIPanel> UIPanels = new Dictionary<Panels, UIPanel>();
4242

0 commit comments

Comments
 (0)