Skip to content

Commit e03487f

Browse files
authored
Merge pull request #70 from colyseus/array-map-primitive-types
fix decoding primitive types inside maps and arrays
2 parents 06998fe + 041b153 commit e03487f

File tree

8 files changed

+51
-23
lines changed

8 files changed

+51
-23
lines changed

Assets/Enemy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
33
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
44
//
5-
// GENERATED USING @colyseus/schema 0.4.25-alpha.0
5+
// GENERATED USING @colyseus/schema 0.4.30
66
//
77

88
using Colyseus.Schema;

Assets/Entity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
33
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
44
//
5-
// GENERATED USING @colyseus/schema 0.4.25-alpha.0
5+
// GENERATED USING @colyseus/schema 0.4.30
66
//
77

88
using Colyseus.Schema;

Assets/Player.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
33
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
44
//
5-
// GENERATED USING @colyseus/schema 0.4.25-alpha.0
5+
// GENERATED USING @colyseus/schema 0.4.30
66
//
77

88
using Colyseus.Schema;

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
"uint64"
2020
"float32"
2121
"float64"
22-
23-
Allowed reference types:
22+
23+
Allowed reference types:
2424
"ref"
2525
"array"
2626
"map"
2727
***/
2828

2929
namespace Colyseus.Schema
3030
{
31-
class Context
31+
class Context
3232
{
3333
protected static Context instance = new Context();
3434
protected List<System.Type> types = new List<System.Type>();
@@ -56,16 +56,24 @@ public class Type : Attribute
5656
public int Index;
5757
public string FieldType;
5858
public System.Type ChildType;
59+
public string ChildPrimitiveType;
5960

6061
public Type(int index, string type, System.Type childType = null)
6162
{
6263
Index = index; // GetType().GetFields() doesn't guarantee order of fields, need to manually track them here!
6364
FieldType = type;
6465
ChildType = childType;
6566
}
67+
68+
public Type(int index, string type, string childPrimitiveType)
69+
{
70+
Index = index; // GetType().GetFields() doesn't guarantee order of fields, need to manually track them here!
71+
FieldType = type;
72+
ChildPrimitiveType = childPrimitiveType;
73+
}
6674
}
6775

68-
public class Iterator {
76+
public class Iterator {
6977
public int Offset = 0;
7078
}
7179

@@ -110,7 +118,7 @@ public interface ISchemaCollection
110118
void InvokeOnAdd(object item, object index);
111119
void InvokeOnChange(object item, object index);
112120
void InvokeOnRemove(object item, object index);
113-
121+
114122
object GetItems();
115123
void SetItems(object items);
116124
void TriggerAll();
@@ -184,7 +192,7 @@ public object this[object key]
184192
Items.TryGetValue((int)key, out value);
185193
return value;
186194
}
187-
set { Items[(int)key] = (T)value; }
195+
set { Items[(int)key] = (T)Convert.ChangeType(value, typeof(T)); }
188196
}
189197

190198
public object GetItems()
@@ -408,6 +416,7 @@ public class Schema
408416
{
409417
protected Dictionary<int, string> fieldsByIndex = new Dictionary<int, string>();
410418
protected Dictionary<string, string> fieldTypes = new Dictionary<string, string>();
419+
protected Dictionary<string, string> fieldChildPrimitiveTypes = new Dictionary<string, string>();
411420
protected Dictionary<string, System.Type> fieldChildTypes = new Dictionary<string, System.Type>();
412421

413422
public event EventHandler<OnChangeEventArgs> OnChange;
@@ -426,21 +435,28 @@ public Schema()
426435
fieldTypes.Add(field.Name, t.FieldType);
427436
if (t.FieldType == "ref" || t.FieldType == "array" || t.FieldType == "map")
428437
{
429-
fieldChildTypes.Add(field.Name, t.ChildType);
438+
if (t.ChildPrimitiveType != null)
439+
{
440+
fieldChildPrimitiveTypes.Add(field.Name, t.ChildPrimitiveType);
441+
}
442+
else
443+
{
444+
fieldChildTypes.Add(field.Name, t.ChildType);
445+
}
430446
}
431447
}
432448
}
433449
}
434450

