forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcrun.js
481 lines (455 loc) · 14.3 KB
/
mcrun.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright (c) 2016-2017 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { FILE } from "tool";
import { MakeFile, TSConfigFile, PrerequisiteFile, FormatFile, RotationFile, Tool } from "mcmanifest";
var formatStrings = {
gray16: "Gray16",
gray256: "Gray256",
rgb332: "RGB332",
rgb565le: "RGB565LE",
rgb565be: "RGB565BE",
clut16: "CLUT16",
x: "",
};
class CheckFile extends PrerequisiteFile {
generate(tool) {
this.line('/* WARNING: This file is automatically generated. Do not edit. */');
this.line("");
this.line('import config from "mc/config";');
this.line("");
this.line('export default function() {');
if (this.hasAssets(tool)) {
let format = formatStrings[tool.format];
let rotation = tool.rotation;
this.line('\tif (config.format != "', format, '")');
this.line('\t\tthrow new Error("incompatible assets: pixelformat: " + config.format + " instead of ', format, '");');
this.line('\tif (config.rotation != ', rotation, ')');
this.line('\t\tthrow new Error("incompatible assets: rotation: " + config.rotation + " instead of ', rotation, '");');
}
this.line('}');
this.line("");
this.close();
}
hasAssets(tool) {
if (tool.bmpAlphaFiles.length)
return true;
if (tool.bmpColorFiles.length)
return true;
if (tool.bmpFontFiles.length)
return true;
if (tool.bmpMaskFiles.length)
return true;
if (tool.imageFiles.length)
return true;
return false;
}
}
class ConfigFile extends PrerequisiteFile {
generate(tool) {
this.line('/* WARNING: This file is automatically generated. Do not edit. */');
this.line("");
this.line("export default {");
for (let key in tool.config) {
this.write("\t\"");
this.write(key);
this.write("\": ");
this.write(JSON.stringify(tool.config[key], null, "\t"));
this.line(",");
}
this.line("};");
this.close();
}
}
class ToDoFile extends FILE {
constructor(path) {
super(path)
}
generate(tool) {
let lines = [];
this.generateModulesRules(tool, lines);
this.generateDataRules(tool, lines);
this.generateResourcesRules(tool, lines);
this.generateArchiveRule(tool, lines);
let string = JSON.stringify(lines, null, "\t");
this.write(string);
this.close();
}
generateArchiveRule(tool, lines) {
let line = [];
line.push("xsa");
line.push("-b");
line.push(tool.modulesPath);
line.push("-n");
line.push(tool.environment.DOT_SIGNATURE);
line.push("-o");
line.push(tool.binPath);
for (var result of tool.jsFiles) {
line.push(tool.modulesPath + tool.slash + result.target);
}
for (var result of tool.resourcesFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.dataFiles) {
line.push(tool.dataPath + tool.slash + result.target);
}
for (var result of tool.bmpColorFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.bmpAlphaFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.bmpFontFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.bmpMaskFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.clutFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.imageFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.soundFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
for (var result of tool.stringFiles) {
line.push(tool.resourcesPath + tool.slash + result.target);
}
if (tool.stringFiles.length) {
line.push(tool.resourcesPath + tool.slash + tool.localsName + ".mhi");
}
lines.push(line);
}
generateDataRules(tool, lines) {
for (var result of tool.dataFiles) {
var source = result.source;
var target = tool.dataPath + tool.slash + result.target;
let line = ["cp"];
line.push(source, target);
lines.push(line);
}
}
generateModulesRules(tool, lines) {
for (var result of tool.jsFiles) {
var source = result.source;
var sourceParts = tool.splitPath(source);
var target = result.target;
var targetParts = tool.splitPath(target);
let targetDirectory = tool.modulesPath;
if (targetParts.directory)
targetDirectory += tool.slash + targetParts.directory;
let line = ["xsc"];
if (tool.debug)
line.push("-d");
line.push("-e", source, "-o", targetDirectory, "-r", targetParts.name);
lines.push(line);
}
}
generateResourcesRules(tool, lines) {
for (var result of tool.resourcesFiles) {
var source = result.source;
var target = tool.resourcesPath + tool.slash + result.target;
let line = ["cp"];
if (tool.isDirectoryOrFile(source) < 0)
line.push("-r");
line.push(source, target);
lines.push(line);
}
if (tool.clutFiles) {
for (var result of tool.clutFiles) {
var source = result.source;
var target = result.target;
lines.push(["buildclut", source, "-o", tool.resourcesPath]);
}
}
for (var result of tool.bmpAlphaFiles) {
if (result.colorFile)
continue
var parts;
var source = result.source;
var target = result.target;
parts = tool.splitPath(target);
let line = ["png2bmp"];
line.push(source);
var sources = result.sources;
if (sources) {
for (var path of sources)
line.push(path);
}
line.push("-a");
if (result.monochrome)
line.push("-m", "-4");
line.push("-o", tool.resourcesPath, "-r", tool.rotation.toString());
line.push("-n", parts.name.slice(0, -6));
lines.push(line);
}
for (var result of tool.bmpColorFiles) {
var parts;
var source = result.source;
var target = result.target;
parts = tool.splitPath(target);
var alphaTarget = result.alphaFile ? result.alphaFile.target : null;
var clutSource = result.clutName ? tool.resourcesPath + tool.slash + result.clutName + ".cct" : null;
var sources = result.sources;
let line = ["png2bmp"];
line.push(source);
if (sources) {
for (var path of sources)
line.push(path);
}
if (!alphaTarget)
line.push("-c");
if (result.monochrome)
line.push("-m", "-4");
else {
if (result.format)
this.write(result.format);
else
line.push("-f", tool.format);
if (clutSource)
line.push("-clut", clutSource);
}
line.push("-o", tool.resourcesPath, "-r", tool.rotation.toString());
line.push("-n", parts.name.slice(0, -6));
lines.push(line);
}
for (var result of tool.bmpFontFiles) {
var parts;
var source = result.source;
parts = tool.splitPath(source);
parts.extension = ".png";
var pngSource = tool.joinPath(parts);
var target = result.target;
parts = tool.splitPath(target);
var bmpTarget = parts.name + "-alpha.bmp";
var bmpSource = tool.resourcesPath + tool.slash + bmpTarget;
let line = ["png2bmp"];
line.push(pngSource, "-a", "-o", tool.resourcesPath, "-r", tool.rotation.toString(), "-t");
lines.push(line);
line = ["compressbmf"];
line.push(source, "-i", bmpSource, "-o", tool.resourcesPath, "-r", tool.rotation.toString());
lines.push(line);
}
for (var result of tool.bmpMaskFiles) {
var parts;
var source = result.source;
var target = result.target;
parts = tool.splitPath(target);
var bmpTarget = parts.name + ".bmp";
var bmpSource = tool.resourcesPath + tool.slash + bmpTarget;
var sources = result.sources;
let line = ["png2bmp"];
line.push(source);
if (sources) {
for (var path of sources)
line.push(path);
}
line.push("-a", "-o", tool.resourcesPath, "-r", tool.rotation.toString(), "-t");
line.push("-n", parts.name.slice(0, -6));
lines.push(line);
lines.push(["rle4encode", bmpSource, "-o", tool.resourcesPath]);
}
for (var result of tool.imageFiles) {
var source = result.source;
var target = result.target;
if (result.quality !== undefined) {
var temporary = target + result.quality;
lines.push(["image2cs", source, "-o", tool.resourcesPath, "-q", result.quality, "-r", tool.rotation.toString()]);
lines.push(["cp", tool.resourcesPath + tool.slash + temporary, tool.resourcesPath + tool.slash + result.target]);
}
else {
lines.push(["image2cs", source, "-o", tool.resourcesPath, "-r", tool.rotation.toString()]);
}
}
let bitsPerSample = 16, numChannels = 1, sampleRate = 11025, audioFormat = "uncompressed";
let defines = tool.defines;
if (defines) {
let audioOut = defines.audioOut;
if (audioOut) {
if ("bitsPerSample" in audioOut) bitsPerSample = audioOut.bitsPerSample;
if ("numChannels" in audioOut) numChannels = audioOut.numChannels;
if ("sampleRate" in audioOut) sampleRate = audioOut.sampleRate;
if ("format" in audioOut) audioFormat = audioOut.format;
}
}
for (var result of tool.soundFiles) {
var source = result.source;
var target = result.target;
lines.push(["wav2maud", source, "-o", tool.resourcesPath, "-r", sampleRate.toString(), "-c", numChannels.toString(), "-s", bitsPerSample.toString(), "-f", audioFormat]);
}
if (tool.stringFiles.length) {
let line = ["mclocal"];
for (var result of tool.stringFiles)
line.push(result.source);
if (!defines || !defines.locals || !defines.locals.all)
line.push("-d");
if (tool.format)
line.push("-s");
line.push("-o", tool.resourcesPath);
line.push("-r", tool.localsName);
lines.push(line);
}
}
}
export default class extends Tool {
constructor(argv) {
super(argv);
if (this.platform == "wasm") {
this.fragmentPath = null;
}
else {
var path = this.moddablePath + this.slash + "tools" + this.slash + "mcrun" + this.slash;
if (this.windows)
path += "nmake.";
else
path += "make.";
path += this.platform + ".mk";
path = this.resolveFilePath(path);
if (!path)
throw new Error("unknown platform!");
this.fragmentPath = path;
}
}
createDirectories(path, first, last) {
this.createDirectory(path);
path += this.slash + first;
this.createDirectory(path);
var platform = this.platform;
path += this.slash + platform;
this.createDirectory(path);
if ((platform == "mac") || (platform == "win") || (platform == "lin")) {
path += this.slash + "mc";
this.createDirectory(path);
}
if (this.debug)
path += this.slash + "debug";
else
path += this.slash + "release";
this.createDirectory(path);
path += this.slash + last;
this.createDirectory(path);
return path;
}
filterRun(archive) {
var jsFiles = [];
var tsFiles = [];
for (var name in archive) {
var target = archive[name] + ".xsb";
var result = this.jsFiles.find(result => result.target == target);
if (result) {
result.target = name + ".xsb";
jsFiles.push(result);
continue;
}
var result = this.tsFiles.find(result => result.target == target);
if (result) {
result.target = name + ".xsb";
tsFiles.push(result);
continue;
}
tool.reportError(null, 0, "run module not found: " + archive[name]);
}
if (jsFiles.length || tsFiles.length) {
this.jsFiles = jsFiles;
this.tsFiles = tsFiles;
}
}
run() {
this.localsName = "modLocals";
super.run();
if ("URL" in this.config)
this.environment.URL = this.config.URL;
if (this.manifest.config) {
const commandLine = this.config;
this.config = {...this.manifest.config};
this.mergeProperties(this.config, commandLine);
}
this.filterRun(this.manifest.run);
this.creation = null;
this.defines = null;
this.preloads = null;
this.recipes = null;
this.strip = null;
this.tsconfig = this.manifest.tsconfig;
if (this.rotation === undefined)
this.rotation = this.config.rotation ?? 0;
if (this.format === "UNDEFINED")
this.format = (this.config.format ?? "rgb565le").toLowerCase();
var name = this.environment.NAME
this.binPath = this.createDirectories(this.outputPath, "bin", name);
this.tmpPath = this.createDirectories(this.outputPath, "tmp", name);
var path = this.tmpPath + this.slash + "files";
this.dataPath = this.modulesPath = this.resourcesPath = path;
this.createDirectory(path);
for (var folder of this.dataFolders)
this.createFolder(path, folder);
for (var folder of this.jsFolders)
this.createFolder(path, folder);
for (var folder of this.resourcesFolders)
this.createFolder(path, folder);
var file;
if (this.format) {
var target = "check.xsb";
if (!this.jsFiles.find(file => file.target == target)) {
var source = this.tmpPath + this.slash + "check.js";
file = new CheckFile(source, this);
file.generate(this);
this.jsFiles.push({ source, target });
}
if (this.config) {
this.createDirectory(this.modulesPath + this.slash + "mod");
let source = this.tmpPath + this.slash + "mod.config.js";
file = new ConfigFile(source, this);
file.generate(this);
this.jsFiles.push({ source, target: "mod" + this.slash + "config.xsb" });
}
if (this.fragmentPath) {
file = new FormatFile(this.tmpPath + this.slash + "mc.format.h", this);
file.generate(this);
file = new RotationFile(this.tmpPath + this.slash + "mc.rotation.h", this);
file.generate(this);
}
}
if (this.fragmentPath) {
if (this.tsFiles.length) {
file = new TSConfigFile(this.modulesPath + this.slash + "tsconfig.json");
file.generate(this);
}
var path = this.tmpPath + this.slash + "makefile";
file = new MakeFile(path);
file.generate(this);
if (this.make) {
if (this.windows)
this.then("nmake", "/nologo", "/f", path);
else
this.then("make", "-f", path);
}
}
else {
if (this.tsFiles.length) {
throw new Error("TypeScript unsupported!");
}
var path = this.tmpPath + this.slash + "make.json";
file = new ToDoFile(path);
file.generate(this);
}
}
}