forked from geckosio/typed-array-buffer-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.ts
276 lines (243 loc) · 7.88 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import {Schema} from './schema';
import {flatten, set} from './utils';
import type {SchemaDefinition} from './types';
export class Model<T> {
protected _schema: Schema<T>;
protected _buffer: ArrayBuffer;
protected _dataView: DataView;
protected _bytes: number;
public constructor(schema: Schema<T>) {
this._bytes = 0;
this._buffer = new ArrayBuffer(this._bytes);
this._dataView = new DataView(this._buffer);
this._schema = schema;
}
public static fromSchemaDefinition<T extends Record<string, any>>(
name: string,
struct: SchemaDefinition<T>
): Model<T> {
const newSchema = new Schema(name, struct);
const newModel = new Model<T>(newSchema);
return newModel;
}
/**
* Get a model's ID from an ArrayBuffer.
* @param buffer
*/
public static getIdFromBuffer(buffer: ArrayBuffer): string {
const dataView = new DataView(buffer);
let id = '';
for (let i = 0; i < 5; i++) {
const uInt8 = dataView.getUint8(i);
id += String.fromCharCode(uInt8);
}
return id;
}
/**
* Get the schema definition.
*/
public get schema(): Schema {
return this._schema;
}
/**
* Get the ID of the schema definition.
*/
public get id(): string {
return this._schema.id;
}
public flatten(schema: Schema<T>, data: any): {d: any; t: string}[] {
const accumulator: any[] = [];
flatten(schema, data, accumulator);
return accumulator;
}
public toBuffer(worldState: T): ArrayBuffer {
this.refresh();
// deep clone the worldState
const data = JSON.parse(JSON.stringify(worldState));
const flat = this.flatten(this._schema, data);
console.log('flattened:', flat);
for (let i = 0; i < flat.length; i++) {
const item = flat[i];
switch (item.t) {
case 'String8': {
for (let j = 0; j < item.d.length; j++) {
this._dataView.setUint8(this._bytes, (item.d as string)[j].charCodeAt(0));
this._bytes++;
}
continue;
}
case 'String16': {
for (let j = 0; j < item.d.length; j++) {
this._dataView.setUint16(this._bytes, (item.d as string)[j].charCodeAt(0));
this._bytes += 2;
}
continue;
}
case 'Int8Array': {
this._dataView.setInt8(this._bytes, item.d);
this._bytes++;
continue;
}
case 'Uint8Array': {
this._dataView.setUint8(this._bytes, item.d);
this._bytes++;
continue;
}
case 'Int16Array': {
this._dataView.setInt16(this._bytes, item.d);
this._bytes += 2;
continue;
}
case 'Uint16Array': {
this._dataView.setUint16(this._bytes, item.d);
this._bytes += 2;
continue;
}
case 'Int32Array': {
this._dataView.setInt32(this._bytes, item.d);
this._bytes += 4;
continue;
}
case 'Uint32Array': {
this._dataView.setUint32(this._bytes, item.d);
this._bytes += 4;
continue;
}
case 'BigInt64Array': {
this._dataView.setBigInt64(this._bytes, BigInt(item.d));
this._bytes += 8;
continue;
}
case 'BigUint64Array': {
this._dataView.setBigUint64(this._bytes, BigInt(item.d));
this._bytes += 8;
continue;
}
case 'Float32Array': {
this._dataView.setFloat32(this._bytes, item.d);
this._bytes += 4;
continue;
}
case 'Float64Array': {
this._dataView.setFloat64(this._bytes, item.d);
this._bytes += 8;
}
}
}
const newBuffer = new ArrayBuffer(this._bytes);
const view = new DataView(newBuffer);
// copy all data to a new resized ArrayBuffer
for (let i = 0; i < this._bytes; i++) {
view.setUint8(i, this._dataView.getUint8(i));
}
return newBuffer;
}
private populateData = (obj: any, key: any, value: any, path: string = '', isArray = false) => {
if (obj?._id && obj._id === key) {
const p = path.replace(/_struct\./, '').replace(/\.$/, '');
// if it is a schema[], but only has one set, we manually have to make sure it transforms to an array
if (isArray && !Array.isArray(value)) {
value = [value];
}
// '' is the top level
if (p === '') {
data = {...data, ...value};
} else {
set(data, p, value);
}
} else {
for (const props in obj) {
if (Object.prototype.hasOwnProperty.call(obj, props)) {
if (typeof obj[props] === 'object') {
const p = Array.isArray(obj) ? '' : `${props}.`;
this.populateData(obj[props], key, value, path + p, Array.isArray(obj));
}
// obj
}
}
}
};
public fromBuffer(buffer: ArrayBuffer): T {
const view = new DataView(buffer);
const int8 = new Int8Array(buffer);
// Find the schema id indexes in the buffer
let index = 0;
const idIndexes: number[] = [];
while (index > -1) {
index = int8.indexOf(35, index); // charCode for '#' is 35
if (index !== -1) {
idIndexes.push(index);
index++;
}
}
// Read the buffer at the indexes to get the schema ids
const schemaIds = idIndexes.map((index) => {
let id = '';
for (let i = 0; i < 5; i++) {
const char = String.fromCharCode(int8[index + i]);
id += char;
}
return id;
});
// Assemble schema information
const schemas: Schema[] = [];
for (let i = 0; i < schemaIds.length; i++) {
// Ensure that the Schema with the specified id exists in our instance map
const schema = Schema.getInstanceById(schemaIds[i]);
if (schema) {
schema.startsAt = idIndexes[i] + 5;
schemas.push(schema);
}
}
let data: any = {}; // holds all the data we want to give back
const bytesRef = {bytes: 0}; // The current byte position of the ArrayBuffer
const dataPerSchema: any = {};
for (let i = 0; i < schemas.length; i++) {
const schema = schemas[i];
const next = schemas[i + 1];
const end = next?.startsAt ? next.startsAt - 5 : buffer.byteLength;
console.log('what is the schema:', schema);
// TOOD(yandeu) bytes is not accurate since it includes child schemas
const length = schema.bytes || 1;
// Determine the number of iterations for an array of items (e.g. 5 objects = 5 iterations)
const iterations = Math.floor((end - schema.startsAt!) / length);
console.log('iterations:', iterations);
// No iterations, this is the root schema or a {prop: Schema}
// if (iterations === 0) {
// bytesRef.bytes = schema.startsAt! + length;
// console.log('deserializing iteration 0:', schema.deserialize(view, bytesRef));
// }
for (let j = 0; j < iterations; j++) {
bytesRef.bytes = schema.startsAt! + j * length;
const schemaData = schema.deserialize(view, bytesRef);
if (iterations <= 1) {
dataPerSchema[schema.id] = {...schemaData};
} else {
if (typeof dataPerSchema[schema.id] === 'undefined') {
dataPerSchema[schema.id] = [];
}
dataPerSchema[schema.id].push(schemaData);
}
}
}
console.log('data:', data);
console.log('data per schema:', dataPerSchema);
return;
// add dataPerScheme to data
data = {};
for (let i = 0; i < Object.keys(dataPerSchema).length; i++) {
const key = Object.keys(dataPerSchema)[i];
const value = dataPerSchema[key];
this.populateData(this._schema, key, value, '');
}
return data;
}
/**
* Refresh this Model's internal buffer and DataView before toBuffer is called.
*/
protected refresh(): void {
this._buffer = new ArrayBuffer(8 * 1024);
this._dataView = new DataView(this._buffer);
this._bytes = 0;
}
}