diff --git a/core/src/main/java/dev/puicore/modbridge/core/bds/BdsInstaller.java b/core/src/main/java/dev/puicore/modbridge/core/bds/BdsInstaller.java index 2d92796..1bd37a2 100644 --- a/core/src/main/java/dev/puicore/modbridge/core/bds/BdsInstaller.java +++ b/core/src/main/java/dev/puicore/modbridge/core/bds/BdsInstaller.java @@ -83,7 +83,11 @@ public static final class InstallException extends Exception { } } - private record Pack(String uuid, List version, Map files) {} + private record Pack(String uuid, List version, JsonObject manifest, Map files) {} + + private record Bundle(Pack behavior, Pack resource) {} + + private record PreparedPlan(Plan plan, Bundle bundle) {} private BdsInstaller() {} @@ -97,11 +101,16 @@ private BdsInstaller() {} * @param world ワールド名。null なら server.properties の level-name */ public static Plan plan(Path bds, Path packDir, String world) { + return prepare(bds, packDir, world).plan(); + } + + private static PreparedPlan prepare(Path bds, Path packDir, String world) { List problems = new ArrayList<>(); List actions = new ArrayList<>(); Path root = bds.toAbsolutePath().normalize(); if (!Files.isDirectory(root, LinkOption.NOFOLLOW_LINKS)) { - return new Plan(world, actions, List.of("BDS のフォルダーが無い(シンボリックリンクは使えない)"), null, null, null, null); + return new PreparedPlan(new Plan(world, actions, List.of("BDS のフォルダーが無い(シンボリックリンクは使えない)"), + null, null, null, null), null); } if (!Files.isRegularFile(root.resolve("bedrock_server")) && !Files.isRegularFile(root.resolve("bedrock_server.exe"))) { problems.add("bedrock_server(.exe)が無い。BDS のフォルダーを指定する"); @@ -119,9 +128,11 @@ public static Plan plan(Path bds, Path packDir, String world) { } Pack bp = null; Pack rp = null; + Bundle bundle = null; try { - bp = readPack(packDir.resolve("modbridge-bp.mcpack"), "data"); - rp = readPack(packDir.resolve("modbridge-rp.mcpack"), "resources"); + bundle = readBundle(packDir); + bp = bundle.behavior(); + rp = bundle.resource(); } catch (InstallException e) { problems.add(e.getMessage()); } @@ -147,8 +158,9 @@ public static Plan plan(Path bds, Path packDir, String world) { actions.add(new Action("UPDATE_JSON", "worlds/" + w + "/world_resource_packs.json", "pack_id " + rp.uuid() + " を " + rp.version() + " にする")); } } - return new Plan(w, List.copyOf(actions), List.copyOf(problems), bp == null ? null : bp.uuid(), bp == null ? null : bp.version(), - rp == null ? null : rp.uuid(), rp == null ? null : rp.version()); + return new PreparedPlan(new Plan(w, List.copyOf(actions), List.copyOf(problems), + bp == null ? null : bp.uuid(), bp == null ? null : bp.version(), + rp == null ? null : rp.uuid(), rp == null ? null : rp.version()), bundle); } /** ワールド名として使えるか。解決したパスが worlds/ の直下でなければならない。 */ @@ -169,7 +181,8 @@ static boolean validWorld(Path root, String w) { * @return 作ったバックアップの ID(rollback に使う) */ public static String apply(Path bds, Path packDir, String world, boolean confirmedStopped) throws InstallException { - Plan plan = plan(bds, packDir, world); + PreparedPlan prepared = prepare(bds, packDir, world); + Plan plan = prepared.plan(); if (!plan.ok()) { throw new InstallException("配置できない: " + String.join(" / ", plan.problems())); } @@ -178,14 +191,8 @@ public static String apply(Path bds, Path packDir, String world, boolean confirm } Path root = bds.toAbsolutePath().normalize(); Path worldDir = root.resolve("worlds").resolve(plan.world()); - Pack bp; - Pack rp; - try { - bp = readPack(packDir.resolve("modbridge-bp.mcpack"), "data"); - rp = readPack(packDir.resolve("modbridge-rp.mcpack"), "resources"); - } catch (InstallException e) { - throw e; - } + Pack bp = prepared.bundle().behavior(); + Pack rp = prepared.bundle().resource(); try (WorldLock lock = WorldLock.acquire(worldDir)) { lock.ensureHeld(); String id = backup(root, plan.world()); @@ -619,6 +626,33 @@ private static String levelName(Path root, List problems) { return name.trim(); } + /** 一緒に生成したパックの組であることを、BP が要求する RP の UUID と版で確認する。 */ + private static Bundle readBundle(Path packDir) throws InstallException { + Pack bp = readPack(packDir.resolve("modbridge-bp.mcpack"), "data"); + Pack rp = readPack(packDir.resolve("modbridge-rp.mcpack"), "resources"); + if (bp.uuid().equalsIgnoreCase(rp.uuid())) { + throw new InstallException("ビヘイビアパックとリソースパックの UUID が同じ"); + } + if (!(bp.manifest().get("dependencies") instanceof JsonArray dependencies)) { + throw new InstallException("ビヘイビアパックにリソースパックへの依存が無い"); + } + var references = dependencies.asList().stream().filter(JsonElement::isJsonObject) + .map(JsonElement::getAsJsonObject) + .filter(entry -> entry.get("uuid") instanceof JsonPrimitive uuid && uuid.isString() + && !entry.has("module_name") && rp.uuid().equalsIgnoreCase(uuid.getAsString())) + .toList(); + if (references.size() != 1 || !matchesVersion(references.getFirst().get("version"), rp.version())) { + throw new InstallException("ビヘイビアパックの依存先とリソースパックの UUID または版が一致しない。同じ compile の出力を使う"); + } + return new Bundle(bp, rp); + } + + private static boolean matchesVersion(JsonElement declared, List version) { + String dotted = version.get(0) + "." + version.get(1) + "." + version.get(2); + return GSON.toJsonTree(version).equals(declared) + || (declared instanceof JsonPrimitive value && value.isString() && dotted.equals(value.getAsString())); + } + /** .mcpack を上限付きで展開し、manifest の UUID・版・モジュールの種類を確かめる。 */ private static Pack readPack(Path mcpack, String moduleType) throws InstallException { if (!Files.isRegularFile(mcpack)) { @@ -666,7 +700,7 @@ private static Pack readPack(Path mcpack, String moduleType) throws InstallExcep throw new InstallException(mcpack.getFileName() + " のモジュールの種類か版が違う"); } java.util.UUID.fromString(uuid); - return new Pack(uuid, List.copyOf(version), files); + return new Pack(uuid, List.copyOf(version), manifest, files); } catch (RuntimeException e) { throw new InstallException(mcpack.getFileName() + " の manifest.json が無いか不正", e); } diff --git a/core/src/test/java/dev/puicore/modbridge/core/bds/BdsInstallerTest.java b/core/src/test/java/dev/puicore/modbridge/core/bds/BdsInstallerTest.java index bffb816..de84b77 100644 --- a/core/src/test/java/dev/puicore/modbridge/core/bds/BdsInstallerTest.java +++ b/core/src/test/java/dev/puicore/modbridge/core/bds/BdsInstallerTest.java @@ -17,6 +17,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; import java.util.List; import java.util.Map; @@ -113,6 +114,27 @@ Map snapshot() throws IOException { assertEquals(before, snapshot()); } + @Test + void 同じUUIDで版が違うパックを組み合わせると何も変更せず拒否する() throws Exception { + BdsInstaller.Plan initial = BdsInstaller.plan(bds, packs, null); + BdsInstaller.apply(bds, packs, null, true); + Path newer = dir.resolve("new-output"); + compilePacks(newer, "other_ingot"); + BdsInstaller.Plan updated = BdsInstaller.plan(bds, newer, null); + assertTrue(updated.ok(), updated.problems().toString()); + assertEquals(initial.rpUuid(), updated.rpUuid()); + assertFalse(initial.rpVersion().equals(updated.rpVersion())); + Files.copy(newer.resolve("modbridge-rp.mcpack"), packs.resolve("modbridge-rp.mcpack"), + StandardCopyOption.REPLACE_EXISTING); + Map before = snapshot(); + BdsInstaller.Plan mixed = BdsInstaller.plan(bds, packs, null); + assertFalse(mixed.ok()); + assertTrue(mixed.actions().isEmpty()); + assertTrue(mixed.problems().stream().anyMatch(reason -> reason.contains("UUID または版"))); + assertThrows(BdsInstaller.InstallException.class, () -> BdsInstaller.apply(bds, packs, null, true)); + assertEquals(before, snapshot()); + } + @Test void ワールドのDBがロックされていればBDSが動いているとみなして配置しない() throws IOException { Map before = snapshot(); diff --git a/docs/adr/0007-bds-install-and-rollback.md b/docs/adr/0007-bds-install-and-rollback.md index d5b606f..38c0e4d 100644 --- a/docs/adr/0007-bds-install-and-rollback.md +++ b/docs/adr/0007-bds-install-and-rollback.md @@ -38,6 +38,7 @@ - **dry-run が既定**: `--apply` を付けない限り、何も変更しない。 - **停止の確認**: `--confirm-stopped` の明示に加えて、`db/LOCK` があれば LevelDB のロックを取れるか確かめる。Windows では `MANIFEST-<番号>` と `<番号>.log` も、書き込みを共有しない読み取りモードで開く。開いている書き込みハンドルがあれば断る。読み取りは共有するため、配置中にハンドルを保持したままバックアップできる。DB がまだ無いワールドでは、明示だけを使う。 +- **パックの組み合わせ**: BP の manifest にある RP への依存 UUID・版と、提供された RP のヘッダーを照合する。組が違えば dry-run と apply の両方で拒否する。apply は検査した組を使って配置する。Script API のモジュール依存を含む BP も同じ検査を通す。[Bedrock のパック manifest](https://learn.microsoft.com/en-us/minecraft/creator/reference/content/addonsreference/packmanifest) - **バックアップ**: 配置の前に、ワールドのフォルダー全体・自分のパックのフォルダー・2 つの JSON を `modbridge-backups//` に保存する。何があり、何が無かったかを `backup.json` に記録する。`db/LOCK` は保存しない(ロック中で読めない場合があり、LevelDB が作り直すため)。 - **入れ替え**: パックのフォルダーもワールドも、一時フォルダーに書き終えてから入れ替える。複製の途中で失敗しても、元のフォルダーは残る。入れ替えに失敗し、元のフォルダーを戻すのにも失敗したときは、元のフォルダーを消さずに残し、その名前を示す。JSON を戻すときも、一時ファイルを経由する。 - **失敗時の復旧**: 配置の途中で失敗したら、配置で変えたもの(パックのフォルダーと 2 つの JSON)をバックアップから戻す。ワールドの DB はロック中で、配置では触らないので戻さない。復旧にも失敗したら、バックアップの ID を示して rollback を促す。