Archiviert
13
0

Attempt to alleviate some issues with backwards compatibility

Addresses #98
Dieser Commit ist enthalten in:
Dan Mulloy 2015-07-12 13:58:31 -04:00
Ursprung b50133d322
Commit 714e631dd0
6 geänderte Dateien mit 50 neuen und 52 gelöschten Zeilen

Datei anzeigen

@ -31,22 +31,21 @@ import com.google.common.collect.Range;
public class Guava { public class Guava {
private static GuavaCompat compat; private static GuavaCompat compat;
private static GuavaCompat getCompat() { static {
if (compat == null) {
try { try {
Range.closed(1, 2); Range.closed(1, 2);
return compat = new Guava17(); compat = new Guava17();
} catch (Throwable ex) { } catch (Throwable ex) {
try { try {
ProtocolLibrary.log("Falling back to Guava 10 compat");
Class<?> clazz = Class.forName("com.comphenix.protocol.compat.guava.Guava10"); Class<?> clazz = Class.forName("com.comphenix.protocol.compat.guava.Guava10");
return compat = (GuavaCompat) clazz.newInstance(); compat = (GuavaCompat) clazz.newInstance();
} catch (Throwable ex1) { } catch (Throwable ex1) {
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create Guava 10 compat:", ex1); ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create Guava 10 compat:", ex1);
} }
} }
} }
private static GuavaCompat getCompat() {
return compat; return compat;
} }

Datei anzeigen

@ -35,22 +35,21 @@ import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage;
public class Netty { public class Netty {
private static NettyCompat compat; private static NettyCompat compat;
private static NettyCompat getCompat() { static {
if (compat == null) {
try { try {
Class.forName("io.netty.buffer.ByteBuf"); Class.forName("io.netty.buffer.ByteBuf");
return compat = new IndependentNetty(); compat = new IndependentNetty();
} catch (Throwable ex) { } catch (ClassNotFoundException ex) {
try { try {
ProtocolLibrary.log("Falling back to legacy Netty compat");
Class<?> clazz = Class.forName("com.comphenix.protocol.compat.netty.shaded.ShadedNetty"); Class<?> clazz = Class.forName("com.comphenix.protocol.compat.netty.shaded.ShadedNetty");
return compat = (NettyCompat) clazz.newInstance(); compat = (NettyCompat) clazz.newInstance();
} catch (Throwable ex1) { } catch (Exception ex1) {
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create legacy netty compat:", ex1); ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create legacy netty compat:", ex1);
} }
} }
} }
private static NettyCompat getCompat() {
return compat; return compat;
} }

Datei anzeigen

@ -45,15 +45,14 @@ public class IndependentNetty implements NettyCompat {
@Override @Override
public WrappedByteBuf createPacketBuffer() { 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(); Class<?> packetSerializer = MinecraftReflection.getPacketDataSerializerClass();
try { try {
return new NettyByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()) return new NettyByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()).newInstance(buffer));
.newInstance(buffer.getHandle()));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Cannot construct packet serializer.", e); throw new RuntimeException("Cannot construct packet serializer.", e);
} }

Datei anzeigen

