-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtako80.js
executable file
·229 lines (186 loc) · 7.43 KB
/
tako80.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
#!/usr/bin/env node
const fs = require('fs');
const ncp = require('ncp');
const path = require('path');
const http = require('http');
const static = require('node-static');
const png = require('pngjs').PNG;
const npmview = require('npmview');
const semver = require('semver');
const PROJECT_TEMPLATE_DIR = 'project-template';
const PROJECT_EXAMPLE_DIR = 'example';
const CART_TEMPLATE = 'cart.png';
const TAKO_MAIN_FILE = 'tako80.min.js';
const LABEL_WIDTH = 100;
const LABEL_HEIGHT = 80;
function printHelp () {
console.log('Usage:');
console.log('\ttako80 new PROJECT_NAME');
console.log('\ttako80 devel');
console.log('\ttako80 compile');
console.log('\ttako80 example');
}
npmview('tako80', function(err, version, moduleInfo) {
if (!err) {
const currentVersion = require(path.join(__dirname, 'package.json')).version;
if(semver.gt(version, currentVersion)) {
console.log(`\nA new version of tako80 (${ version }) is available. Please run:\n\tsudo npm update -g tako80\n`);
}
}
});
if (['new', 'compile', 'devel', 'example', 'update'].indexOf(process.argv[2]) === -1
|| process.argv.length <= 2) {
printHelp();
}
if (process.argv[2] === 'new') {
if (process.argv.length === 3) {
console.log('Usage: tako80 new PROJECT_NAME');
process.exit(0);
}
const projectName = process.argv[3];
ncp(path.join(__dirname, PROJECT_TEMPLATE_DIR), projectName);
ncp(path.join(__dirname, TAKO_MAIN_FILE), path.join(projectName, TAKO_MAIN_FILE));
}
if (process.argv[2] === 'update') {
ncp(path.join(__dirname, TAKO_MAIN_FILE), TAKO_MAIN_FILE);
}
if (process.argv[2] === 'example') {
ncp(path.join(__dirname, PROJECT_TEMPLATE_DIR), 'tako80example', () => {
ncp(path.join(__dirname, PROJECT_EXAMPLE_DIR), 'tako80example');
});
ncp(path.join(__dirname, TAKO_MAIN_FILE), path.join('tako80example', TAKO_MAIN_FILE));
}
function readCartJs () {
let cartJs = fs.readFileSync('cart.js').toString();
const assets = JSON.parse(fs.readFileSync('assets.json').toString());
if (assets.sources) {
for (let source of assets.sources) {
if (source === 'cart.js') continue;
cartJs = `${ fs.readFileSync(source).toString() }\n\n${ cartJs }`;
}
}
return cartJs;
}
if (process.argv[2] === 'devel') {
let port = 3000;
if (!isNaN(process.argv[3])) {
port = parseInt(process.argv[3]);
}
// TODO: check if it doesn't seem like a tako80 project directory
// if (process.argv.length === 3) {
// console.log('Usage: tako80 new PROJECT_NAME');
// process.exit(0);
// }
const file = new static.Server('.');
const server = http.createServer(function (req, res) {
if (req.url === '/') {
let htmlFile = fs.readFileSync('index.html').toString();
const assets = fs.readFileSync('assets.json').toString();
try {
JSON.parse(assets);
} catch (e) {
console.log('Invalid asset file');
}
htmlFile = htmlFile.replace('ASSETS_JSON', assets);
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(htmlFile);
} else if (req.url === '/compile') {
console.log('Compiled cart');
compile();
} else if (req.url === '/cart.js') {
const cartJs = readCartJs();
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(cartJs);
} else {
file.serve(req, res);
}
});
server.listen(port, '127.0.0.1');
console.log(`Tako 80 dev server running at http://127.0.0.1:${ port }/`);
}
if (process.argv[2] === 'compile') {
compile();
}
function compile () {
const data = []
const separatorBuffer = Buffer.from('***TAKO80-SEPARATOR***');
const separatorBufferSub = Buffer.from('***TAKO80-SUB-SEPARATOR***');
if (fs.existsSync('dist/cart.png')) {
fs.unlinkSync('dist/cart.png');
}
function packCart () {
data.push(fs.readFileSync('dist/cart.png'));
const cartJs = readCartJs();
let cartData = Buffer.from(cartJs);
data.push(cartData);
data.push(separatorBuffer);
const assets = JSON.parse(fs.readFileSync('assets.json'));
for (let img in assets.images) {
let imgData = new Buffer(img.padEnd(100))
imgData = Buffer.concat([imgData, fs.readFileSync(assets.images[img])]);
data.push(imgData);
data.push(separatorBufferSub);
}
data.push(separatorBuffer);
for (let sfx in assets.sfx || {}) {
let sfxData = new Buffer(sfx.padEnd(100))
sfxData = Buffer.concat([sfxData, fs.readFileSync(assets.sfx[sfx])]);
data.push(sfxData);
data.push(separatorBufferSub);
}
data.push(separatorBuffer);
for (let mod in assets.mods || {}) {
let modData = new Buffer(mod.padEnd(100))
modData = Buffer.concat([modData, fs.readFileSync(assets.mods[mod])]);
data.push(modData);
data.push(separatorBufferSub);
}
data.push(separatorBuffer);
for (let map in assets.maps || {}) {
let mapData = new Buffer(map.padEnd(100))
mapData = Buffer.concat([mapData, fs.readFileSync(assets.maps[map])]);
data.push(mapData);
data.push(separatorBufferSub);
}
data.push(separatorBuffer);
fs.writeFileSync('dist/cart.png', Buffer.concat(data));
ncp(path.join(__dirname, TAKO_MAIN_FILE), path.join('dist/', TAKO_MAIN_FILE));
}
ncp(path.join(__dirname, 'src', CART_TEMPLATE), 'dist/cart.png', (err) => {
// label
if (fs.existsSync('label.png')) {
const cartData = fs.readFileSync('dist/cart.png');
const labelData = fs.readFileSync('label.png');
new png({filterType: 4}).parse(cartData)
.on('parsed', function () {
const cart = this;
new png({filterType: 4}).parse(labelData)
.on('parsed', function () {
const label = this;
if (label.width !== LABEL_WIDTH || label.height !== LABEL_HEIGHT) {
console.log(`Label found, but the size is wrong. It must be ${ LABEL_WIDTH } x ${ LABEL_HEIGHT }`);
process.exit(0);
}
for (let y = 0; y < label.height; y++) {
for (let x = 0; x < label.width; x++) {
const labelIdx = (label.width * y + x) << 2;
const cartX = x + 23;
const cartY = y + 28;
const cartIdx = (cart.width * cartY + cartX) << 2;
// invert color
cart.data[cartIdx] = label.data[labelIdx];
cart.data[cartIdx+1] = label.data[labelIdx+1];
cart.data[cartIdx+2] = label.data[labelIdx+2];
cart.data[cartIdx+3] = label.data[labelIdx+3];
}
}
const ws = fs.createWriteStream('dist/cart.png');
ws.on('finish', packCart);
cart.pack().pipe(ws);
});
});
} else {
packCart();
}
});
}