extracted array list serializer

This commit is contained in:
SpaceToad 2014-03-29 18:38:17 +01:00
parent 53b8f86db2
commit d08d15d4c1
2 changed files with 60 additions and 36 deletions

View file

@ -74,9 +74,6 @@ public class ClassMapping extends ClassSerializer {
private LinkedList<Field> booleanFields = new LinkedList<Field>();
private LinkedList<Field> enumFields = new LinkedList<Field>();
// TODO: Make a specific serializer for this one
private LinkedList<Field> arrayListFields = new LinkedList<Field>();
class FieldObject {
public Field field;
public ClassSerializer mapping;
@ -152,8 +149,6 @@ public class ClassMapping extends ClassSerializer {
floatFields.add(f);
} else if (fieldClass.equals(double.class)) {
doubleFields.add(f);
} else if (ArrayList.class.isAssignableFrom(fieldClass)) {
arrayListFields.add(f);
} else {
FieldObject obj = new FieldObject();
obj.mapping = get (fieldClass);
@ -299,21 +294,6 @@ public class ClassMapping extends ClassSerializer {
data.writeDouble(f.getDouble(obj));
}
for (Field f : arrayListFields) {
ArrayList list = (ArrayList) f.get(obj);
if (list == null) {
data.writeBoolean(false);
} else {
data.writeBoolean(true);
data.writeShort(list.size());
for (Object o : list) {
anonymousSerializer.write(data, o, context);
}
}
}
for (FieldObject f : objectFields) {
Object cpt = f.field.get(obj);
f.mapping.write(data, cpt, context);
@ -382,22 +362,6 @@ public class ClassMapping extends ClassSerializer {
f.setDouble(obj, data.readDouble());
}
for (Field f : arrayListFields) {
if (data.readBoolean()) {
int size = data.readShort();
ArrayList arr = new ArrayList();
for (int i = 0; i < size; ++i) {
arr.add(anonymousSerializer.read(data, null, context));
}
f.set(obj, arr);
} else {
f.set(obj, null);
}
}
for (FieldObject f : objectFields) {
f.field.set(obj, f.mapping.read(data, f.field.get(obj), context));
}
@ -637,6 +601,7 @@ public class ClassMapping extends ClassSerializer {
registerSerializer(String.class, new SerializerString());
registerSerializer(HashMap.class, new SerializerHashMap());
registerSerializer(LinkedList.class, new SerializerLinkedList());
registerSerializer(ArrayList.class, new SerializerArrayList());
registerSerializer(Block.class, new SerializerBlock());
registerSerializer(Item.class, new SerializerItem());
registerSerializer(NBTTagCompound.class, new SerializerNBT());

View file

@ -0,0 +1,59 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.network.serializers;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
public class SerializerArrayList extends ClassSerializer {
private static SerializerObject anonymousSerializer = new SerializerObject();
@Override
public void write(ByteBuf data, Object o, SerializationContext context)
throws IllegalArgumentException, IllegalAccessException {
ArrayList list = (ArrayList) o;
if (o == null) {
data.writeBoolean(false);
} else {
data.writeBoolean(true);
data.writeShort(list.size());
for (Object val : list) {
anonymousSerializer.write(data, val, context);
}
}
}
@Override
public Object read(ByteBuf data, Object o, SerializationContext context)
throws IllegalArgumentException, IllegalAccessException,
InstantiationException, ClassNotFoundException {
if (!data.readBoolean()) {
return null;
} else {
int size = data.readShort();
ArrayList list = new ArrayList ();
for (int i = 0; i < size; ++i) {
Object val = anonymousSerializer.read(data, null, context);
list.add(val);
}
return list;
}
}
}