Skip to content

Only add lz4 #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/hollowcube/polar/CompressFormats.java
Original file line number Diff line number Diff line change
@@ -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();
}
13 changes: 13 additions & 0 deletions src/main/java/net/hollowcube/polar/PolarReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/hollowcube/polar/PolarWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/hollowcube/polar/PolarWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
});
}
Expand Down