diff --git a/README.md b/README.md index acd9a36..e00bf29 100644 --- a/README.md +++ b/README.md @@ -6,41 +6,51 @@ Converts Arx Fatalis level data to JSON, YAML, BSON and vice versa. `npm i arx-level-json-converter -g` -This will give you access to the following commands: +This will give you access to the following commands, both do the same: -- `to-json` -- `from-json` -- `to-yaml` -- `from-yaml` -- `to-bson` -- `from-bson` -- `combine` -- `separate` +- `arx-level-json-converter` +- `arx-convert` -## examples +### format of the commands + +`arx-convert --from= --to= --output=` -`--version` or `-v` will give you the version for any tool +the inputfile and --output parameters can be omitted and then the code can be used in pipelines -`cat fast.fts | to-json --ext=fts --pretty > fast.fts.json` +`cat | arx-convert --from= --to= > ` -`to-json level8.dlf --output=level8.dlf.min.json` +the format parameter can be one of the following arx formats: `dlf`, `llf`, `fts` and `ftl`(WIP) -`cat level8.dlf.min.json | from-json --ext=dlf > level8.dlf.repacked` +and it can also a data format for the other side: `json`, `bson` and `yaml`(can also be spelled as `yml`) + +## examples -`from-json fast.fts.json --ext=fts --output=fast.fts.repacked` +```sh +# print out version +--version +-v -`cat level8.dlf.unpacked | to-yaml --ext=dlf > level8.dlf.yml` +# convert and fts file to a json through piping +cat fast.fts | arx-convert --from=fts --to=json --pretty > fast.fts.json -`cat level8.dlf.yml | from-yaml --ext=dlf > level8.dlf.unpacked` +# convert a dlf file to a minified json through files +arx-convert level8.dlf --from=dlf --to=json --output=level8.dlf.min.json -`cat level8.dlf.unpacked | to-bson --ext=dlf > level8.dlf.bson` +# convert a json to a dlf +cat level8.dlf.min.json | arx-convert --from=json --to=dlf > level8.dlf.repacked -`cat level8.dlf.bson | from-bson --ext=dlf > level8.dlf.unpacked` +# convert json to fts +arx-convert fast.fts.json --from=json --to=fts --output=fast.fts.repacked -`combine fast.fts.json level8.llf.json level8.dlf.json --pretty --output=level8.json` +# convert dlf to yaml +cat level8.dlf.unpacked | arx-convert --to=yaml --from=dlf > level8.dlf.yml -`combine fast.fts.json level8.llf.json level8.dlf.json --pretty > level8.json` +# convert yaml to dlf +cat level8.dlf.yml | arx-convert --from=yaml --to=dlf > level8.dlf.unpacked -`separate level8.json --pretty --llf=level8.llf.json --dlf=level8.dlf.json --fts=fast.fts.json` +# convert dlf to bson +cat level8.dlf.unpacked | arx-convert --to=bson --from=dlf > level8.dlf.bson -`cat level8.json | separate --pretty --llf=level8.llf.json --dlf=level8.dlf.json --fts=fast.fts.json` +# convert bson to dlf +cat level8.dlf.bson | arx-convert --from=bson --to=dlf > level8.dlf.unpacked +``` diff --git a/bin/combine.js b/bin/combine.js deleted file mode 100755 index 1587607..0000000 --- a/bin/combine.js +++ /dev/null @@ -1,170 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); - -const { - fileExists, - getPackageVersion, - streamToBuffer, - stringifyJSON, - outputInChunks, -} = require("./helpers.js"); -const { isZeroVertex } = require("../src/common/helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output"], - boolean: ["version", "pretty"], - alias: { - v: "version", - p: "pretty", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename1 = args._[0]; - let filename2 = args._[1]; - let filename3 = args._[2]; - let output = args.output; - - let hasErrors = false; - - let input1; - if (await fileExists(filename1)) { - input1 = fs.createReadStream(filename1); - } else { - console.error("error: 1st input file does not exist"); - hasErrors = true; - } - - let input2; - if (await fileExists(filename2)) { - input2 = fs.createReadStream(filename2); - } else { - console.error("error: 2nd input file does not exist"); - hasErrors = true; - } - - let input3; - if (await fileExists(filename3)) { - input3 = fs.createReadStream(filename3); - } else { - console.error("error: 2nd input file does not exist"); - hasErrors = true; - } - - const source = { - dlf: null, - llf: null, - fts: null, - }; - - let json = JSON.parse(await streamToBuffer(input1)); - let extension = json.meta.type; - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type for input file #1, expected "dlf", "fts" or "llf" ' - ); - hasErrors = true; - } else { - source[extension] = json; - } - - json = JSON.parse(await streamToBuffer(input2)); - extension = json.meta.type; - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type for input file #2, expected "dlf", "fts" or "llf" ' - ); - hasErrors = true; - } else { - source[extension] = json; - } - - json = JSON.parse(await streamToBuffer(input3)); - extension = json.meta.type; - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type for input file #3, expected "dlf", "fts" or "llf" ' - ); - hasErrors = true; - } else { - source[extension] = json; - } - - if (source.dlf === null) { - console.error("error: missing dlf data"); - hasErrors = true; - } - if (source.fts === null) { - console.error("error: missing fts data"); - hasErrors = true; - } - if (source.llf === null) { - console.error("error: missing llf data"); - hasErrors = true; - } - - if (hasErrors) { - process.exit(1); - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - const name = source.dlf.scene.name; - - const { dlf, fts, llf } = source; - - delete dlf.meta; - delete dlf.scene; - delete dlf.header.numberOfBackgroundPolygons; - - delete fts.meta; - delete fts.header.path; - - delete llf.meta; - delete llf.header.numberOfBackgroundPolygons; - - const data = { - meta: { - type: "combined", - }, - header: { - name, - }, - dlf, - fts, - llf, - }; - - // TODO: when lighting generation is implemented, then we don't need to deal with colors at all - data.fts.polygons = data.fts.polygons.map((polygon) => { - polygon.vertices = polygon.vertices.map((vertex) => { - const color = - isZeroVertex(vertex) || typeof vertex.llfColorIdx === "undefined" - ? null - : data.llf.colors[vertex.llfColorIdx]; - vertex.color = color; - delete vertex.llfColorIdx; - return vertex; - }); - return polygon; - }); - - delete data.llf.colors; - - outputInChunks(stringifyJSON(data, args.pretty), output); -})(); diff --git a/bin/constants.js b/bin/constants.js index 781841c..b8b1d22 100644 --- a/bin/constants.js +++ b/bin/constants.js @@ -1,5 +1,7 @@ -const SUPPORTED_EXTENSIONS = ["dlf", "fts", "llf", "ftl"]; +const SUPPORTED_ARX_FORMATS = ["dlf", "fts", "llf", "ftl"]; +const SUPPORTED_DATA_FORMATS = ["json", "yaml", "yml", "bson"]; module.exports = { - SUPPORTED_EXTENSIONS, + SUPPORTED_ARX_FORMATS, + SUPPORTED_DATA_FORMATS, }; diff --git a/bin/convert.js b/bin/convert.js new file mode 100755 index 0000000..67f7c2c --- /dev/null +++ b/bin/convert.js @@ -0,0 +1,123 @@ +#!/usr/bin/env node + +const fs = require("fs"); +const minimist = require("minimist-lite"); +const BSON = require("bson"); +const YAML = require("yaml"); +const { + fileExists, + getPackageVersion, + streamToBuffer, + stringifyYAML, + stringifyJSON, + stringifyBSON, + outputInChunks, + validateFromToPair, +} = require("./helpers.js"); +const { DLF, FTS, LLF, FTL } = require("../src/index.js"); + +// --------------------------- + +const args = minimist(process.argv.slice(2), { + string: ["output", "from", "to"], + boolean: ["version", "pretty"], + alias: { + v: "version", + p: "pretty", + }, +}); + +if (args.version) { + console.log(getPackageVersion()); + process.exit(0); +} + +let filename = args._[0]; +let output = args.output; +let hasErrors = false; + +(async () => { + let input; + if (filename) { + if (await fileExists(filename)) { + input = fs.createReadStream(filename); + } else { + console.error("error: input file does not exist"); + hasErrors = true; + } + } else { + input = process.openStdin(); + } + + try { + validateFromToPair(args.from, args.to); + } catch (e) { + console.error(`error: ${e.message}`); + hasErrors = true; + } + + if (output) { + output = fs.createWriteStream(output); + } else { + output = process.stdout; + } + + if (hasErrors) { + process.exit(1); + } + + const rawIn = await streamToBuffer(input); + let parsedIn; + switch (args.from) { + case "json": + parsedIn = JSON.parse(rawIn); + break; + case "yml": + case "yaml": + parsedIn = YAML.parse(rawIn.toString()); + break; + case "bson": + parsedIn = BSON.deserialize(rawIn); + break; + case "dlf": + parsedIn = DLF.load(rawIn); + break; + case "fts": + parsedIn = FTS.load(rawIn); + break; + case "llf": + parsedIn = LLF.load(rawIn); + break; + case "ftl": + parsedIn = FTL.load(rawIn); + break; + } + + let rawOut; + switch (args.to) { + case "json": + rawOut = stringifyJSON(parsedIn, args.pretty); + break; + case "yml": + case "yaml": + rawOut = stringifyYAML(parsedIn); + break; + case "bson": + rawOut = stringifyBSON(parsedIn); + break; + case "dlf": + rawOut = DLF.save(parsedIn); + break; + case "fts": + rawOut = FTS.save(parsedIn); + break; + case "llf": + rawOut = LLF.save(parsedIn); + break; + case "ftl": + rawOut = FTL.save(parsedIn); + break; + } + + outputInChunks(rawOut, output); +})(); diff --git a/bin/from-bson.js b/bin/from-bson.js deleted file mode 100755 index 26913ff..0000000 --- a/bin/from-bson.js +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const BSON = require("bson"); -const { DLF, FTS, LLF, FTL } = require("../src/index.js"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - outputInChunks, -} = require("./helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output"], - boolean: ["version"], - alias: { - v: "version", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - let output = args.output; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - const json = BSON.deserialize(await streamToBuffer(input)); - const extension = json.meta.type; - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type, expected "dlf", "fts" or "llf"' - ); - hasErrors = true; - } - - if (hasErrors) { - process.exit(1); - } - - let binary; - switch (extension) { - case "fts": - binary = FTS.save(json); - break; - case "dlf": - binary = DLF.save(json); - break; - case "llf": - binary = LLF.save(json); - break; - case "ftl": - binary = FTL.save(json); - break; - } - - outputInChunks(binary, output); -})(); diff --git a/bin/from-json.js b/bin/from-json.js deleted file mode 100755 index 2d4756f..0000000 --- a/bin/from-json.js +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const { DLF, FTS, LLF, FTL } = require("../src/index.js"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - outputInChunks, -} = require("./helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output"], - boolean: ["version"], - alias: { - v: "version", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - let output = args.output; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - const json = JSON.parse(await streamToBuffer(input)); - const extension = json.meta.type; - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type, expected "dlf", "fts", "llf" or "ftl"' - ); - hasErrors = true; - } - - if (hasErrors) { - process.exit(1); - } - - let binary; - switch (extension) { - case "fts": - binary = FTS.save(json); - break; - case "dlf": - binary = DLF.save(json); - break; - case "llf": - binary = LLF.save(json); - break; - case "ftl": - binary = FTL.save(json); - break; - } - - outputInChunks(binary, output); -})(); diff --git a/bin/from-yaml.js b/bin/from-yaml.js deleted file mode 100755 index 2959275..0000000 --- a/bin/from-yaml.js +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const YAML = require("yaml"); -const { DLF, FTS, LLF, FTL } = require("../src/index.js"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - outputInChunks, -} = require("./helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output"], - boolean: ["version"], - alias: { - v: "version", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - let output = args.output; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - const raw = await streamToBuffer(input); - const json = YAML.parse(raw.toString()); - const extension = json.meta.type; - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type, expected "dlf", "fts", "llf" or "ftl"' - ); - hasErrors = true; - } - - if (hasErrors) { - process.exit(1); - } - - let binary; - switch (extension) { - case "fts": - binary = FTS.save(json); - break; - case "dlf": - binary = DLF.save(json); - break; - case "llf": - binary = LLF.save(json); - break; - case "ftl": - binary = FTL.save(json); - break; - } - - outputInChunks(binary, output); -})(); diff --git a/bin/helpers.js b/bin/helpers.js index fe3f30d..2cf901c 100644 --- a/bin/helpers.js +++ b/bin/helpers.js @@ -1,4 +1,8 @@ const fs = require("fs"); +const { + SUPPORTED_ARX_FORMATS, + SUPPORTED_DATA_FORMATS, +} = require("./constants"); const fileExists = async (filename) => { try { @@ -59,6 +63,48 @@ const outputInChunks = (buffer, stream) => { stream.end(); }; +const validTypes = [...SUPPORTED_ARX_FORMATS, ...SUPPORTED_DATA_FORMATS]; + +const validateFromToPair = (from, to) => { + if (typeof from === "undefined" || from === "") { + throw new Error('"from" argument is missing or empty'); + } + if (typeof to === "undefined" || to === "") { + throw new Error('"to" argument is missing or empty'); + } + + if (!validTypes.includes(from)) { + throw new Error(`unknown format '${from}' in "from"`); + } + + if (!validTypes.includes(to)) { + throw new Error(`unknown format '${to}' in "to"`); + } + + if (from === to) { + throw new Error('"from" and "to" have the same format'); + } + + // if "from" is sourcetype then "to" is targettype and vice-versa? (sourcetype=fts,dlf,...; targettype=json,bson,...) + if ( + SUPPORTED_ARX_FORMATS.includes(from) && + SUPPORTED_ARX_FORMATS.includes(to) + ) { + throw new Error( + '"from" and "to" are both referencing arx formats, expected one of them to be a data type' + ); + } + + if ( + SUPPORTED_DATA_FORMATS.includes(from) && + SUPPORTED_DATA_FORMATS.includes(to) + ) { + throw new Error( + '"from" and "to" are both referencing data types, expected one of them to be an arx format' + ); + } +}; + module.exports = { fileExists, getPackageVersion, @@ -67,4 +113,5 @@ module.exports = { stringifyYAML, stringifyBSON, outputInChunks, + validateFromToPair, }; diff --git a/bin/separate.js b/bin/separate.js deleted file mode 100755 index ae6fc56..0000000 --- a/bin/separate.js +++ /dev/null @@ -1,127 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - stringifyJSON, - outputInChunks, -} = require("./helpers.js"); -const { isZeroVertex } = require("../src/common/helpers.js"); -const FTS = require("../src/fts/FTS.js"); - -const args = minimist(process.argv.slice(2), { - string: ["dlf", "llf", "fts"], - boolean: ["version", "pretty"], - alias: { - v: "version", - p: "pretty", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - const json = JSON.parse(await streamToBuffer(input)); - const extension = json.meta.type; - - if (extension !== "combined") { - console.error('error: unsupported meta type, expected "combined"'); - hasErrors = true; - } - - if (hasErrors) { - process.exit(1); - } - - const output = { - dlf: fs.createWriteStream(args.dlf), - llf: fs.createWriteStream(args.llf), - fts: fs.createWriteStream(args.fts), - }; - - const numberOfPolygons = json.fts.polygons.length; - - const dlf = json.dlf; - dlf.header.numberOfBackgroundPolygons = numberOfPolygons; - dlf.meta = { - type: "dlf", - }; - dlf.scene = { - name: json.header.name, - }; - - outputInChunks(stringifyJSON(dlf, args.pretty), output.dlf); - - const llf = json.llf; - llf.meta = { - type: "llf", - }; - llf.header.numberOfBackgroundPolygons = numberOfPolygons; - - const polygons = ((json) => { - const sizeX = json.fts.sceneHeader.sizeX; - - // predicting the final location of polygons based on how FTS.js calculates cell indices - const _cells = json.fts.polygons.reduce( - (cells, polygon) => { - const cellX = FTS.getCellCoordinateFromPolygon("x", polygon); - const cellY = FTS.getCellCoordinateFromPolygon("z", polygon); - - const polygons = cells[cellY * sizeX + cellX].polygons; - const idx = polygons.length; - cells[cellY * sizeX + cellX].polygons.push({ ...polygon }); - polygon.idx = idx; // TODO: this is a rather ugly hack for getting the indexes into polygons - - return cells; - }, - json.fts.cells.map((cell) => { - cell.polygons = []; - return cell; - }) - ); - - return FTS.getPolygons(_cells); - })(json); - - llf.colors = polygons - .flatMap(({ vertices }) => vertices) - .filter((vertex) => !isZeroVertex(vertex)) - .map(({ color }) => color); - - outputInChunks(stringifyJSON(llf, args.pretty), output.llf); - - const fts = json.fts; - fts.meta = { - type: "fts", - }; - fts.header.path = `C:\\ARX\\Game\\${json.header.name}`; - fts.polygons.forEach(({ vertices }) => { - vertices.forEach((vertex) => { - delete vertex.color; - }); - }); - - outputInChunks(stringifyJSON(fts, args.pretty), output.fts); -})(); diff --git a/bin/to-bson.js b/bin/to-bson.js deleted file mode 100755 index 3fb73ac..0000000 --- a/bin/to-bson.js +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const { DLF, FTS, LLF, FTL } = require("../src/index.js"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - stringifyBSON, - outputInChunks, -} = require("./helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output", "ext"], - boolean: ["version", "pretty"], - alias: { - v: "version", - p: "pretty", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - let extension = args.ext ? args.ext.toLowerCase() : ""; - let output = args.output; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - if (!extension) { - extension = filename.match(/\.([a-zA-Z]+)$/)[1].toLowerCase(); - } - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported extension, expected "dlf", "fts" or "llf"' - ); - hasErrors = true; - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - if (hasErrors) { - process.exit(1); - } - - const raw = await streamToBuffer(input); - - let json; - switch (extension) { - case "fts": - json = FTS.load(raw); - break; - case "dlf": - json = DLF.load(raw); - break; - case "llf": - json = LLF.load(raw); - break; - case "ftl": - json = FTL.load(raw); - break; - } - - outputInChunks(stringifyBSON(json), output); -})(); diff --git a/bin/to-json.js b/bin/to-json.js deleted file mode 100755 index 9d6978d..0000000 --- a/bin/to-json.js +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const { DLF, FTS, LLF, FTL } = require("../src/index.js"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - stringifyJSON, - outputInChunks, -} = require("./helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output", "ext"], - boolean: ["version", "pretty"], - alias: { - v: "version", - p: "pretty", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - let extension = args.ext ? args.ext.toLowerCase() : ""; - let output = args.output; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - if (!extension) { - extension = filename.match(/\.([a-zA-Z]+)$/)[1].toLowerCase(); - } - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported extension, expected "dlf", "fts", "llf" or "ftl"' - ); - hasErrors = true; - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - if (hasErrors) { - process.exit(1); - } - - const raw = await streamToBuffer(input); - - let json; - switch (extension) { - case "fts": - json = FTS.load(raw); - break; - case "dlf": - json = DLF.load(raw); - break; - case "llf": - json = LLF.load(raw); - break; - case "ftl": - json = FTL.load(raw); - break; - } - - outputInChunks(stringifyJSON(json, args.pretty), output); -})(); diff --git a/bin/to-yaml.js b/bin/to-yaml.js deleted file mode 100755 index d8af966..0000000 --- a/bin/to-yaml.js +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env node - -const fs = require("fs"); -const minimist = require("minimist-lite"); -const { DLF, FTS, LLF, FTL } = require("../src/index.js"); -const { - fileExists, - getPackageVersion, - streamToBuffer, - stringifyYAML, - outputInChunks, -} = require("./helpers.js"); -const { SUPPORTED_EXTENSIONS } = require("./constants.js"); - -const args = minimist(process.argv.slice(2), { - string: ["output", "ext"], - boolean: ["version", "pretty"], - alias: { - v: "version", - p: "pretty", - }, -}); - -(async () => { - if (args.version) { - console.log(getPackageVersion()); - process.exit(0); - } - - let filename = args._[0]; - let extension = args.ext ? args.ext.toLowerCase() : ""; - let output = args.output; - - let hasErrors = false; - - let input; - if (filename) { - if (await fileExists(filename)) { - input = fs.createReadStream(filename); - if (!extension) { - extension = filename.match(/\.([a-zA-Z]+)$/)[1].toLowerCase(); - } - } else { - console.error("error: input file does not exist"); - hasErrors = true; - } - } else { - input = process.openStdin(); - } - - if (!SUPPORTED_EXTENSIONS.includes(extension)) { - console.error( - 'error: unsupported meta type, expected "dlf", "fts", "llf" or "ftl"' - ); - hasErrors = true; - } - - if (output) { - output = fs.createWriteStream(output); - } else { - output = process.stdout; - } - - if (hasErrors) { - process.exit(1); - } - - const raw = await streamToBuffer(input); - - let json; - switch (extension) { - case "fts": - json = FTS.load(raw); - break; - case "dlf": - json = DLF.load(raw); - break; - case "llf": - json = LLF.load(raw); - break; - case "ftl": - json = FTL.load(raw); - break; - } - - outputInChunks(stringifyYAML(json), output); -})(); diff --git a/package.json b/package.json index 44f7b64..e75c2a8 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,15 @@ { "name": "arx-level-json-converter", "version": "1.3.1", - "description": "Converts Arx Fatalis level data to JSON/YAML/BSON and back", + "description": "Converts various Arx Fatalis formats to JSON/YAML/BSON and back", "main": "src/index.js", "engines": { "npm": ">=7.0.0", "node": ">=16.0.0" }, "bin": { - "to-json": "bin/to-json.js", - "from-json": "bin/from-json.js", - "to-yaml": "bin/to-yaml.js", - "from-yaml": "bin/from-yaml.js", - "to-bson": "bin/to-bson.js", - "from-bson": "bin/from-bson.js", - "combine": "bin/combine.js", - "separate": "bin/separate.js" + "arx-level-json-converter": "bin/convert.js", + "arx-convert": "bin/convert.js" }, "scripts": {}, "repository": {