Skip to content

Commit

Permalink
adjust to new MBINCompiler format
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni009 committed May 31, 2024
1 parent 376bc18 commit 290d1be
Showing 1 changed file with 62 additions and 61 deletions.
123 changes: 62 additions & 61 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<LangObj>;
"@value": string;
Property: Array<LangObj>;
"@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");

0 comments on commit 290d1be

Please sign in to comment.