19
19
"uint64"
20
20
"float32"
21
21
"float64"
22
-
23
- Allowed reference types:
22
+
23
+ Allowed reference types:
24
24
"ref"
25
25
"array"
26
26
"map"
27
27
***/
28
28
29
29
namespace Colyseus . Schema
30
30
{
31
- class Context
31
+ class Context
32
32
{
33
33
protected static Context instance = new Context ( ) ;
34
34
protected List < System . Type > types = new List < System . Type > ( ) ;
@@ -56,16 +56,24 @@ public class Type : Attribute
56
56
public int Index ;
57
57
public string FieldType ;
58
58
public System . Type ChildType ;
59
+ public string ChildPrimitiveType ;
59
60
60
61
public Type ( int index , string type , System . Type childType = null )
61
62
{
62
63
Index = index ; // GetType().GetFields() doesn't guarantee order of fields, need to manually track them here!
63
64
FieldType = type ;
64
65
ChildType = childType ;
65
66
}
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
+ }
66
74
}
67
75
68
- public class Iterator {
76
+ public class Iterator {
69
77
public int Offset = 0 ;
70
78
}
71
79
@@ -110,7 +118,7 @@ public interface ISchemaCollection
110
118
void InvokeOnAdd ( object item , object index ) ;
111
119
void InvokeOnChange ( object item , object index ) ;
112
120
void InvokeOnRemove ( object item , object index ) ;
113
-
121
+
114
122
object GetItems ( ) ;
115
123
void SetItems ( object items ) ;
116
124
void TriggerAll ( ) ;
@@ -184,7 +192,7 @@ public object this[object key]
184
192
Items . TryGetValue ( ( int ) key , out value ) ;
185
193
return value ;
186
194
}
187
- set { Items [ ( int ) key ] = ( T ) value ; }
195
+ set { Items [ ( int ) key ] = ( T ) Convert . ChangeType ( value , typeof ( T ) ) ; }
188
196
}
189
197
190
198
public object GetItems ( )
@@ -408,6 +416,7 @@ public class Schema
408
416
{
409
417
protected Dictionary < int , string > fieldsByIndex = new Dictionary < int , string > ( ) ;
410
418
protected Dictionary < string , string > fieldTypes = new Dictionary < string , string > ( ) ;
419
+ protected Dictionary < string , string > fieldChildPrimitiveTypes = new Dictionary < string , string > ( ) ;
411
420
protected Dictionary < string , System . Type > fieldChildTypes = new Dictionary < string , System . Type > ( ) ;
412
421
413
422
public event EventHandler < OnChangeEventArgs > OnChange ;
@@ -426,21 +435,28 @@ public Schema()
426
435
fieldTypes . Add ( field . Name , t . FieldType ) ;
427
436
if ( t . FieldType == "ref" || t . FieldType == "array" || t . FieldType == "map" )
428
437
{
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
+ }
430
446
}
431
447
}
432
448
}
433
449
}
434
450
435
- /* allow to retrieve property values by its string name */
451
+ /* allow to retrieve property values by its string name */
436
452
public object this [ string propertyName ]
437
453
{
438
- get {
439
- return GetType ( ) . GetField ( propertyName ) . GetValue ( this ) ;
454
+ get {
455
+ return GetType ( ) . GetField ( propertyName ) . GetValue ( this ) ;
440
456
}
441
457
set {
442
458
var field = GetType ( ) . GetField ( propertyName ) ;
443
- field . SetValue ( this , Convert . ChangeType ( value , field . FieldType ) ) ;
459
+ field . SetValue ( this , Convert . ChangeType ( value , field . FieldType ) ) ;
444
460
}
445
461
}
446
462
@@ -470,9 +486,13 @@ public void Decode(byte[] bytes, Iterator it = null)
470
486
471
487
var field = fieldsByIndex [ index ] ;
472
488
var fieldType = fieldTypes [ field ] ;
489
+
473
490
System . Type childType ;
474
491
fieldChildTypes . TryGetValue ( field , out childType ) ;
475
492
493
+ string childPrimitiveType ;
494
+ fieldChildPrimitiveTypes . TryGetValue ( field , out childPrimitiveType ) ;
495
+
476
496
object value = null ;
477
497
478
498
object change = null ;
@@ -578,7 +598,7 @@ public void Decode(byte[] bytes, Iterator it = null)
578
598
}
579
599
else
580
600
{
581
- currentValue [ newIndex ] = decode . DecodePrimitiveType ( fieldType , bytes , it ) ;
601
+ currentValue [ newIndex ] = decode . DecodePrimitiveType ( childPrimitiveType , bytes , it ) ;
582
602
}
583
603
584
604
if ( isNew )
@@ -666,7 +686,7 @@ public void Decode(byte[] bytes, Iterator it = null)
666
686
667
687
} else if ( ! isSchemaType )
668
688
{
669
- currentValue [ newKey ] = decode . DecodePrimitiveType ( fieldType , bytes , it ) ;
689
+ currentValue [ newKey ] = decode . DecodePrimitiveType ( childPrimitiveType , bytes , it ) ;
670
690
}
671
691
else
672
692
{
@@ -743,8 +763,8 @@ protected object CreateTypeInstance(byte[] bytes, Iterator it, System.Type type)
743
763
uint typeId = Decoder . GetInstance ( ) . DecodeUint8 ( bytes , it ) ;
744
764
System . Type anotherType = Context . GetInstance ( ) . Get ( typeId ) ;
745
765
return Activator . CreateInstance ( anotherType ) ;
746
- }
747
- else
766
+ }
767
+ else
748
768
{
749
769
return Activator . CreateInstance ( type ) ;
750
770
}
0 commit comments