diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index 9e9dfb36f2..9ea7c2cd4f 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -253,7 +253,7 @@ public class Packets{ public void read(ByteBuffer buffer){ id = buffer.getInt(); total = buffer.getInt(); - type = (Class) Registrator.getByID(buffer.get()); + type = (Class) Registrator.getByID(buffer.get()).type; } } diff --git a/core/src/io/anuke/mindustry/net/Registrator.java b/core/src/io/anuke/mindustry/net/Registrator.java index 6dbabdcc15..fe94f43413 100644 --- a/core/src/io/anuke/mindustry/net/Registrator.java +++ b/core/src/io/anuke/mindustry/net/Registrator.java @@ -1,31 +1,30 @@ package io.anuke.mindustry.net; import com.badlogic.gdx.utils.ObjectIntMap; -import com.badlogic.gdx.utils.reflect.ClassReflection; import io.anuke.mindustry.net.Packets.*; +import io.anuke.ucore.function.Supplier; +import io.anuke.ucore.util.Pooling; public class Registrator{ - private static Class[] classes = { - StreamBegin.class, - StreamChunk.class, - WorldStream.class, - ConnectPacket.class, - ClientSnapshotPacket.class, - InvokePacket.class + private static ClassEntry[] classes = { + new ClassEntry(StreamBegin.class, StreamBegin::new), + new ClassEntry(StreamChunk.class, StreamChunk::new), + new ClassEntry(WorldStream.class, WorldStream::new), + new ClassEntry(ConnectPacket.class, ConnectPacket::new), + new ClassEntry(ClientSnapshotPacket.class, ClientSnapshotPacket::new), + new ClassEntry(InvokePacket.class, InvokePacket::new) }; - private static ObjectIntMap> ids = new ObjectIntMap<>(); + private static ObjectIntMap ids = new ObjectIntMap<>(); static{ if(classes.length > 127) throw new RuntimeException("Can't have more than 127 registered classes!"); for(int i = 0; i < classes.length; i++){ - if(!ClassReflection.isAssignableFrom(Packet.class, classes[i]) && - !ClassReflection.isAssignableFrom(Streamable.class, classes[i])) - throw new RuntimeException("Not a packet: " + classes[i]); - ids.put(classes[i], i); + Pooling.registerType((Class) classes[i].type, (Supplier) classes[i].constructor); + ids.put(classes[i].type, i); } } - public static Class getByID(byte id){ + public static ClassEntry getByID(byte id){ return classes[id]; } @@ -33,7 +32,17 @@ public class Registrator{ return (byte) ids.get(type, -1); } - public static Class[] getClasses(){ + public static ClassEntry[] getClasses(){ return classes; } + + public static class ClassEntry{ + public final Class type; + public final Supplier constructor; + + public ClassEntry(Class type, Supplier constructor){ + this.type = type; + this.constructor = constructor; + } + } } diff --git a/kryonet/src/io/anuke/kryonet/ByteSerializer.java b/kryonet/src/io/anuke/kryonet/ByteSerializer.java index df278e09aa..89075a09ba 100644 --- a/kryonet/src/io/anuke/kryonet/ByteSerializer.java +++ b/kryonet/src/io/anuke/kryonet/ByteSerializer.java @@ -35,9 +35,8 @@ public class ByteSerializer implements Serialization { if(id == -2){ return FrameworkSerializer.read(byteBuffer); }else{ - Class type = Registrator.getByID(id); synchronized (packetPoolLock) { - Packet packet = (Packet) Pooling.obtain(type); + Packet packet = (Packet) Pooling.obtain(Registrator.getByID(id).type); packet.read(byteBuffer); return packet; }