Skip to content

Instantly share code, notes, and snippets.

@IanSSenne
Created May 27, 2020 02:36
Show Gist options
  • Save IanSSenne/236dffb39e4e9fb5964d991f98d38b7b to your computer and use it in GitHub Desktop.
Save IanSSenne/236dffb39e4e9fb5964d991f98d38b7b to your computer and use it in GitHub Desktop.
const out = { positions: {}, blocks: {} };
const fs = require("fs");
const xml2js = require("xml2js");
function toObject(o) {
let res = {};
Object.entries(o).forEach(([name, value]) => {
if (name === "$") {
res = { ...res, ...value };
return;
}
const [val] = value;
if (typeof val === "object") {
res[name.toLowerCase()] = toObject(val);
} else {
res[name.toLowerCase()] = val;
}
});
return res;
}
fs.readdirSync("./input").forEach(file => {
const xmlString = fs.readFileSync("./input/" + file, 'utf8');
xml2js.parseString(xmlString, (err, xml) => {
const Blocks = xml.Definitions.CubeBlocks;
const Positions = xml.Definitions.BlockPositions;
if (Positions) {
Positions.forEach(item => {
item.BlockPosition.forEach(pos => {
const v = { x: pos.Position[0].X[0], y: pos.Position[0].Y[0] };
out.positions[pos.Name[0]] = { position: v };
});
});
}
if (Blocks) {
Blocks[0].Definition.forEach(block => {
if (block.BlockPairName) {
const name = block.BlockPairName[0];
out.blocks[name] = toObject(block);
}
});
}
});
});
fs.writeFileSync("./output/dataset.json", JSON.stringify(out));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment