brs-js 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/debug/Conf Spacestation.brs +0 -0
  2. package/debug/Electrks_Feminine_Challenge.brs +0 -0
  3. package/debug/a5bricks.brs +0 -0
  4. package/debug/a5p2.2.brs +0 -0
  5. package/debug/a5p2.brs +0 -0
  6. package/debug/audio.brs +0 -0
  7. package/debug/brick a5.brs +0 -0
  8. package/debug/brick qa.brs +0 -0
  9. package/debug/brsv10.brs +0 -0
  10. package/debug/brsv10brick.brs +0 -0
  11. package/debug/bruteforce.js +254 -0
  12. package/debug/clone.html +36 -0
  13. package/debug/ctf_tileset.brs +0 -0
  14. package/debug/ctf_tileset_5.brs +0 -0
  15. package/debug/evil.brs +0 -0
  16. package/debug/evilwrite.js +390 -0
  17. package/debug/foo.txt +3080 -0
  18. package/debug/kenko_big.brs +0 -0
  19. package/debug/light.brs +0 -0
  20. package/debug/out.json +105 -0
  21. package/debug/read.js +30 -0
  22. package/debug/readSpeed.js +32 -0
  23. package/debug/readTest.js +45 -0
  24. package/debug/temp.brs +0 -0
  25. package/debug/western.brs +0 -0
  26. package/dist/src/constants.d.ts +6 -0
  27. package/dist/src/constants.d.ts.map +1 -0
  28. package/dist/src/index.d.ts +23 -0
  29. package/dist/src/index.d.ts.map +1 -0
  30. package/dist/src/read.d.ts +3 -0
  31. package/dist/src/read.d.ts.map +1 -0
  32. package/dist/src/read.v1.d.ts +3 -0
  33. package/dist/src/read.v1.d.ts.map +1 -0
  34. package/dist/src/read.v10.d.ts +3 -0
  35. package/dist/src/read.v10.d.ts.map +1 -0
  36. package/dist/src/read.v2.d.ts +3 -0
  37. package/dist/src/read.v2.d.ts.map +1 -0
  38. package/dist/src/read.v3.d.ts +3 -0
  39. package/dist/src/read.v3.d.ts.map +1 -0
  40. package/dist/src/read.v4.d.ts +3 -0
  41. package/dist/src/read.v4.d.ts.map +1 -0
  42. package/dist/src/read.v8.d.ts +3 -0
  43. package/dist/src/read.v8.d.ts.map +1 -0
  44. package/dist/src/read.v9.d.ts +3 -0
  45. package/dist/src/read.v9.d.ts.map +1 -0
  46. package/dist/src/types.d.ts +170 -0
  47. package/dist/src/types.d.ts.map +1 -0
  48. package/dist/src/utils.d.ts +87 -0
  49. package/dist/src/utils.d.ts.map +1 -0
  50. package/dist/src/uuid.d.ts +4 -0
  51. package/dist/src/uuid.d.ts.map +1 -0
  52. package/dist/src/write.d.ts +3 -0
  53. package/dist/src/write.d.ts.map +1 -0
  54. package/examples/ATCFort.brs +0 -0
  55. package/examples/read_example.html +34 -0
  56. package/examples/read_example.js +21 -0
  57. package/examples/write_example.js +25 -0
  58. package/examples/write_planet.html +144 -0
  59. package/examples/write_simplex.html +82 -0
  60. package/examples/write_wedge_sphere.html +173 -0
  61. package/package.json +1 -1
  62. package/src/constants.ts +6 -0
  63. package/src/index.ts +24 -0
  64. package/src/read.ts +57 -0
  65. package/src/read.v1.ts +99 -0
  66. package/src/read.v10.ts +185 -0
  67. package/src/read.v2.ts +102 -0
  68. package/src/read.v3.ts +111 -0
  69. package/src/read.v4.ts +112 -0
  70. package/src/read.v8.ts +172 -0
  71. package/src/read.v9.ts +181 -0
  72. package/src/types.ts +240 -0
  73. package/src/utils.ts +640 -0
  74. package/src/uuid.ts +78 -0
  75. package/src/write.ts +209 -0
  76. package/test/lib.test.js +51 -0
  77. package/test/utils.test.js +209 -0
