-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsx2json.js
46 lines (39 loc) · 1.29 KB
/
tsx2json.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
var fs = require('fs');
var parse = require('xml-parser');
var inspect = require('util').inspect;
var xml = fs.readFileSync('tiled/tileset.tsx', 'utf8');
function filterTiles (children) {
var tileArray = [];
for (var i=0; i<children.length; i++) {
var child = children[i];
if (child.name === 'tile') {
var tile = {
id: parseInt(child.attributes.id),
type: child.attributes.type
};
if (child.children.length > 0) {
var properties = child.children[0];
for (var j=0; j<properties.children.length; j++) {
var prop = properties.children[j];
tile[prop.attributes.name] = prop.attributes.value;
}
}
tileArray.push(tile);
}
}
return tileArray;
}
var obj = parse(xml);
// console.log(inspect(obj.root.children, { colors: true, depth: Infinity }));
var tiles = filterTiles(obj.root.children);
var outputPath = 'static/data/tileset.json';
var outputObject = {
'$schema': './tileset-schema.json',
'tiles': tiles
};
fs.writeFile(outputPath, JSON.stringify(outputObject), function (err) {
if (err) {
return console.log(err);
}
console.log("Saved tilemap to " + outputPath);
});