Map locale from modules into the save's locale

Map locale files located at locale/{lang}/{file} in a module to
locale/{lang}/{module-name}-{file} in the save. This additionally
support a shorthand notation by mapping locale/{lange}.cfg to
locale/{lang}/{module-name}.cfg.

Resolves #500.
This commit is contained in:
Hornwitser committed 2023-08-25 16:46:13 +02:00
1 parent a1148c7ec9
commit cc8ec36e5a
5 files changed
+47 -1

No files matched your search

+16 -1
View File
@@ -21,6 +21,21 @@ export class SaveModule {
public files = new Map<string, Buffer>,
) { }
static moduleFilePath(filePath: string, moduleName: string) {
// Map locale files to the save's locale folder
if (filePath.startsWith("locale/")) {
const slashes = filePath.match(/\//g)!.length;
if (slashes > 1) {
const secondSlash = filePath.indexOf("/", "locale/".length);
return `${filePath.slice(0, secondSlash)}/${moduleName}-${filePath.slice(secondSlash + 1)}`;
}
const period = `${filePath}.`.indexOf(".");
return `${filePath.slice(0, period)}/${moduleName}${filePath.slice(period)}`;
}
// Map all other files into modules/name in the save.
return path.posix.join("modules", moduleName, filePath);
}
async loadFiles(moduleDirectory: string) {
let dirs: [string, string][] = [[moduleDirectory, ""]];
while (dirs.length) {
@@ -28,9 +43,9 @@ export class SaveModule {
for (let entry of await fs.readdir(dir, { withFileTypes: true })) {
let fsPath = path.join(dir, entry.name);
let relativePath = path.posix.join(relativeDir, entry.name);
let savePath = path.posix.join("modules", this.info.name, relativePath);
if (entry.isFile()) {
let savePath = SaveModule.moduleFilePath(relativePath, this.info.name);
this.files.set(savePath, await fs.readFile(fsPath));
} else if (entry.isDirectory()) {
+1
View File
@@ -0,0 +1 @@
module-test=A Test
@@ -0,0 +1 @@
module-test-locale=Test Locale
+21
View File
@@ -9,6 +9,27 @@ const patch = require("@clusterio/host/dist/src/patch");
describe("host/patch", function() {
describe("class SaveModule", function() {
describe("static moduleFilePath()", function() {
it("Should remap locale files", function() {
const mappings = [
["locale/en/foo.cfg", "locale/en/test-foo.cfg"],
["locale/en/foo.txt", "locale/en/test-foo.txt"],
["locale/en/foo.txt.tmp", "locale/en/test-foo.txt.tmp"],
["locale/en/foo", "locale/en/test-foo"],
["locale/en.cfg", "locale/en/test.cfg"],
["locale/en.txt", "locale/en/test.txt"],
["locale/en.txt.tmp", "locale/en/test.txt.tmp"],
["locale/en", "locale/en/test"],
["locale/en/bar/foo.cfg", "locale/en/test-bar/foo.cfg"],
];
for (let [input, expected] of mappings) {
assert.equal(patch.SaveModule.moduleFilePath(input, "test"), expected);
}
});
});
});
describe("generateLoader()", function() {
let reference = [
"-- Auto generated scenario module loader created by Clusterio",
+8
View File
@@ -27,6 +27,14 @@ describe("Integration of lib/factorio/patch", function() {
let zip = await JSZip.loadAsync(await fs.readFile(savePath));
assert.equal(await zip.file("test/modules/test/test.lua").async("string"), "-- test\n");
assert.equal(await zip.file("test/modules/subdir/dir/test.lua").async("string"), "-- test\n");
assert.equal(
await zip.file("test/locale/en/test.cfg").async("string"),
"module-test=A Test\n"
);
assert.equal(
await zip.file("test/locale/en/test-locale.cfg").async("string"),
"module-test-locale=Test Locale\n"
);
});
it("should remove old modules in a save", async function() {
slowTest(this);