Merge branch 'reflectionless' of https://github.com/Anuken/Mindustry

This commit is contained in:
Anuken
2018-08-31 16:15:43 -04:00
3 changed files with 26 additions and 18 deletions

View File

@@ -253,7 +253,7 @@ public class Packets{
public void read(ByteBuffer buffer){
id = buffer.getInt();
total = buffer.getInt();
type = (Class<? extends Streamable>) Registrator.getByID(buffer.get());
type = (Class<? extends Streamable>) Registrator.getByID(buffer.get()).type;
}
}

View File

@@ -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<Class<?>> ids = new ObjectIntMap<>();
private static ObjectIntMap<Class> 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<Packet>) classes[i].type, (Supplier<Packet>) 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 <T extends Packet> ClassEntry(Class<T> type, Supplier<T> constructor){
this.type = type;
this.constructor = constructor;
}
}
}