diff --git a/build.gradle.kts b/build.gradle.kts index 06a2a8c..1a1b547 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,6 +21,7 @@ dependencies { compileOnly(minestom) implementation(libs.zstd) + implementation(libs.lz4) // Fastutil is only included because minestom already uses it, otherwise it is a crazy dependency // for how it is used in this project. implementation(libs.fastutil) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c11f178..382dc53 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,15 +1,17 @@ metadata.format.version = "1.1" [versions] -minestom = "6932647e24" -zstd = "1.5.5-3" -fastutil = "8.5.12" +minestom = "12794d4263" +zstd = "1.5.6-4" +lz4 = "1.8.0" +fastutil = "8.5.13" nexuspublish = "1.3.0" [libraries] minestom = { group = "net.minestom", name = "minestom-snapshots", version.ref = "minestom" } zstd = { group = "com.github.luben", name = "zstd-jni", version.ref = "zstd" } +lz4 = { group = "org.lz4", name = "lz4-java", version.ref = "lz4" } fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" } [plugins] diff --git a/src/main/java/net/hollowcube/polar/CompressFormats.java b/src/main/java/net/hollowcube/polar/CompressFormats.java new file mode 100644 index 0000000..f237e15 --- /dev/null +++ b/src/main/java/net/hollowcube/polar/CompressFormats.java @@ -0,0 +1,10 @@ +package net.hollowcube.polar; + +import net.jpountz.lz4.LZ4Factory; + +public final class CompressFormats { + + private CompressFormats() {} + + public static final LZ4Factory LZ_4_FACTORY = LZ4Factory.fastestInstance(); +} diff --git a/src/main/java/net/hollowcube/polar/PolarReader.java b/src/main/java/net/hollowcube/polar/PolarReader.java index d65c2d9..ec70c47 100644 --- a/src/main/java/net/hollowcube/polar/PolarReader.java +++ b/src/main/java/net/hollowcube/polar/PolarReader.java @@ -2,6 +2,7 @@ import com.github.luben.zstd.Zstd; import net.hollowcube.polar.PolarSection.LightContent; +import net.jpountz.lz4.LZ4Factory; import net.kyori.adventure.nbt.BinaryTag; import net.kyori.adventure.nbt.CompoundBinaryTag; import net.minestom.server.network.NetworkBuffer; @@ -216,6 +217,18 @@ private static void validateVersion(int version) { newBuffer.writeIndex(bytes.length); yield newBuffer; } + case LZ4_FAST, LZ4_HIGH_FAST -> { + var bytes = CompressFormats.LZ_4_FACTORY.fastDecompressor().decompress(buffer.readBytes(buffer.readableBytes()), length); + var newBuffer = new NetworkBuffer(ByteBuffer.wrap(bytes)); + newBuffer.writeIndex(bytes.length); + yield newBuffer; + } + case LZ4_SAFE, LZ4_HIGH_SAFE -> { + var bytes = CompressFormats.LZ_4_FACTORY.safeDecompressor().decompress(buffer.readBytes(buffer.readableBytes()), length); + var newBuffer = new NetworkBuffer(ByteBuffer.wrap(bytes)); + newBuffer.writeIndex(bytes.length); + yield newBuffer; + } }; } diff --git a/src/main/java/net/hollowcube/polar/PolarWorld.java b/src/main/java/net/hollowcube/polar/PolarWorld.java index 8b54c0f..60fee85 100644 --- a/src/main/java/net/hollowcube/polar/PolarWorld.java +++ b/src/main/java/net/hollowcube/polar/PolarWorld.java @@ -113,7 +113,11 @@ public void updateChunkAt(int x, int z, @NotNull PolarChunk chunk) { public enum CompressionType { NONE, - ZSTD; + ZSTD, + LZ4_FAST, + LZ4_SAFE, + LZ4_HIGH_FAST, + LZ4_HIGH_SAFE; private static final CompressionType[] VALUES = values(); diff --git a/src/main/java/net/hollowcube/polar/PolarWriter.java b/src/main/java/net/hollowcube/polar/PolarWriter.java index 77d21e2..553d521 100644 --- a/src/main/java/net/hollowcube/polar/PolarWriter.java +++ b/src/main/java/net/hollowcube/polar/PolarWriter.java @@ -42,6 +42,10 @@ public static byte[] write(@NotNull PolarWorld world, @NotNull PolarDataConverte buffer.write(VAR_INT, content.readableBytes()); buffer.write(RAW_BYTES, Zstd.compress(content.readBytes(content.readableBytes()))); } + case LZ4_FAST, LZ4_SAFE -> + buffer.write(RAW_BYTES, CompressFormats.LZ_4_FACTORY.fastCompressor().compress(content.readBytes(content.readableBytes()))); + case LZ4_HIGH_FAST, LZ4_HIGH_SAFE -> + buffer.write(RAW_BYTES, CompressFormats.LZ_4_FACTORY.highCompressor().compress(content.readBytes(content.readableBytes()))); } }); }