From 290d1be48f8f3d70dc8ea3211fc3ede431492357 Mon Sep 17 00:00:00 2001 From: Lenni009 Date: Fri, 31 May 2024 23:03:28 +0200 Subject: [PATCH] adjust to new MBINCompiler format --- main.ts | 123 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/main.ts b/main.ts index c614d1a..839ddbc 100644 --- a/main.ts +++ b/main.ts @@ -5,10 +5,12 @@ import { parse } from "flags"; // get CLI args const args = parse(Deno.args); -const exmlDir: string = args['input-path'] || './EXML/'; // --input-path="path/to/file" default: "./EXML/" -const outputDir: string = args['output-path'] || './output/'; // --output-path="path/to/file" default: "./output/" -const outputFileName: string = args.filename || 'translation.txt'; // --filename=Lenni.txt default: "translation.txt" -const languageArgs = args['_'].map(language => language.toString().toLowerCase()); // english german -- this needs the .toString() method because TS would complain +const exmlDir: string = args["input-path"] || "./EXML/"; // --input-path="path/to/file" default: "./EXML/" +const outputDir: string = args["output-path"] || "./output/"; // --output-path="path/to/file" default: "./output/" +const outputFileName: string = args.filename || "translation.txt"; // --filename=Lenni.txt default: "translation.txt" +const languageArgs = args["_"].map((language) => + language.toString().toLowerCase() +); // english german -- this needs the .toString() method because TS would complain const timer: boolean = args.timer; // create directories if they don't exist yet @@ -17,94 +19,93 @@ Deno.mkdirSync(outputDir, { recursive: true }); // set up interfaces interface Xml { - '@version': string; - '@encoding': string; + "@version": string; + "@encoding": string; } interface LangObj { - "@name": string; - "@value": string; - Property?: { - "@name": string; - "@value": string; - }; + "@name": string; + "@value": string; } interface TkLocalisationEntry { - Property: Array; - "@value": string; + Property: Array; + "@value": string; } interface Data { - Property: { - Property: TkLocalisationEntry[]; - "@name": string; - } - "@template": string; + Property: { + Property: TkLocalisationEntry[]; + "@name": string; + }; + "@template": string; } interface RootObject { - '?xml': Xml; - Data: Data; + "?xml": Xml; + Data: Data; } interface LangData { - [key: string]: Partial<{ - [key: string]: string; - }>; + [key: string]: { + [key: string]: string; + }; } // initialise global variables -const files = Array.from(Deno.readDirSync(exmlDir)).filter(file => file.isFile && file.name.toLowerCase().endsWith('.exml')); +const files = Array.from(Deno.readDirSync(exmlDir)).filter( + (file) => file.isFile && file.name.toLowerCase().endsWith(".exml") +); const langData: LangData = {}; // set up XML parser const options = { - ignoreAttributes: false, - attributeNamePrefix: "@", -} + ignoreAttributes: false, + attributeNamePrefix: "@", +}; const parser = new XMLParser(options); -if (timer) console.time('Total time'); -console.log('Starting'); +if (timer) console.time("Total time"); +console.log("Starting"); // loop through EXML files for (let i = 0; i < files.length; i++) { - const file = files[i]; - const fullFileName = file.name; - const fileData = Deno.readTextFileSync(exmlDir + fullFileName); - const document: RootObject = parser.parse(fileData); - const langElements = document.Data.Property.Property; + const file = files[i]; + const fullFileName = file.name; + const fileData = Deno.readTextFileSync(exmlDir + fullFileName); + const document: RootObject = parser.parse(fileData); + const langElements = document.Data.Property.Property; - // loop over TkLocalisationData sections - for (const locEntry of langElements) { - const locEntryData = locEntry.Property; - let langKey: string; + // loop over TkLocalisationData sections + for (const locEntry of langElements) { + const locEntryData = locEntry.Property; + const langKey = locEntryData[0]["@value"]; + langData[langKey] ??= {}; - // loop over individual lang keys and their assigned values - for (let k = 0; k < locEntryData.length; k++) { - const entry = locEntryData[k]; - if (k == 0) { - langKey = entry['@value']; - langData[langKey] ??= {}; - continue; - } - if (!entry?.Property) continue; - const langValue = entry.Property['@value']; - if (!langValue) continue; - const language = entry['@name']; - if (languageArgs.length && !languageArgs.includes(language.toLowerCase())) continue; - langData[langKey!][language] = decode(langValue, { level: 'xml' }); // NoSonar this is necessary, for some reason TS complains here :shrug: - } - } - console.log(`${i + 1} / ${files.length} (${Math.round(((i + 1) / files.length) * 100)}%) - Processed ${fullFileName}`); // NoSonar this is just percentage calculation + // loop over individual lang keys and their assigned values + // we're skipping index 0 since that's the langkey and already covered above + for (let j = 1; j < locEntryData.length; j++) { + const entry = locEntryData[j]; + const language = entry["@name"]; + if (languageArgs.length && !languageArgs.includes(language.toLowerCase())) + continue; + const langValue = entry["@value"]; + if (!langValue) continue; + langData[langKey][language] = decode(langValue, { level: "xml" }); + } + } + console.log( + `${i + 1} / ${files.length} (${Math.round( + ((i + 1) / files.length) * 100 + )}%) - Processed ${fullFileName}` + ); } const textContent = []; for (const key in langData) { - textContent.push(key + '\n'); - Object.values(langData[key]).forEach(item => textContent.push(item + '\n')); - textContent.push('\n'); + textContent.push(key + "\n"); + Object.values(langData[key]).forEach((item) => textContent.push(item + "\n")); + textContent.push("\n"); } -Deno.writeTextFileSync(outputDir + outputFileName, textContent.join('').trim()); +Deno.writeTextFileSync(outputDir + outputFileName, textContent.join("").trim()); console.log("done!"); -if (timer) console.timeEnd('Total time'); +if (timer) console.timeEnd("Total time");