File size: 6,656 Bytes
37a34fd 91a1e3f 37a34fd 91a1e3f 37a34fd 91a1e3f 37a34fd 91a1e3f 37a34fd 91a1e3f 37a34fd e93c9cc 37a34fd 91a1e3f 37a34fd e93c9cc 37a34fd e93c9cc 37a34fd 2a8d2d3 37a34fd 2a8d2d3 37a34fd 2a8d2d3 37a34fd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 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);
}
|