brs-js 2.0.4 → 2.0.7

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
@@ -23,6 +23,22 @@ export type UnrealType =
23
23
  | UnrealByte
24
24
  | UnrealRotator;
25
25
 
26
+ type UnrealTypeFromString<T> = T extends 'Class'
27
+ ? UnrealClass
28
+ : T extends 'Object'
29
+ ? UnrealObject
30
+ : T extends 'Boolean'
31
+ ? UnrealBoolean
32
+ : T extends 'Float'
33
+ ? UnrealFloat
34
+ : T extends 'Color'
35
+ ? UnrealColor
36
+ : T extends 'Byte'
37
+ ? UnrealByte
38
+ : T extends 'Rotator'
39
+ ? UnrealRotator
40
+ : UnrealType;
41
+
26
42
  export interface User {
27
43
  id: Uuid;
28
44
  name: string;
@@ -61,10 +77,18 @@ export interface AppliedComponent {
61
77
  [property: string]: UnrealType;
62
78
  }
63
79
 
64
- export interface DefinedComponents {
65
- 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: {
66
90
  version: 1;
67
- brick_indices: number[];
91
+ brick_indices?: number[];
68
92
  properties: {
69
93
  Rotation: 'Rotator';
70
94
  InnerConeAngle: 'Float';
@@ -76,9 +100,9 @@ export interface DefinedComponents {
76
100
  bCastShadows: 'Boolean';
77
101
  };
78
102
  };
79
- BCD_PointLight?: {
103
+ BCD_PointLight: {
80
104
  version: 1;
81
- brick_indices: number[];
105
+ brick_indices?: number[];
82
106
  properties: {
83
107
  bMatchBrickShape: 'Boolean';
84
108
  Brightness: 'Float';
@@ -88,9 +112,9 @@ export interface DefinedComponents {
88
112
  bCastShadows: 'Boolean';
89
113
  };
90
114
  };
91
- BCD_ItemSpawn?: {
115
+ BCD_ItemSpawn: {
92
116
  version: 1;
93
- brick_indices: number[];
117
+ brick_indices?: number[];
94
118
  properties: {
95
119
  PickupClass: 'Class';
96
120
  bPickupEnabled: 'Boolean';
@@ -111,14 +135,14 @@ export interface DefinedComponents {
111
135
  PickupAnimationPhase: 'Float';
112
136
  };
113
137
  };
114
- BCD_Interact?: {
138
+ BCD_Interact: {
115
139
  version: 1;
116
- brick_indices: number[];
140
+ brick_indices?: number[];
117
141
  properties: { bPlayInteractSound: 'Boolean' };
118
142
  };
119
- BCD_AudioEmitter?: {
143
+ BCD_AudioEmitter: {
120
144
  version: 1;
121
- brick_indices: number[];
145
+ brick_indices?: number[];
122
146
  properties: {
123
147
  AudioDescriptor: 'Object';
124
148
  VolumeMultiplier: 'Float';
@@ -128,64 +152,19 @@ export interface DefinedComponents {
128
152
  bSpatialization: 'Boolean';
129
153
  };
130
154
  };
131
- [component_name: string]: {
132
- version: number;
133
- brick_indices: number[];
134
- properties: { [property: string]: string };
135
- };
136
- }
155
+ };
137
156
 
138
- export interface Components {
139
- BCD_SpotLight?: {
140
- Rotation: UnrealRotator;
141
- InnerConeAngle: UnrealFloat;
142
- OuterConeAngle: UnrealFloat;
143
- Brightness: UnrealFloat;
144
- Radius: UnrealFloat;
145
- Color: UnrealColor;
146
- bUseBrickColor: UnrealBoolean;
147
- bCastShadows: UnrealBoolean;
148
- };
149
- BCD_PointLight?: {
150
- bMatchBrickShape: UnrealBoolean;
151
- Brightness: UnrealFloat;
152
- Radius: UnrealFloat;
153
- Color: UnrealColor;
154
- bUseBrickColor: UnrealBoolean;
155
- bCastShadows: UnrealBoolean;
156
- };
157
- BCD_ItemSpawn?: {
158
- PickupClass: UnrealClass;
159
- bPickupEnabled: UnrealBoolean;
160
- bPickupRespawnOnMinigameReset: UnrealBoolean;
161
- PickupMinigameResetRespawnDelay: UnrealFloat;
162
- bPickupAutoDisableOnPickup: UnrealBoolean;
163
- PickupRespawnTime: UnrealFloat;
164
- PickupOffsetDirection: UnrealByte;
165
- PickupOffsetDistance: UnrealFloat;
166
- PickupRotation: UnrealRotator;
167
- PickupScale: UnrealFloat;
168
- bPickupAnimationEnabled: UnrealBoolean;
169
- PickupAnimationAxis: UnrealByte;
170
- bPickupAnimationAxisLocal: UnrealBoolean;
171
- PickupSpinSpeed: UnrealFloat;
172
- PickupBobSpeed: UnrealFloat;
173
- PickupBobHeight: UnrealFloat;
174
- PickupAnimationPhase: UnrealFloat;
175
- };
176
- BCD_Interact?: {
177
- bPlayInteractSound: UnrealBoolean;
178
- };
179
- BCD_AudioEmitter?: {
180
- AudioDescriptor: UnrealObject;
181
- VolumeMultiplier: UnrealFloat;
182
- PitchMultiplier: UnrealFloat;
183
- InnerRadius: UnrealFloat;
184
- MaxDistance: UnrealFloat;
185
- bSpatialization: UnrealBoolean;
157
+ export interface DefinedComponents
158
+ extends UnknownComponents,
159
+ Partial<KnownComponents> {}
160
+
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]
165
+ >;
186
166
  };
187
- [component_name: string]: AppliedComponent;
188
- }
167
+ } & { [component_name: string]: AppliedComponent };
189
168
 
190
169
  export type Vector = [number, number, number];
191
170
 
@@ -209,7 +188,7 @@ export interface BrickV3 extends BrickV2 {
209
188
  }
210
189
 
211
190
  export interface BrickV8 extends BrickV3 {
212
- components: Components;
191
+ components: Components<DefinedComponents>;
213
192
  }
214
193
 
215
194
  export type BrickV9 = Modify<
@@ -322,7 +301,7 @@ export interface Brick {
322
301
  material_intensity?: number;
323
302
  color?: ColorRgb | number;
324
303
  owner_index?: number;
325
- components?: Components;
304
+ components?: Components<DefinedComponents>;
326
305
  }
327
306
 
328
307
  // save a user can write
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
  );