• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* global BigInt */
2
3import assert from 'assert';
4import { readFileSync, writeFileSync } from 'fs';
5import * as flatbuffers from 'flatbuffers';
6import { ArrayStructT } from './arrays_test_complex/my-game/example/array-struct.js'
7import { ArrayTable, ArrayTableT } from './arrays_test_complex/my-game/example/array-table.js'
8import { InnerStructT } from './arrays_test_complex/my-game/example/inner-struct.js'
9import { NestedStructT } from './arrays_test_complex/my-game/example/nested-struct.js'
10import { OuterStructT } from './arrays_test_complex/my-game/example/outer-struct.js'
11import { TestEnum } from './arrays_test_complex/my-game/example/test-enum.js'
12// eslint-disable-next-line @typescript-eslint/no-explicit-any
13BigInt.prototype.toJSON = function () {
14  return this.toString();
15};
16function fbObjToObj(fbObj) {
17  const ret = {};
18  for (const propName of Object.keys(fbObj)) {
19    const key = propName;
20    const prop = fbObj[key];
21    if (prop.valueOf) {
22      ret[key] = prop.valueOf();
23    } else if (typeof prop === 'object') {
24      ret[key] = fbObjToObj(prop);
25    }
26  }
27  return ret;
28}
29function testBuild(monFile, jsFile) {
30  const arrayTable = new ArrayTableT(
31    'Complex Array Test',
32    new ArrayStructT(
33      221.139008,
34      [-700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400, 500, 600, 700],
35      13,
36      [
37        new NestedStructT(
38          [233, -123],
39          TestEnum.B,
40          [TestEnum.A, TestEnum.C],
41          [
42            new OuterStructT(
43              false,
44              123.456,
45              new InnerStructT(
46                123456792.0,
47                [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
48                91,
49                BigInt('9007199254740999')
50              ),
51              [
52                new InnerStructT(
53                  -987654321.9876,
54                  [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243],
55                  123,
56                  BigInt('9007199254741000')
57                ),
58                new InnerStructT(
59                  123000987.9876,
60                  [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113],
61                  -123,
62                  BigInt('9007199254741000')
63                ),
64              ],
65              new InnerStructT(
66                987654321.9876,
67                [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
68                19,
69                BigInt('9007199254741000')
70              ),
71              [111000111.222, 222000222.111, 333000333.333, 444000444.444]
72            ),
73          ]
74        ),
75      ],
76      -123456789
77    )
78  );
79  const builder = new flatbuffers.Builder();
80  builder.finish(arrayTable.pack(builder));
81  if (jsFile) {
82    const obj = fbObjToObj(arrayTable);
83    writeFileSync(jsFile, `export default ${JSON.stringify(obj, null, 2)}`);
84  }
85  if (monFile) {
86    writeFileSync(monFile, builder.asUint8Array());
87  }
88  return builder.asUint8Array();
89}
90function testParse(monFile, jsFile, buffer) {
91  if (!buffer) {
92    if (!monFile) {
93      console.log(`Please specify mon file read the buffer from.`);
94      process.exit(1);
95    }
96    buffer = readFileSync(monFile);
97  }
98  const byteBuffer = new flatbuffers.ByteBuffer(new Uint8Array(buffer));
99  const arrayTable = ArrayTable.getRootAsArrayTable(byteBuffer).unpack();
100  const json = JSON.stringify(arrayTable, null, 2);
101  if (jsFile) {
102    writeFileSync(jsFile, `export default ${json}`);
103  }
104  return arrayTable;
105}
106if (process.argv[2] === 'build') {
107  testBuild(process.argv[3], process.argv[4]);
108} else if (process.argv[2] === 'parse') {
109  testParse(process.argv[3], process.argv[4], null);
110} else {
111  const arr = testBuild(null, null);
112  const parsed = testParse(null, null, Buffer.from(arr));
113  assert.strictEqual(parsed.a, 'Complex Array Test', 'String Test');
114  assert.strictEqual(parsed?.cUnderscore?.aUnderscore, 221.13900756835938, 'Float Test');
115  assert.deepEqual(parsed?.cUnderscore?.bUnderscore, [-700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400, 500, 600, 700], 'Array of signed integers');
116  assert.strictEqual(parsed?.cUnderscore.d?.[0].dOuter[0].d[1].a, 123000987.9876, 'Float in deep');
117  assert.deepEqual(parsed?.cUnderscore?.d[0].dOuter?.[0]?.e, {
118    a: 987654321.9876,
119    b: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
120    c: 19,
121    dUnderscore: '9007199254741000',
122  }, 'Object in deep');
123  assert.deepEqual(parsed?.cUnderscore.g, ['0', '0'], 'Last object');
124
125  console.log('Arrays test: completed successfully');
126}
127