Voxel Umstellung vollendet

This commit is contained in:
2026-10-05 08:58:59 +02:00
parent 4ed61998dd
commit 7069c5f632
90 changed files with 67 additions and 84 deletions

View File

@@ -74,73 +74,10 @@ public final class VoxelChunkIO {
}
}
// ── Pre-Bake-Backups ──────────────────────────────────────────────────────
/** Backup-Pfad der .blvc-Datei (wird vor dem Bake angelegt). */
public static Path getPreBakePath(int cx, int cy, int cz) {
return getPath(cx, cy, cz).resolveSibling(
getPath(cx, cy, cz).getFileName() + ".prebake");
}
public static boolean preBakeExists(int cx, int cy, int cz) {
return Files.exists(getPreBakePath(cx, cy, cz));
}
/** Kopiert die aktuelle .blvc-Datei als Pre-Bake-Backup. */
public static void savePreBake(int cx, int cy, int cz) throws IOException {
Path src = getPath(cx, cy, cz);
if (!Files.exists(src)) return;
Files.copy(src, getPreBakePath(cx, cy, cz), StandardCopyOption.REPLACE_EXISTING);
}
public static void deletePreBake(int cx, int cy, int cz) throws IOException {
Files.deleteIfExists(getPreBakePath(cx, cy, cz));
}
/** Stellt die .blvc-Datei aus dem Backup wieder her. */
public static void restorePreBake(int cx, int cy, int cz) throws IOException {
Path backup = getPreBakePath(cx, cy, cz);
if (!Files.exists(backup)) return;
Files.copy(backup, getPath(cx, cy, cz), StandardCopyOption.REPLACE_EXISTING);
}
/** true wenn mindestens ein Pre-Bake-Backup im Chunks-Verzeichnis existiert. */
public static boolean hasAnyPreBake() {
Path dir = ChunkTerrainIO.chunksDir();
if (!Files.isDirectory(dir)) return false;
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, "voxel_*.blvc.prebake")) {
return ds.iterator().hasNext();
} catch (IOException e) { return false; }
}
/** Alle Chunk-Koordinaten für die ein Pre-Bake-Backup existiert. */
public static List<int[]> listPreBakeCoords() {
List<int[]> result = new java.util.ArrayList<>();
Path dir = ChunkTerrainIO.chunksDir();
if (!Files.isDirectory(dir)) return result;
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, "voxel_*.blvc.prebake")) {
for (Path p : ds) {
String name = p.getFileName().toString()
.replace("voxel_", "").replace(".blvc.prebake", "");
String[] parts = name.split("_");
if (parts.length != 3) continue;
try {
int cx = Integer.parseInt(parts[0]);
int cy = parts[1].startsWith("m")
? -Integer.parseInt(parts[1].substring(1))
: Integer.parseInt(parts[1]);
int cz = Integer.parseInt(parts[2]);
result.add(new int[]{cx, cy, cz});
} catch (NumberFormatException ignored) {}
}
} catch (IOException ignored) {}
return result;
}
/**
* Liest alle vorhandenen VoxelChunks aus dem Chunks-Verzeichnis.
* Für Koordinaten, bei denen die .blvc nach dem Backen gelöscht wurde,
* wird automatisch die .blvc.prebake als Fallback herangezogen.
* Vorhandene .blvc.prebake-Dateien (Altbestand) werden einmalig als .blvc migriert
* und danach gelöscht.
* Gibt leere Liste zurück wenn kein Chunks-Verzeichnis existiert.
*/
public static List<VoxelChunk> loadAll() {
@@ -148,7 +85,6 @@ public final class VoxelChunkIO {
Path dir = ChunkTerrainIO.chunksDir();
if (!Files.isDirectory(dir)) return result;
// Aktive .blvc-Dateien einlesen
Set<String> loaded = new java.util.HashSet<>();
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, "voxel_*.blvc")) {
for (Path p : ds) {
@@ -168,12 +104,17 @@ public final class VoxelChunkIO {
}
} catch (IOException ignored) {}
// Prebake-Fallback: Koordinaten die gebacken wurden haben keine .blvc mehr
// Einmalige Migration: .blvc.prebake → .blvc für Altbestand ohne aktive .blvc.
// Erst kopieren, dann löschen – umgekehrte Reihenfolge würde Daten vernichten.
List<Path> prebakeToDelete = new java.util.ArrayList<>();
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, "voxel_*.blvc.prebake")) {
for (Path p : ds) {
String name = p.getFileName().toString()
.replace("voxel_", "").replace(".blvc.prebake", "");
if (loaded.contains(name)) continue; // aktive .blvc hat Vorrang
if (loaded.contains(name)) {
prebakeToDelete.add(p); // .blvc existiert bereits → nur löschen
continue;
}
String[] parts = name.split("_");
if (parts.length != 3) continue;
try {
@@ -182,10 +123,15 @@ public final class VoxelChunkIO {
? -Integer.parseInt(parts[1].substring(1))
: Integer.parseInt(parts[1]);
int cz = Integer.parseInt(parts[2]);
result.add(VoxelChunk.deserialize(Files.readAllBytes(p), cx, cy, cz));
Path blvc = getPath(cx, cy, cz);
Files.copy(p, blvc, StandardCopyOption.REPLACE_EXISTING); // erst kopieren
result.add(VoxelChunk.deserialize(Files.readAllBytes(blvc), cx, cy, cz));
loaded.add(name);
prebakeToDelete.add(p); // dann löschen
} catch (Exception ignored) {}
}
} catch (IOException ignored) {}
for (Path p : prebakeToDelete) { try { Files.deleteIfExists(p); } catch (IOException ignored) {} }
return result;
}