|
| 1 | +package net.modtale.launcher.config; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.util.List; |
| 7 | +import java.util.zip.ZipEntry; |
| 8 | +import java.util.zip.ZipOutputStream; |
| 9 | +import net.modtale.launcher.install.WorldListConfigInstaller; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.io.TempDir; |
| 12 | + |
| 13 | +class HytaleConfigOwnershipTest { |
| 14 | + @TempDir Path directory; |
| 15 | + |
| 16 | + @Test void matchesManifestIdentityRatherThanFilenameNameOrCase() throws Exception { |
| 17 | + jar("Mods/unrelated-download-name.jar", "{\"Group\":\"com.azuredoom\",\"Name\":\"levelingcore\"}"); |
| 18 | + jar("Mods/other.jar", "{\"Group\":\"Other\",\"Name\":\"levelingcore\"}"); |
| 19 | + config("world/mods/com.azuredoom_levelingcore/config.json"); |
| 20 | + config("world/mods/Other_levelingcore/settings.toml"); |
| 21 | + config("world/mods/com.azuredoom_Levelingcore/config.json"); |
| 22 | + config("world/mods/unrelated-download-name/config.json"); |
| 23 | + config("world/mods/levelingcore/config.json"); |
| 24 | + var found = new HytaleConfigFiles().discover(directory.resolve("Mods"), directory.resolve("world")); |
| 25 | + assertEquals(5, found.size()); |
| 26 | + assertEquals(2, found.stream().filter(file -> !file.pluginId().isEmpty()).count()); |
| 27 | + assertTrue(found.stream().anyMatch(file -> file.pluginId().equals("com.azuredoom:levelingcore") |
| 28 | + && file.label().contains("World mods / com.azuredoom:levelingcore /"))); |
| 29 | + assertEquals(3, found.stream().filter(file -> file.label().contains("Unattributed")).count()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test void handlesLocalWorldModsInheritedSubPluginsAndAmbiguousUnderscores() throws Exception { |
| 33 | + jar("world/mods/bundle.jar", "{\"Group\":\"Author\",\"Name\":\"Parent\",\"SubPlugins\":[{\"Name\":\"Child\"}]}"); |
| 34 | + jar("Mods/one.jar", "{\"Group\":\"A_B\",\"Name\":\"C\"}"); |
| 35 | + jar("Mods/two.jar", "{\"Group\":\"A\",\"Name\":\"B_C\"}"); |
| 36 | + config("world/mods/Author_Child/config.json"); |
| 37 | + config("world/mods/A_B_C/config.json"); |
| 38 | + var found = new HytaleConfigFiles().discover(directory.resolve("Mods"), directory.resolve("world")); |
| 39 | + assertTrue(found.stream().anyMatch(file -> file.pluginId().equals("Author:Child"))); |
| 40 | + assertTrue(found.stream().anyMatch(file -> file.pluginId().isEmpty() && file.label().contains("Ambiguous mod"))); |
| 41 | + } |
| 42 | + |
| 43 | + @Test void excludesLooseAssetJsonAndDoesNotGuessCustomFolders() throws Exception { |
| 44 | + Path manifest = config("Mods/loose-pack/manifest.json"); |
| 45 | + Files.writeString(manifest, "{\"Group\":\"Author\",\"Name\":\"Assets\"}"); |
| 46 | + config("Mods/loose-pack/Server/Item/Items/example.json"); |
| 47 | + jar("Mods/corrupt.jar", "{broken"); |
| 48 | + config("world/mods/custom-author-folder/nested/settings.json"); |
| 49 | + var found = new HytaleConfigFiles().discover(directory.resolve("Mods"), directory.resolve("world")); |
| 50 | + assertEquals(1, found.size()); |
| 51 | + assertEquals("", found.getFirst().pluginId()); |
| 52 | + assertTrue(found.getFirst().label().contains("Unattributed")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test void sharedConfigsPreserveExactManifestFolderAndOwnerAfterRebasing() throws Exception { |
| 56 | + jar("Mods/modtale-project-slug-v2.jar", "{\"Group\":\"org.example_mods\",\"Name\":\"Fancy_Mod\"}"); |
| 57 | + config("world/mods/org.example_mods_Fancy_Mod/nested/Gameplay.json"); |
| 58 | + var capture = new WorldListConfigCapture(); |
| 59 | + var found = capture.discover(directory.resolve("Mods"), directory.resolve("world")); |
| 60 | + assertEquals("org.example_mods:Fancy_Mod", found.getFirst().pluginId()); |
| 61 | + var configs = capture.capture(found, directory.resolve("Mods"), directory.resolve("world")); |
| 62 | + assertEquals("org.example_mods_Fancy_Mod/nested/Gameplay.json", configs.getFirst().path()); |
| 63 | + WorldListConfigInstaller.install(configs, "WORLD", directory.resolve("recipient/mods")); |
| 64 | + var restored = new HytaleConfigFiles().discover(directory.resolve("Mods"), directory.resolve("recipient")); |
| 65 | + assertEquals(List.of("org.example_mods:Fancy_Mod"), restored.stream().map(HytaleConfigFiles.ConfigFile::pluginId).toList()); |
| 66 | + } |
| 67 | + |
| 68 | + private Path config(String relative) throws Exception { |
| 69 | + Path path = directory.resolve(relative); Files.createDirectories(path.getParent()); Files.writeString(path, "{}"); return path; |
| 70 | + } |
| 71 | + private void jar(String relative, String manifest) throws Exception { |
| 72 | + Path path = directory.resolve(relative); Files.createDirectories(path.getParent()); |
| 73 | + try (var zip = new ZipOutputStream(Files.newOutputStream(path))) { |
| 74 | + zip.putNextEntry(new ZipEntry("manifest.json")); zip.write(manifest.getBytes(java.nio.charset.StandardCharsets.UTF_8)); zip.closeEntry(); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments