diff --git a/packages/host/src/patch.ts b/packages/host/src/patch.ts index d1d2071e..ecc22d83 100644 --- a/packages/host/src/patch.ts +++ b/packages/host/src/patch.ts @@ -21,6 +21,21 @@ export class SaveModule { public files = new Map, ) { } + 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()) { diff --git a/test/file/modules/test/locale/en.cfg b/test/file/modules/test/locale/en.cfg new file mode 100644 index 00000000..c09e0d7b --- /dev/null +++ b/test/file/modules/test/locale/en.cfg @@ -0,0 +1 @@ +module-test=A Test diff --git a/test/file/modules/test/locale/en/locale.cfg b/test/file/modules/test/locale/en/locale.cfg new file mode 100644 index 00000000..251af2d4 --- /dev/null +++ b/test/file/modules/test/locale/en/locale.cfg @@ -0,0 +1 @@ +module-test-locale=Test Locale diff --git a/test/host/patch.js b/test/host/patch.js index cf48fc27..0ed2219c 100644 --- a/test/host/patch.js +++ b/test/host/patch.js @@ -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", diff --git a/test/integration/patch.js b/test/integration/patch.js index ae3bb9a5..bfc1f224 100644 --- a/test/integration/patch.js +++ b/test/integration/patch.js @@ -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);