Fixed many various bugs with syncing things
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
package io.anuke.mindustry.io;
|
||||
|
||||
import com.badlogic.gdx.utils.ObjectMap;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import com.badlogic.gdx.utils.reflect.Constructor;
|
||||
import com.badlogic.gdx.utils.reflect.ReflectionException;
|
||||
import io.anuke.mindustry.game.Difficulty;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
@@ -11,8 +7,6 @@ import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class SaveFileVersion {
|
||||
private static final ObjectMap<Class<?>, Constructor> cachedConstructors = new ObjectMap<>();
|
||||
|
||||
public final int version;
|
||||
|
||||
public SaveFileVersion(int version){
|
||||
@@ -30,19 +24,4 @@ public abstract class SaveFileVersion {
|
||||
|
||||
public abstract void read(DataInputStream stream) throws IOException;
|
||||
public abstract void write(DataOutputStream stream) throws IOException;
|
||||
|
||||
protected <T> T construct(Class<T> type){
|
||||
try {
|
||||
if (!cachedConstructors.containsKey(type)) {
|
||||
Constructor cons = ClassReflection.getDeclaredConstructor(type);
|
||||
cons.setAccessible(true);
|
||||
cachedConstructors.put(type, cons);
|
||||
}
|
||||
|
||||
return (T)cachedConstructors.get(type).newInstance();
|
||||
|
||||
}catch (ReflectionException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +124,16 @@ public class TypeIO {
|
||||
return Upgrade.getByID(buffer.get());
|
||||
}
|
||||
|
||||
@WriteClass(Liquid.class)
|
||||
public static void writeLiquid(ByteBuffer buffer, Liquid liquid){
|
||||
buffer.put((byte)liquid.id);
|
||||
}
|
||||
|
||||
@ReadClass(Liquid.class)
|
||||
public static Liquid readLiquid(ByteBuffer buffer){
|
||||
return Liquid.getByID(buffer.get());
|
||||
}
|
||||
|
||||
@WriteClass(AmmoType.class)
|
||||
public static void writeAmmo(ByteBuffer buffer, AmmoType type){
|
||||
buffer.put(type.id);
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.TimeUtils;
|
||||
import io.anuke.mindustry.content.blocks.Blocks;
|
||||
import io.anuke.mindustry.content.blocks.StorageBlocks;
|
||||
import io.anuke.mindustry.entities.traits.SaveTrait;
|
||||
import io.anuke.mindustry.entities.traits.TypeTrait;
|
||||
import io.anuke.mindustry.game.Difficulty;
|
||||
import io.anuke.mindustry.game.GameMode;
|
||||
import io.anuke.mindustry.game.Team;
|
||||
@@ -69,11 +70,10 @@ public class Save16 extends SaveFileVersion {
|
||||
|
||||
for (int i = 0; i < groups; i++) {
|
||||
int amount = stream.readInt();
|
||||
byte gid = stream.readByte();
|
||||
EntityGroup<?> group = Entities.getGroup(gid);
|
||||
for (int j = 0; j < amount; j++) {
|
||||
Entity entity = construct(group.getType());
|
||||
((SaveTrait)entity).readSave(stream);
|
||||
byte typeid = stream.readByte();
|
||||
SaveTrait trait = (SaveTrait) TypeTrait.getTypeByID(typeid).get();
|
||||
trait.readSave(stream);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,8 +172,8 @@ public class Save16 extends SaveFileVersion {
|
||||
for(EntityGroup<?> group : Entities.getAllGroups()){
|
||||
if(!group.isEmpty() && group.all().get(0) instanceof SaveTrait){
|
||||
stream.writeInt(group.size());
|
||||
stream.writeByte(group.getID());
|
||||
for(Entity entity : group.all()){
|
||||
stream.writeByte(((SaveTrait)entity).getTypeID());
|
||||
((SaveTrait)entity).writeSave(stream);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user