forked from github/game-off-2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-maps.js
64 lines (54 loc) · 1.92 KB
/
process-maps.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
var through = require("through2");
var PluginError = require("gulp-util").PluginError;
var PLUGIN_NAME = "process-maps";
module.exports = function () {
return through.obj(function (file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
this.emit("error", new PluginError(PLUGIN_NAME, "Streams not supported!"));
} else if (file.isBuffer()) {
// use a cursor to process the file
var c = 0;
var lines = file.contents.toString().split("\n");
var sizes = lines[c].split(",");
c++;
var numRows = parseInt(sizes[0]);
var numCols = parseInt(sizes[1]);
var mapObj = {};
var layerIndex = 0;
// is an array of arrays to store layers
mapObj.layers = [];
while (lines[c].length === numCols) { // scary, I know
mapObj.layers.push([]);
var lastRow = c + numRows;
for (; c < lastRow; c++) {
var row = [];
for (var i = 0; i < numCols; i++) {
row.push(lines[c][i]);
}
mapObj.layers[layerIndex].push(row);
}
layerIndex++;
}
// advance cursor until we get to the METADATA section
while(lines[c].indexOf("METADATA") < 0) { c++; }
c++; // once more to skip over the METADATA header
// process the rest of the METADATA section directly into mapObj
mapObj.metadata = {};
for(; c < lines.length; c++) {
// SO shoutout for greedy operator use: http://stackoverflow.com/a/4607799/1159255
var keyVal = lines[c].split(/: (.+)/);
if (keyVal[0] === "link_items") {
mapObj.metadata[keyVal[0]] = JSON.parse(keyVal[1]);
} else {
mapObj.metadata[keyVal[0]] = keyVal[1];
}
}
file.contents = new Buffer(JSON.stringify(mapObj));
file.path = file.path.replace(/map(\d+)\.txt$/, "$1");
return callback(null, file);
}
});
};