forked from Suficio/node-brs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
245 lines (222 loc) · 7.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const fs = require('fs');
const brs = require('./build/brs.js');
/** Class representing a UUID */
class Uuid extends String
{
}
/** Class representing a datetime */
class DateTime extends String
{
}
/** Class representing a color */
class Color extends Number
{
}
// oof
const defaultColors = [4294967295, 4287137928, 4284045657, 4281940281, 4280492835, 4279308561, 4278584838, 4278190080, 4283893001, 4293527046, 4294330630, 4293565702, 4278749701, 4278491306, 4288881493, 4284092983, 4279632897, 4281406477, 4284026884, 4287642642, 4289095742, 4294942542, 4290945850, 4294946607, 4278522373, 4278525442, 4279575552, 4278209536, 4278924810, 4282601228, 4294939146, 4285349893, 4278853163, 4280166185, 4282866784, 4286819509, 4283470754, 4278744776, 4278206586, 4278264384, 2583629326, 2583677957, 2568982546, 2569766333, 2572616969, 2583054598, 2567449359, 2583691263, 2583691263, 2575861896, 2572769625, 2570664249, 2569216803, 2568032529, 2567308806, 2566914048].map(function(e) {return new Color(e);});
/** Class representing a User */
class User
{
/**
* Create a User
* @param {Uuid} id - ID
* @param {String} name - Name
*/
constructor(id, name)
{
this.id = id || new Uuid('00000000-0000-0000-0000-000000000000');
this.name = name || '';
}
}
const Direction = {
XPositive: 'XPositive',
XNegative: 'XNegative',
YPositive: 'YPositive',
YNegative: 'YNegative',
ZPositive: 'ZPositive',
ZNegative: 'ZNegative',
};
const Rotation = {
Deg0: 'Deg0',
Deg90: 'Deg90',
Deg180: 'Deg180',
Deg270: 'Deg270',
};
/** Class representing a ColorMode */
class ColorMode
{
/**
* Creates a new ColorMode
* @param {Number} set - A color from the lookup table
* @param {Color} custom - A custom color
*/
constructor(set, custom)
{
if (set !== undefined)
this.Set = set;
if (custom !== undefined)
this.Custom = custom;
}
/**
* Creates a new ColorMode with a color from the lookup table
* @param {Number} index - Index of color
* @return {ColorMode}
*/
static fromSet(index)
{
return new this(index);
}
/**
* Creates a new ColorMode with a color from the lookup table
* @param {Color} custom - Index of color
* @return {ColorMode}
*/
static fromCustom(custom)
{
return new this(undefined, custom);
}
}
/** Class representing a brick */
class Brick
{
/**
* Create new brick
* @param {Number} assetNameIndex - Index of asset name
* @param {{Number}[]} size - Size
* @param {{Number}[]} position - Position
* @param {Direction} direction - Direction
* @param {Rotation} rotation - Rotation
* @param {Boolean} collision - Collision
* @param {Boolean} visibility - Visibility
* @param {Number} materialIndex - Index of material
* @param {ColorMode} color - Index of color
* @param {Number} ownerIndex - Index of owner
*/
constructor(assetNameIndex, size, position, direction, rotation, collision, visibility, materialIndex, color, ownerIndex)
{
this.asset_name_index = assetNameIndex || 0;
this.size = size || [0, 0, 0];
this.position = position || [0, 0, 0];
this.direction = direction || Direction.ZPositive;
this.rotation = rotation || Rotation.Deg0;
this.collision = collision || true;
this.visibility = visibility || true;
this.material_index = materialIndex || 2;
this.color = color || new ColorMode(0);
this.owner_index = ownerIndex || 0;
}
}
/** Class representing save file data */
class WriteData
{
/**
* Create new save data
* @param {String} map - The name of the map that the save file was created on.
* @param {User} author - The user that created the save.
* @param {String} description - A short description of the save file.
* @param {DateTime} saveTime - When the save file was created.
* @param {{String}[]} mods - The mods used by the save file. Format not yet defined.
* @param {{String}[]} brickAssets - The name lookup table used by bricks. Example values include:
* `"PB_DefaultBrick"`, `"PB_DefaultTile"`, `"B_1x_Octo_T"`, etc.
* @param {{Color}[]} colors - The color lookup table used by bricks.
* @param {{String}[]} materials - The material lookup table used by bricks. Common values include:
* - `"BMC_Plastic"`
* - `"BMC_Glow"`
* - `"BMC_Metallic"`
* - `"BMC_Hologram"`
* @param {{User}[]} brickOwners - The brick owner lookup table used by bricks.
* @param {{Brick}[]} bricks - All the bricks in the save file
*/
constructor(map, author, description, saveTime, mods, brickAssets, colors, materials, brickOwners, bricks)
{
this.map = map || 'Plate';
this.author = author || new User();
this.description = description || '';
this.save_time = saveTime || new DateTime('1970-01-01T00:00:00+00:00');
this.mods = mods || [];
this.brick_assets = brickAssets || ['PB_DefaultBrick'];
this.colors = colors || defaultColors;
this.materials = materials || [
'BMC_Ghost',
'BMC_Ghost_Fail',
'BMC_Plastic',
'BMC_Glow',
'BMC_Metallic',
'BMC_Hologram',
];
this.brick_owners = brickOwners || [];
this.bricks = bricks || [];
}
}
/**
* Writes save to file
* @param {String} filename - Path of file to write to
* @param {WriteData} writeData - Save data to write
*/
function writeSave(filename, writeData)
{
const rawData = Buffer.from(brs.write_save_from_js(writeData || new WriteData()));
fs.writeFileSync(filename, rawData);
};
/**
* Read save file
* @param {String} filename - Path of file to read
* @return {WriteData}
*/
function readSave(filename)
{
const rawData = fs.readFileSync(filename);
const data = brs.read_save_from_js([...rawData]); // Nothing else worked
data.colors = data.colors.map(function(e)
{
return new Color(e);
});
data.brick_owners = data.brick_owners.map(function(e)
{
return new User(e.id, e.name);
});
data.bricks = data.bricks.map(function(e)
{
return new Brick(
e.asset_name_index,
e.size,
e.position,
e.direction,
e.rotation,
e.collision,
e.visibility,
e.material_index,
new ColorMode(e.color.Set, e.color.Custom),
e.owner_index
);
});
return new WriteData(
data.map,
new User(
new Uuid(data.author.id),
data.author.name
),
data.description,
new DateTime(data.save_time),
data.mods,
data.brick_assets,
data.colors,
data.materials,
data.brick_owners,
data.bricks
);
}
module.exports = {
WriteData: WriteData,
Brick: Brick,
ColorMode: ColorMode,
Rotation: Rotation,
Direction: Direction,
User: User,
Color: Color,
DateTime: DateTime,
Uuid: Uuid,
// Functions
writeSave: writeSave,
readSave: readSave,
};