435-
/* allow to retrieve property values by its string name */
451+
/* allow to retrieve property values by its string name */
436452
public object this[string propertyName]
437453
{
438-
get {
439-
return GetType().GetField(propertyName).GetValue(this);
454+
get {
455+
return GetType().GetField(propertyName).GetValue(this);
440456
}
441457
set {
442458
var field = GetType().GetField(propertyName);
443-
field.SetValue(this, Convert.ChangeType(value, field.FieldType));
459+
field.SetValue(this, Convert.ChangeType(value, field.FieldType));
444460
}
445461
}
446462

@@ -470,9 +486,13 @@ public void Decode(byte[] bytes, Iterator it = null)
470486

471487
var field = fieldsByIndex[index];
472488
var fieldType = fieldTypes[field];
489+
473490
System.Type childType;
474491
fieldChildTypes.TryGetValue(field, out childType);
475492

493+
string childPrimitiveType;
494+
fieldChildPrimitiveTypes.TryGetValue(field, out childPrimitiveType);
495+
476496
object value = null;
477497

478498
object change = null;
@@ -578,7 +598,7 @@ public void Decode(byte[] bytes, Iterator it = null)
578598
}
579599
else
580600
{
581-
currentValue[newIndex] = decode.DecodePrimitiveType(fieldType, bytes, it);
601+
currentValue[newIndex] = decode.DecodePrimitiveType(childPrimitiveType, bytes, it);
582602
}
583603

584604
if (isNew)
@@ -666,7 +686,7 @@ public void Decode(byte[] bytes, Iterator it = null)
666686

667687
} else if (!isSchemaType)
668688
{
669-
currentValue[newKey] = decode.DecodePrimitiveType(fieldType, bytes, it);
689+
currentValue[newKey] = decode.DecodePrimitiveType(childPrimitiveType, bytes, it);
670690
}
671691
else
672692
{
@@ -743,8 +763,8 @@ protected object CreateTypeInstance(byte[] bytes, Iterator it, System.Type type)
743763
uint typeId = Decoder.GetInstance().DecodeUint8(bytes, it);
744764
System.Type anotherType = Context.GetInstance().Get(typeId);
745765
return Activator.CreateInstance(anotherType);
746-
}
747-
else
766+
}
767+
else
748768
{
749769
return Activator.CreateInstance(type);
750770
}

Assets/Plugins/Colyseus/Serializer/SchemaSerializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Handshake (byte[] bytes, int offset)
3737

3838
Type[] allTypes = targetType.Assembly.GetTypes();
3939
Type[] namespaceSchemaTypes = Array.FindAll(allTypes, t => (
40-
t.Namespace == targetType.Namespace &&
40+
t.Namespace == targetType.Namespace &&
4141
typeof(Schema.Schema).IsAssignableFrom(targetType)
4242
));
4343

@@ -80,8 +80,8 @@ private bool CompareTypes(System.Type schemaType, Schema.ReflectionType reflecti
8080
Schema.ReflectionField reflectionField = reflectionType.fields[typedField.Index];
8181

8282
if (
83-
reflectionField == null ||
84-
reflectionField.type != typedField.FieldType ||
83+
reflectionField == null ||
84+
reflectionField.type.IndexOf(typedField.FieldType) != 0 ||
8585
reflectionField.name != field.Name
8686
) {
8787
return false;

Assets/State.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
33
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
44
//
5-
// GENERATED USING @colyseus/schema 0.4.25-alpha.0
5+
// GENERATED USING @colyseus/schema 0.4.30
66
//
77

88
using Colyseus.Schema;
99

1010
public class State : Schema {
1111
[Type(0, "map", typeof(MapSchema<Entity>))]
1212
public MapSchema<Entity> entities = new MapSchema<Entity>();
13+
14+
[Type(1, "array", "number")]
15+
public ArraySchema<float> arrayOfNumbers = new ArraySchema<float>();
1316
}
1417

Server/DemoRoom.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Room, Client, generateId } from "colyseus";
2-
import { Schema, type, MapSchema } from "@colyseus/schema";
2+
import { Schema, type, MapSchema, ArraySchema } from "@colyseus/schema";
33

44
class Entity extends Schema {
55
@type("number")
@@ -22,6 +22,9 @@ class Enemy extends Entity {
2222
class State extends Schema {
2323
@type({ map: Entity })
2424
entities = new MapSchema<Entity>();
25+
26+
@type(["number"])
27+
arrayOfNumbers = new ArraySchema<number>();
2528
}
2629

2730
export class DemoRoom extends Room {
@@ -42,6 +45,7 @@ export class DemoRoom extends Room {
4245
enemy.x = Math.random() * 2;
4346
enemy.y = Math.random() * 2;
4447
this.state.entities[generateId()] = enemy;
48+
this.state.arrayOfNumbers.push(Math.random());
4549
}
4650
}
4751

Server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"url": "git://github.com/gamestdio/colyseus-unity3d.git"
77
},
88
"scripts": {
9+
"schema-codegen": "schema-codegen DemoRoom.ts --csharp --output ../Assets/",
910
"start": "nodemon --watch '**.ts' --exec ts-node ./index.ts"
1011
},
1112
"engines": {

0 commit comments

Comments
 (0)