-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaystack.builder.js
85 lines (63 loc) · 2.05 KB
/
haystack.builder.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
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const MB02_1ST_SAFE_PG = 4;
const mode = '-t36o7o14';
const cityflyPath =
(n => `cityflyout/pg/cityfly.${('00' + n.toString(10)).substr(-3)}`);
const lzxName = (a => a.substr(0, a.lastIndexOf('.')) + mode + '.lzx');
const toHex = (num, width) => {
let a = num.toString(16);
return ('0000' + a).substr(-Math.max(width || 4, a.length)).toUpperCase();
}
const fd = fs.openSync('haystack', 'w');
const fx = fs.openSync('haystack.next', 'w');
let bin;
let a80 = ';; haystack block table (pointers and counts)\n\n';
let counts = [];
let pg = 16384; // page limit
let a, b, c, i, j, l;
const pages = [
...[...Array(20).keys()].map(cityflyPath),
'reglogo/gfx/stlpce.bin',
'transformy/pg/trafo.001',
'transformy/pg/trafo.002',
'transformy/pg/trafo.000',
'transformy/pg/trafo.003',
'transformy/pg/trafo.004',
'reglogo/gfx/squashy.pg0',
'reglogo/gfx/squashy.pg1'
];
for (i = 0, j = 0, l = 0, c = 0; i < pages.length; i++, c++) {
a = path.normalize(pages[i]);
bin = fs.readFileSync(a);
fs.writeSync(fx, bin, 0, 16384, i * 16384);
console.log(`~ compressing '${a}'...`);
spawnSync('lzxpack', [mode, a],
{ cwd: '.', shell: true, windowsHide: true });
a = lzxName(a);
bin = fs.readFileSync(a);
fs.unlinkSync(a);
b = j + bin.length;
if (b > pg) {
console.log(`~ splitting after ${c} pages (${pg - j} bytes excess)...`);
j = pg;
pg += 16384;
l += 16384;
counts.push(c);
c = 0;
}
fs.writeSync(fd, bin, 0, bin.length, j);
b = 0xC000 + (j - l);
a80 += `.pg${toHex(i + MB02_1ST_SAFE_PG, 2)}:\t dw #${toHex(b)} ; (${bin.length})\n`;
console.log(`~ compressed to ${bin.length} bytes (stored on #${toHex(b)})...`);
j += bin.length;
}
console.log(`~ finishig after ${c} pages (${pg - j} bytes excess)...`);
fs.writeSync(fd, Buffer.from([0]), 0, 1, pg - 1);
fs.closeSync(fd);
fs.closeSync(fx);
counts.push(c, 0);
a80 += `\n.firstct:\t equ ${counts.shift()}`;
a80 += `\n.counts:\t db ${counts.join(',')}\n`;
fs.writeFileSync('haystack.inc', a80, { flag: 'w' });