brs-js 2.0.3 → 2.0.6

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,17 +77,94 @@ export interface AppliedComponent {
61
77
  [property: string]: UnrealType;
62
78
  }
63
79
 
64
- export interface DefinedComponents {
80
+ export interface UnknownComponents {
65
81
  [component_name: string]: {
66
82
  version: number;
67
- brick_indices: number[];
83
+ brick_indices?: number[];
68
84
  properties: { [property: string]: string };
69
85
  };
70
86
  }
71
87
 
72
- export interface Components {
73
- [component_name: string]: AppliedComponent;
74
- }
88
+ export type KnownComponents = {
89
+ BCD_SpotLight: {
90
+ version: 1;
91
+ brick_indices?: number[];
92
+ properties: {
93
+ Rotation: 'Rotator';
94
+ InnerConeAngle: 'Float';
95
+ OuterConeAngle: 'Float';
96
+ Brightness: 'Float';
97
+ Radius: 'Float';
98
+ Color: 'Color';
99
+ bUseBrickColor: 'Boolean';
100
+ bCastShadows: 'Boolean';
101
+ };
102
+ };
103
+ BCD_PointLight: {
104
+ version: 1;
105
+ brick_indices?: number[];
106
+ properties: {
107
+ bMatchBrickShape: 'Boolean';
108
+ Brightness: 'Float';
109
+ Radius: 'Float';
110
+ Color: 'Color';
111
+ bUseBrickColor: 'Boolean';
112
+ bCastShadows: 'Boolean';
113
+ };
114
+ };
115
+ BCD_ItemSpawn: {
116
+ version: 1;
117
+ brick_indices?: number[];
118
+ properties: {
119
+ PickupClass: 'Class';
120
+ bPickupEnabled: 'Boolean';
121
+ bPickupRespawnOnMinigameReset: 'Boolean';
122
+ PickupMinigameResetRespawnDelay: 'Float';
123
+ bPickupAutoDisableOnPickup: 'Boolean';
124
+ PickupRespawnTime: 'Float';
125
+ PickupOffsetDirection: 'Byte';
126
+ PickupOffsetDistance: 'Float';
127
+ PickupRotation: 'Rotator';
128
+ PickupScale: 'Float';
129
+ bPickupAnimationEnabled: 'Boolean';
130
+ PickupAnimationAxis: 'Byte';
131
+ bPickupAnimationAxisLocal: 'Boolean';
132
+ PickupSpinSpeed: 'Float';
133
+ PickupBobSpeed: 'Float';
134
+ PickupBobHeight: 'Float';
135
+ PickupAnimationPhase: 'Float';
136
+ };
137
+ };
138
+ BCD_Interact: {
139
+ version: 1;
140
+ brick_indices?: number[];
141
+ properties: { bPlayInteractSound: 'Boolean' };
142
+ };
143
+ BCD_AudioEmitter: {
144
+ version: 1;
145
+ brick_indices?: number[];
146
+ properties: {
147
+ AudioDescriptor: 'Object';
148
+ VolumeMultiplier: 'Float';
149
+ PitchMultiplier: 'Float';
150
+ InnerRadius: 'Float';
151
+ MaxDistance: 'Float';
152
+ bSpatialization: 'Boolean';
153
+ };
154
+ };
155
+ };
156
+
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
+ >;
166
+ };
167
+ } & { [component_name: string]: AppliedComponent };
75
168
 
76
169
  export type Vector = [number, number, number];
77
170
 
@@ -95,7 +188,7 @@ export interface BrickV3 extends BrickV2 {
95
188
  }
96
189
 
97
190
  export interface BrickV8 extends BrickV3 {
98
- components: Components;
191
+ components: Components<DefinedComponents>;
99
192
  }
100
193
 
101
194
  export type BrickV9 = Modify<
@@ -208,7 +301,7 @@ export interface Brick {
208
301
  material_intensity?: number;
209
302
  color?: ColorRgb | number;
210
303
  owner_index?: number;
211
- components?: Components;
304
+ components?: Components<DefinedComponents>;
212
305
  }
213
306
 
214
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
  );