Skip to content

Commit

Permalink
fixes detecting inherited Schema types inside ArraySchema/MapSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Apr 12, 2020
1 parent bc717d1 commit b8505f9
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Assets/Plugins/Colyseus/Serializer/Schema/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class ArraySchema<T> : ISchemaCollection
public event KeyValueEventHandler<T, int> OnAdd;
public event KeyValueEventHandler<T, int> OnChange;
public event KeyValueEventHandler<T, int> OnRemove;
private bool _hasSchemaChild = Schema.CheckSchemaChild(typeof(T));

public ArraySchema()
{
Expand Down Expand Up @@ -156,7 +157,7 @@ public bool ContainsKey(object key)

public bool HasSchemaChild
{
get { return typeof(T).BaseType == typeof(Schema); }
get { return _hasSchemaChild; }
}

public int Count
Expand Down Expand Up @@ -232,6 +233,7 @@ public class MapSchema<T> : ISchemaCollection
public event KeyValueEventHandler<T, string> OnAdd;
public event KeyValueEventHandler<T, string> OnChange;
public event KeyValueEventHandler<T, string> OnRemove;
private bool _hasSchemaChild = Schema.CheckSchemaChild(typeof(T));

public MapSchema()
{
Expand Down Expand Up @@ -266,7 +268,7 @@ public bool ContainsKey(object key)

public bool HasSchemaChild
{
get { return typeof(T).BaseType == typeof(Schema); }
get { return _hasSchemaChild; }
}

public T this[string key]
Expand Down Expand Up @@ -760,6 +762,22 @@ protected object CreateTypeInstance(byte[] bytes, Iterator it, System.Type type)
return Activator.CreateInstance(type);
}
}

public static bool CheckSchemaChild(System.Type toCheck) {
System.Type generic = typeof(Schema);

while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;

if (generic == cur) {
return true;
}

toCheck = toCheck.BaseType;
}

return false;
}
}

public class ReflectionField : Schema
Expand Down

0 comments on commit b8505f9

Please sign in to comment.