Skip to content

Commit 8e2c5c8

Browse files
committed
SchemaSerializer: handshake - prevent assigning the same Type for more than one typeid
1 parent 1c2c547 commit 8e2c5c8

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

Assets/Colyseus/Runtime/Colyseus/Serializer/SchemaSerializer.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public void Handshake(byte[] bytes, int offset)
5252
System.Type targetType = typeof(T);
5353

5454
System.Type[] allTypes = targetType.Assembly.GetTypes();
55-
System.Type[] namespaceSchemaTypes = Array.FindAll(allTypes, t => t.Namespace == targetType.Namespace &&
56-
typeof(Schema.Schema).IsAssignableFrom(
57-
targetType));
55+
List<System.Type> namespaceSchemaTypes = new List<System.Type>(Array.FindAll(allTypes, t => t.Namespace == targetType.Namespace && typeof(Schema.Schema).IsAssignableFrom( targetType)));
5856

5957
Iterator it = new Iterator { Offset = offset };
6058

@@ -69,31 +67,43 @@ public void Handshake(byte[] bytes, int offset)
6967
var reflectionType = reflection.types[i];
7068
var reflectionFields = GetFieldsFromType(reflectionType, types);
7169

72-
var schemaType = Array.Find(namespaceSchemaTypes, t => CompareTypes(t, reflectionFields));
70+
var schemaType = namespaceSchemaTypes.Find(t => CompareTypes(t, reflectionFields));
7371

7472
if (schemaType != null)
7573
{
74+
// UnityEngine.Debug.Log("FOUND: " + schemaType.FullName + " => " + DebugReflectionType(reflectionType, reflectionFields));
75+
7676
Decoder.Context.SetTypeId(schemaType, reflection.types[i].id);
77+
78+
// Remove from list to avoid duplicate checks
79+
namespaceSchemaTypes.Remove(schemaType);
80+
7781
}
7882
else
7983
{
84+
// UnityEngine.Debug.Log("NOT FOUND: " + DebugReflectionType(reflectionType, reflectionFields));
85+
8086
UnityEngine.Debug.LogWarning(
8187
"Local schema mismatch from server. Use \"schema-codegen\" to generate up-to-date local definitions.");
8288
}
8389
}
8490
}
8591

92+
private static string DebugReflectionType(ReflectionType reflectionType, List<ReflectionField> reflectionFields)
93+
{
94+
List<string> fieldNames = new List<string>();
95+
for (int i = 0; i < reflectionFields.Count; i++)
96+
{
97+
fieldNames.Add(reflectionFields[i].name);
98+
}
99+
return $"TypeId: {reflectionType.id} (extendsId: {reflectionType.extendsId}), Fields: {string.Join(", ", fieldNames)}";
100+
}
101+
86102
private static bool CompareTypes(System.Type schemaType, List<ReflectionField> reflectionFields)
87103
{
88104
FieldInfo[] fields = schemaType.GetFields();
89105
int typedFieldCount = 0;
90106

91-
// string fieldNames = "";
92-
// for (int i = 0; i < fields.Length; i++)
93-
// {
94-
// fieldNames += fields[i].Name + ", ";
95-
// }
96-
97107
foreach (FieldInfo field in fields)
98108
{
99109
object[] typeAttributes = field.GetCustomAttributes(typeof(Type), true);

Assets/Colyseus/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io.colyseus.sdk",
3-
"version": "0.16.6",
3+
"version": "0.16.7",
44
"displayName": "Colyseus SDK",
55
"description": "Colyseus Multiplayer SDK for Unity",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)