mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-06 07:21:32 +01:00
Merge branch 'master' into new-block-api
This commit is contained in:
commit
3bb5801ec7
@ -36,9 +36,6 @@ allprojects {
|
||||
name 'sponge'
|
||||
url 'https://repo.spongepowered.org/maven'
|
||||
}
|
||||
maven {
|
||||
url "https://repo.velocitypowered.com/snapshots/"
|
||||
}
|
||||
}
|
||||
javadoc {
|
||||
options {
|
||||
@ -150,9 +147,6 @@ dependencies {
|
||||
api "org.ow2.asm:asm-commons:${asmVersion}"
|
||||
api "org.spongepowered:mixin:${mixinVersion}"
|
||||
|
||||
// Compression
|
||||
implementation "com.velocitypowered:velocity-native:1.1.0-SNAPSHOT"
|
||||
|
||||
// Path finding
|
||||
api 'com.github.MadMartian:hydrazine-path-finding:1.6.0'
|
||||
|
||||
|
@ -29,6 +29,7 @@ import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class ExtensionManager {
|
||||
@ -379,8 +380,14 @@ public class ExtensionManager {
|
||||
*/
|
||||
@Nullable
|
||||
private DiscoveredExtension discoverFromJar(@NotNull File file) {
|
||||
try (ZipFile f = new ZipFile(file);
|
||||
InputStreamReader reader = new InputStreamReader(f.getInputStream(f.getEntry("extension.json")))) {
|
||||
try (ZipFile f = new ZipFile(file);) {
|
||||
|
||||
ZipEntry entry = f.getEntry("extension.json");
|
||||
|
||||
if (entry == null)
|
||||
throw new IllegalStateException("Missing extension.json in extension " + file.getName() + ".");
|
||||
|
||||
InputStreamReader reader = new InputStreamReader(f.getInputStream(entry));
|
||||
|
||||
// Initialize DiscoveredExtension from GSON.
|
||||
DiscoveredExtension extension = GSON.fromJson(reader, DiscoveredExtension.class);
|
||||
|
@ -16,10 +16,8 @@
|
||||
|
||||
package net.minestom.server.network.netty.codec;
|
||||
|
||||
import com.velocitypowered.natives.compression.VelocityCompressor;
|
||||
import com.velocitypowered.natives.util.MoreByteBufUtils;
|
||||
import com.velocitypowered.natives.util.Natives;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageCodec;
|
||||
import io.netty.handler.codec.DecoderException;
|
||||
@ -27,6 +25,8 @@ import net.minestom.server.utils.PacketUtils;
|
||||
import net.minestom.server.utils.Utils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
|
||||
@ -34,7 +34,8 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
|
||||
private final int threshold;
|
||||
|
||||
private final VelocityCompressor compressor = Natives.compress.get().create(4);
|
||||
private final Deflater deflater = new Deflater();
|
||||
private final Inflater inflater = new Inflater();
|
||||
|
||||
public PacketCompressor(int threshold) {
|
||||
this.threshold = threshold;
|
||||
@ -42,7 +43,7 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
|
||||
PacketUtils.compressBuffer(compressor, from, to);
|
||||
PacketUtils.compressBuffer(deflater, from, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -61,19 +62,17 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
throw new DecoderException("Badly compressed packet - size of " + claimedUncompressedSize + " is larger than protocol maximum of " + MAX_SIZE);
|
||||
}
|
||||
|
||||
// TODO optimize to do not initialize arrays each time
|
||||
|
||||
ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, in);
|
||||
ByteBuf uncompressed = MoreByteBufUtils.preferredBuffer(ctx.alloc(), compressor, claimedUncompressedSize);
|
||||
try {
|
||||
compressor.inflate(compatibleIn, uncompressed, claimedUncompressedSize);
|
||||
out.add(uncompressed);
|
||||
in.clear();
|
||||
} catch (Exception e) {
|
||||
uncompressed.release();
|
||||
throw e;
|
||||
} finally {
|
||||
compatibleIn.release();
|
||||
}
|
||||
byte[] input = new byte[in.readableBytes()];
|
||||
in.readBytes(input);
|
||||
|
||||
inflater.setInput(input);
|
||||
byte[] output = new byte[claimedUncompressedSize];
|
||||
inflater.inflate(output);
|
||||
inflater.reset();
|
||||
|
||||
out.add(Unpooled.wrappedBuffer(output));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,9 @@
|
||||
package net.minestom.server.utils;
|
||||
|
||||
import com.velocitypowered.natives.compression.VelocityCompressor;
|
||||
import com.velocitypowered.natives.util.Natives;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.adventure.AdventureSerializer;
|
||||
import net.minestom.server.adventure.MinestomAdventure;
|
||||
import net.minestom.server.adventure.audience.PacketGroupingAudience;
|
||||
import net.minestom.server.entity.Player;
|
||||
@ -21,8 +18,9 @@ import net.minestom.server.utils.callback.validator.PlayerValidator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collection;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
/**
|
||||
* Utils class for packets. Including writing a {@link ServerPacket} into a {@link ByteBuf}
|
||||
@ -31,7 +29,7 @@ import java.util.zip.DataFormatException;
|
||||
public final class PacketUtils {
|
||||
|
||||
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
|
||||
private static final ThreadLocal<VelocityCompressor> COMPRESSOR = ThreadLocal.withInitial(() -> Natives.compress.get().create(4));
|
||||
private static final ThreadLocal<Deflater> COMPRESSOR = ThreadLocal.withInitial(Deflater::new);
|
||||
|
||||
private PacketUtils() {
|
||||
}
|
||||
@ -189,27 +187,35 @@ public final class PacketUtils {
|
||||
* <p>
|
||||
* {@code packetBuffer} needs to be the packet content without any header (if you want to use it to write a Minecraft packet).
|
||||
*
|
||||
* @param compressor the deflater for zlib compression
|
||||
* @param deflater the deflater for zlib compression
|
||||
* @param packetBuffer the buffer containing all the packet fields
|
||||
* @param compressionTarget the buffer which will receive the compressed version of {@code packetBuffer}
|
||||
*/
|
||||
public static void compressBuffer(@NotNull VelocityCompressor compressor, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
|
||||
public static void compressBuffer(@NotNull Deflater deflater, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
|
||||
final int packetLength = packetBuffer.readableBytes();
|
||||
final boolean compression = packetLength > MinecraftServer.getCompressionThreshold();
|
||||
Utils.writeVarInt(compressionTarget, compression ? packetLength : 0);
|
||||
if (compression) {
|
||||
compress(compressor, packetBuffer, compressionTarget);
|
||||
compress(deflater, packetBuffer, compressionTarget);
|
||||
} else {
|
||||
compressionTarget.writeBytes(packetBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
private static void compress(@NotNull VelocityCompressor compressor, @NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
|
||||
try {
|
||||
compressor.deflate(uncompressed, compressed);
|
||||
} catch (DataFormatException e) {
|
||||
e.printStackTrace();
|
||||
private static void compress(@NotNull Deflater deflater, @NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
|
||||
deflater.setInput(uncompressed.nioBuffer());
|
||||
deflater.finish();
|
||||
|
||||
while (!deflater.finished()) {
|
||||
ByteBuffer nioBuffer = compressed.nioBuffer(compressed.writerIndex(), compressed.writableBytes());
|
||||
compressed.writerIndex(deflater.deflate(nioBuffer) + compressed.writerIndex());
|
||||
|
||||
if (compressed.writableBytes() == 0) {
|
||||
compressed.ensureWritable(8192);
|
||||
}
|
||||
}
|
||||
|
||||
deflater.reset();
|
||||
}
|
||||
|
||||
public static void writeFramedPacket(@NotNull ByteBuf buffer,
|
||||
|
Loading…
Reference in New Issue
Block a user