Created
May 27, 2020 02:36
-
-
Save IanSSenne/236dffb39e4e9fb5964d991f98d38b7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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