This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
exportmap.js
executable file
·62 lines (51 loc) · 1.94 KB
/
exportmap.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
#!/usr/bin/env node
var util = require('util'),
Log = require('log'),
path = require("path"),
fs = require("fs"),
processMap = require('./processmap'),
log = new Log(Log.DEBUG);
var source = process.argv[2],
destination = process.argv[3],
mode = process.argv[4];
if(!source || !destination) {
util.puts("Usage : ./exportmap.js map_file json_file [mode]");
util.puts("Optional parameter : mode. Values: \"server\" (default) or \"client\".");
process.exit(0);
}
function main() {
getTiledJSONmap(source, function(json) {
var options = { mode: mode || "server" },
map = processMap(json, options);
var jsonMap = JSON.stringify(map); // Save the processed map object as JSON data
if(mode === "client") {
// map in a .json file for ajax loading
fs.writeFile(destination+".json", jsonMap, function(err, file) {
log.info("Finished processing map file: "+ destination + ".json was saved.");
});
// map in a .js file for web worker loading
jsonMap = "var mapData = "+JSON.stringify(map);
fs.writeFile(destination+".js", jsonMap, function(err, file) {
log.info("Finished processing map file: "+ destination + ".js was saved.");
});
} else {
fs.writeFile(destination, jsonMap, function(err, file) {
log.info("Finished processing map file: "+ destination + " was saved.");
});
}
});
}
// Loads the temporary JSON Tiled map converted by tmx2json.py
function getTiledJSONmap(filename, callback) {
var self = this;
path.exists(filename, function(exists) {
if(!exists) {
log.error(filename + " doesn't exist.")
return;
}
fs.readFile(source, function(err, file) {
callback(JSON.parse(file.toString()));
});
});
}
main();