diff --git a/build-data/divinemc.at b/build-data/divinemc.at index 016f220..dda3c44 100644 --- a/build-data/divinemc.at +++ b/build-data/divinemc.at @@ -15,6 +15,11 @@ public net.minecraft.world.entity.ai.sensing.Sensor timeToTick public net.minecraft.world.entity.animal.armadillo.Armadillo scuteTime public net.minecraft.world.entity.animal.frog.Tadpole getTicksLeftUntilAdult()I public net.minecraft.world.level.chunk.PaletteResize +public net.minecraft.world.level.chunk.storage.RegionFile getOversizedData(II)Lnet/minecraft/nbt/CompoundTag; +public net.minecraft.world.level.chunk.storage.RegionFile isOversized(II)Z +public net.minecraft.world.level.chunk.storage.RegionFile recalculateHeader()Z +public net.minecraft.world.level.chunk.storage.RegionFile setOversized(IIZ)V +public net.minecraft.world.level.chunk.storage.RegionFile write(Lnet/minecraft/world/level/ChunkPos;Ljava/nio/ByteBuffer;)V public net.minecraft.world.level.entity.EntityTickList entities public net.minecraft.world.level.levelgen.DensityFunctions$BlendAlpha public net.minecraft.world.level.levelgen.DensityFunctions$BlendDensity diff --git a/divinemc-server/build.gradle.kts.patch b/divinemc-server/build.gradle.kts.patch index 4540517..8658595 100644 --- a/divinemc-server/build.gradle.kts.patch +++ b/divinemc-server/build.gradle.kts.patch @@ -72,7 +72,7 @@ } } val log4jPlugins = sourceSets.create("log4jPlugins") { -@@ -156,10 +_,20 @@ +@@ -156,10 +_,21 @@ } dependencies { @@ -86,6 +86,7 @@ + } + implementation("com.github.luben:zstd-jni:1.5.7-3") + implementation("org.lz4:lz4-java:1.8.0") ++ implementation("net.openhft:zero-allocation-hashing:0.16") + // DivineMC end - Dependencies + implementation("ca.spottedleaf:concurrentutil:0.0.3") diff --git a/divinemc-server/minecraft-patches/features/0096-Buffered-Linear-region-format.patch b/divinemc-server/minecraft-patches/features/0096-Linear-region-file-format.patch similarity index 90% rename from divinemc-server/minecraft-patches/features/0096-Buffered-Linear-region-format.patch rename to divinemc-server/minecraft-patches/features/0096-Linear-region-file-format.patch index 2f42a2f..907250e 100644 --- a/divinemc-server/minecraft-patches/features/0096-Buffered-Linear-region-format.patch +++ b/divinemc-server/minecraft-patches/features/0096-Linear-region-file-format.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Fri, 11 Jul 2025 21:47:45 +0300 -Subject: [PATCH] Buffered Linear region format +Subject: [PATCH] Linear region file format diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/io/ChunkSystemRegionFileStorage.java b/ca/spottedleaf/moonrise/patches/chunk_system/io/ChunkSystemRegionFileStorage.java @@ -126,7 +126,7 @@ index 79d57ca8a7870a02e95562d89cbd4341d8282660..1156772217b139d54266f470b18d4a98 class PoiUpgrader extends WorldUpgrader.SimpleRegionStorageUpgrader { diff --git a/net/minecraft/world/level/chunk/storage/RegionFile.java b/net/minecraft/world/level/chunk/storage/RegionFile.java -index 22f3aa1674664906e8ec45372d758d79017e3987..55eaf7a5d4ceb957717298991fecce0b81c0f377 100644 +index ae0a893498d0bfe90c14508f15b431d4885e06ff..00656cf8634e06f7ce1067ef7ba44edfb4519be3 100644 --- a/net/minecraft/world/level/chunk/storage/RegionFile.java +++ b/net/minecraft/world/level/chunk/storage/RegionFile.java @@ -22,7 +22,7 @@ import net.minecraft.util.profiling.jfr.JvmProfiler; @@ -138,24 +138,6 @@ index 22f3aa1674664906e8ec45372d758d79017e3987..55eaf7a5d4ceb957717298991fecce0b private static final Logger LOGGER = LogUtils.getLogger(); public static final int MAX_CHUNK_SIZE = 500 * 1024 * 1024; // Paper - don't write garbage data to disk if writing serialization fails private static final int SECTOR_BYTES = 4096; -@@ -130,7 +130,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche - return this.recalculateCount.get(); - } - -- boolean recalculateHeader() throws IOException { -+ public boolean recalculateHeader() throws IOException { // DivineMC - Buffered Linear region format - if (!this.canRecalcHeader) { - return false; - } -@@ -794,7 +794,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche - } - } - -- protected synchronized void write(ChunkPos chunkPos, ByteBuffer chunkData) throws IOException { -+ public synchronized void write(ChunkPos chunkPos, ByteBuffer chunkData) throws IOException { // DivineMC - Buffered Linear region format - int offsetIndex = getOffsetIndex(chunkPos); - int i = this.offsets.get(offsetIndex); - int sectorNumber = getSectorNumber(i); @@ -912,7 +912,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche } @@ -165,29 +147,6 @@ index 22f3aa1674664906e8ec45372d758d79017e3987..55eaf7a5d4ceb957717298991fecce0b regionFile.write(this.pos, ByteBuffer.wrap(this.buf, 0, this.count)); } // Paper end - rewrite chunk system -@@ -978,11 +978,11 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche - return (x & 31) + (z & 31) * 32; - } - -- synchronized boolean isOversized(int x, int z) { -+ public synchronized boolean isOversized(int x, int z) { // DivineMC - Buffered Linear region format - return this.oversized[getChunkIndex(x, z)] == 1; - } - -- synchronized void setOversized(int x, int z, boolean oversized) throws IOException { -+ public synchronized void setOversized(int x, int z, boolean oversized) throws IOException { // DivineMC - Buffered Linear region format - final int offset = getChunkIndex(x, z); - boolean previous = this.oversized[offset] == 1; - this.oversized[offset] = (byte) (oversized ? 1 : 0); -@@ -1021,7 +1021,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche - return this.path.getParent().resolve(this.path.getFileName().toString().replaceAll("\\.mca$", "") + "_oversized_" + x + "_" + z + ".nbt"); - } - -- synchronized net.minecraft.nbt.CompoundTag getOversizedData(int x, int z) throws IOException { -+ public synchronized net.minecraft.nbt.CompoundTag getOversizedData(int x, int z) throws IOException { - Path file = getOversizedFile(x, z); - try (DataInputStream out = new DataInputStream(new java.io.BufferedInputStream(new java.util.zip.InflaterInputStream(Files.newInputStream(file))))) { - return net.minecraft.nbt.NbtIo.read((java.io.DataInput) out); diff --git a/net/minecraft/world/level/chunk/storage/RegionFileStorage.java b/net/minecraft/world/level/chunk/storage/RegionFileStorage.java index 8d1174f25e0e90d0533970f4ddd8448442024936..ee797d6b3cd898cba1abd3422cb54b17eb4a639f 100644 --- a/net/minecraft/world/level/chunk/storage/RegionFileStorage.java diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/config/DivineConfig.java b/divinemc-server/src/main/java/org/bxteam/divinemc/config/DivineConfig.java index 1b4303f..7fd8e16 100644 --- a/divinemc-server/src/main/java/org/bxteam/divinemc/config/DivineConfig.java +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/config/DivineConfig.java @@ -12,6 +12,7 @@ import org.bukkit.configuration.MemoryConfiguration; import org.bxteam.divinemc.config.annotations.Experimental; import org.bxteam.divinemc.entity.pathfinding.PathfindTaskRejectPolicy; import org.bxteam.divinemc.region.EnumRegionFileExtension; +import org.bxteam.divinemc.region.type.LinearRegionFile; import org.bxteam.divinemc.server.network.AsyncJoinHandler; import org.jetbrains.annotations.Nullable; import org.simpleyaml.configuration.comments.CommentType; @@ -599,6 +600,9 @@ public class DivineConfig { // Region Format public static EnumRegionFileExtension regionFileType = EnumRegionFileExtension.MCA; public static int linearCompressionLevel = 1; + public static int linearIoThreadCount = 6; + public static int linearIoFlushDelayMs = 100; + public static boolean linearUseVirtualThreads = true; // Sentry public static String sentryDsn = ""; @@ -649,18 +653,39 @@ public class DivineConfig { } private static void regionFileExtension() { - regionFileType = EnumRegionFileExtension.fromString(getString(ConfigCategory.MISC.key("region-format.type"), regionFileType.toString(), + EnumRegionFileExtension configuredType = EnumRegionFileExtension.fromString(getString(ConfigCategory.MISC.key("region-format.type"), regionFileType.toString(), "The type of region file format to use for storing chunk data.", "Valid values:", " - MCA: Default Minecraft region file format", - " - B_LINEAR: Buffered region file format")); + " - LINEAR: Linear region file format V2", + " - B_LINEAR: Buffered region file format (just uses Zstd)")); + + if (configuredType != null) { + regionFileType = configuredType; + } else { + LOGGER.warn("Invalid region file type: {}, resetting to default (MCA)", getString(ConfigCategory.MISC.key("region-format.type"), regionFileType.toString())); + regionFileType = EnumRegionFileExtension.MCA; + } + linearCompressionLevel = getInt(ConfigCategory.MISC.key("region-format.compression-level"), linearCompressionLevel, "The compression level to use for the linear region file format."); + linearIoThreadCount = getInt(ConfigCategory.MISC.key("region-format.linear-io-thread-count"), linearIoThreadCount, + "The number of threads to use for IO operations."); + linearIoFlushDelayMs = getInt(ConfigCategory.MISC.key("region-format.linear-io-flush-delay-ms"), linearIoFlushDelayMs, + "The delay in milliseconds to wait before flushing IO operations."); + linearUseVirtualThreads = getBoolean(ConfigCategory.MISC.key("region-format.linear-use-virtual-threads"), linearUseVirtualThreads, + "Whether to use virtual threads for IO operations that was introduced in Java 21."); if (linearCompressionLevel > 22 || linearCompressionLevel < 1) { LOGGER.warn("Invalid linear compression level: {}, resetting to default (1)", linearCompressionLevel); linearCompressionLevel = 1; } + + if (regionFileType == EnumRegionFileExtension.LINEAR) { + LinearRegionFile.SAVE_DELAY_MS = linearIoFlushDelayMs; + LinearRegionFile.SAVE_THREAD_MAX_COUNT = linearIoThreadCount; + LinearRegionFile.USE_VIRTUAL_THREAD = linearUseVirtualThreads; + } } private static void sentrySettings() { diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/region/EnumRegionFileExtension.java b/divinemc-server/src/main/java/org/bxteam/divinemc/region/EnumRegionFileExtension.java index 99e9eff..17eef9a 100644 --- a/divinemc-server/src/main/java/org/bxteam/divinemc/region/EnumRegionFileExtension.java +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/region/EnumRegionFileExtension.java @@ -2,10 +2,13 @@ package org.bxteam.divinemc.region; import net.minecraft.world.level.chunk.storage.RegionFile; import org.bxteam.divinemc.config.DivineConfig; +import org.bxteam.divinemc.region.type.BufferedRegionFile; +import org.bxteam.divinemc.region.type.LinearRegionFile; import org.jetbrains.annotations.Nullable; public enum EnumRegionFileExtension { - MCA("mca", "mca" , (info) -> new RegionFile(info.info(), info.filePath(), info.folder(), info.sync())), + MCA("mca", "mca", (info) -> new RegionFile(info.info(), info.filePath(), info.folder(), info.sync())), + LINEAR("linear", "linear", (info) -> new LinearRegionFile(info.info(), info.filePath(), info.folder(), info.sync(), DivineConfig.MiscCategory.linearCompressionLevel)), B_LINEAR("b_linear", "b_linear", (info) -> new BufferedRegionFile(info.filePath(), DivineConfig.MiscCategory.linearCompressionLevel)); private final String name; diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/region/BufferedRegionFile.java b/divinemc-server/src/main/java/org/bxteam/divinemc/region/type/BufferedRegionFile.java similarity index 99% rename from divinemc-server/src/main/java/org/bxteam/divinemc/region/BufferedRegionFile.java rename to divinemc-server/src/main/java/org/bxteam/divinemc/region/type/BufferedRegionFile.java index bd25e6f..5f773dd 100644 --- a/divinemc-server/src/main/java/org/bxteam/divinemc/region/BufferedRegionFile.java +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/region/type/BufferedRegionFile.java @@ -1,10 +1,12 @@ -package org.bxteam.divinemc.region; +package org.bxteam.divinemc.region.type; import ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO; import net.jpountz.xxhash.XXHash32; import net.jpountz.xxhash.XXHashFactory; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.ChunkPos; +import org.bxteam.divinemc.region.BufferReleaser; +import org.bxteam.divinemc.region.IRegionFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/region/type/LinearRegionFile.java b/divinemc-server/src/main/java/org/bxteam/divinemc/region/type/LinearRegionFile.java new file mode 100644 index 0000000..5b71ff5 --- /dev/null +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/region/type/LinearRegionFile.java @@ -0,0 +1,605 @@ +package org.bxteam.divinemc.region.type; + +import ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO; +import com.github.luben.zstd.ZstdInputStream; +import com.github.luben.zstd.ZstdOutputStream; +import net.jpountz.lz4.LZ4Compressor; +import net.jpountz.lz4.LZ4Factory; +import net.jpountz.lz4.LZ4FastDecompressor; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.chunk.storage.RegionFileVersion; +import net.minecraft.world.level.chunk.storage.RegionStorageInfo; +import net.openhft.hashing.LongHashFunction; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bxteam.divinemc.region.IRegionFile; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; +import java.util.concurrent.locks.ReentrantLock; + +public class LinearRegionFile implements IRegionFile { + private static final long SUPERBLOCK = 0xc3ff13183cca9d9aL; + private static final byte VERSION = 3; + private static final int HEADER_SIZE = 27; + private static final int FOOTER_SIZE = 8; + private static final Logger LOGGER = LogManager.getLogger(LinearRegionFile.class.getSimpleName()); + private static final Object saveLock = new Object(); + + public static final int MAX_CHUNK_SIZE = 500 * 1024 * 1024; + public static int SAVE_THREAD_MAX_COUNT = 6; + public static int SAVE_DELAY_MS = 100; + public static boolean USE_VIRTUAL_THREAD = true; + private static int activeSaveThreads = 0; + + public final ReentrantLock fileLock = new ReentrantLock(true); + public Path regionFile; + public boolean regionFileOpen = false; + + private final byte[][] buffer = new byte[1024][]; + private final int[] bufferUncompressedSize = new int[1024]; + private final long[] chunkTimestamps = new long[1024]; + private final Object markedToSaveLock = new Object(); + private final LZ4Compressor compressor; + private final LZ4FastDecompressor decompressor; + private final int compressionLevel; + private final Thread bindThread; + private final java.util.concurrent.atomic.AtomicInteger recalculateCount = new java.util.concurrent.atomic.AtomicInteger(); + + private byte[][] bucketBuffers; + private boolean markedToSave = false; + private boolean close = false; + private int gridSize = 8; + private int bucketSize = 4; + + public LinearRegionFile(RegionStorageInfo storageKey, Path directory, Path path, boolean dsync, int compressionLevel) throws IOException { + this(storageKey, directory, path, RegionFileVersion.getCompressionFormat(), dsync, compressionLevel); + } + + public LinearRegionFile(RegionStorageInfo storageKey, Path path, Path directory, RegionFileVersion compressionFormat, boolean dsync, int compressionLevel) throws IOException { + Runnable flushCheck = () -> { + while (!close) { + synchronized (saveLock) { + if (markedToSave && activeSaveThreads < SAVE_THREAD_MAX_COUNT) { + activeSaveThreads++; + Runnable flushOperation = () -> { + try { + flush(); + } catch (IOException ex) { + LOGGER.error("Region file {} flush failed", this.regionFile.toAbsolutePath(), ex); + } finally { + synchronized (saveLock) { + activeSaveThreads--; + } + } + }; + + Thread saveThread = USE_VIRTUAL_THREAD ? + Thread.ofVirtual().name("Linear IO - " + LinearRegionFile.this.hashCode()).unstarted(flushOperation) : + Thread.ofPlatform().name("Linear IO - " + LinearRegionFile.this.hashCode()).unstarted(flushOperation); + saveThread.setPriority(Thread.NORM_PRIORITY - 3); + saveThread.start(); + } + } + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(SAVE_DELAY_MS)); + } + }; + this.bindThread = USE_VIRTUAL_THREAD ? Thread.ofVirtual().unstarted(flushCheck) : Thread.ofPlatform().unstarted(flushCheck); + this.bindThread.setName("Linear IO Schedule - " + this.hashCode()); + this.regionFile = path; + this.compressionLevel = compressionLevel; + + this.compressor = LZ4Factory.fastestInstance().fastCompressor(); + this.decompressor = LZ4Factory.fastestInstance().fastDecompressor(); + } + + public Path getRegionFile() { + return this.regionFile; + } + + public ReentrantLock getFileLock() { + return this.fileLock; + } + + public Path getPath() { + return this.regionFile; + } + + public int getRecalculateCount() { + return this.recalculateCount.get(); + } + + public boolean recalculateHeader() { + return false; + } + + private int chunkToBucketIdx(int chunkX, int chunkZ) { + int bx = chunkX / bucketSize, bz = chunkZ / bucketSize; + return bx * gridSize + bz; + } + + private void openBucket(int chunkX, int chunkZ) { + chunkX = Math.floorMod(chunkX, 32); + chunkZ = Math.floorMod(chunkZ, 32); + int idx = chunkToBucketIdx(chunkX, chunkZ); + + if (bucketBuffers == null) return; + if (bucketBuffers[idx] != null) { + try { + ByteArrayInputStream bucketByteStream = new ByteArrayInputStream(bucketBuffers[idx]); + ZstdInputStream zstdStream = new ZstdInputStream(bucketByteStream); + ByteBuffer bucketBuffer = ByteBuffer.wrap(zstdStream.readAllBytes()); + + int bx = chunkX / bucketSize, bz = chunkZ / bucketSize; + + for (int cx = 0; cx < 32 / gridSize; cx++) { + for (int cz = 0; cz < 32 / gridSize; cz++) { + int chunkIndex = (bx * (32 / gridSize) + cx) + (bz * (32 / gridSize) + cz) * 32; + + int chunkSize = bucketBuffer.getInt(); + long timestamp = bucketBuffer.getLong(); + this.chunkTimestamps[chunkIndex] = timestamp; + + if (chunkSize > 0) { + byte[] chunkData = new byte[chunkSize - 8]; + bucketBuffer.get(chunkData); + + int maxCompressedLength = this.compressor.maxCompressedLength(chunkData.length); + byte[] compressed = new byte[maxCompressedLength]; + int compressedLength = this.compressor.compress(chunkData, 0, chunkData.length, compressed, 0, maxCompressedLength); + byte[] finalCompressed = new byte[compressedLength]; + System.arraycopy(compressed, 0, finalCompressed, 0, compressedLength); + + if (chunkX == cx && chunkZ == cz) { + this.buffer[chunkIndex] = finalCompressed; + this.bufferUncompressedSize[chunkIndex] = chunkData.length; + return; + } + this.buffer[chunkIndex] = finalCompressed; + this.bufferUncompressedSize[chunkIndex] = chunkData.length; + } + } + } + } catch (IOException ex) { + LOGGER.error("Region file corrupted: {} bucket: {}", regionFile, idx); + MinecraftServer.getServer().safeShutdown(true, false); + } + bucketBuffers[idx] = null; + } + } + + private synchronized void openRegionFile() { + if (regionFileOpen) return; + regionFileOpen = true; + + File regionFile = new File(this.regionFile.toString()); + + if(!regionFile.canRead()) { + this.bindThread.start(); + return; + } + + try { + byte[] fileContent = Files.readAllBytes(this.regionFile); + ByteBuffer buffer = ByteBuffer.wrap(fileContent); + + long superBlock = buffer.getLong(); + if (superBlock != SUPERBLOCK) + throw new RuntimeException("Invalid superblock: " + superBlock + " file " + this.regionFile); + + byte version = buffer.get(); + if (version == 1 || version == 2) { + parseLinearV1(buffer); + } else if (version == 3) { + parseLinearV2(buffer); + } else { + throw new RuntimeException("Invalid version: " + version + " file " + this.regionFile); + } + + this.bindThread.start(); + } catch (IOException e) { + throw new RuntimeException("Failed to open region file " + this.regionFile, e); + } + } + + private void parseLinearV1(ByteBuffer buffer) throws IOException { + final int HEADER_SIZE = 32; + final int FOOTER_SIZE = 8; + + // Skip newestTimestamp (Long) + Compression level (Byte) + Chunk count (Short): Unused. + buffer.position(buffer.position() + 11); + + int dataCount = buffer.getInt(); + long fileLength = this.regionFile.toFile().length(); + if (fileLength != HEADER_SIZE + dataCount + FOOTER_SIZE) { + throw new IOException("Invalid file length: " + this.regionFile + " " + fileLength + " " + (HEADER_SIZE + dataCount + FOOTER_SIZE)); + } + + buffer.position(buffer.position() + 8); // Skip data hash (Long): Unused. + + byte[] rawCompressed = new byte[dataCount]; + buffer.get(rawCompressed); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(rawCompressed); + ZstdInputStream zstdInputStream = new ZstdInputStream(byteArrayInputStream); + ByteBuffer decompressedBuffer = ByteBuffer.wrap(zstdInputStream.readAllBytes()); + + int[] starts = new int[1024]; + for (int i = 0; i < 1024; i++) { + starts[i] = decompressedBuffer.getInt(); + decompressedBuffer.getInt(); // Skip timestamps (Int): Unused. + } + + for (int i = 0; i < 1024; i++) { + if (starts[i] > 0) { + int size = starts[i]; + byte[] chunkData = new byte[size]; + decompressedBuffer.get(chunkData); + + int maxCompressedLength = this.compressor.maxCompressedLength(size); + byte[] compressed = new byte[maxCompressedLength]; + int compressedLength = this.compressor.compress(chunkData, 0, size, compressed, 0, maxCompressedLength); + byte[] finalCompressed = new byte[compressedLength]; + System.arraycopy(compressed, 0, finalCompressed, 0, compressedLength); + + this.buffer[i] = finalCompressed; + this.bufferUncompressedSize[i] = size; + this.chunkTimestamps[i] = getTimestamp(); // Use current timestamp as we don't have the original + } + } + } + + private void parseLinearV2(ByteBuffer buffer) throws IOException { + buffer.getLong(); // Skip newestTimestamp (Long) + gridSize = buffer.get(); + if (gridSize != 1 && gridSize != 2 && gridSize != 4 && gridSize != 8 && gridSize != 16 && gridSize != 32) + throw new RuntimeException("Invalid grid size: " + gridSize + " file " + this.regionFile); + bucketSize = 32 / gridSize; + + buffer.getInt(); // Skip region_x (Int) + buffer.getInt(); // Skip region_z (Int) + + boolean[] chunkExistenceBitmap = deserializeExistenceBitmap(buffer); + + while (true) { + byte featureNameLength = buffer.get(); + if (featureNameLength == 0) break; + byte[] featureNameBytes = new byte[featureNameLength]; + buffer.get(featureNameBytes); + String featureName = new String(featureNameBytes); + int featureValue = buffer.getInt(); + // System.out.println("NBT Feature: " + featureName + " = " + featureValue); + } + + int[] bucketSizes = new int[gridSize * gridSize]; + byte[] bucketCompressionLevels = new byte[gridSize * gridSize]; + long[] bucketHashes = new long[gridSize * gridSize]; + for (int i = 0; i < gridSize * gridSize; i++) { + bucketSizes[i] = buffer.getInt(); + bucketCompressionLevels[i] = buffer.get(); + bucketHashes[i] = buffer.getLong(); + } + + bucketBuffers = new byte[gridSize * gridSize][]; + for (int i = 0; i < gridSize * gridSize; i++) { + if (bucketSizes[i] > 0) { + bucketBuffers[i] = new byte[bucketSizes[i]]; + buffer.get(bucketBuffers[i]); + long rawHash = LongHashFunction.xx().hashBytes(bucketBuffers[i]); + if (rawHash != bucketHashes[i]) throw new IOException("Region file hash incorrect " + this.regionFile); + } + } + + long footerSuperBlock = buffer.getLong(); + if (footerSuperBlock != SUPERBLOCK) + throw new IOException("Footer superblock invalid " + this.regionFile); + } + + @Override + public MoonriseRegionFileIO.RegionDataController.WriteData moonrise$startWrite(CompoundTag data, ChunkPos pos) throws IOException { + final DataOutputStream out = this.getChunkDataOutputStream(pos); + + return new ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO.RegionDataController.WriteData( + data, ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO.RegionDataController.WriteData.WriteResult.WRITE, + out, regionFile -> out.close() + ); + } + + private synchronized void markToSave() { + synchronized(markedToSaveLock) { + markedToSave = true; + } + } + + private synchronized boolean isMarkedToSave() { + synchronized(markedToSaveLock) { + if(markedToSave) { + markedToSave = false; + return true; + } + return false; + } + } + + public synchronized boolean doesChunkExist(ChunkPos pos) throws Exception { + openRegionFile(); + throw new Exception("doesChunkExist is a stub"); + } + + public synchronized boolean hasChunk(ChunkPos pos) { + openRegionFile(); + openBucket(pos.x, pos.z); + return this.bufferUncompressedSize[getChunkIndex(pos.x, pos.z)] > 0; + } + + public synchronized void write(ChunkPos pos, ByteBuffer buffer) { + openRegionFile(); + openBucket(pos.x, pos.z); + try { + byte[] b = toByteArray(new ByteArrayInputStream(buffer.array())); + int uncompressedSize = b.length; + + if (uncompressedSize > MAX_CHUNK_SIZE) { + LOGGER.error("Chunk dupe attempt {}", this.regionFile); + clear(pos); + } else { + int maxCompressedLength = this.compressor.maxCompressedLength(b.length); + byte[] compressed = new byte[maxCompressedLength]; + int compressedLength = this.compressor.compress(b, 0, b.length, compressed, 0, maxCompressedLength); + b = new byte[compressedLength]; + System.arraycopy(compressed, 0, b, 0, compressedLength); + + int index = getChunkIndex(pos.x, pos.z); + this.buffer[index] = b; + this.chunkTimestamps[index] = getTimestamp(); + this.bufferUncompressedSize[getChunkIndex(pos.x, pos.z)] = uncompressedSize; + } + } catch (IOException e) { + LOGGER.error("Chunk write IOException {} {}", e, this.regionFile); + } + markToSave(); + } + + public DataOutputStream getChunkDataOutputStream(ChunkPos pos) { + openRegionFile(); + openBucket(pos.x, pos.z); + return new DataOutputStream(new BufferedOutputStream(new LinearRegionFile.ChunkBuffer(pos))); + } + + @Nullable + public synchronized DataInputStream getChunkDataInputStream(ChunkPos pos) { + openRegionFile(); + openBucket(pos.x, pos.z); + + if(this.bufferUncompressedSize[getChunkIndex(pos.x, pos.z)] != 0) { + byte[] content = new byte[bufferUncompressedSize[getChunkIndex(pos.x, pos.z)]]; + this.decompressor.decompress(this.buffer[getChunkIndex(pos.x, pos.z)], 0, content, 0, bufferUncompressedSize[getChunkIndex(pos.x, pos.z)]); + return new DataInputStream(new ByteArrayInputStream(content)); + } + return null; + } + + public synchronized void clear(ChunkPos pos) { + openRegionFile(); + openBucket(pos.x, pos.z); + int i = getChunkIndex(pos.x, pos.z); + this.buffer[i] = null; + this.bufferUncompressedSize[i] = 0; + this.chunkTimestamps[i] = 0; + markToSave(); + } + + public synchronized void close() throws IOException { + openRegionFile(); + close = true; + try { + flush(); + } catch(IOException e) { + throw new IOException("Region flush IOException " + e + " " + this.regionFile); + } + } + + public synchronized void flush() throws IOException { + if (!isMarkedToSave()) return; + + openRegionFile(); + + long timestamp = getTimestamp(); + + long writeStart = System.nanoTime(); + File tempFile = new File(regionFile.toString() + ".tmp"); + FileOutputStream fileStream = new FileOutputStream(tempFile); + DataOutputStream dataStream = new DataOutputStream(fileStream); + + dataStream.writeLong(SUPERBLOCK); + dataStream.writeByte(VERSION); + dataStream.writeLong(timestamp); + dataStream.writeByte(gridSize); + + String fileName = regionFile.getFileName().toString(); + String[] parts = fileName.split("\\."); + int regionX = 0; + int regionZ = 0; + try { + if (parts.length >= 4) { + regionX = Integer.parseInt(parts[1]); + regionZ = Integer.parseInt(parts[2]); + } else { + LOGGER.warn("Unexpected file name format: {}", fileName); + } + } catch (NumberFormatException e) { + LOGGER.error("Failed to parse region coordinates from file name: {}", fileName, e); + } + + dataStream.writeInt(regionX); + dataStream.writeInt(regionZ); + + boolean[] chunkExistenceBitmap = new boolean[1024]; + for (int i = 0; i < 1024; i++) { + chunkExistenceBitmap[i] = (this.bufferUncompressedSize[i] > 0); + } + writeSerializedExistenceBitmap(dataStream, chunkExistenceBitmap); + + writeNBTFeatures(dataStream); + + int bucketMisses = 0; + byte[][] buckets = new byte[gridSize * gridSize][]; + for (int bx = 0; bx < gridSize; bx++) { + for (int bz = 0; bz < gridSize; bz++) { + if (bucketBuffers != null && bucketBuffers[bx * gridSize + bz] != null) { + buckets[bx * gridSize + bz] = bucketBuffers[bx * gridSize + bz]; + continue; + } + bucketMisses++; + + ByteArrayOutputStream bucketStream = new ByteArrayOutputStream(); + ZstdOutputStream zstdStream = new ZstdOutputStream(bucketStream, this.compressionLevel); + DataOutputStream bucketDataStream = new DataOutputStream(zstdStream); + + boolean hasData = false; + for (int cx = 0; cx < 32 / gridSize; cx++) { + for (int cz = 0; cz < 32 / gridSize; cz++) { + int chunkIndex = (bx * 32 / gridSize + cx) + (bz * 32 / gridSize + cz) * 32; + if (this.bufferUncompressedSize[chunkIndex] > 0) { + hasData = true; + byte[] chunkData = new byte[this.bufferUncompressedSize[chunkIndex]]; + this.decompressor.decompress(this.buffer[chunkIndex], 0, chunkData, 0, this.bufferUncompressedSize[chunkIndex]); + bucketDataStream.writeInt(chunkData.length + 8); + bucketDataStream.writeLong(this.chunkTimestamps[chunkIndex]); + bucketDataStream.write(chunkData); + } else { + bucketDataStream.writeInt(0); + bucketDataStream.writeLong(this.chunkTimestamps[chunkIndex]); + } + } + } + bucketDataStream.close(); + + if (hasData) { + buckets[bx * gridSize + bz] = bucketStream.toByteArray(); + } + } + } + + for (int i = 0; i < gridSize * gridSize; i++) { + dataStream.writeInt(buckets[i] != null ? buckets[i].length : 0); + dataStream.writeByte(this.compressionLevel); + long rawHash = 0; + if (buckets[i] != null) { + rawHash = LongHashFunction.xx().hashBytes(buckets[i]); + } + dataStream.writeLong(rawHash); + } + + for (int i = 0; i < gridSize * gridSize; i++) { + if (buckets[i] != null) { + dataStream.write(buckets[i]); + } + } + + dataStream.writeLong(SUPERBLOCK); + + dataStream.flush(); + fileStream.getFD().sync(); + fileStream.getChannel().force(true); // Ensure atomicity on Btrfs + dataStream.close(); + + fileStream.close(); + Files.move(tempFile.toPath(), this.regionFile, StandardCopyOption.REPLACE_EXISTING); + } + + private void writeNBTFeatures(DataOutputStream dataStream) throws IOException { + // writeNBTFeature(dataStream, "example", 1); + dataStream.writeByte(0); // End of NBT features + } + + private void writeNBTFeature(DataOutputStream dataStream, String featureName, int featureValue) throws IOException { + byte[] featureNameBytes = featureName.getBytes(); + dataStream.writeByte(featureNameBytes.length); + dataStream.write(featureNameBytes); + dataStream.writeInt(featureValue); + } + + private boolean[] deserializeExistenceBitmap(ByteBuffer buffer) { + boolean[] result = new boolean[1024]; + for (int i = 0; i < 128; i++) { + byte b = buffer.get(); + for (int j = 0; j < 8; j++) { + result[i * 8 + j] = ((b >> (7 - j)) & 1) == 1; + } + } + return result; + } + + private void writeSerializedExistenceBitmap(DataOutputStream out, boolean[] bitmap) throws IOException { + for (int i = 0; i < 128; i++) { + byte b = 0; + for (int j = 0; j < 8; j++) { + if (bitmap[i * 8 + j]) { + b |= (1 << (7 - j)); + } + } + out.writeByte(b); + } + } + + private byte[] toByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] tempBuffer = new byte[4096]; + + int length; + while ((length = in.read(tempBuffer)) >= 0) { + out.write(tempBuffer, 0, length); + } + + return out.toByteArray(); + } + + private static int getChunkIndex(int x, int z) { + return (x & 31) + ((z & 31) << 5); + } + + private static int getTimestamp() { + return (int) (System.currentTimeMillis() / 1000L); + } + + public void setOversized(int x, int z, boolean something) { } + + public CompoundTag getOversizedData(int x, int z) throws IOException { + throw new IOException("getOversizedData is a stub " + this.regionFile); + } + + public boolean isOversized(int x, int z) { + return false; + } + + private class ChunkBuffer extends ByteArrayOutputStream { + private final ChunkPos pos; + + public ChunkBuffer(ChunkPos chunkcoordintpair) { + super(); + this.pos = chunkcoordintpair; + } + + public void close() throws IOException { + ByteBuffer bytebuffer = ByteBuffer.wrap(this.buf, 0, this.count); + LinearRegionFile.this.write(this.pos, bytebuffer); + } + } +}