UNPKG

@jtff/extract-mission

Version:

extract mission file in a DCS .miz and translate it to json format

129 lines (113 loc) 4.69 kB
#!/usr/bin/env node "use strict"; const fs = require("fs"); const path = require("path"); const yargs = require('yargs/yargs') const { hideBin } = require('yargs/helpers') const templatelib = require('@jtff/miztemplate-lib'); const mizlib = new templatelib.Mizlib(); let presetName; if (process.env.NODE_ENV === "production") { console.debug = () => { }; } function cleanupOutFolder() { fs.rmSync("./out", {recursive: true, force: true}); fs.mkdirSync("./out", {recursive: true}); } function commandWeatherPreset(file, preset, hide) { if (!(fs.existsSync(file))) { console.error("file " + file + " not found !!\nExiting."); process.exit(2); } if (preset) { presetName = preset; } else { presetName = path.parse(file).name; } console.debug("preset name is : " + presetName); const outputMizPath = "./out/" + path.basename(file); cleanupOutFolder(); fs.copyFileSync(file,outputMizPath); mizlib.getZipObjectFromMizPath(outputMizPath).then(mizData => { console.debug("miz File loaded"); mizData.files["mission"].async('nodebuffer').then(content => { fs.writeFileSync("./out/" + path.parse(file).name + "-mission.lua", content); }); mizlib.getMissionObjectFromZipObject(mizData).then(async missionObject => { fs.writeFileSync("./out/" + path.parse(file).name + "-mission.json", Buffer.from(JSON.stringify(missionObject, null, 4))); fs.writeFileSync("./out/" + path.parse(file).name + "-preset.json", Buffer.from( JSON.stringify(JSON.parse([ "{ \"" + path.parse(file).name + "\": ", JSON.stringify(await mizlib.generatePresetObjectFromZipObject(mizData, hide)), "}" ].join("")),null,4))); }); }); } function commandWeatherPresetBatch(folder, hide) { function iterate(folder, hide, callback) { const consolidatedPresetObject = {}; const files = fs.readdirSync( folder ).filter( ( elm ) => elm.match(/\.miz/ig)); files.forEach(function (file, index) { const mizFilePath = folder + '/' + file; mizlib.getZipObjectFromMizPath(mizFilePath).then(mizData => { console.debug("miz File loaded"); mizlib.getMissionObjectFromZipObject(mizData).then(missionObject => { consolidatedPresetObject[path.parse(mizFilePath).name] = mizlib.generatePresetObjectFromMissionObject(missionObject, hide); if (index==files.length-1) { callback(consolidatedPresetObject); } }); }); }); } if (!(fs.existsSync(folder) && fs.lstatSync(folder).isDirectory() )) { console.error("Folder " + folder + " not found !!\nExiting."); process.exit(2); } cleanupOutFolder(); iterate(folder, hide, function(result) { console.debug(result); fs.writeFileSync("./out/presets.json", Buffer.from( JSON.stringify(result,null,4))); }); } console.log("extract-mission v" + require('./package.json').version); yargs(hideBin(process.argv)) .command('weather_preset <file> [--preset=preset_name] [--hide=true]', 'Extract the weather preset from mission', (yargs) => { yargs.positional('file', { type: "string", describe: 'the .miz file from which you want to generate the weather_preset', }) .option('preset', { alias: 'p', type: 'string', describe: 'the preset name you want to generate' }) .option('hide', { alias: 'H', type: 'boolean', default: true, description: 'Is the preset hidden or not ?' }) }, (argv) => commandWeatherPreset(argv.file, argv.preset, argv.hide)) .command('weather_batch <folder> [--hide=true]', 'Extract all the weather preset from all the .miz files in the given <folder>', (yargs) => { yargs.positional('folder', { type: 'string', describe: 'The input folder where to collect all the .miz files', }) .option('hide', { alias: 'H', type: 'boolean', default: true, description: 'Is the preset hidden or not ?' }) }, (argv) => commandWeatherPresetBatch(argv.folder, argv.hide)) .option('debug', { alias: 'd', type: 'boolean', description: 'Run with debug' }) .demandCommand() .help() .parse();