Skip to content

Instantly share code, notes, and snippets.

@danthemango
Last active August 10, 2023 20:31
Show Gist options
  • Save danthemango/b3f46b23f88a768ee908862a654b21af to your computer and use it in GitHub Desktop.
Save danthemango/b3f46b23f88a768ee908862a654b21af to your computer and use it in GitHub Desktop.
nodejs turn factorio blueprint to json
/*
https://wiki.factorio.com/Blueprint_string_format
turn blueprint string to json
*/
import fs from "fs";
import { unzip } from "zlib";
// returns a flagged argument value, if found
function getFlaggedArg(flag) {
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i] == flag && process.argv.length > i + 1) {
return process.argv[i + 1];
}
}
return;
}
async function main() {
const inFileName = getFlaggedArg("-i");
if (!inFileName) {
console.log('usage: -i <infile.txt>');
return 1;
}
const blueprintString = fs.readFileSync(inFileName).toString();
const blueprintVersion = blueprintString[0];
if (blueprintVersion != 0) {
console.error('Cannot read bluprint version ', blueprintVersion);
return 1;
}
const buffer = Buffer.from(blueprintString.substring(1), 'base64');
unzip(buffer, (err, buffer) => {
if (err) {
console.error('An error occurred:', err);
process.exitCode = 1;
return 1;
}
console.log(buffer.toString());
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment