@@ -29,11 +29,25 @@ export class ChatCodeStruct {
29
29
this . bytes . push ( ( value >> 0x10 ) & 0xff )
30
30
}
31
31
32
- writeTraitSelection ( [ trait1 , trait2 , trait3 ] : Array < number > ) {
32
+ write4Bytes ( value : number ) {
33
+ this . bytes . push ( ( value >> 0x00 ) & 0xff )
34
+ this . bytes . push ( ( value >> 0x08 ) & 0xff )
35
+ this . bytes . push ( ( value >> 0x10 ) & 0xff )
36
+ this . bytes . push ( ( value >> 0x18 ) & 0xff )
37
+ }
38
+
39
+ writeTraitSelection ( [ trait1 , trait2 , trait3 ] : [ number , number , number ] ) {
33
40
const value = ( ( trait3 & 3 ) << 4 ) | ( ( trait2 & 3 ) << 2 ) | ( ( trait1 & 3 ) << 0 )
34
41
this . write1Byte ( value )
35
42
}
36
43
44
+ writeDynamicArray ( values : number [ ] , bytesPerValue : 2 | 4 ) {
45
+ this . write1Byte ( values . length )
46
+ for ( const value of values ) {
47
+ bytesPerValue === 2 ? this . write2Bytes ( value ) : this . write4Bytes ( value )
48
+ }
49
+ }
50
+
37
51
// -- DECODING --
38
52
39
53
decodeFromChatCode ( chatCode : string ) {
@@ -60,11 +74,40 @@ export class ChatCodeStruct {
60
74
)
61
75
}
62
76
63
- readTraitSelection ( ) : Array < number > {
77
+ read4Bytes ( ) {
78
+ return (
79
+ this . bytes [ this . offset ++ ] |
80
+ ( this . bytes [ this . offset ++ ] << 8 ) |
81
+ ( this . bytes [ this . offset ++ ] << 16 ) |
82
+ ( this . bytes [ this . offset ++ ] << 24 )
83
+ )
84
+ }
85
+
86
+ readTraitSelection ( ) : [ number , number , number ] {
64
87
return [
65
88
this . bytes [ this . offset ] & 3 ,
66
89
( this . bytes [ this . offset ] >> 2 ) & 3 ,
67
- ( this . bytes [ this . offset ++ ] >> 4 ) & 3
90
+ ( this . bytes [ this . offset ++ ] >> 4 ) & 3 ,
68
91
]
69
92
}
93
+
94
+ readDynamicArray ( bytesPerValue : 2 | 4 ) : undefined | number [ ] {
95
+ const length = this . read1Byte ( )
96
+
97
+ if ( length === 0 ) {
98
+ return undefined
99
+ }
100
+
101
+ const values : number [ ] = [ ]
102
+
103
+ for ( let i = 0 ; i < length ; i ++ ) {
104
+ values . push ( bytesPerValue === 2 ? this . read2Bytes ( ) : this . read4Bytes ( ) )
105
+ }
106
+
107
+ return values
108
+ }
109
+
110
+ atEnd ( ) : boolean {
111
+ return this . offset >= this . bytes . length
112
+ }
70
113
}
0 commit comments