Skip to content

Commit 9cf34ae

Browse files
committed
Don't include velocity native as shaded dependency, add fallback and only use velocity compression if available
1 parent 4084b55 commit 9cf34ae

File tree

10 files changed

+213
-19
lines changed

10 files changed

+213
-19
lines changed

common/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ val viaProxy: Configuration by configurations.creating
99
dependencies {
1010
compileOnly("io.netty:netty-all:4.0.20.Final")
1111
compileOnly("com.google.guava:guava:17.0")
12-
13-
implementation("com.velocitypowered:velocity-native:3.4.0-SNAPSHOT")
12+
compileOnly("com.velocitypowered:velocity-native:3.4.0-SNAPSHOT")
1413

1514
viaProxy("net.raphimc:ViaProxy:[3.0.0,4.0.0)") {
1615
isTransitive = false

common/src/main/java/com/viaversion/viarewind/api/type/chunk/BulkChunkType1_7_6.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package com.viaversion.viarewind.api.type.chunk;
1919

20-
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.Protocol1_8To1_7_6_10;
20+
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor.CompressorUtil;
2121
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
2222
import com.viaversion.viaversion.api.type.Type;
2323
import io.netty.buffer.ByteBuf;
@@ -90,7 +90,7 @@ public void write(ByteBuf buffer, Chunk[] chunks) {
9090

9191
final int startCompressIndex = buffer.writerIndex();
9292
try {
93-
Protocol1_8To1_7_6_10.COMPRESSOR_THREAD_LOCAL.get().deflate(Unpooled.wrappedBuffer(data), buffer);
93+
CompressorUtil.getCompressor().deflate(Unpooled.wrappedBuffer(data), buffer);
9494
} catch (DataFormatException e) {
9595
throw new RuntimeException(e);
9696
}

common/src/main/java/com/viaversion/viarewind/api/type/chunk/ChunkType1_7_6.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package com.viaversion.viarewind.api.type.chunk;
1919

20-
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.Protocol1_8To1_7_6_10;
20+
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor.CompressorUtil;
2121
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
2222
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
2323
import com.viaversion.viaversion.api.minecraft.chunks.DataPalette;
@@ -67,7 +67,7 @@ public void write(ByteBuf buffer, Chunk chunk) {
6767

6868
final int startCompressIndex = buffer.writerIndex();
6969
try {
70-
Protocol1_8To1_7_6_10.COMPRESSOR_THREAD_LOCAL.get().deflate(Unpooled.wrappedBuffer(data), buffer);
70+
CompressorUtil.getCompressor().deflate(Unpooled.wrappedBuffer(data), buffer);
7171
} catch (DataFormatException e) {
7272
throw new RuntimeException(e);
7373
}

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/Protocol1_8To1_7_6_10.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
*/
1818
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10;
1919

20-
import com.velocitypowered.natives.compression.VelocityCompressor;
21-
import com.velocitypowered.natives.util.Natives;
2220
import com.viaversion.viabackwards.api.BackwardsProtocol;
2321
import com.viaversion.viarewind.ViaRewind;
2422
import com.viaversion.viarewind.api.data.RewindMappingData;
@@ -56,10 +54,6 @@
5654
import java.util.concurrent.TimeUnit;
5755

5856
public class Protocol1_8To1_7_6_10 extends BackwardsProtocol<ClientboundPackets1_8, ClientboundPackets1_7_2_5, ServerboundPackets1_8, ServerboundPackets1_7_2_5> {
59-
60-
public static final ThreadLocal<VelocityCompressor> COMPRESSOR_THREAD_LOCAL = ThreadLocal.withInitial(
61-
() -> Natives.compress.get().create(-1)
62-
);
6357
public static final RewindMappingData MAPPINGS = new RewindMappingData("1.8", "1.7.10");
6458

6559
private final BlockItemPacketRewriter1_8 itemRewriter = new BlockItemPacketRewriter1_8(this);

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/provider/compression/CompressionDecoder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
*/
1818
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression;
1919

20-
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.Protocol1_8To1_7_6_10;
20+
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor.CompressorUtil;
2121
import com.viaversion.viaversion.api.type.Types;
2222
import io.netty.buffer.ByteBuf;
2323
import io.netty.channel.ChannelHandlerContext;
2424
import io.netty.handler.codec.DecoderException;
2525
import io.netty.handler.codec.MessageToMessageDecoder;
2626
import java.util.List;
27-
import java.util.zip.Inflater;
2827

2928
public class CompressionDecoder extends MessageToMessageDecoder<ByteBuf> {
30-
private final Inflater inflater = new Inflater();
31-
3229
private int threshold;
3330

3431
public CompressionDecoder(final int threshold) {
@@ -57,7 +54,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
5754

5855
ByteBuf output = ctx.alloc().buffer(outLength);
5956
try {
60-
Protocol1_8To1_7_6_10.COMPRESSOR_THREAD_LOCAL.get().inflate(in, output, outLength);
57+
CompressorUtil.getCompressor().inflate(in, output, outLength);
6158
out.add(output.retain());
6259
} finally {
6360
output.release();

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/provider/compression/CompressionEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression;
1919

20-
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.Protocol1_8To1_7_6_10;
20+
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor.CompressorUtil;
2121
import com.viaversion.viaversion.api.type.Types;
2222
import io.netty.buffer.ByteBuf;
2323
import io.netty.channel.ChannelHandlerContext;
@@ -45,6 +45,6 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws
4545

4646
Types.VAR_INT.writePrimitive(out, frameLength);
4747

48-
Protocol1_8To1_7_6_10.COMPRESSOR_THREAD_LOCAL.get().deflate(in, out);
48+
CompressorUtil.getCompressor().deflate(in, out);
4949
}
5050
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This file is part of ViaRewind - https://github.com/ViaVersion/ViaRewind
3+
* Copyright (C) 2018-2025 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor;
19+
20+
import io.netty.buffer.ByteBuf;
21+
22+
import java.util.zip.DataFormatException;
23+
24+
public interface Compressor {
25+
void inflate(ByteBuf source, ByteBuf destination, int uncompressedSize) throws DataFormatException;
26+
27+
void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of ViaRewind - https://github.com/ViaVersion/ViaRewind
3+
* Copyright (C) 2018-2025 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor;
19+
20+
public class CompressorUtil {
21+
public static boolean VELOCITY_COMPRESSION_PRESENT;
22+
23+
static {
24+
try {
25+
Class.forName("com.velocitypowered.natives.compression.VelocityCompressor");
26+
VELOCITY_COMPRESSION_PRESENT = true;
27+
} catch (ClassNotFoundException e) {
28+
29+
}
30+
}
31+
32+
public static ThreadLocal<Compressor> COMPRESSOR = ThreadLocal.withInitial(() -> VELOCITY_COMPRESSION_PRESENT ? new VelocityCompressor() : new JavaCompressor());
33+
34+
public static Compressor getCompressor() {
35+
return COMPRESSOR.get();
36+
}
37+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (C) 2018-2023 Velocity Contributors
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
// Based on: https://github.com/PaperMC/Velocity/blob/dev/3.0.0/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java
19+
// This fallback exists so that velocity-natives is not a shaded dependency
20+
21+
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor;
22+
23+
import static com.google.common.base.Preconditions.checkArgument;
24+
25+
import io.netty.buffer.ByteBuf;
26+
import java.nio.ByteBuffer;
27+
import java.util.zip.DataFormatException;
28+
import java.util.zip.Deflater;
29+
import java.util.zip.Inflater;
30+
31+
/**
32+
* Implements deflate compression by wrapping {@link Deflater} and {@link Inflater}.
33+
*/
34+
public class JavaCompressor implements Compressor {
35+
static final int ZLIB_BUFFER_SIZE = 8192;
36+
37+
private final Deflater deflater;
38+
private final Inflater inflater;
39+
40+
public JavaCompressor() {
41+
this.deflater = new Deflater();
42+
this.inflater = new Inflater();
43+
}
44+
45+
@Override
46+
public void inflate(ByteBuf source, ByteBuf destination, int uncompressedSize)
47+
throws DataFormatException {
48+
// We (probably) can't nicely deal with >=1 buffer nicely, so let's scream loudly.
49+
checkArgument(source.nioBufferCount() == 1, "source has multiple backing buffers");
50+
checkArgument(destination.nioBufferCount() == 1, "destination has multiple backing buffers");
51+
52+
final int origIdx = source.readerIndex();
53+
inflater.setInput(source.nioBuffer());
54+
55+
try {
56+
final int readable = source.readableBytes();
57+
while (!inflater.finished() && inflater.getBytesRead() < readable) {
58+
if (!destination.isWritable()) {
59+
destination.ensureWritable(ZLIB_BUFFER_SIZE);
60+
}
61+
62+
ByteBuffer destNioBuf = destination.nioBuffer(destination.writerIndex(),
63+
destination.writableBytes());
64+
int produced = inflater.inflate(destNioBuf);
65+
destination.writerIndex(destination.writerIndex() + produced);
66+
}
67+
68+
if (!inflater.finished()) {
69+
throw new DataFormatException("Received a deflate stream that was too large, wanted "
70+
+ uncompressedSize);
71+
}
72+
source.readerIndex(origIdx + inflater.getTotalIn());
73+
} finally {
74+
inflater.reset();
75+
}
76+
}
77+
78+
@Override
79+
public void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException {
80+
// We (probably) can't nicely deal with >=1 buffer nicely, so let's scream loudly.
81+
checkArgument(source.nioBufferCount() == 1, "source has multiple backing buffers");
82+
checkArgument(destination.nioBufferCount() == 1, "destination has multiple backing buffers");
83+
84+
final int origIdx = source.readerIndex();
85+
deflater.setInput(source.nioBuffer());
86+
deflater.finish();
87+
88+
while (!deflater.finished()) {
89+
if (!destination.isWritable()) {
90+
destination.ensureWritable(ZLIB_BUFFER_SIZE);
91+
}
92+
93+
ByteBuffer destNioBuf = destination.nioBuffer(destination.writerIndex(),
94+
destination.writableBytes());
95+
int produced = deflater.deflate(destNioBuf);
96+
destination.writerIndex(destination.writerIndex() + produced);
97+
}
98+
99+
source.readerIndex(origIdx + deflater.getTotalIn());
100+
deflater.reset();
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of ViaRewind - https://github.com/ViaVersion/ViaRewind
3+
* Copyright (C) 2018-2025 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viarewind.protocol.v1_8to1_7_6_10.provider.compression.compressor;
19+
20+
import com.velocitypowered.natives.util.Natives;
21+
import io.netty.buffer.ByteBuf;
22+
23+
import java.util.zip.DataFormatException;
24+
25+
public class VelocityCompressor implements Compressor {
26+
private final com.velocitypowered.natives.compression.VelocityCompressor velocityCompressor = Natives.compress.get().create(-1);
27+
28+
@Override
29+
public void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException {
30+
velocityCompressor.deflate(source, destination);
31+
}
32+
33+
@Override
34+
public void inflate(ByteBuf source, ByteBuf destination, int uncompressedSize) throws DataFormatException {
35+
velocityCompressor.inflate(source, destination, uncompressedSize);
36+
}
37+
}

0 commit comments

Comments
 (0)