Attempt to alleviate some issues with backwards compatibility
Addresses #98
Dieser Commit ist enthalten in:
Ursprung
b50133d322
Commit
714e631dd0
@ -31,22 +31,21 @@ import com.google.common.collect.Range;
|
||||
public class Guava {
|
||||
private static GuavaCompat compat;
|
||||
|
||||
private static GuavaCompat getCompat() {
|
||||
if (compat == null) {
|
||||
static {
|
||||
try {
|
||||
Range.closed(1, 2);
|
||||
compat = new Guava17();
|
||||
} catch (Throwable ex) {
|
||||
try {
|
||||
Range.closed(1, 2);
|
||||
return compat = new Guava17();
|
||||
} catch (Throwable ex) {
|
||||
try {
|
||||
ProtocolLibrary.log("Falling back to Guava 10 compat");
|
||||
Class<?> clazz = Class.forName("com.comphenix.protocol.compat.guava.Guava10");
|
||||
return compat = (GuavaCompat) clazz.newInstance();
|
||||
} catch (Throwable ex1) {
|
||||
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create Guava 10 compat:", ex1);
|
||||
}
|
||||
Class<?> clazz = Class.forName("com.comphenix.protocol.compat.guava.Guava10");
|
||||
compat = (GuavaCompat) clazz.newInstance();
|
||||
} catch (Throwable ex1) {
|
||||
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create Guava 10 compat:", ex1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static GuavaCompat getCompat() {
|
||||
return compat;
|
||||
}
|
||||
|
||||
|
@ -35,22 +35,21 @@ import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage;
|
||||
public class Netty {
|
||||
private static NettyCompat compat;
|
||||
|
||||
private static NettyCompat getCompat() {
|
||||
if (compat == null) {
|
||||
static {
|
||||
try {
|
||||
Class.forName("io.netty.buffer.ByteBuf");
|
||||
compat = new IndependentNetty();
|
||||
} catch (ClassNotFoundException ex) {
|
||||
try {
|
||||
Class.forName("io.netty.buffer.ByteBuf");
|
||||
return compat = new IndependentNetty();
|
||||
} catch (Throwable ex) {
|
||||
try {
|
||||
ProtocolLibrary.log("Falling back to legacy Netty compat");
|
||||
Class<?> clazz = Class.forName("com.comphenix.protocol.compat.netty.shaded.ShadedNetty");
|
||||
return compat = (NettyCompat) clazz.newInstance();
|
||||
} catch (Throwable ex1) {
|
||||
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create legacy netty compat:", ex1);
|
||||
}
|
||||
Class<?> clazz = Class.forName("com.comphenix.protocol.compat.netty.shaded.ShadedNetty");
|
||||
compat = (NettyCompat) clazz.newInstance();
|
||||
} catch (Exception ex1) {
|
||||
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create legacy netty compat:", ex1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static NettyCompat getCompat() {
|
||||
return compat;
|
||||
}
|
||||
|
||||
|
@ -45,15 +45,14 @@ public class IndependentNetty implements NettyCompat {
|
||||
|
||||
@Override
|
||||
public WrappedByteBuf createPacketBuffer() {
|
||||
return getPacketDataSerializer(allocateUnpooled());
|
||||
return getPacketDataSerializer(UnpooledByteBufAllocator.DEFAULT.buffer());
|
||||
}
|
||||
|
||||
private WrappedByteBuf getPacketDataSerializer(WrappedByteBuf buffer) {
|
||||
private WrappedByteBuf getPacketDataSerializer(ByteBuf buffer) {
|
||||
Class<?> packetSerializer = MinecraftReflection.getPacketDataSerializerClass();
|
||||
|
||||
try {
|
||||
return new NettyByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass())
|
||||
.newInstance(buffer.getHandle()));
|
||||
return new NettyByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()).newInstance(buffer));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Cannot construct packet serializer.", e);
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import io.netty.buffer.ByteBuf;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import com.comphenix.protocol.compat.netty.WrappedByteBuf;
|
||||
|
||||
@ -29,49 +30,49 @@ import com.comphenix.protocol.compat.netty.WrappedByteBuf;
|
||||
*/
|
||||
|
||||
public class NettyByteBuf implements WrappedByteBuf {
|
||||
private final ByteBuf handle;
|
||||
private WeakReference<ByteBuf> handle;
|
||||
|
||||
public NettyByteBuf(ByteBuf handle) {
|
||||
this.handle = handle;
|
||||
this.handle = new WeakReference<ByteBuf>(handle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ObjectInputStream input, int id) throws IOException {
|
||||
handle.writeBytes(input, id);
|
||||
handle.get().writeBytes(input, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getHandle() {
|
||||
return handle;
|
||||
return handle.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readableBytes() {
|
||||
return handle.readableBytes();
|
||||
return handle.get().readableBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException {
|
||||
handle.readBytes(output, readableBytes);
|
||||
handle.get().readBytes(output, readableBytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] data) {
|
||||
handle.readBytes(data);
|
||||
handle.get().readBytes(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(byte b) {
|
||||
handle.writeByte(b);
|
||||
handle.get().writeByte(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int i) {
|
||||
handle.writeByte(i);
|
||||
handle.get().writeByte(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] bytes) {
|
||||
handle.writeBytes(bytes);
|
||||
handle.get().writeBytes(bytes);
|
||||
}
|
||||
}
|
@ -19,6 +19,7 @@ package com.comphenix.protocol.compat.netty.shaded;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import net.minecraft.util.io.netty.buffer.ByteBuf;
|
||||
|
||||
@ -29,49 +30,49 @@ import com.comphenix.protocol.compat.netty.WrappedByteBuf;
|
||||
*/
|
||||
|
||||
public class ShadedByteBuf implements WrappedByteBuf {
|
||||
private final ByteBuf handle;
|
||||
private WeakReference<ByteBuf> handle;
|
||||
|
||||
public ShadedByteBuf(ByteBuf handle) {
|
||||
this.handle = handle;
|
||||
this.handle = new WeakReference<ByteBuf>(handle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getHandle() {
|
||||
return handle;
|
||||
return handle.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ObjectInputStream input, int id) throws IOException {
|
||||
handle.writeBytes(input, id);
|
||||
handle.get().writeBytes(input, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readableBytes() {
|
||||
return handle.readableBytes();
|
||||
return handle.get().readableBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException {
|
||||
handle.readBytes(output, readableBytes);
|
||||
handle.get().readBytes(output, readableBytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] data) {
|
||||
handle.readBytes(data);
|
||||
handle.get().readBytes(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(byte b) {
|
||||
handle.writeByte(b);
|
||||
handle.get().writeByte(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int i) {
|
||||
handle.writeByte(i);
|
||||
handle.get().writeByte(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] bytes) {
|
||||
handle.writeBytes(bytes);
|
||||
handle.get().writeBytes(bytes);
|
||||
}
|
||||
}
|
@ -45,15 +45,14 @@ public class ShadedNetty implements NettyCompat {
|
||||
|
||||
@Override
|
||||
public WrappedByteBuf createPacketBuffer() {
|
||||
return getPacketDataSerializer(allocateUnpooled());
|
||||
return getPacketDataSerializer(UnpooledByteBufAllocator.DEFAULT.buffer());
|
||||
}
|
||||
|
||||
private WrappedByteBuf getPacketDataSerializer(WrappedByteBuf buffer) {
|
||||
private WrappedByteBuf getPacketDataSerializer(ByteBuf buffer) {
|
||||
Class<?> packetSerializer = MinecraftReflection.getPacketDataSerializerClass();
|
||||
|
||||
try {
|
||||
return new ShadedByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass())
|
||||
.newInstance(buffer.getHandle()));
|
||||
return new ShadedByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()).newInstance(buffer));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Cannot construct packet serializer.", e);
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren