-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.mjs
executable file
·284 lines (260 loc) · 8.27 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
284
#!/usr/bin/env node
import fs, { rmdir } 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() {
await mkdir('out');
await createObject('openrct2.audio.additional');
await createAssetPack('openrct2.sound');
await createPackage();
await rm('temp');
}
async function createPackage() {
const packageFileName = "artifacts/opensound.zip";
console.log(`Creating package: ${packageFileName}`);
const contents = await getContents("out", {
includeDirectories: true,
includeFiles: true
});
await zip("out", path.join('..', packageFileName), contents);
}
async function createObject(dir) {
const workDir = 'temp';
await rmmkdir(workDir);
const root = await readJsonFile(path.join(dir, 'object.json'));
console.log(`Creating ${root.id}`);
const samples = root.samples;
for (let i = 0; i < samples.length; i++) {
const newPath = changeExtension(samples[i], '.wav');
const srcPath = path.join(dir, samples[i]);
const dstPath = path.join(workDir, newPath);
await encodeSample(dstPath, srcPath);
samples[i] = newPath;
}
const outJsonPath = path.join(workDir, 'object.json');
await writeJsonFile(outJsonPath, root);
const parkobjPath = path.join('../out/object/official/audio', root.id + '.parkobj');
const contents = await getContents(workDir, {
includeDirectories: true,
includeFiles: true
});
await zip(workDir, parkobjPath, contents);
}
async function createAssetPack(dir) {
const workDir = 'temp';
await rmmkdir(workDir);
const root = await readJsonFile(path.join(dir, 'openrct2.sound.json'));
console.log(`Creating ${root.id}`);
for (const obj of root.objects) {
for (let i = 0; i < obj.samples.length; i++) {
const sample = obj.samples[i];
if (!sample.startsWith('$')) {
const newPath = changeExtension(sample, '.wav');
const srcPath = path.join(dir, sample);
const dstPath = path.join(workDir, newPath);
await encodeSample(dstPath, srcPath);
obj.samples[i] = newPath;
}
}
}
const outJsonPath = path.join(workDir, 'manifest.json');
await writeJsonFile(outJsonPath, root);
const parkapPath = path.join('../out/assetpack', root.id + '.parkap');
const contents = await getContents(workDir, {
includeDirectories: true,
includeFiles: true
});
await zip(workDir, parkapPath, contents);
}
function changeExtension(path, newExtension) {
const fullStopIndex = path.lastIndexOf('.');
if (fullStopIndex != -1) {
return path.substr(0, fullStopIndex) + newExtension;
}
return path + newExtension;
}
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();
}
});
});
}
async function zip(cwd, outputFile, paths) {
await ensureDirectoryExists(path.join(cwd, outputFile));
await rm(path.join(cwd, outputFile));
if (platform() == 'win32') {
await startProcess('7z', ['a', '-r', '-tzip', outputFile, ...paths], cwd);
} else {
await startProcess('zip', ['-r', outputFile, ...paths], cwd);
}
}
async function encodeSample(dstPath, srcPath) {
await ensureDirectoryExists(dstPath);
await startProcess(
'ffmpeg', [
'-i', srcPath,
'-acodec', 'pcm_s16le',
'-ar', '22050',
'-ac', '1',
'-map_metadata', '-1',
'-y',
dstPath
]);
}
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);
}
});
});
}
async function ensureDirectoryExists(filename) {
const dirname = path.dirname(filename);
await mkdir(dirname);
}
async function rmmkdir(path) {
await rm(path);
await mkdir(path);
}
function mkdir(path) {
return new Promise((resolve, reject) => {
fs.access(path, error => {
if (error) {
if (verbose) {
console.log(`Creating directory ${path}`);
}
fs.mkdir(path, { recursive: true }, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
resolve();
}
});
});
}
function getContents(root, options) {
return new Promise((resolve, reject) => {
const results = [];
let pending = 0;
const find = (root) => {
pending++;
fs.readdir(root, (err, fileNames) => {
for (const fileName of fileNames) {
const fullPath = path.join(root, fileName);
pending++;
fs.stat(fullPath, (err, stat) => {
if (stat) {
const result = options.useFullPath === true ? fullPath : fileName;
if (stat.isDirectory()) {
if (options.includeDirectories === true) {
results.push(result);
}
if (options.recurse === true) {
find(fullPath);
}
} else {
if (options.includeFiles === true) {
results.push(result);
}
}
}
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;
}