-
Notifications
You must be signed in to change notification settings - Fork 15
/
build.mjs
executable file
·283 lines (261 loc) · 8.46 KB
/
build.mjs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { spawn } from 'child_process';
import { platform } from 'os';
const verbose = process.argv.indexOf('--verbose') != -1;
async function main() {
const objects = [];
for (const dir of ['rct2', 'openrct2']) {
const moreObjects = await getObjects(dir);
objects.push(...moreObjects);
}
// Process all objects with gx files first before those with .png files
objects.sort((a, b) => {
if (typeof a.images === typeof b.images) return 0;
if (typeof a.images === 'string') return -1;
return 1;
});
await mkdir('out');
await mkdir('temp');
const manifest = await readJsonFile('manifest.json');
manifest.objects = [];
const singleSpriteManifest = [];
const gxMergeList = [];
let imageIndex = 0;
for (const obj of objects) {
console.log(`Processing ${obj.id}...`);
let numImages;
const images = obj.images;
if (typeof images === 'string') {
const result = images.match(/^\$LGX:(.+)\[([0-9]+)\.\.([0-9]+)\]$/);
if (result) {
const fileName = result[1];
const index = parseInt(result[2]);
const length = parseInt(result[3]);
gxMergeList.push(path.join(obj.cwd, fileName));
numImages = length - index + 1;
} else {
const result = images.match(/^\$LGX:(.+)$/);
if (result) {
const fileName = result[1];
const fullPath = path.join(obj.cwd, fileName);
gxMergeList.push(fullPath);
numImages = await getGxImageCount('out', fullPath);
} else {
throw new Error(`Unsupported image format: ${images}`);
}
}
} else {
for (const image of images) {
image.path = path.join(obj.cwd, image.path);
}
singleSpriteManifest.push(...images);
numImages = images.length;
}
obj.images = `$LGX:images.dat[${imageIndex}..${imageIndex + numImages - 1}]`;
obj.cwd = undefined;
manifest.objects.push(obj);
imageIndex += numImages;
}
gxMergeList.push('images.dat');
console.log(`Building asset pack...`);
await writeJsonFile('temp/manifest.json', manifest);
await writeJsonFile('temp/images.json', singleSpriteManifest);
await compileGx('temp', 'images.json', 'images.dat');
if (gxMergeList.length >= 2) {
await mergeGx('temp', gxMergeList, 'images.dat');
}
const outFilename = `${manifest.id}.parkap`
const outPath = path.join('../out/', outFilename);
await zip('temp', outPath, ['manifest.json', 'images.dat']);
rm('temp');
console.log(`${outFilename} created successfully`);
}
async function getObjects(dir) {
const result = [];
const files = await getAllFiles(dir);
for (const file of files) {
const jsonRegex = /^.+\..+\.json$/;
if (jsonRegex.test(file)) {
const cwd = path.join('..', path.dirname(file));
const obj = await readJsonFile(file);
obj.cwd = cwd;
result.push(obj);
}
}
return result;
}
function compileGx(cwd, manifest, outputFile) {
return startProcess('gxc', ['build', outputFile, manifest], cwd);
}
async function mergeGx(cwd, inputFiles, outputFile) {
await startProcess('gxc', ['merge', outputFile, inputFiles[inputFiles.length - 2], inputFiles[inputFiles.length - 1]], cwd);
for (let i = inputFiles.length - 3; i >= 0; i--) {
await startProcess('gxc', ['merge', outputFile, inputFiles[i], outputFile], cwd);
}
}
async function getGxImageCount(cwd, inputFile) {
const stdout = await startProcess('gxc', ['details', inputFile], cwd);
const result = stdout.match(/numEntries: ([0-9]+)/);
if (result) {
return parseInt(result[1]);
} else {
throw new Error(`Unable to get number of images for gx file: ${inputFile}`);
}
}
function readJsonFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(data));
}
});
});
}
function writeJsonFile(path, data) {
return new Promise((resolve, reject) => {
const json = JSON.stringify(data, null, 4) + '\n';
fs.writeFile(path, json, 'utf8', err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function zip(cwd, outputFile, paths) {
if (platform() == 'win32') {
return startProcess('7z', ['a', '-tzip', outputFile, ...paths], cwd);
} else {
return startProcess('zip', [outputFile, ...paths], cwd);
}
}
function startProcess(name, args, cwd) {
return new Promise((resolve, reject) => {
const options = {};
if (cwd) options.cwd = cwd;
if (verbose) {
console.log(`Launching \"${name} ${args.join(' ')}\"`);
}
const child = spawn(name, args, options);
let stdout = '';
child.stdout.on('data', data => {
stdout += data;
});
child.stderr.on('data', data => {
stdout += data;
});
child.on('error', err => {
if (err.code == 'ENOENT') {
reject(new Error(`${name} was not found`));
} else {
reject(err);
}
});
child.on('close', code => {
if (code !== 0) {
reject(new Error(`${name} failed:\n${stdout}`));
} else {
resolve(stdout);
}
});
});
}
function mkdir(path) {
return new Promise((resolve, reject) => {
fs.access(path, (error) => {
if (error) {
fs.mkdir(path, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
resolve();
}
});
});
}
function getAllFiles(root) {
return new Promise((resolve, reject) => {
const results = [];
let pending = 0;
const find = (root) => {
pending++;
fs.readdir(root, (err, fileNames) => {
// if (err) {
// reject(err);
// }
for (const fileName of fileNames) {
const fullPath = path.join(root, fileName);
pending++;
fs.stat(fullPath, (err, stat) => {
// if (err) {
// reject(err);
// }
if (stat) {
if (stat.isDirectory()) {
find(fullPath);
} else {
results.push(fullPath);
}
}
pending--;
if (pending === 0) {
resolve(results);
}
});
}
pending--;
if (pending === 0) {
resolve(results.sort());
}
});
};
find(root);
});
}
function rm(filename) {
if (verbose) {
console.log(`Deleting ${filename}`)
}
return new Promise((resolve, reject) => {
fs.stat(filename, (err, stat) => {
if (err) {
if (err.code == 'ENOENT') {
resolve();
} else {
reject();
}
} else {
if (stat.isDirectory()) {
fs.rm(filename, { recursive: true }, err => {
if (err) {
reject(err);
}
resolve();
});
} else {
fs.unlink(filename, err => {
if (err) {
reject(err);
}
resolve();
});
}
}
});
});
}
try {
await main();
} catch (err) {
console.log(err.message);
process.exitCode = 1;
}