/** * Shared semicolon-separated localization CSV parser for files in `lang.vromfs.bin`. */ export interface LocalizedCsvText { readonly en: string; readonly zh: string; } export function parseSemicolonCsvRow(line: string): string[] { const fields: string[] = []; let current = ''; let inQuotes = false; for (let i = 0; i < line.length; i += 1) { const ch = line[i]!; if (ch === '"') { if (inQuotes && line[i + 1] === '"') { current += '"'; i += 1; } else { inQuotes = !inQuotes; } } else if (ch === ';' && !inQuotes) { fields.push(current); current = ''; } else { current += ch; } } fields.push(current); return fields; } /** Strip zero-width word-break spaces Gaijin inserts into CJK localization strings. */ export function cleanLangText(raw: string): string { return raw.replace(/\u200b/g, '').trim(); } export function parseLocalizedCsvMap(csvText: string): Map { const lines = csvText.split(/\r?\n/); const header = parseSemicolonCsvRow(lines[0] ?? ''); const enIdx = Math.max(1, header.findIndex((col) => col.includes(''))); const zhIdx = header.findIndex((col) => col.includes('')); const hzhIdx = header.findIndex((col) => col.includes('')); const map = new Map(); for (let i = 1; i < lines.length; i += 1) { const line = lines[i]; if (!line) continue; const cols = parseSemicolonCsvRow(line); const id = cols[0]; if (!id) continue; const en = cleanLangText(cols[enIdx] ?? '') || id; const zh = cleanLangText((zhIdx >= 0 ? cols[zhIdx] : '') || (hzhIdx >= 0 ? cols[hzhIdx] : '') || '') || en; map.set(id, { en, zh }); } return map; }