|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Merge translated board JSONs back into the original OBZ archive so that |
| 5 | + * we can keep the original media assets (images/sounds/etc.) alongside the new text. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node scripts/translation/merge-translation-assets.js \ |
| 9 | + * original.obz translated.obz \ |
| 10 | + * merged-with-assets.obz |
| 11 | + */ |
| 12 | + |
| 13 | +const path = require('path'); |
| 14 | +const fs = require('fs'); |
| 15 | +const AdmZip = require('adm-zip'); |
| 16 | + |
| 17 | +function printUsage() { |
| 18 | + console.log('Usage: node scripts/translation/merge-translation-assets.js <original.obz> <translated.obz> [output.obz]'); |
| 19 | + console.log('If no output path is supplied, the script will append "-with-assets" to the translated file name.'); |
| 20 | +} |
| 21 | + |
| 22 | +async function main() { |
| 23 | + const [, , originalPath, translatedPath, outputArg] = process.argv; |
| 24 | + |
| 25 | + if (!originalPath || !translatedPath) { |
| 26 | + printUsage(); |
| 27 | + process.exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + if (!fs.existsSync(originalPath)) { |
| 31 | + console.error(`Original file not found: ${originalPath}`); |
| 32 | + process.exit(1); |
| 33 | + } |
| 34 | + |
| 35 | + if (!fs.existsSync(translatedPath)) { |
| 36 | + console.error(`Translated file not found: ${translatedPath}`); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + |
| 40 | + const outputPath = |
| 41 | + outputArg || |
| 42 | + path.join( |
| 43 | + path.dirname(translatedPath), |
| 44 | + `${path.basename(translatedPath, path.extname(translatedPath))}-with-assets${path.extname(translatedPath)}` |
| 45 | + ); |
| 46 | + |
| 47 | + const originalZip = new AdmZip(originalPath); |
| 48 | + const translatedZip = new AdmZip(translatedPath); |
| 49 | + |
| 50 | + const resultZip = new AdmZip(); |
| 51 | + |
| 52 | + const normalizeName = (name) => { |
| 53 | + if (name.endsWith('.obf.obf')) { |
| 54 | + return name.replace(/\.obf\.obf$/, '.obf'); |
| 55 | + } |
| 56 | + return name; |
| 57 | + }; |
| 58 | + |
| 59 | + const translationEntries = translatedZip.getEntries().map((entry) => ({ |
| 60 | + entry, |
| 61 | + normalizedName: normalizeName(entry.entryName), |
| 62 | + })); |
| 63 | + |
| 64 | + const translationNames = new Set(translationEntries.map((item) => item.normalizedName)); |
| 65 | + |
| 66 | + originalZip.getEntries().forEach((entry) => { |
| 67 | + const normalized = normalizeName(entry.entryName); |
| 68 | + |
| 69 | + if (translationNames.has(normalized)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const data = entry.isDirectory ? Buffer.alloc(0) : entry.getData(); |
| 74 | + resultZip.addFile(entry.entryName, data, entry.comment || ''); |
| 75 | + }); |
| 76 | + |
| 77 | + translationEntries.forEach(({ entry, normalizedName }) => { |
| 78 | + const data = entry.isDirectory ? Buffer.alloc(0) : entry.getData(); |
| 79 | + resultZip.addFile(normalizedName, data, entry.comment || ''); |
| 80 | + }); |
| 81 | + |
| 82 | + resultZip.writeZip(outputPath); |
| 83 | + console.log(`Merged archive written to ${outputPath}`); |
| 84 | +} |
| 85 | + |
| 86 | +main().catch((error) => { |
| 87 | + console.error('Failed to merge translation assets:', error); |
| 88 | + process.exit(1); |
| 89 | +}); |
0 commit comments