brs-js 2.0.5 → 2.0.8

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.
package/src/types.ts CHANGED
@@ -77,8 +77,16 @@ export interface AppliedComponent {
77
77
  [property: string]: UnrealType;
78
78
  }
79
79
 
80
- export interface DefinedComponents {
81
- BCD_SpotLight?: {
80
+ export interface UnknownComponents {
81
+ [component_name: string]: {
82
+ version: number;
83
+ brick_indices?: number[];
84
+ properties: { [property: string]: string };
85
+ };
86
+ }
87
+
88
+ export type KnownComponents = {
89
+ BCD_SpotLight: {
82
90
  version: 1;
83
91
  brick_indices?: number[];
84
92
  properties: {
@@ -92,7 +100,7 @@ export interface DefinedComponents {
92
100
  bCastShadows: 'Boolean';
93
101
  };
94
102
  };
95
- BCD_PointLight?: {
103
+ BCD_PointLight: {
96
104
  version: 1;
97
105
  brick_indices?: number[];
98
106
  properties: {
@@ -104,7 +112,7 @@ export interface DefinedComponents {
104
112
  bCastShadows: 'Boolean';
105
113
  };
106
114
  };
107
- BCD_ItemSpawn?: {
115
+ BCD_ItemSpawn: {
108
116
  version: 1;
109
117
  brick_indices?: number[];
110
118
  properties: {
@@ -127,12 +135,12 @@ export interface DefinedComponents {
127
135
  PickupAnimationPhase: 'Float';
128
136
  };
129
137
  };
130
- BCD_Interact?: {
138
+ BCD_Interact: {
131
139
  version: 1;
132
140
  brick_indices?: number[];
133
141
  properties: { bPlayInteractSound: 'Boolean' };
134
142
  };
135
- BCD_AudioEmitter?: {
143
+ BCD_AudioEmitter: {
136
144
  version: 1;
137
145
  brick_indices?: number[];
138
146
  properties: {
@@ -144,17 +152,16 @@ export interface DefinedComponents {
144
152
  bSpatialization: 'Boolean';
145
153
  };
146
154
  };
147
- [component_name: string]: {
148
- version: number;
149
- brick_indices?: number[];
150
- properties: { [property: string]: string };
151
- };
152
- }
155
+ };
156
+
157
+ export interface DefinedComponents
158
+ extends UnknownComponents,
159
+ Partial<KnownComponents> {}
153
160
 
154
- export type Components = {
155
- [T in keyof DefinedComponents]: {
156
- [V in keyof DefinedComponents[T]['properties']]: UnrealTypeFromString<
157
- DefinedComponents[T]['properties'][V]
161
+ export type Components<C extends DefinedComponents> = {
162
+ [T in keyof C]: {
163
+ [V in keyof C[T]['properties']]: UnrealTypeFromString<
164
+ C[T]['properties'][V]
158
165
  >;
159
166
  };
160
167
  } & { [component_name: string]: AppliedComponent };
@@ -181,7 +188,7 @@ export interface BrickV3 extends BrickV2 {
181
188
  }
182
189
 
183
190
  export interface BrickV8 extends BrickV3 {
184
- components: Components;
191
+ components: Components<DefinedComponents>;
185
192
  }
186
193
 
187
194
  export type BrickV9 = Modify<
@@ -294,7 +301,7 @@ export interface Brick {
294
301
  material_intensity?: number;
295
302
  color?: ColorRgb | number;
296
303
  owner_index?: number;
297
- components?: Components;
304
+ components?: Components<DefinedComponents>;
298
305
  }
299
306
 
300
307
  // save a user can write
package/src/utils.ts CHANGED
@@ -266,7 +266,7 @@ function write_array<T>(arr: T[], fn: (_: T) => Uint8Array) {
266
266
  }
267
267
 
268
268
  // Tool for reading byte arrays 1 bit at a time
269
- class BitReader {
269
+ export class BitReader {
270
270
  buffer: Uint8Array;
271
271
  pos: number = 0;
272
272
 
@@ -410,7 +410,7 @@ class BitReader {
410
410
  }
411
411
  }
412
412
 
413
- class BitWriter {
413
+ export class BitWriter {
414
414
  buffer: number[] = [];
415
415
  cur: number = 0;
416
416
  bitNum: number = 0;
package/src/write.ts CHANGED
@@ -1,9 +1,80 @@
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';
1
+ import { DEFAULT_UUID, LATEST_VERSION, MAGIC, MAX_INT } from './constants';
2
+ import {
3
+ Brick,
4
+ DefinedComponents,
5
+ KnownComponents,
6
+ Owner,
7
+ WriteOptions,
8
+ WriteSaveObject,
9
+ } from './types';
10
+ import { concat, isEqual, write } from './utils';
4
11
 
5
12
  const EMPTY_ARR = new Uint8Array([]);
6
13
 
14
+ export const DEFAULT_COMPONENTS: DefinedComponents = {
15
+ BCD_SpotLight: {
16
+ version: 1,
17
+ properties: {
18
+ Rotation: 'Rotator',
19
+ InnerConeAngle: 'Float',
20
+ OuterConeAngle: 'Float',
21
+ Brightness: 'Float',
22
+ Radius: 'Float',
23
+ Color: 'Color',
24
+ bUseBrickColor: 'Boolean',
25
+ bCastShadows: 'Boolean',
26
+ },
27
+ },
28
+ BCD_PointLight: {
29
+ version: 1,
30
+ properties: {
31
+ bMatchBrickShape: 'Boolean',
32
+ Brightness: 'Float',
33
+ Radius: 'Float',
34
+ Color: 'Color',
35
+ bUseBrickColor: 'Boolean',
36
+ bCastShadows: 'Boolean',
37
+ },
38
+ },
39
+ BCD_ItemSpawn: {
40
+ version: 1,
41
+ properties: {
42
+ PickupClass: 'Class',
43
+ bPickupEnabled: 'Boolean',
44
+ bPickupRespawnOnMinigameReset: 'Boolean',
45
+ PickupMinigameResetRespawnDelay: 'Float',
46
+ bPickupAutoDisableOnPickup: 'Boolean',
47
+ PickupRespawnTime: 'Float',
48
+ PickupOffsetDirection: 'Byte',
49
+ PickupOffsetDistance: 'Float',
50
+ PickupRotation: 'Rotator',
51
+ PickupScale: 'Float',
52
+ bPickupAnimationEnabled: 'Boolean',
53
+ PickupAnimationAxis: 'Byte',
54
+ bPickupAnimationAxisLocal: 'Boolean',
55
+ PickupSpinSpeed: 'Float',
56
+ PickupBobSpeed: 'Float',
57
+ PickupBobHeight: 'Float',
58
+ PickupAnimationPhase: 'Float',
59
+ },
60
+ },
61
+ BCD_Interact: {
62
+ version: 1,
63
+ properties: { bPlayInteractSound: 'Boolean' },
64
+ },
65
+ BCD_AudioEmitter: {
66
+ version: 1,
67
+ properties: {
68
+ AudioDescriptor: 'Object',
69
+ VolumeMultiplier: 'Float',
70
+ PitchMultiplier: 'Float',
71
+ InnerRadius: 'Float',
72
+ MaxDistance: 'Float',
73
+ bSpatialization: 'Boolean',
74
+ },
75
+ },
76
+ };
77
+
7
78
  export default function writeBrs(
8
79
  save: WriteSaveObject,
9
80
  options: WriteOptions = {}
@@ -160,7 +231,7 @@ export default function writeBrs(
160
231
  // write components section
161
232
  compress(
162
233
  write.array(
163
- Object.keys(save.components ?? {}).filter(
234
+ Object.keys(save.components ?? DEFAULT_COMPONENTS).filter(
164
235
  name => componentBrickOwnership[name]
165
236
  ),
166
237
  name =>
@@ -169,7 +240,8 @@ export default function writeBrs(
169
240
  write
170
241
  .bits()
171
242
  .self(function () {
172
- const component = save.components[name];
243
+ const component =
244
+ save.components?.[name] ?? DEFAULT_COMPONENTS[name];
173
245
  const brick_indices = componentBrickOwnership[name];
174
246
  const properties = Object.entries(component.properties);
175
247
 
@@ -190,7 +262,7 @@ export default function writeBrs(
190
262
  // read brick indices
191
263
  for (const i of brick_indices) {
192
264
  for (const [prop, type] of properties) {
193
- if (!(prop in save.bricks[i].components[name])) {
265
+ if (!(prop in (save.bricks[i].components?.[name] ?? {}))) {
194
266
  throw new Error(
195
267
  `Expected save.bricks[${i}].components[${name}] to have property '${prop}' of type '${type}'`
196
268
  );