package/src/write.ts ADDED
@@ -0,0 +1,209 @@
1
+ import { MAGIC, LATEST_VERSION, MAX_INT, DEFAULT_UUID } from './constants';
2
+ import { Brick, Owner, WriteOptions, WriteSaveObject } from './types';
3
+ import { write, isEqual, concat } from './utils';
4
+
5
+ const EMPTY_ARR = new Uint8Array([]);
6
+
7
+ export default function writeBrs(
8
+ save: WriteSaveObject,
9
+ options: WriteOptions = {}
10
+ ) {
11
+ if (typeof save !== 'object') {
12
+ throw new Error('Expected save to be an object');
13
+ }
14
+
15
+ if (!Array.isArray(save.bricks) || save.bricks.length === 0) {
16
+ throw new Error('Expected save to have bricks field');
17
+ }
18
+
19
+ if (save.bricks.length > MAX_INT) {
20
+ throw new Error('Brick count out of range');
21
+ }
22
+
23
+ // compression is disabled by default
24
+ const compress = options.compress ? write.compressed : write.uncompressed;
25
+
26
+ // Convert from BGRA to RGBA
27
+ const rgba = ([r, g, b, a]: number[]) => new Uint8Array([b, g, r, a]);
28
+
29
+ // stored brick indices from components on the bricks
30
+ const componentBrickOwnership: { [component_name: string]: number[] } = {};
31
+
32
+ const numColors = Math.max(save.colors?.length ?? 0, 2);
33
+ const numAssets = Math.max(save.brick_assets?.length ?? 0, 2);
34
+ const numMats = Math.max(save.materials?.length ?? 0, 2);
35
+ const numPhysMats = Math.max(save.physical_materials?.length ?? 0, 2);
36
+
37
+ if (save.preview && !Array.isArray(save.preview))
38
+ throw new Error('Expected preview to be an array');
39
+
40
+ const buff = concat(
41
+ // Write magic bytes
42
+ MAGIC,
43
+ write.u16(LATEST_VERSION),
44
+ write.i32(save.game_version ?? 0),
45
+
46
+ // Header 1
47
+ compress(
48
+ write.string(save.map ?? 'Unknown'),
49
+ write.string(save.author?.name ?? 'Unknown'),
50
+ write.string(save.description ?? ''),
51
+ write.uuid(save.author?.id ?? DEFAULT_UUID),
52
+ concat(
53
+ write.string(save.host?.name ?? 'Unknown'),
54
+ write.uuid(save.host?.id ?? DEFAULT_UUID)
55
+ ),
56
+ new Uint8Array(
57
+ save.save_time &&
58
+ (Array.isArray(save.save_time) ||
59
+ save.save_time instanceof Uint8Array) &&
60
+ save.save_time.length === 8
61
+ ? save.save_time
62
+ : [0, 0, 0, 0, 0, 0, 0, 0]
63
+ ),
64
+ write.i32(save.bricks.length)
65
+ ),
66
+
67
+ // Header 2
68
+ compress(
69
+ write.array(save.mods ?? [], write.string),
70
+ write.array(save.brick_assets ?? ['PB_DefaultBrick'], write.string),
71
+ write.array(save.colors ?? [], rgba),
72
+ write.array(save.materials ?? ['BMC_Plastic'], write.string),
73
+ write.array(
74
+ save.brick_owners ?? [],
75
+ ({ id = DEFAULT_UUID, name = 'Unknown', bricks = 0 }: Partial<Owner>) =>
76
+ concat(write.uuid(id), write.string(name), write.i32(bricks))
77
+ ),
78
+ write.array(save.physical_materials ?? ['BPMC_Default'], write.string)
79
+ ),
80
+
81
+ // write the save preview if it exists
82
+ concat(
83
+ new Uint8Array([save.preview ? 1 : 0]),
84
+ save.preview ? write.i32(save.preview.length) : EMPTY_ARR, // <- Sorry @Uxie https://i.imgur.com/hSRxdbf.png
85
+ save.preview ?? EMPTY_ARR
86
+ ),
87
+
88
+ // Bricks
89
+ compress(
90
+ write
91
+ .bits()
92
+ .each(save.bricks, function (brick: Brick, i) {
93
+ if (typeof brick !== 'object')
94
+ throw new Error(`Expected save.bricks[${i}] to be an object`);
95
+
96
+ if (!Array.isArray(brick.size) || brick.size.length !== 3)
97
+ throw new Error(
98
+ `Expected save.bricks[${i}].size to be an array of length 3`
99
+ );
100
+
101
+ if (!Array.isArray(brick.position) || brick.position.length !== 3)
102
+ throw new Error(
103
+ `Expected save.bricks[${i}].position to be an array of length 3`
104
+ );
105
+
106
+ this.align();
107
+ this.int(brick.asset_name_index ?? 0, numAssets);
108
+
109
+ const isNonProcedural = isEqual(brick.size, [0, 0, 0]);
110
+ this.bit(!isNonProcedural);
111
+ if (!isNonProcedural) {
112
+ brick.size.map(s => this.uint_packed(s));
113
+ }
114
+ brick.position.map(s => this.int_packed(s));
115
+ this.int(((brick.direction ?? 4) << 2) | (brick.rotation ?? 0), 24);
116
+
117
+ if (typeof brick.collision === 'boolean') {
118
+ this.bit(brick.collision);
119
+ this.bit(brick.collision);
120
+ this.bit(brick.collision);
121
+ this.bit(true);
122
+ } else {
123
+ this.bit(brick.collision?.player ?? true);
124
+ this.bit(brick.collision?.weapon ?? true);
125
+ this.bit(brick.collision?.interaction ?? true);
126
+ this.bit(brick.collision?.tool ?? true);
127
+ }
128
+
129
+ this.bit(brick?.visibility ?? true);
130
+ this.int(brick?.material_index ?? 0, numMats);
131
+ this.int(brick.physical_index ?? 0, numPhysMats);
132
+ this.int(brick.material_intensity ?? 5, 11);
133
+
134
+ if (typeof brick.color === 'number') {
135
+ this.bit(false);
136
+ this.int(brick.color, numColors);
137
+ } else {
138
+ this.bit(true);
139
+ if (
140
+ brick.color &&
141
+ (!Array.isArray(brick.color) || brick.color.length !== 3)
142
+ )
143
+ throw new Error(
144
+ `Expected save.bricks[${i}].color to be an array of length 3`
145
+ );
146
+ this.bytes(new Uint8Array(brick.color ?? [255, 255, 255]));
147
+ }
148
+
149
+ this.uint_packed(brick.owner_index ?? 1);
150
+
151
+ // add all the brick indices to the components list
152
+ for (const key in brick.components ?? {}) {
153
+ componentBrickOwnership[key] ??= [];
154
+ componentBrickOwnership[key].push(i);
155
+ }
156
+ })
157
+ .finish()
158
+ ),
159
+
160
+ // write components section
161
+ compress(
162
+ write.array(
163
+ Object.keys(save.components ?? {}).filter(
164
+ name => componentBrickOwnership[name]
165
+ ),
166
+ name =>
167
+ concat(
168
+ write.string(name),
169
+ write
170
+ .bits()
171
+ .self(function () {
172
+ const component = save.components[name];
173
+ const brick_indices = componentBrickOwnership[name];
174
+ const properties = Object.entries(component.properties);
175
+
176
+ // write version
177
+ this.bytes(write.i32(component.version));
178
+
179
+ // write bricks;
180
+ this.array(brick_indices, i => {
181
+ this.int(i, Math.max(save.bricks.length, 2));
182
+ });
183
+
184
+ // write properties
185
+ this.array(properties, ([name, type]) => {
186
+ this.string(name);
187
+ this.string(type);
188
+ });
189
+
190
+ // read brick indices
191
+ for (const i of brick_indices) {
192
+ for (const [prop, type] of properties) {
193
+ if (!(prop in save.bricks[i].components[name])) {
194
+ throw new Error(
195
+ `Expected save.bricks[${i}].components[${name}] to have property '${prop}' of type '${type}'`
196
+ );
197
+ }
198
+ this.unreal(type, save.bricks[i].components[name][prop]);
199
+ }
200
+ }
201
+ this.align();
202
+ })
203
+ .finishSection()
204
+ )
205
+ )
206
+ )
207
+ );
208
+ return buff;
209
+ }
@@ -0,0 +1,51 @@
1
+ const { read, write } = require('..');
2
+
3
+ // const uuid0 = '00000000-0000-0000-0000-000000000000';
4
+ const uuid0 = '12345678-4321-1234-4321-123456789012';
5
+ const save = {
6
+ version: 10,
7
+ map: 'Unknown',
8
+ description: '',
9
+ author: { id: uuid0, name: 'Test' },
10
+ save_time: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]),
11
+ brick_count: 1,
12
+ mods: [],
13
+ preview: null,
14
+ brick_assets: [],
15
+ colors: [],
16
+ components: {},
17
+ game_version: 0,
18
+ host: { id: uuid0, name: 'Test' },
19
+ materials: ['BMC_Hologram', 'BMC_Plastic', 'BMC_Glow', 'BMC_Metallic'],
20
+ physical_materials: ['BPMC_Default'],
21
+ brick_owners: [{ id: uuid0, name: 'Test', bricks: 0 }],
22
+ bricks: [
23
+ {
24
+ asset_name_index: 1,
25
+ size: [10, 10, 10],
26
+ position: [0, 0, 0],
27
+ components: {},
28
+ direction: 4,
29
+ owner_index: 1,
30
+ rotation: 0,
31
+ collision: {
32
+ player: true,
33
+ interaction: true,
34
+ tool: true,
35
+ weapon: true,
36
+ },
37
+ visibility: true,
38
+ material_index: 1,
39
+ material_intensity: 5,
40
+ physical_index: 0,
41
+ color: [0, 0, 0],
42
+ },
43
+ ],
44
+ };
45
+ test('creating a brs from thin air', () => {
46
+ expect(read(write(save))).toEqual(save);
47
+ });
48
+
49
+ test('reads no bricks when the option is passed in', () => {
50
+ expect(read(write(save), { bricks: false })).toEqual({ ...save, bricks: [] });
51
+ });
@@ -0,0 +1,209 @@
1
+ const {
2
+ utils: { read, write, subarray, chunk },
3
+ } = require('..');
4
+
5
+ describe('buffer read/writing', () => {
6
+ // Generic testing of read and write
7
+ const rwTest =
8
+ fn =>
9
+ (bytes, val, ...args) => {
10
+ expect(read[fn](new Uint8Array(bytes), ...args)).toEqual(val);
11
+ expect(write[fn](val, ...args)).toMatchObject(new Uint8Array(bytes));
12
+ };
13
+
14
+ // Testing both endiannesses for read and write
15
+ const endianTest = fn => (bytes, val) => {
16
+ // Little Endian (default)
17
+ rwTest(fn)(bytes, val);
18
+
19
+ // Big Endian
20
+ rwTest(fn)(new Uint8Array(bytes).reverse(), val, false);
21
+ };
22
+
23
+ describe('byte manipulating util', () => {
24
+ test('subarray', () => {
25
+ const arr = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
26
+ expect(subarray(arr, 2)).toMatchObject({ 0: 1, 1: 2 });
27
+ expect(arr.brsOffset).toEqual(2);
28
+ expect(subarray(arr, 4)).toMatchObject({ 0: 3, 1: 4, 2: 5, 3: 6 });
29
+ expect(arr.brsOffset).toEqual(6);
30
+ });
31
+
32
+ test('chunk', () => {
33
+ const arr = new Uint8Array([
34
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
35
+ ]);
36
+ expect(chunk(arr, 4)).toMatchObject([
37
+ { 0: 1, 1: 2, 2: 3, 3: 4 },
38
+ { 0: 5, 1: 6, 2: 7, 3: 8 },
39
+ { 0: 9, 1: 10, 2: 11, 3: 12 },
40
+ { 0: 13, 1: 14, 2: 15, 3: 16 },
41
+ ]);
42
+ });
43
+ });
44
+
45
+ describe('unsigned short', () => {
46
+ const shortTest = endianTest('u16');
47
+
48
+ test('00 00 -> 0', () => {
49
+ const bytes = [0x00, 0x00];
50
+ const val = 0;
51
+
52
+ shortTest(bytes, val);
53
+ });
54
+
55
+ test('ff ff -> 65535', () => {
56
+ const bytes = [0xff, 0xff];
57
+ const val = 0xffff;
58
+
59
+ shortTest(bytes, val);
60
+ });
61
+
62
+ test('01 20 -> 8193', () => {
63
+ const bytes = [0x01, 0x20];
64
+ const val = 0x2001;
65
+
66
+ shortTest(bytes, val);
67
+ });
68
+ });
69
+
70
+ describe('signed int', () => {
71
+ const intTest = endianTest('i32');
72
+
73
+ test('00 00 00 00 -> 0', () => {
74
+ const bytes = [0x00, 0x00, 0x00, 0x00];
75
+ const val = 0;
76
+
77
+ intTest(bytes, val);
78
+ });
79
+
80
+ test('ff ff ff ff -> -1', () => {
81
+ const bytes = [0xff, 0xff, 0xff, 0xff];
82
+ const val = -1;
83
+
84
+ intTest(bytes, val);
85
+ });
86
+
87
+ test('13 37 69 69 -> 322398569', () => {
88
+ const bytes = [0x69, 0x69, 0x37, 0x13];
89
+ const val = 322398569;
90
+
91
+ intTest(bytes, val);
92
+ });
93
+ });
94
+
95
+ describe('string', () => {
96
+ test('04 00 00 00 66 6f 6f 00 -> "foo"', () => {
97
+ const bytes = [0x04, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00];
98
+ const val = 'foo';
99
+
100
+ rwTest('string')(bytes, val);
101
+ });
102
+ });
103
+
104
+ describe('uuid', () => {
105
+ test('can parse uuid', () => {
106
+ const bytes = [
107
+ 205, 107, 157, 27, 45, 75, 253, 187, 141, 171, 93, 155, 237, 75, 189,
108
+ 251,
109
+ ];
110
+ const uuid = '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
111
+
112
+ rwTest('uuid')(bytes, uuid);
113
+ });
114
+ });
115
+
116
+ describe('array', () => {
117
+ /*
118
+ const bytes = [0x04, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00];
119
+ const val = 'foo';
120
+ */
121
+ const read_byte = b => read.bytes(b, 1)[0];
122
+
123
+ const arrTest = (bytes, arr, read_fn, write_fn) => {
124
+ expect(read.array(new Uint8Array(bytes), read_fn)).toMatchObject(arr);
125
+ expect(write.array(arr, write_fn)).toMatchObject(new Uint8Array(bytes));
126
+ };
127
+
128
+ test('00 00 00 00 -> []', () => {
129
+ const bytes = [0x00, 0x00, 0x00, 0x00];
130
+ const arr = [];
131
+
132
+ arrTest(bytes, arr, read_byte, b => [b]);
133
+ });
134
+
135
+ test('03 00 00 00 01 02 03 -> [1, 2, 3]', () => {
136
+ const bytes = [0x03, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03];
137
+ const arr = [1, 2, 3];
138
+
139
+ arrTest(bytes, arr, read_byte, b => [b]);
140
+ });
141
+
142
+ test('string array', () => {
143
+ const str_bytes = [0x04, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00];
144
+ const str = 'foo';
145
+ const bytes = [
146
+ 0x05,
147
+ 0,
148
+ 0,
149
+ 0,
150
+ ...str_bytes,
151
+ ...str_bytes,
152
+ ...str_bytes,
153
+ ...str_bytes,
154
+ ...str_bytes,
155
+ ];
156
+ const arr = [str, str, str, str, str];
157
+ arrTest(bytes, arr, read.string, write.string);
158
+ });
159
+ });
160
+
161
+ describe('bit reader', () => {
162
+ it('reads bits', () => {
163
+ const bits = read.bits([0b10101111]);
164
+ expect(bits.bit()).toBe(true);
165
+ expect(bits.bit()).toBe(true);
166
+ expect(bits.bit()).toBe(true);
167
+ expect(bits.bit()).toBe(true);
168
+ expect(bits.bit()).toBe(false);
169
+ expect(bits.bit()).toBe(true);
170
+ expect(bits.bit()).toBe(false);
171
+ expect(bits.bit()).toBe(true);
172
+ });
173
+
174
+ it('reads aligned bits', () => {
175
+ const bits = read.bits([0b00000001, 0b10101111]);
176
+ expect(bits.bit()).toBe(true);
177
+ bits.align();
178
+ expect(bits.bit()).toBe(true);
179
+ expect(bits.bit()).toBe(true);
180
+ expect(bits.bit()).toBe(true);
181
+ expect(bits.bit()).toBe(true);
182
+ expect(bits.bit()).toBe(false);
183
+ expect(bits.bit()).toBe(true);
184
+ expect(bits.bit()).toBe(false);
185
+ expect(bits.bit()).toBe(true);
186
+ });
187
+
188
+ it('reads ints', () => {
189
+ const bits = read.bits([0b11001111]);
190
+ expect(bits.int(16)).toBe(15);
191
+ expect(bits.int(16)).toBe(12);
192
+ });
193
+
194
+ it('reads floats', () => {
195
+ const bits = read.bits([0x42, 0xf6, 0xe6, 0x66].reverse());
196
+ expect(bits.float()).toBeCloseTo(123.45);
197
+ });
198
+
199
+ it('writes floats', () => {
200
+ const bits = write.bits();
201
+ bits.float(123.45);
202
+ expect(bits.finish()).toStrictEqual(
203
+ new Uint8Array([0x42, 0xf6, 0xe6, 0x66]).reverse()
204
+ );
205
+ });
206
+
207
+ // TODO: tests for int_packed, uint_packed, bytes
208
+ });
209
+ });