@ -21,6 +21,7 @@ import io.netty.buffer.ByteBuf;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.lang.ref.WeakReference;
import com.comphenix.protocol.compat.netty.WrappedByteBuf; import com.comphenix.protocol.compat.netty.WrappedByteBuf;
@ -29,49 +30,49 @@ import com.comphenix.protocol.compat.netty.WrappedByteBuf;
*/ */
public class NettyByteBuf implements WrappedByteBuf { public class NettyByteBuf implements WrappedByteBuf {
private final ByteBuf handle; private WeakReference<ByteBuf> handle;
public NettyByteBuf(ByteBuf handle) { public NettyByteBuf(ByteBuf handle) {
this.handle = handle; this.handle = new WeakReference<ByteBuf>(handle);
} }
@Override @Override
public void writeBytes(ObjectInputStream input, int id) throws IOException { public void writeBytes(ObjectInputStream input, int id) throws IOException {
handle.writeBytes(input, id); handle.get().writeBytes(input, id);
} }
@Override @Override
public Object getHandle() { public Object getHandle() {
return handle; return handle.get();
} }
@Override @Override
public int readableBytes() { public int readableBytes() {
return handle.readableBytes(); return handle.get().readableBytes();
} }
@Override @Override
public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException { public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException {
handle.readBytes(output, readableBytes); handle.get().readBytes(output, readableBytes);
} }
@Override @Override
public void readBytes(byte[] data) { public void readBytes(byte[] data) {
handle.readBytes(data); handle.get().readBytes(data);
} }
@Override @Override
public void writeByte(byte b) { public void writeByte(byte b) {
handle.writeByte(b); handle.get().writeByte(b);
} }
@Override @Override
public void writeByte(int i) { public void writeByte(int i) {
handle.writeByte(i); handle.get().writeByte(i);
} }
@Override @Override
public void writeBytes(byte[] bytes) { public void writeBytes(byte[] bytes) {
handle.writeBytes(bytes); handle.get().writeBytes(bytes);
} }
} }

Datei anzeigen

@ -19,6 +19,7 @@ package com.comphenix.protocol.compat.netty.shaded;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.lang.ref.WeakReference;
import net.minecraft.util.io.netty.buffer.ByteBuf; import net.minecraft.util.io.netty.buffer.ByteBuf;
@ -29,49 +30,49 @@ import com.comphenix.protocol.compat.netty.WrappedByteBuf;
*/ */
public class ShadedByteBuf implements WrappedByteBuf { public class ShadedByteBuf implements WrappedByteBuf {
private final ByteBuf handle; private WeakReference<ByteBuf> handle;
public ShadedByteBuf(ByteBuf handle) { public ShadedByteBuf(ByteBuf handle) {
this.handle = handle; this.handle = new WeakReference<ByteBuf>(handle);
} }
@Override @Override
public Object getHandle() { public Object getHandle() {
return handle; return handle.get();
} }
@Override @Override
public void writeBytes(ObjectInputStream input, int id) throws IOException { public void writeBytes(ObjectInputStream input, int id) throws IOException {
handle.writeBytes(input, id); handle.get().writeBytes(input, id);
} }
@Override @Override
public int readableBytes() { public int readableBytes() {
return handle.readableBytes(); return handle.get().readableBytes();
} }
@Override @Override
public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException { public void readBytes(ObjectOutputStream output, int readableBytes) throws IOException {
handle.readBytes(output, readableBytes); handle.get().readBytes(output, readableBytes);
} }
@Override @Override
public void readBytes(byte[] data) { public void readBytes(byte[] data) {
handle.readBytes(data); handle.get().readBytes(data);
} }
@Override @Override
public void writeByte(byte b) { public void writeByte(byte b) {
handle.writeByte(b); handle.get().writeByte(b);
} }
@Override @Override
public void writeByte(int i) { public void writeByte(int i) {
handle.writeByte(i); handle.get().writeByte(i);
} }
@Override @Override
public void writeBytes(byte[] bytes) { public void writeBytes(byte[] bytes) {
handle.writeBytes(bytes); handle.get().writeBytes(bytes);
} }
} }

Datei anzeigen

@ -45,15 +45,14 @@ public class ShadedNetty implements NettyCompat {
@Override @Override
public WrappedByteBuf createPacketBuffer() { 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(); Class<?> packetSerializer = MinecraftReflection.getPacketDataSerializerClass();
try { try {
return new ShadedByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()) return new ShadedByteBuf((ByteBuf) packetSerializer.getConstructor(MinecraftReflection.getByteBufClass()).newInstance(buffer));
.newInstance(buffer.getHandle()));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Cannot construct packet serializer.", e); throw new RuntimeException("Cannot construct packet serializer.", e);
} }