Skip to content

Commit 5c5ad6d

Browse files
committed
also fix reading, fix all tests
1 parent 96c5a6b commit 5c5ad6d

2 files changed

Lines changed: 46 additions & 45 deletions

File tree

Codecs/KeyValues2.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void WriteAttribute(string name, Type type, object value, bool in_array)
258258
else if (type == typeof(float))
259259
value = FormattableString.Invariant($"{(float)value}");
260260
else if (type == typeof(byte[]))
261-
value = BitConverter.ToString((byte[])value).Replace("-", string.Empty, false, CultureInfo.InvariantCulture);
261+
value = Convert.ToHexString((byte[])value).Replace("-", string.Empty, false, CultureInfo.InvariantCulture);
262262
else if (type == typeof(TimeSpan))
263263
value = ((TimeSpan)value).TotalSeconds.ToString(CultureInfo.InvariantCulture);
264264
else if (type == typeof(Color))
@@ -267,7 +267,7 @@ void WriteAttribute(string name, Type type, object value, bool in_array)
267267
value = FormattableString.Invariant($"{castValue.R} {castValue.G} {castValue.B} {castValue.A}");
268268
}
269269
else if (value is ulong ulong_value)
270-
value = $"0x{ulong_value.ToString(CultureInfo.InvariantCulture):X}";
270+
value = $"0x{ulong_value.ToString("x", CultureInfo.InvariantCulture)}";
271271
else if (type == typeof(Vector2))
272272
{
273273
var castValue = (Vector2)value;
@@ -554,35 +554,35 @@ object Decode_ParseValue(Type type, string value)
554554
if (type == typeof(Element))
555555
return Decode_ParseElement(value);
556556
if (type == typeof(int))
557-
return int.Parse(value);
557+
return int.Parse(value, CultureInfo.InvariantCulture);
558558
else if (type == typeof(float))
559-
return float.Parse(value);
559+
return float.Parse(value, CultureInfo.InvariantCulture);
560560
else if (type == typeof(bool))
561-
return byte.Parse(value) == 1;
561+
return byte.Parse(value, CultureInfo.InvariantCulture) == 1;
562562
else if (type == typeof(byte[]))
563563
{
564564
byte[] result = new byte[value.Length / 2];
565565
for (int i = 0; i * 2 < value.Length; i++)
566566
{
567-
result[i] = byte.Parse(value.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
567+
result[i] = byte.Parse(value.AsSpan(i * 2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture);
568568
}
569569
return result;
570570
}
571571
else if (type == typeof(TimeSpan))
572-
return TimeSpan.FromTicks((long)(double.Parse(value) * TimeSpan.TicksPerSecond));
572+
return TimeSpan.FromTicks((long)(double.Parse(value, CultureInfo.InvariantCulture) * TimeSpan.TicksPerSecond));
573573

574574
var num_list = value.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
575575

576576
if (type == typeof(Color))
577577
{
578-
var rgba = num_list.Select(i => byte.Parse(i)).ToArray();
578+
var rgba = num_list.Select(i => byte.Parse(i, CultureInfo.InvariantCulture)).ToArray();
579579
return Color.FromBytes(rgba);
580580
}
581581

582-
if (type == typeof(ulong)) return ulong.Parse(value.Remove(0, 2), System.Globalization.NumberStyles.HexNumber);
583-
if (type == typeof(byte)) return byte.Parse(value);
582+
if (type == typeof(ulong)) return ulong.Parse(value.Remove(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture);
583+
if (type == typeof(byte)) return byte.Parse(value, CultureInfo.InvariantCulture);
584584

585-
var f_list = num_list.Select(i => float.Parse(i)).ToArray();
585+
var f_list = num_list.Select(i => float.Parse(i, CultureInfo.InvariantCulture)).ToArray();
586586
if (type == typeof(Vector2)) return new Vector2(f_list[0], f_list[1]);
587587
else if (type == typeof(Vector3)) return new Vector3(f_list[0], f_list[1], f_list[2]);
588588
else if (type == typeof(Vector4)) return new Vector4(f_list[0], f_list[1], f_list[2], f_list[3]);

Tests/Tests.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static DatamodelTests()
5656

5757
TestValues_V3 = TestValues_V1.Concat(new object[] {
5858
(byte)0xFF,
59-
(ulong)0xFFFFFFFF,
59+
(UInt64)0xFFFFFFFF,
6060
//new QAngle(0, 90, 180)
6161
}).ToList();
6262
}
@@ -224,6 +224,39 @@ protected static void ValidatePopulated(string encoding_name, int encoding_versi
224224
dm.Dispose();
225225
}
226226

227+
protected static DM Create(string encoding, int version, bool memory_save = false)
228+
{
229+
var dm = MakeDatamodel();
230+
Populate(dm, encoding, version);
231+
232+
dm.Root["Arr"] = new System.Collections.ObjectModel.ObservableCollection<int>();
233+
dm.Root.GetArray<int>("Arr");
234+
235+
if (memory_save)
236+
dm.Save(new MemoryStream(), encoding, version);
237+
else
238+
{
239+
dm.Save(DmxSavePath, encoding, version);
240+
if (SaveAndConvert(dm, encoding, version))
241+
{
242+
ValidatePopulated(encoding, version);
243+
}
244+
Cleanup();
245+
}
246+
247+
dm.AllElements.Remove(dm.Root.GetArray<Element>("ElemArray")[3], DM.ElementList.RemoveMode.MakeStubs);
248+
Assert.AreEqual(true, dm.Root.GetArray<Element>("ElemArray")[3].Stub);
249+
250+
dm.AllElements.Remove(dm.Root, DM.ElementList.RemoveMode.MakeStubs);
251+
Assert.AreEqual(true, dm.Root.Stub);
252+
253+
return dm;
254+
}
255+
}
256+
257+
[TestFixture]
258+
public class Functionality : DatamodelTests
259+
{
227260
[Test]
228261
public static void TypedArrayAddingRemoving()
229262
{
@@ -257,43 +290,11 @@ public static void TypedArrayAddingRemoving()
257290

258291
array.Remove(elementA);
259292
array.Remove(elementB);
260-
293+
261294
Assert.AreEqual(0, array.Count);
262295
}
263296

264-
protected static DM Create(string encoding, int version, bool memory_save = false)
265-
{
266-
var dm = MakeDatamodel();
267-
Populate(dm, encoding, version);
268297

269-
dm.Root["Arr"] = new System.Collections.ObjectModel.ObservableCollection<int>();
270-
dm.Root.GetArray<int>("Arr");
271-
272-
if (memory_save)
273-
dm.Save(new MemoryStream(), encoding, version);
274-
else
275-
{
276-
dm.Save(DmxSavePath, encoding, version);
277-
if (SaveAndConvert(dm, encoding, version))
278-
{
279-
ValidatePopulated(encoding, version);
280-
}
281-
Cleanup();
282-
}
283-
284-
dm.AllElements.Remove(dm.Root.GetArray<Element>("ElemArray")[3], DM.ElementList.RemoveMode.MakeStubs);
285-
Assert.AreEqual(true, dm.Root.GetArray<Element>("ElemArray")[3].Stub);
286-
287-
dm.AllElements.Remove(dm.Root, DM.ElementList.RemoveMode.MakeStubs);
288-
Assert.AreEqual(true, dm.Root.Stub);
289-
290-
return dm;
291-
}
292-
}
293-
294-
[TestFixture]
295-
public class Functionality : DatamodelTests
296-
{
297298
[Test]
298299
public void Create_Datamodel_Vmap()
299300
{

0 commit comments

Comments
 (0)