9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-22 16:39:30 +00:00
Files
LeavesMC/patches/server/0102-Linear-region-file-format.patch
2025-01-15 14:10:10 +08:00

888 lines
44 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: violetc <58360096+s-yh-china@users.noreply.github.com>
Date: Sun, 14 Jan 2024 22:22:57 +0800
Subject: [PATCH] Linear region file format
This patch is Powered by LinearPurpur(https://github.com/StupidCraft/LinearPurpur)
diff --git a/build.gradle.kts b/build.gradle.kts
index 0add94373b7b38f271893b1c44a448051b845e8b..8559918f8f91e6137ac873b37af29aa3b6c0c4a0 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -43,6 +43,10 @@ dependencies {
alsoShade(log4jPlugins.output)
implementation("io.netty:netty-codec-haproxy:4.1.97.Final") // Paper - Add support for proxy protocol
// Paper end
+ // Leaves start - Linear format
+ implementation("com.github.luben:zstd-jni:1.5.6-3")
+ implementation("org.lz4:lz4-java:1.8.0")
+ // Leaves end - Linear format
implementation("org.apache.logging.log4j:log4j-iostreams:2.22.1") // Paper - remove exclusion
implementation("org.ow2.asm:asm-commons:9.7.1")
implementation("org.spongepowered:configurate-yaml:4.2.0-SNAPSHOT") // Paper - config files
diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/ChunkSystemRegionFileStorage.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/ChunkSystemRegionFileStorage.java
index a814512fcfb85312474ae2c2c21443843bf57831..c56812494d2cd270eb66bde1f0a8a9b4100777bd 100644
--- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/ChunkSystemRegionFileStorage.java
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/ChunkSystemRegionFileStorage.java
@@ -8,9 +8,9 @@ public interface ChunkSystemRegionFileStorage {
public boolean moonrise$doesRegionFileNotExistNoIO(final int chunkX, final int chunkZ);
- public RegionFile moonrise$getRegionFileIfLoaded(final int chunkX, final int chunkZ);
+ public org.leavesmc.leaves.region.AbstractRegionFile moonrise$getRegionFileIfLoaded(final int chunkX, final int chunkZ);
- public RegionFile moonrise$getRegionFileIfExists(final int chunkX, final int chunkZ) throws IOException;
+ public org.leavesmc.leaves.region.AbstractRegionFile moonrise$getRegionFileIfExists(final int chunkX, final int chunkZ) throws IOException;
public MoonriseRegionFileIO.RegionDataController.WriteData moonrise$startWrite(
final int chunkX, final int chunkZ, final CompoundTag compound
diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/MoonriseRegionFileIO.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/MoonriseRegionFileIO.java
index 1acea58838f057ab87efd103cbecb6f5aeaef393..740cb0c60e6220ca20f3dd9b6f4063737c8adf06 100644
--- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/MoonriseRegionFileIO.java
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/io/MoonriseRegionFileIO.java
@@ -1462,7 +1462,7 @@ public final class MoonriseRegionFileIO {
public static interface IORunnable {
- public void run(final RegionFile regionFile) throws IOException;
+ public void run(final org.leavesmc.leaves.region.AbstractRegionFile regionFile) throws IOException; // Leaves
}
}
diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/storage/ChunkSystemChunkBuffer.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/storage/ChunkSystemChunkBuffer.java
index 51c126735ace8fdde89ad97b5cab62f244212db0..054d03b0417077b30c34dbea1f3dc6b6de11b41b 100644
--- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/storage/ChunkSystemChunkBuffer.java
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/storage/ChunkSystemChunkBuffer.java
@@ -8,5 +8,5 @@ public interface ChunkSystemChunkBuffer {
public void moonrise$setWriteOnClose(final boolean value);
- public void moonrise$write(final RegionFile regionFile) throws IOException;
+ public void moonrise$write(final org.leavesmc.leaves.region.AbstractRegionFile regionFile) throws IOException; // Leaves
}
diff --git a/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java b/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
index 622d0cbe023774d92d212f242b60b96317720835..c182e2d26a72ebd28636b50bbbc050e34fa600cc 100644
--- a/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
+++ b/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
@@ -75,7 +75,7 @@ public class WorldUpgrader implements AutoCloseable {
volatile int skipped;
final Reference2FloatMap<ResourceKey<Level>> progressMap = Reference2FloatMaps.synchronize(new Reference2FloatOpenHashMap());
volatile Component status = Component.translatable("optimizeWorld.stage.counting");
- static final Pattern REGEX = Pattern.compile("^r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.mca$");
+ static final Pattern REGEX = Pattern.compile("^r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.(linear | mca)$"); // Leaves
final DimensionDataStorage overworldDataStorage;
public WorldUpgrader(LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, RegistryAccess dynamicRegistryManager, boolean eraseCache, boolean recreateRegionFiles) {
@@ -399,7 +399,7 @@ public class WorldUpgrader implements AutoCloseable {
private static List<WorldUpgrader.FileToUpgrade> getAllChunkPositions(RegionStorageInfo key, Path regionDirectory) {
File[] afile = regionDirectory.toFile().listFiles((file, s) -> {
- return s.endsWith(".mca");
+ return s.endsWith(".mca") || s.endsWith(".linear"); // Leaves
});
if (afile == null) {
@@ -419,7 +419,7 @@ public class WorldUpgrader implements AutoCloseable {
List<ChunkPos> list1 = Lists.newArrayList();
try {
- RegionFile regionfile = new RegionFile(key, file.toPath(), regionDirectory, true);
+ org.leavesmc.leaves.region.AbstractRegionFile regionfile = org.leavesmc.leaves.region.AbstractRegionFileFactory.getAbstractRegionFile(key, file.toPath(), regionDirectory, true); // Leaves
try {
for (int i1 = 0; i1 < 32; ++i1) {
@@ -482,7 +482,7 @@ public class WorldUpgrader implements AutoCloseable {
protected abstract boolean tryProcessOnePosition(T storage, ChunkPos chunkPos, ResourceKey<Level> worldKey);
- private void onFileFinished(RegionFile regionFile) {
+ private void onFileFinished(org.leavesmc.leaves.region.AbstractRegionFile regionFile) { // Leaves
if (WorldUpgrader.this.recreateRegionFiles) {
if (this.previousWriteFuture != null) {
this.previousWriteFuture.join();
@@ -507,7 +507,7 @@ public class WorldUpgrader implements AutoCloseable {
}
}
- static record FileToUpgrade(RegionFile file, List<ChunkPos> chunksToUpgrade) {
+ static record FileToUpgrade(org.leavesmc.leaves.region.AbstractRegionFile file, List<ChunkPos> chunksToUpgrade) { // Leaves
}
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
index 16f07007a0f73ec0c6f421c9b082518e87e8cc7b..b171304ad750a70944f48ddacb93836d3696af90 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
@@ -28,7 +28,7 @@ import net.minecraft.nbt.NbtIo; // Paper
import net.minecraft.world.level.ChunkPos;
import org.slf4j.Logger;
-public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patches.chunk_system.storage.ChunkSystemRegionFile { // Paper - rewrite chunk system
+public class RegionFile implements AutoCloseable, org.leavesmc.leaves.region.AbstractRegionFile { // Paper - rewrite chunk system // Leaves
private static final Logger LOGGER = LogUtils.getLogger();
private static final int SECTOR_BYTES = 4096;
@@ -129,7 +129,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche
}
// note: only call for CHUNK regionfiles
- boolean recalculateHeader() throws IOException {
+ public boolean recalculateHeader() throws IOException { // Leaves - make it public
if (!this.canRecalcHeader) {
return false;
}
@@ -810,7 +810,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche
}
}
- protected synchronized void write(ChunkPos pos, ByteBuffer buf) throws IOException {
+ public synchronized void write(ChunkPos pos, ByteBuffer buf) throws IOException { // Leaves - protected -> public
int i = RegionFile.getOffsetIndex(pos);
int j = this.offsets.get(i);
int k = RegionFile.getSectorNumber(j);
@@ -952,10 +952,10 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche
private static int getChunkIndex(int x, int z) {
return (x & 31) + (z & 31) * 32;
}
- synchronized boolean isOversized(int x, int z) {
+ public synchronized boolean isOversized(int x, int z) { // Leaves
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 { // Leaves
final int offset = getChunkIndex(x, z);
boolean previous = this.oversized[offset] == 1;
this.oversized[offset] = (byte) (oversized ? 1 : 0);
@@ -994,7 +994,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 CompoundTag getOversizedData(int x, int z) throws IOException {
+ public synchronized CompoundTag getOversizedData(int x, int z) throws IOException { // Leaves
Path file = getOversizedFile(x, z);
try (DataInputStream out = new DataInputStream(new java.io.BufferedInputStream(new InflaterInputStream(Files.newInputStream(file))))) {
return NbtIo.read((java.io.DataInput) out);
@@ -1021,7 +1021,7 @@ public class RegionFile implements AutoCloseable, ca.spottedleaf.moonrise.patche
}
@Override
- public final void moonrise$write(final RegionFile regionFile) throws IOException {
+ public final void moonrise$write(final org.leavesmc.leaves.region.AbstractRegionFile regionFile) throws IOException { // Leaves
regionFile.write(this.pos, ByteBuffer.wrap(this.buf, 0, this.count));
}
// Paper end - rewrite chunk system
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
index e40665cead218502b44dd49051a53326ed94f061..a6f5d79b12a118bc9ed3d1bdba8a68dc10ed5be3 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
@@ -23,7 +23,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
public static final String ANVIL_EXTENSION = ".mca";
private static final int MAX_CACHE_SIZE = 256;
- public final Long2ObjectLinkedOpenHashMap<RegionFile> regionCache = new Long2ObjectLinkedOpenHashMap();
+ public final Long2ObjectLinkedOpenHashMap<org.leavesmc.leaves.region.AbstractRegionFile> regionCache = new Long2ObjectLinkedOpenHashMap(); // Leaves
private final RegionStorageInfo info;
private final Path folder;
private final boolean sync;
@@ -33,9 +33,15 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
private static final int MAX_NON_EXISTING_CACHE = 1024 * 4;
private final it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet nonExistingRegionFiles = new it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet();
private static String getRegionFileName(final int chunkX, final int chunkZ) {
- return "r." + (chunkX >> REGION_SHIFT) + "." + (chunkZ >> REGION_SHIFT) + ".mca";
+ return "r." + (chunkX >> REGION_SHIFT) + "." + (chunkZ >> REGION_SHIFT) + (org.leavesmc.leaves.LeavesConfig.region.format != org.leavesmc.leaves.region.RegionFileFormat.LINEAR ? ".mca" : ".linear"); // Leaves
}
+ // Leaves start
+ private static String getOtherRegionFileName(final int chunkX, final int chunkZ) {
+ return "r." + (chunkX >> REGION_SHIFT) + "." + (chunkZ >> REGION_SHIFT) + (org.leavesmc.leaves.LeavesConfig.region.format == org.leavesmc.leaves.region.RegionFileFormat.LINEAR ? ".mca" : ".linear");
+ }
+ // Leaves end
+
private boolean doesRegionFilePossiblyExist(final long position) {
synchronized (this.nonExistingRegionFiles) {
if (this.nonExistingRegionFiles.contains(position)) {
@@ -68,15 +74,15 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
}
@Override
- public synchronized final RegionFile moonrise$getRegionFileIfLoaded(final int chunkX, final int chunkZ) {
+ public synchronized final org.leavesmc.leaves.region.AbstractRegionFile moonrise$getRegionFileIfLoaded(final int chunkX, final int chunkZ) { // Leaves
return this.regionCache.getAndMoveToFirst(ChunkPos.asLong(chunkX >> REGION_SHIFT, chunkZ >> REGION_SHIFT));
}
@Override
- public synchronized final RegionFile moonrise$getRegionFileIfExists(final int chunkX, final int chunkZ) throws IOException {
+ public synchronized final org.leavesmc.leaves.region.AbstractRegionFile moonrise$getRegionFileIfExists(final int chunkX, final int chunkZ) throws IOException { // Leaves
final long key = ChunkPos.asLong(chunkX >> REGION_SHIFT, chunkZ >> REGION_SHIFT);
- RegionFile ret = this.regionCache.getAndMoveToFirst(key);
+ org.leavesmc.leaves.region.AbstractRegionFile ret = this.regionCache.getAndMoveToFirst(key); // Leaves
if (ret != null) {
return ret;
}
@@ -88,19 +94,23 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
if (this.regionCache.size() >= io.papermc.paper.configuration.GlobalConfiguration.get().misc.regionFileCacheSize) { // Paper
this.regionCache.removeLast().close();
}
-
- final Path regionPath = this.folder.resolve(getRegionFileName(chunkX, chunkZ));
+ // Leaves start
+ Path regionPath = this.folder.resolve(getRegionFileName(chunkX, chunkZ));
if (!java.nio.file.Files.exists(regionPath)) {
- this.markNonExisting(key);
- return null;
+ regionPath = this.folder.resolve(getOtherRegionFileName(chunkX, chunkZ));
+ if (!java.nio.file.Files.exists(regionPath)) {
+ this.markNonExisting(key);
+ return null;
+ }
}
+ // Leaves end
this.createRegionFile(key);
FileUtil.createDirectoriesSafe(this.folder);
- ret = new RegionFile(this.info, regionPath, this.folder, this.sync);
+ ret = org.leavesmc.leaves.region.AbstractRegionFileFactory.getAbstractRegionFile(this.info, regionPath, this.folder, this.sync); // Leaves
this.regionCache.putAndMoveToFirst(key, ret);
@@ -119,7 +129,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
}
final ChunkPos pos = new ChunkPos(chunkX, chunkZ);
- final RegionFile regionFile = this.getRegionFile(pos);
+ final org.leavesmc.leaves.region.AbstractRegionFile regionFile = this.getRegionFile(pos); // Leaves
// note: not required to keep regionfile loaded after this call, as the write param takes a regionfile as input
// (and, the regionfile parameter is unused for writing until the write call)
@@ -153,7 +163,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
) throws IOException {
final ChunkPos pos = new ChunkPos(chunkX, chunkZ);
if (writeData.result() == ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO.RegionDataController.WriteData.WriteResult.DELETE) {
- final RegionFile regionFile = this.moonrise$getRegionFileIfExists(chunkX, chunkZ);
+ final org.leavesmc.leaves.region.AbstractRegionFile regionFile = this.moonrise$getRegionFileIfExists(chunkX, chunkZ); // Leaves
if (regionFile != null) {
regionFile.clear(pos);
} // else: didn't exist
@@ -168,7 +178,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
public final ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO.RegionDataController.ReadData moonrise$readData(
final int chunkX, final int chunkZ
) throws IOException {
- final RegionFile regionFile = this.moonrise$getRegionFileIfExists(chunkX, chunkZ);
+ final org.leavesmc.leaves.region.AbstractRegionFile regionFile = this.moonrise$getRegionFileIfExists(chunkX, chunkZ); // Leaves
final DataInputStream input = regionFile == null ? null : regionFile.getChunkDataInputStream(new ChunkPos(chunkX, chunkZ));
@@ -250,12 +260,12 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
}
// Paper start - rewrite chunk system
- public RegionFile getRegionFile(ChunkPos chunkcoordintpair) throws IOException {
+ public org.leavesmc.leaves.region.AbstractRegionFile getRegionFile(ChunkPos chunkcoordintpair) throws IOException { // Leaves
return this.getRegionFile(chunkcoordintpair, false);
}
// Paper end - rewrite chunk system
- public RegionFile getRegionFile(ChunkPos chunkcoordintpair, boolean existingOnly) throws IOException { // CraftBukkit // Paper - public
+ public org.leavesmc.leaves.region.AbstractRegionFile getRegionFile(ChunkPos chunkcoordintpair, boolean existingOnly) throws IOException { // CraftBukkit // Paper - public // Leaves
// Paper start - rewrite chunk system
if (existingOnly) {
return this.moonrise$getRegionFileIfExists(chunkcoordintpair.x, chunkcoordintpair.z);
@@ -263,7 +273,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
synchronized (this) {
final long key = ChunkPos.asLong(chunkcoordintpair.x >> REGION_SHIFT, chunkcoordintpair.z >> REGION_SHIFT);
- RegionFile ret = this.regionCache.getAndMoveToFirst(key);
+ org.leavesmc.leaves.region.AbstractRegionFile ret = this.regionCache.getAndMoveToFirst(key); // Leaves
if (ret != null) {
return ret;
}
@@ -278,7 +288,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
FileUtil.createDirectoriesSafe(this.folder);
- ret = new RegionFile(this.info, regionPath, this.folder, this.sync);
+ ret = org.leavesmc.leaves.region.AbstractRegionFileFactory.getAbstractRegionFile(this.info, regionPath, this.folder, this.sync); // Leaves
this.regionCache.putAndMoveToFirst(key, ret);
@@ -292,7 +302,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
org.apache.logging.log4j.LogManager.getLogger().fatal(msg + " (" + file.toString().replaceAll(".+[\\\\/]", "") + " - " + x + "," + z + ") Go clean it up to remove this message. /minecraft:tp " + (x<<4)+" 128 "+(z<<4) + " - DO NOT REPORT THIS TO PAPER - You may ask for help on Discord, but do not file an issue. These error messages can not be removed.");
}
- private static CompoundTag readOversizedChunk(RegionFile regionfile, ChunkPos chunkCoordinate) throws IOException {
+ private static CompoundTag readOversizedChunk(org.leavesmc.leaves.region.AbstractRegionFile regionfile, ChunkPos chunkCoordinate) throws IOException { // Leaves
synchronized (regionfile) {
try (DataInputStream datainputstream = regionfile.getChunkDataInputStream(chunkCoordinate)) {
CompoundTag oversizedData = regionfile.getOversizedData(chunkCoordinate.x, chunkCoordinate.z);
@@ -327,7 +337,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
@Nullable
public CompoundTag read(ChunkPos pos) throws IOException {
// CraftBukkit start - SPIGOT-5680: There's no good reason to preemptively create files on read, save that for writing
- RegionFile regionfile = this.getRegionFile(pos, true);
+ org.leavesmc.leaves.region.AbstractRegionFile regionfile = this.getRegionFile(pos, true); // Paper // Leaves
if (regionfile == null) {
return null;
}
@@ -391,7 +401,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
public void scanChunk(ChunkPos chunkPos, StreamTagVisitor scanner) throws IOException {
// CraftBukkit start - SPIGOT-5680: There's no good reason to preemptively create files on read, save that for writing
- RegionFile regionfile = this.getRegionFile(chunkPos, true);
+ org.leavesmc.leaves.region.AbstractRegionFile regionfile = this.getRegionFile(chunkPos, true); // Leaves
if (regionfile == null) {
return;
}
@@ -421,7 +431,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
}
public void write(ChunkPos pos, @Nullable CompoundTag nbt) throws IOException { // Paper - rewrite chunk system - public
- RegionFile regionfile = this.getRegionFile(pos, nbt == null); // CraftBukkit // Paper - rewrite chunk system
+ org.leavesmc.leaves.region.AbstractRegionFile regionfile = this.getRegionFile(pos, nbt == null); // CraftBukkit // Paper - rewrite chunk system // Leaves
// Paper start - rewrite chunk system
if (regionfile == null) {
// if the RegionFile doesn't exist, no point in deleting from it
@@ -432,8 +442,33 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
if (nbt == null) {
regionfile.clear(pos);
} else {
- DataOutputStream dataoutputstream = regionfile.getChunkDataOutputStream(pos);
+ // Leaves start - auto convert anvil to linear
+ DataOutputStream dataoutputstream;
+
+ if (regionfile instanceof RegionFile && org.leavesmc.leaves.LeavesConfig.region.format == org.leavesmc.leaves.region.RegionFileFormat.LINEAR && org.leavesmc.leaves.LeavesConfig.region.linear.autoConvertAnvilToLinear) {
+ Path linearFilePath = Path.of(regionfile.getPath().toString().replaceAll(".mca", ".linear"));
+ try (org.leavesmc.leaves.region.LinearRegionFile linearRegionFile = new org.leavesmc.leaves.region.LinearRegionFile(linearFilePath, org.leavesmc.leaves.LeavesConfig.region.linear.compressionLevel)) {
+ DataInputStream regionDataInputStream = regionfile.getChunkDataInputStream(pos);
+ if (regionDataInputStream == null) {
+ return; // ?
+ }
+ CompoundTag compoundTag = NbtIo.read(regionDataInputStream);
+ try (DataOutputStream linearDataOutputStream = linearRegionFile.getChunkDataOutputStream(pos)) {
+ NbtIo.write(compoundTag, linearDataOutputStream);
+ }
+
+ linearRegionFile.flush();
+ if(java.nio.file.Files.isRegularFile(regionfile.getPath())) {
+ java.nio.file.Files.delete(regionfile.getPath());
+ }
+
+ dataoutputstream = linearRegionFile.getChunkDataOutputStream(pos);
+ }
+ } else {
+ dataoutputstream = regionfile.getChunkDataOutputStream(pos);
+ }
+ // leaves end - auto convert anvil to linear
try {
NbtIo.write(nbt, (DataOutput) dataoutputstream);
regionfile.setOversized(pos.x, pos.z, false); // Paper - We don't do this anymore, mojang stores differently, but clear old meta flag if it exists to get rid of our own meta file once last oversized is gone
@@ -465,7 +500,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
// Paper start - rewrite chunk system
synchronized (this) {
final ExceptionCollector<IOException> exceptionCollector = new ExceptionCollector<>();
- for (final RegionFile regionFile : this.regionCache.values()) {
+ for (final org.leavesmc.leaves.region.AbstractRegionFile regionFile : this.regionCache.values()) { // Leaves
try {
regionFile.close();
} catch (final IOException ex) {
@@ -482,7 +517,7 @@ public class RegionFileStorage implements AutoCloseable, ca.spottedleaf.moonrise
// Paper start - rewrite chunk system
synchronized (this) {
final ExceptionCollector<IOException> exceptionCollector = new ExceptionCollector<>();
- for (final RegionFile regionFile : this.regionCache.values()) {
+ for (final org.leavesmc.leaves.region.AbstractRegionFile regionFile : this.regionCache.values()) { // Leaves
try {
regionFile.flush();
} catch (final IOException ex) {
diff --git a/src/main/java/org/leavesmc/leaves/region/AbstractRegionFile.java b/src/main/java/org/leavesmc/leaves/region/AbstractRegionFile.java
new file mode 100644
index 0000000000000000000000000000000000000000..3f95d7ffa880c598242d38899f71174e4e264338
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/region/AbstractRegionFile.java
@@ -0,0 +1,41 @@
+package org.leavesmc.leaves.region;
+
+import ca.spottedleaf.moonrise.patches.chunk_system.storage.ChunkSystemRegionFile;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.world.level.ChunkPos;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Path;
+
+public interface AbstractRegionFile extends ChunkSystemRegionFile {
+
+ Path getPath();
+
+ DataInputStream getChunkDataInputStream(ChunkPos pos) throws IOException;
+
+ DataOutputStream getChunkDataOutputStream(ChunkPos pos) throws IOException;
+
+ CompoundTag getOversizedData(int x, int z) throws IOException;
+
+ boolean recalculateHeader() throws IOException ;
+
+ boolean hasChunk(ChunkPos pos);
+
+ boolean doesChunkExist(ChunkPos pos);
+
+ boolean isOversized(int x, int z);
+
+ void flush() throws IOException;
+
+ void close() throws IOException;
+
+ void clear(ChunkPos pos) throws IOException;
+
+ void setOversized(int x, int z, boolean oversized) throws IOException;
+
+ void write(ChunkPos pos, ByteBuffer buf) throws IOException;
+}
+
diff --git a/src/main/java/org/leavesmc/leaves/region/AbstractRegionFileFactory.java b/src/main/java/org/leavesmc/leaves/region/AbstractRegionFileFactory.java
new file mode 100644
index 0000000000000000000000000000000000000000..8df8f28d15736c4e363e2abdec0a56d6ccb15861
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/region/AbstractRegionFileFactory.java
@@ -0,0 +1,24 @@
+package org.leavesmc.leaves.region;
+
+import net.minecraft.world.level.chunk.storage.RegionFile;
+import net.minecraft.world.level.chunk.storage.RegionFileVersion;
+import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
+import org.leavesmc.leaves.LeavesConfig;
+
+import java.io.IOException;
+import java.nio.file.Path;
+
+public class AbstractRegionFileFactory {
+
+ public static AbstractRegionFile getAbstractRegionFile(RegionStorageInfo storageKey, Path directory, Path path, boolean dsync) throws IOException {
+ return getAbstractRegionFile(storageKey, directory, path, RegionFileVersion.getCompressionFormat(), dsync);
+ }
+
+ public static AbstractRegionFile getAbstractRegionFile(RegionStorageInfo storageKey, Path path, Path directory, RegionFileVersion compressionFormat, boolean dsync) throws IOException {
+ if (path.toString().endsWith(".linear")) {
+ return new LinearRegionFile(path, LeavesConfig.region.linear.compressionLevel);
+ } else {
+ return new RegionFile(storageKey, path, directory, compressionFormat, dsync);
+ }
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/region/LinearRegionFile.java b/src/main/java/org/leavesmc/leaves/region/LinearRegionFile.java
new file mode 100644
index 0000000000000000000000000000000000000000..f903895b53eada8795a6ee9ab33cf30bf69c2c98
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/region/LinearRegionFile.java
@@ -0,0 +1,331 @@
+package org.leavesmc.leaves.region;
+
+import ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO;
+import com.github.luben.zstd.ZstdInputStream;
+import com.github.luben.zstd.ZstdOutputStream;
+import com.mojang.logging.LogUtils;
+
+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.FileInputStream;
+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.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import javax.annotation.Nullable;
+
+import net.jpountz.lz4.LZ4Compressor;
+import net.jpountz.lz4.LZ4Factory;
+import net.jpountz.lz4.LZ4FastDecompressor;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.world.level.ChunkPos;
+import org.slf4j.Logger;
+
+// Powered by LinearPurpur(https://github.com/StupidCraft/LinearPurpur)
+public class LinearRegionFile implements AbstractRegionFile, AutoCloseable {
+
+ private static final long SUPERBLOCK = -4323716122432332390L;
+ private static final byte VERSION = 2;
+ private static final int HEADER_SIZE = 32;
+ private static final int FOOTER_SIZE = 8;
+ private static final Logger LOGGER = LogUtils.getLogger();
+ private static final List<Byte> SUPPORTED_VERSIONS = Arrays.asList((byte) 1, (byte) 2);
+ private static final LinearRegionFileFlusher linearRegionFileFlusher = new LinearRegionFileFlusher();
+ private final byte[][] buffer = new byte[1024][];
+ private final int[] bufferUncompressedSize = new int[1024];
+ private final int[] chunkTimestamps = new int[1024];
+ private final LZ4Compressor compressor;
+ private final LZ4FastDecompressor decompressor;
+ private final int compressionLevel;
+ private final AtomicBoolean markedToSave = new AtomicBoolean(false);
+ public boolean closed = false;
+ public Path path;
+
+ public LinearRegionFile(Path file, int compression) throws IOException {
+ this.path = file;
+ this.compressionLevel = compression;
+ this.compressor = LZ4Factory.fastestInstance().fastCompressor();
+ this.decompressor = LZ4Factory.fastestInstance().fastDecompressor();
+
+ File regionFile = new File(this.path.toString());
+
+ Arrays.fill(this.bufferUncompressedSize, 0);
+
+ if (!regionFile.canRead()) {
+ return;
+ }
+
+ try (FileInputStream fileStream = new FileInputStream(regionFile);
+ DataInputStream rawDataStream = new DataInputStream(fileStream)) {
+
+ long superBlock = rawDataStream.readLong();
+ if (superBlock != SUPERBLOCK) {
+ throw new RuntimeException("Invalid superblock: " + superBlock + " in " + file);
+ }
+
+ byte version = rawDataStream.readByte();
+ if (!SUPPORTED_VERSIONS.contains(version)) {
+ throw new RuntimeException("Invalid version: " + version + " in " + file);
+ }
+
+ // Skip newestTimestamp (Long) + Compression level (Byte) + Chunk count (Short): Unused.
+ rawDataStream.skipBytes(11);
+
+ int dataCount = rawDataStream.readInt();
+ long fileLength = file.toFile().length();
+ if (fileLength != HEADER_SIZE + dataCount + FOOTER_SIZE) {
+ throw new IOException("Invalid file length: " + this.path + " " + fileLength + " " + (HEADER_SIZE + dataCount + FOOTER_SIZE));
+ }
+
+ rawDataStream.skipBytes(8); // Skip data hash (Long): Unused.
+
+ byte[] rawCompressed = new byte[dataCount];
+ rawDataStream.readFully(rawCompressed, 0, dataCount);
+
+ superBlock = rawDataStream.readLong();
+ if (superBlock != SUPERBLOCK) {
+ throw new IOException("Footer superblock invalid " + this.path);
+ }
+
+ try (DataInputStream dataStream = new DataInputStream(new ZstdInputStream(new ByteArrayInputStream(rawCompressed)))) {
+ int[] starts = new int[1024];
+ for (int i = 0; i < 1024; i++) {
+ starts[i] = dataStream.readInt();
+ dataStream.skipBytes(4); // Skip timestamps (Int): Unused.
+ }
+
+ for (int i = 0; i < 1024; i++) {
+ if (starts[i] > 0) {
+ int size = starts[i];
+ byte[] b = new byte[size];
+ dataStream.readFully(b, 0, size);
+
+ int maxCompressedLength = this.compressor.maxCompressedLength(size);
+ byte[] compressed = new byte[maxCompressedLength];
+ int compressedLength = this.compressor.compress(b, 0, size, compressed, 0, maxCompressedLength);
+ b = new byte[compressedLength];
+ System.arraycopy(compressed, 0, b, 0, compressedLength);
+
+ this.buffer[i] = b;
+ this.bufferUncompressedSize[i] = size;
+ }
+ }
+ }
+ }
+ }
+
+ private static int getChunkIndex(int x, int z) {
+ return (x & 31) + ((z & 31) << 5);
+ }
+
+ private static int getTimestamp() {
+ return (int) (System.currentTimeMillis() / 1000L);
+ }
+
+ public Path getPath() {
+ return this.path;
+ }
+
+ public void flush() throws IOException {
+ if (isMarkedToSave()) flushWrapper(); // sync
+ }
+
+ private void markToSave() {
+ linearRegionFileFlusher.scheduleSave(this);
+ markedToSave.set(true);
+ }
+
+ public boolean isMarkedToSave() {
+ return markedToSave.getAndSet(false);
+ }
+
+ public void flushWrapper() {
+ try {
+ save();
+ } catch (IOException e) {
+ LOGGER.error("Failed to flush region file {}", path.toAbsolutePath(), e);
+ }
+ }
+
+ public boolean doesChunkExist(ChunkPos pos) {
+ return false;
+ }
+
+ private synchronized void save() throws IOException {
+ long timestamp = getTimestamp();
+ short chunkCount = 0;
+
+ File tempFile = new File(path.toString() + ".tmp");
+
+ try (FileOutputStream fileStream = new FileOutputStream(tempFile);
+ ByteArrayOutputStream zstdByteArray = new ByteArrayOutputStream();
+ ZstdOutputStream zstdStream = new ZstdOutputStream(zstdByteArray, this.compressionLevel);
+ DataOutputStream zstdDataStream = new DataOutputStream(zstdStream);
+ DataOutputStream dataStream = new DataOutputStream(fileStream)) {
+
+ dataStream.writeLong(SUPERBLOCK);
+ dataStream.writeByte(VERSION);
+ dataStream.writeLong(timestamp);
+ dataStream.writeByte(this.compressionLevel);
+
+ ArrayList<byte[]> byteBuffers = new ArrayList<>();
+ for (int i = 0; i < 1024; i++) {
+ if (this.bufferUncompressedSize[i] != 0) {
+ chunkCount += 1;
+ byte[] content = new byte[bufferUncompressedSize[i]];
+ this.decompressor.decompress(buffer[i], 0, content, 0, bufferUncompressedSize[i]);
+
+ byteBuffers.add(content);
+ } else {
+ byteBuffers.add(null);
+ }
+ }
+ for (int i = 0; i < 1024; i++) {
+ zstdDataStream.writeInt(this.bufferUncompressedSize[i]); // Write uncompressed size
+ zstdDataStream.writeInt(this.chunkTimestamps[i]); // Write timestamp
+ }
+ for (int i = 0; i < 1024; i++) {
+ if (byteBuffers.get(i) != null) {
+ zstdDataStream.write(byteBuffers.get(i), 0, byteBuffers.get(i).length);
+ }
+ }
+ zstdDataStream.close();
+
+ dataStream.writeShort(chunkCount);
+
+ byte[] compressed = zstdByteArray.toByteArray();
+
+ dataStream.writeInt(compressed.length);
+ dataStream.writeLong(0);
+
+ dataStream.write(compressed, 0, compressed.length);
+ dataStream.writeLong(SUPERBLOCK);
+
+ dataStream.flush();
+ fileStream.getFD().sync();
+ fileStream.getChannel().force(true); // Ensure atomicity on Btrfs
+ }
+ Files.move(tempFile.toPath(), this.path, StandardCopyOption.REPLACE_EXISTING);
+ }
+
+ public synchronized void write(ChunkPos pos, ByteBuffer buffer) {
+ try {
+ byte[] b = toByteArray(new ByteArrayInputStream(buffer.array()));
+ int uncompressedSize = b.length;
+
+ 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.path);
+ }
+ markToSave();
+ }
+
+ public DataOutputStream getChunkDataOutputStream(ChunkPos pos) {
+ return new DataOutputStream(new BufferedOutputStream(new ChunkBuffer(pos)));
+ }
+
+ 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();
+ }
+
+ @Nullable
+ public synchronized DataInputStream getChunkDataInputStream(ChunkPos pos) {
+ 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 void clear(ChunkPos pos) {
+ int i = getChunkIndex(pos.x, pos.z);
+ this.buffer[i] = null;
+ this.bufferUncompressedSize[i] = 0;
+ this.chunkTimestamps[i] = getTimestamp();
+ markToSave();
+ }
+
+ public boolean hasChunk(ChunkPos pos) {
+ return this.bufferUncompressedSize[getChunkIndex(pos.x, pos.z)] > 0;
+ }
+
+ public void close() throws IOException {
+ if (closed) {
+ return;
+ }
+
+ closed = true;
+ flush(); // sync
+ }
+
+ 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.path);
+ }
+
+ @Override
+ public boolean recalculateHeader() {
+ return false;
+ }
+
+ public boolean isOversized(int x, int z) {
+ return false;
+ }
+
+ @Override
+ public MoonriseRegionFileIO.RegionDataController.WriteData moonrise$startWrite(CompoundTag data, ChunkPos pos) {
+ DataOutputStream buff = getChunkDataOutputStream(pos);
+ return new MoonriseRegionFileIO.RegionDataController.WriteData(
+ data, MoonriseRegionFileIO.RegionDataController.WriteData.WriteResult.WRITE,
+ buff, LinearRegionFile::writeNothing
+ );
+ }
+
+ private static void writeNothing(AbstractRegionFile regionFile) {
+ }
+
+ private class ChunkBuffer extends ByteArrayOutputStream {
+ private final ChunkPos pos;
+
+ public ChunkBuffer(ChunkPos chunkcoordintpair) {
+ super();
+ this.pos = chunkcoordintpair;
+ }
+
+ public void close() {
+ ByteBuffer bytebuffer = ByteBuffer.wrap(this.buf, 0, this.count);
+ LinearRegionFile.this.write(this.pos, bytebuffer);
+ }
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/region/LinearRegionFileFlusher.java b/src/main/java/org/leavesmc/leaves/region/LinearRegionFileFlusher.java
new file mode 100644
index 0000000000000000000000000000000000000000..ab6346b678dd619bb7e2fa5e79bd82b7268485f1
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/region/LinearRegionFileFlusher.java
@@ -0,0 +1,56 @@
+package org.leavesmc.leaves.region;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+import java.util.Queue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.leavesmc.leaves.LeavesConfig;
+import org.leavesmc.leaves.LeavesLogger;
+
+// Powered by LinearPurpur(https://github.com/StupidCraft/LinearPurpur)
+public class LinearRegionFileFlusher {
+
+ private final Queue<LinearRegionFile> savingQueue = new LinkedBlockingQueue<>();
+ private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
+ new ThreadFactoryBuilder()
+ .setNameFormat("linear-flush-scheduler")
+ .build()
+ );
+ private final ExecutorService executor = Executors.newFixedThreadPool(
+ LeavesConfig.region.linear.getLinearFlushThreads(),
+ new ThreadFactoryBuilder()
+ .setNameFormat("linear-flusher-%d")
+ .build()
+ );
+
+ public LinearRegionFileFlusher() {
+ LeavesLogger.LOGGER.info("Using " + LeavesConfig.region.linear.getLinearFlushThreads() + " threads for linear region flushing.");
+ scheduler.scheduleAtFixedRate(this::pollAndFlush, 0L, LeavesConfig.region.linear.flushFrequency, TimeUnit.SECONDS);
+ }
+
+ public void scheduleSave(LinearRegionFile regionFile) {
+ if (savingQueue.contains(regionFile)) {
+ return;
+ }
+ savingQueue.add(regionFile);
+ }
+
+ private void pollAndFlush() {
+ while (!savingQueue.isEmpty()) {
+ LinearRegionFile regionFile = savingQueue.poll();
+ if (!regionFile.closed && regionFile.isMarkedToSave()) {
+ executor.execute(regionFile::flushWrapper);
+ }
+ }
+ }
+
+ public void shutdown() {
+ executor.shutdown();
+ scheduler.shutdown();
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/region/RegionFileFormat.java b/src/main/java/org/leavesmc/leaves/region/RegionFileFormat.java
new file mode 100644
index 0000000000000000000000000000000000000000..2469211b0dfa42d87d855abc7b1303d2852d99a0
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/region/RegionFileFormat.java
@@ -0,0 +1,5 @@
+package org.leavesmc.leaves.region;
+
+public enum RegionFileFormat {
+ ANVIL, LINEAR
+}