| import { execFile } from 'node:child_process'; |
| import { lstat, mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'; |
| import { dirname, join, relative, resolve } from 'node:path'; |
| import process from 'node:process'; |
| import { fileURLToPath } from 'node:url'; |
|
|
| const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| const modDirectory = dirname(root); |
| const chineseSource = join(root, 'localisation_source'); |
| const englishSource = join(root, 'localisation_english_source'); |
| const englishProvenance = join(root, 'localisation_english_provenance.json'); |
| const gameLocalisation = 'C:/Program Files (x86)/Steam/steamapps/common/Europa Universalis IV/localisation'; |
| const englishRoot = join(modDirectory, 'Ab_Integro_English'); |
| const englishDescriptor = join(modDirectory, 'Ab_Integro_English.mod'); |
| const englishOverlayFile = 'ab_integro_english_overlay_l_english.yml'; |
| const historyLanguageTool = join(root, 'tools', 'build_history_english.mjs'); |
|
|
| function runNode(arguments_) { |
| return new Promise((resolvePromise, rejectPromise) => { |
| execFile(process.execPath, arguments_, (error, stdout, stderr) => { |
| if (error) rejectPromise(new Error(`${stderr || stdout}${error.message}`)); |
| else resolvePromise(stdout); |
| }); |
| }); |
| } |
|
|
| async function exists(path) { |
| try { |
| await lstat(path); |
| return true; |
| } catch (error) { |
| if (error.code === 'ENOENT') return false; |
| throw error; |
| } |
| } |
|
|
| async function listFiles(directory, englishOnly = false) { |
| const files = []; |
| async function visit(current) { |
| for (const entry of await readdir(current, { withFileTypes: true })) { |
| const path = join(current, entry.name); |
| if (entry.isDirectory()) await visit(path); |
| else if (entry.isFile() && entry.name.endsWith('.yml') && (!englishOnly || entry.name.endsWith('_l_english.yml'))) { |
| files.push(relative(directory, path).replaceAll('\\', '/')); |
| } |
| } |
| } |
| await visit(directory); |
| return files.sort(); |
| } |
|
|
| function entries(content) { |
| return [...content.split(/\r?\n/)].flatMap((line) => { |
| const match = line.match(/^\s*([^\s:#]+):(?:\d+)?\s+".*"/); |
| return match ? [[match[1], line.trimEnd()]] : []; |
| }); |
| } |
|
|
| async function readKeyLineIndex(directory, rejectDuplicates = false, englishOnly = false) { |
| const index = new Map(); |
| for (const file of await listFiles(directory, englishOnly)) { |
| const content = await readFile(join(directory, file), 'utf8'); |
| for (const [key, line] of entries(content)) { |
| if (rejectDuplicates && index.has(key)) { |
| throw new Error('duplicate localisation key ' + key + ' in ' + index.get(key).file + ' and ' + file); |
| } |
| index.set(key, { file, line }); |
| } |
| } |
| return index; |
| } |
|
|
| async function validateEnglishOverlay() { |
| if (!await exists(englishSource)) throw new Error('missing localisation_english_source'); |
|
|
| const provenance = JSON.parse(await readFile(englishProvenance, 'utf8')); |
| const [chineseKeys, vanillaKeys, customKeys] = await Promise.all([ |
| readKeyLineIndex(chineseSource), |
| readKeyLineIndex(gameLocalisation, false, true), |
| readKeyLineIndex(englishSource, true), |
| ]); |
|
|
| for (const [key, entry] of customKeys) { |
| if (vanillaKeys.has(key)) { |
| const filePolicy = provenance.files?.[entry.file]; |
| if (!filePolicy?.allowVanillaOverride) { |
| throw new Error('English source must not override vanilla key ' + key + ' (' + entry.file + ')'); |
| } |
| } |
| } |
|
|
| const missing = [...chineseKeys.keys()].filter((key) => !vanillaKeys.has(key) && !customKeys.has(key)); |
| if (missing.length) { |
| const grouped = new Map(); |
| for (const key of missing) { |
| const file = chineseKeys.get(key).file; |
| grouped.set(file, (grouped.get(file) ?? 0) + 1); |
| } |
| throw new Error('English source is incomplete:\n' + [...grouped].map(([file, count]) => file + ': ' + count).join('\n')); |
| } |
|
|
| const unused = [...customKeys.keys()].filter((key) => !chineseKeys.has(key)); |
| if (unused.length) { |
| throw new Error('English source contains keys absent from the Chinese base: ' + unused.slice(0, 20).join(', ')); |
| } |
|
|
| const files = await listFiles(englishSource); |
| for (const file of files) { |
| if (!provenance.files?.[file]) throw new Error('missing provenance record: ' + file); |
| } |
| for (const file of Object.keys(provenance.files ?? {})) { |
| if (!files.includes(file)) throw new Error('provenance record has no source file: ' + file); |
| } |
|
|
| return { |
| files: files.length, |
| chineseKeys, |
| vanillaKeys, |
| customKeys, |
| provenance, |
| vanillaCount: [...chineseKeys.keys()].filter((key) => vanillaKeys.has(key)).length, |
| }; |
| } |
|
|
| async function buildEnglish(replace) { |
| const summary = await validateEnglishOverlay(); |
| if (await exists(englishRoot)) { |
| if (!replace) { |
| throw new Error('English overlay already exists: ' + englishRoot + '\nRun with --replace to rebuild that generated directory.'); |
| } |
| await rm(englishRoot, { recursive: true, force: true }); |
| } |
|
|
| const lines = []; |
| for (const key of summary.chineseKeys.keys()) { |
| const customEntry = summary.customKeys.get(key); |
| const overrideVanilla = customEntry && summary.provenance.files?.[customEntry.file]?.allowVanillaOverride; |
| const entry = overrideVanilla ? customEntry : (summary.vanillaKeys.get(key) ?? customEntry); |
| if (!entry) throw new Error('missing English text for ' + key); |
| lines.push(entry.line); |
| } |
|
|
| await mkdir(join(englishRoot, 'localisation'), { recursive: true }); |
| await writeFile( |
| join(englishRoot, 'localisation', englishOverlayFile), |
| '\uFEFFl_english:\n' + lines.join('\n') + '\n', |
| 'utf8', |
| ); |
| const descriptor = 'version="0.1.0"\ntags={\n\t"Expansion"\n\t"Gameplay"\n\t"Fixes"\n}\nname="Ab Integro English"\nsupported_version="v1.37.5.0"\n'; |
| await writeFile(join(englishRoot, 'descriptor.mod'), descriptor, 'utf8'); |
| await writeFile( |
| englishDescriptor, |
| 'name="Ab Integro English"\npath="mod/Ab_Integro_English"\nsupported_version="1.37.5.0"\ntags={\n\t"Expansion"\n\t"Gameplay"\n\t"Fixes"\n}\n', |
| 'utf8', |
| ); |
| process.stdout.write(await runNode([historyLanguageTool, 'build'])); |
| process.stdout.write( |
| 'English overlay built: ' + summary.chineseKeys.size + ' keys (' + summary.vanillaCount + ' vanilla, ' + summary.customKeys.size + ' custom).\n', |
| ); |
| } |
|
|
| const [command = 'build-english', ...arguments_] = process.argv.slice(2); |
| if (command === 'check') { |
| console.error('Use node tools/validate.mjs'); |
| process.exitCode = 1; |
| } else if (command === 'build-english') { |
| await buildEnglish(arguments_.includes('--replace')); |
| } else { |
| throw new Error('unknown command: ' + command); |
| } |
|
|