Skip to content

Commit 17d0e71

Browse files
authored
compatibility with colyseus/schema#45 (#98)
compatibility with colyseus/schema#45
1 parent 19988da commit 17d0e71

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

Assets/Editor/ColyseusTests/SchemaDeserializerTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void Dispose()
2020
public void PrimitiveTypesTest()
2121
{
2222
var state = new SchemaTest.PrimitiveTypes.PrimitiveTypes();
23-
byte[] bytes = { 0, 128, 1, 255, 2, 0, 128, 3, 255, 255, 4, 0, 0, 0, 128, 5, 255, 255, 255, 255, 6, 0, 0, 0, 0, 0, 0, 0, 128, 7, 255, 255, 255, 255, 255, 255, 31, 0, 8, 255, 255, 127, 255, 9, 255, 255, 255, 255, 255, 255, 239, 127, 10, 208, 128, 11, 204, 255, 12, 209, 0, 128, 13, 205, 255, 255, 14, 210, 0, 0, 0, 128, 15, 203, 0, 0, 224, 255, 255, 255, 239, 65, 16, 203, 0, 0, 0, 0, 0, 0, 224, 195, 17, 203, 255, 255, 255, 255, 255, 255, 63, 67, 18, 203, 61, 255, 145, 224, 255, 255, 239, 199, 19, 203, 255, 255, 255, 255, 255, 255, 239, 127, 20, 171, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 21, 1 };
23+
byte[] bytes = { 0, 128, 1, 255, 2, 0, 128, 3, 255, 255, 4, 0, 0, 0, 128, 5, 255, 255, 255, 255, 6, 0, 0, 0, 0, 0, 0, 0, 128, 7, 255, 255, 255, 255, 255, 255, 31, 0, 8, 204, 204, 204, 253, 9, 255, 255, 255, 255, 255, 255, 239, 127, 10, 208, 128, 11, 204, 255, 12, 209, 0, 128, 13, 205, 255, 255, 14, 210, 0, 0, 0, 128, 15, 203, 0, 0, 224, 255, 255, 255, 239, 65, 16, 203, 0, 0, 0, 0, 0, 0, 224, 195, 17, 203, 255, 255, 255, 255, 255, 255, 63, 67, 18, 203, 61, 255, 145, 224, 255, 255, 239, 199, 19, 203, 153, 153, 153, 153, 153, 153, 185, 127, 20, 171, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 21, 1 };
2424
state.Decode(bytes);
2525

2626
Assert.AreEqual(state.int8, -128);
@@ -31,7 +31,7 @@ public void PrimitiveTypesTest()
3131
Assert.AreEqual(state.uint32, 4294967295);
3232
Assert.AreEqual(state.int64, -9223372036854775808);
3333
Assert.AreEqual(state.uint64, 9007199254740991);
34-
Assert.AreEqual(state.float32, -3.40282347E+38f);
34+
Assert.AreEqual(state.float32, -3.40282347E+37f);
3535
Assert.AreEqual(state.float64, 1.7976931348623157e+308);
3636

3737
Assert.AreEqual(state.varint_int8, -128);
@@ -67,7 +67,7 @@ public void ChildSchemaTypesTest()
6767
public void ArraySchemaTypesTest()
6868
{
6969
var state = new SchemaTest.ArraySchemaTypes.ArraySchemaTypes();
70-
byte[] bytes = { 0, 2, 2, 0, 0, 100, 1, 208, 156, 193, 1, 0, 100, 1, 208, 156, 193, 1, 4, 4, 0, 0, 1, 10, 2, 20, 3, 30, 2, 3, 3, 0, 163, 111, 110, 101, 1, 163, 116, 119, 111, 2, 165, 116, 104, 114, 101, 101, 3, 3, 3, 0, 232, 3, 0, 0, 1, 208, 7, 0, 0, 2, 72, 244, 255, 255 };
70+
byte[] bytes = { 0, 2, 2, 0, 0, 100, 1, 208, 156, 193, 1, 0, 100, 1, 208, 156, 193, 1, 4, 4, 0, 0, 1, 10, 2, 20, 3, 205, 192, 13, 2, 3, 3, 0, 163, 111, 110, 101, 1, 163, 116, 119, 111, 2, 165, 116, 104, 114, 101, 101, 3, 3, 3, 0, 232, 3, 0, 0, 1, 192, 13, 0, 0, 2, 72, 244, 255, 255 };
7171

7272
state.arrayOfSchemas.OnAdd += (value, key) => Debug.Log("onAdd, arrayOfSchemas => " + key);
7373
state.arrayOfNumbers.OnAdd += (value, key) => Debug.Log("onAdd, arrayOfNumbers => " + key);
@@ -85,7 +85,7 @@ public void ArraySchemaTypesTest()
8585
Assert.AreEqual(state.arrayOfNumbers[0], 0);
8686
Assert.AreEqual(state.arrayOfNumbers[1], 10);
8787
Assert.AreEqual(state.arrayOfNumbers[2], 20);
88-
Assert.AreEqual(state.arrayOfNumbers[3], 30);
88+
Assert.AreEqual(state.arrayOfNumbers[3], 3520);
8989

9090
Assert.AreEqual(state.arrayOfStrings.Count, 3);
9191
Assert.AreEqual(state.arrayOfStrings[0], "one");
@@ -94,7 +94,7 @@ public void ArraySchemaTypesTest()
9494

9595
Assert.AreEqual(state.arrayOfInt32.Count, 3);
9696
Assert.AreEqual(state.arrayOfInt32[0], 1000);
97-
Assert.AreEqual(state.arrayOfInt32[1], 2000);
97+
Assert.AreEqual(state.arrayOfInt32[1], 3520);
9898
Assert.AreEqual(state.arrayOfInt32[2], -3000);
9999

100100
state.arrayOfSchemas.OnRemove += (value, key) => Debug.Log("onRemove, arrayOfSchemas => " + key);
@@ -116,7 +116,7 @@ public void ArraySchemaTypesTest()
116116
public void MapSchemaTypesTest()
117117
{
118118
var state = new SchemaTest.MapSchemaTypes.MapSchemaTypes();
119-
byte[] bytes = { 0, 3, 163, 111, 110, 101, 0, 100, 1, 204, 200, 193, 163, 116, 119, 111, 0, 205, 44, 1, 1, 205, 144, 1, 193, 165, 116, 104, 114, 101, 101, 0, 205, 244, 1, 1, 205, 88, 2, 193, 1, 3, 163, 111, 110, 101, 1, 163, 116, 119, 111, 2, 165, 116, 104, 114, 101, 101, 3, 2, 3, 163, 111, 110, 101, 163, 79, 110, 101, 163, 116, 119, 111, 163, 84, 119, 111, 165, 116, 104, 114, 101, 101, 165, 84, 104, 114, 101, 101, 3, 3, 163, 111, 110, 101, 232, 3, 0, 0, 163, 116, 119, 111, 24, 252, 255, 255, 165, 116, 104, 114, 101, 101, 208, 7, 0, 0 };
119+
byte[] bytes = { 0, 3, 163, 111, 110, 101, 0, 100, 1, 204, 200, 193, 163, 116, 119, 111, 0, 205, 44, 1, 1, 205, 144, 1, 193, 165, 116, 104, 114, 101, 101, 0, 205, 244, 1, 1, 205, 88, 2, 193, 1, 3, 163, 111, 110, 101, 1, 163, 116, 119, 111, 2, 165, 116, 104, 114, 101, 101, 205, 192, 13, 2, 3, 163, 111, 110, 101, 163, 79, 110, 101, 163, 116, 119, 111, 163, 84, 119, 111, 165, 116, 104, 114, 101, 101, 165, 84, 104, 114, 101, 101, 3, 3, 163, 111, 110, 101, 192, 13, 0, 0, 163, 116, 119, 111, 24, 252, 255, 255, 165, 116, 104, 114, 101, 101, 208, 7, 0, 0 };
120120

121121
state.mapOfSchemas.OnAdd += (value, key) => Debug.Log("OnAdd, mapOfSchemas => " + key);
122122
state.mapOfNumbers.OnAdd += (value, key) => Debug.Log("OnAdd, mapOfNumbers => " + key);
@@ -141,19 +141,19 @@ public void MapSchemaTypesTest()
141141
Assert.AreEqual(state.mapOfNumbers.Count, 3);
142142
Assert.AreEqual(state.mapOfNumbers["one"], 1);
143143
Assert.AreEqual(state.mapOfNumbers["two"], 2);
144-
Assert.AreEqual(state.mapOfNumbers["three"], 3);
144+
Assert.AreEqual(state.mapOfNumbers["three"], 3520);
145145

146146
Assert.AreEqual(state.mapOfStrings.Count, 3);
147147
Assert.AreEqual(state.mapOfStrings["one"], "One");
148148
Assert.AreEqual(state.mapOfStrings["two"], "Two");
149149
Assert.AreEqual(state.mapOfStrings["three"], "Three");
150150

151151
Assert.AreEqual(state.mapOfInt32.Count, 3);
152-
Assert.AreEqual(state.mapOfInt32["one"], 1000);
152+
Assert.AreEqual(state.mapOfInt32["one"], 3520);
153153
Assert.AreEqual(state.mapOfInt32["two"], -1000);
154154
Assert.AreEqual(state.mapOfInt32["three"], 2000);
155155

156-
byte[] deleteBytes = { 1, 2, 1, 192, 2, 192, 0, 2, 1, 192, 2, 192, 2, 2, 1, 192, 2, 192, 3, 2, 1, 192, 2, 192 };
156+
byte[] deleteBytes = { 1, 2, 192, 1, 192, 2, 0, 2, 192, 1, 192, 2, 2, 2, 192, 1, 192, 2, 3, 2, 192, 1, 192, 2 };
157157
state.Decode(deleteBytes);
158158

159159
Assert.AreEqual(state.mapOfSchemas.Count, 1);
@@ -222,7 +222,7 @@ public void InheritedTypesTest()
222222
[Test]
223223
public void BackwardsForwardsTest()
224224
{
225-
byte[] statev1bytes = { 1, 1, 163, 111, 110, 101, 0, 203, 112, 221, 224, 174, 225, 40, 220, 63, 1, 203, 58, 33, 152, 138, 117, 58, 224, 63, 193, 0, 171, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 };
225+
byte[] statev1bytes = { 1, 1, 163, 111, 110, 101, 0, 203, 64, 45, 212, 207, 108, 69, 148, 63, 1, 203, 120, 56, 150, 252, 58, 73, 224, 63, 193, 0, 171, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 };
226226
byte[] statev2bytes = { 0, 171, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 2, 10 };
227227

228228
var statev2 = new SchemaTest.Forwards.StateV2();

Assets/Plugins/Colyseus/Serializer/Schema/Schema.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public object this[string propertyName]
449449
return GetType().GetField(propertyName).GetValue(this);
450450
}
451451
set {
452-
var field = GetType().GetField(propertyName);
452+
var field = GetType().GetField(propertyName);
453453
field.SetValue(this, value);
454454
}
455455
}
@@ -471,6 +471,9 @@ public void Decode(byte[] bytes, Iterator it = null)
471471

472472
while (it.Offset < totalBytes)
473473
{
474+
var isNil = decode.NilCheck(bytes, it);
475+
if (isNil) { it.Offset++; }
476+
474477
var index = bytes[it.Offset++];
475478

476479
if (index == (byte) SPEC.END_OF_STRUCTURE)
@@ -498,19 +501,17 @@ public void Decode(byte[] bytes, Iterator it = null)
498501
object change = null;
499502
bool hasChange = false;
500503

501-
if (fieldType == "ref")
504+
if (isNil)
502505
{
503-
// child schema type
504-
if (decode.NilCheck(bytes, it))
505-
{
506-
it.Offset++;
507506
value = null;
508-
}
509-
else
510-
{
511-
value = this[field] ?? CreateTypeInstance(bytes, it, childType);
512-
(value as Schema).Decode(bytes, it);
513-
}
507+
hasChange = true;
508+
}
509+
510+
// Child schema type
511+
else if (fieldType == "ref")
512+
{
513+
value = this[field] ?? CreateTypeInstance(bytes, it, childType);
514+
(value as Schema).Decode(bytes, it);
514515

515516
hasChange = true;
516517
}
@@ -586,13 +587,6 @@ public void Decode(byte[] bytes, Iterator it = null)
586587
isNew = true;
587588
}
588589

589-
if (decode.NilCheck(bytes, it))
590-
{
591-
it.Offset++;
592-
valueRef.InvokeOnRemove(item, newIndex);
593-
continue;
594-
}
595-
596590
item.Decode(bytes, it);
597591
currentValue[newIndex] = item;
598592
}
@@ -640,6 +634,9 @@ public void Decode(byte[] bytes, Iterator it = null)
640634
break;
641635
}
642636

637+
var isNilItem = decode.NilCheck(bytes, it);
638+
if (isNilItem) { it.Offset++; }
639+
643640
string previousKey = null;
644641
if (decode.IndexChangeCheck(bytes, it))
645642
{
@@ -671,10 +668,8 @@ public void Decode(byte[] bytes, Iterator it = null)
671668
item = valueRef[newKey];
672669
}
673670

674-
if (decode.NilCheck(bytes, it))
671+
if (isNilItem)
675672
{
676-
it.Offset++;
677-
678673
if (item != null && isSchemaType)
679674
{
680675
(item as Schema).OnRemove?.Invoke();

0 commit comments

Comments
 (0)