Added backwards compatibility for legacy saves

This commit is contained in:
Anuken
2020-02-17 11:13:46 -05:00
parent 18d4efa315
commit e215772f30
34 changed files with 1009 additions and 855 deletions
+4 -6
View File
@@ -1,11 +1,9 @@
package mindustry.io;
import arc.struct.ObjectMap;
import arc.struct.ObjectMap.Entry;
import arc.struct.StringMap;
import arc.util.io.CounterInputStream;
import arc.util.io.ReusableByteOutStream;
import mindustry.world.WorldContext;
import arc.struct.*;
import arc.struct.ObjectMap.*;
import arc.util.io.*;
import mindustry.world.*;
import java.io.*;
+2 -1
View File
@@ -5,6 +5,7 @@ import arc.files.Fi;
import arc.util.io.CounterInputStream;
import arc.util.io.FastDeflaterOutputStream;
import mindustry.Vars;
import mindustry.io.legacy.*;
import mindustry.io.versions.*;
import mindustry.world.WorldContext;
@@ -18,7 +19,7 @@ public class SaveIO{
/** Format header. This is the string 'MSAV' in ASCII. */
public static final byte[] header = {77, 83, 65, 86};
public static final IntMap<SaveVersion> versions = new IntMap<>();
public static final Array<SaveVersion> versionArray = Array.with(new Save1(), new Save2(), new Save3());
public static final Array<SaveVersion> versionArray = Array.with(new Save1(), new Save2(), new Save3(), new Save4());
static{
for(SaveVersion version : versionArray){
+2 -2
View File
@@ -125,7 +125,7 @@ public abstract class SaveVersion extends SaveFileReader{
if(tile.entity != null){
writeChunk(stream, true, out -> {
out.writeByte(tile.entity.version());
tile.entity.write(Writes.get(out));
tile.entity.writeAll(Writes.get(out));
});
}else{
//write consecutive non-entity blocks
@@ -188,7 +188,7 @@ public abstract class SaveVersion extends SaveFileReader{
try{
readChunk(stream, true, in -> {
byte revision = in.readByte();
tile.entity.read(Reads.get(in), revision);
tile.entity.readAll(Reads.get(in), revision);
});
}catch(Exception e){
throw new IOException("Failed to read tile entity of block: " + block, e);
@@ -0,0 +1,109 @@
package mindustry.io.legacy;
import arc.util.*;
import arc.util.io.*;
import mindustry.content.*;
import mindustry.game.*;
import mindustry.io.*;
import mindustry.world.*;
import java.io.*;
import static mindustry.Vars.content;
public abstract class LegacySaveVersion extends SaveVersion{
public LegacySaveVersion(int version){
super(version);
}
@Override
public void readMap(DataInput stream, WorldContext context) throws IOException{
int width = stream.readUnsignedShort();
int height = stream.readUnsignedShort();
boolean generating = context.isGenerating();
if(!generating) context.begin();
try{
context.resize(width, height);
//read floor and create tiles first
for(int i = 0; i < width * height; i++){
int x = i % width, y = i / width;
short floorid = stream.readShort();
short oreid = stream.readShort();
int consecutives = stream.readUnsignedByte();
if(content.block(floorid) == Blocks.air) floorid = Blocks.stone.id;
context.create(x, y, floorid, oreid, (short)0);
for(int j = i + 1; j < i + 1 + consecutives; j++){
int newx = j % width, newy = j / width;
context.create(newx, newy, floorid, oreid, (short)0);
}
i += consecutives;
}
//read blocks
for(int i = 0; i < width * height; i++){
int x = i % width, y = i / width;
Block block = content.block(stream.readShort());
Tile tile = context.tile(x, y);
if(block == null) block = Blocks.air;
tile.setBlock(block);
if(tile.entity != null){
try{
readChunk(stream, true, in -> {
byte version = in.readByte();
//legacy impl of TileEntity#read()
tile.entity.health(stream.readUnsignedShort());
byte packedrot = stream.readByte();
byte team = Pack.leftByte(packedrot) == 8 ? stream.readByte() : Pack.leftByte(packedrot);
byte rotation = Pack.rightByte(packedrot);
tile.setTeam(Team.get(team));
tile.rotation(rotation);
if(tile.entity.items() != null) tile.entity.items().read(Reads.get(stream));
if(tile.entity.power() != null) tile.entity.power().read(Reads.get(stream));
if(tile.entity.liquids() != null) tile.entity.liquids().read(Reads.get(stream));
if(tile.entity.cons() != null) tile.entity.cons().read(Reads.get(stream));
//read only from subclasses!
tile.entity.read(Reads.get(in), version);
});
}catch(Exception e){
throw new IOException("Failed to read tile entity of block: " + block, e);
}
}else{
int consecutives = stream.readUnsignedByte();
for(int j = i + 1; j < i + 1 + consecutives; j++){
int newx = j % width, newy = j / width;
context.tile(newx, newy).setBlock(block);
}
i += consecutives;
}
}
}finally{
if(!generating) context.end();
}
}
public void readLegacyEntities(DataInput stream) throws IOException{
byte groups = stream.readByte();
for(int i = 0; i < groups; i++){
int amount = stream.readInt();
for(int j = 0; j < amount; j++){
//simply skip all the entities
skipRegion(stream, true);
}
}
}
}
@@ -1,4 +1,4 @@
package mindustry.io.versions;
package mindustry.io.legacy;
/*
Latest data: [build 81]
+15
View File
@@ -0,0 +1,15 @@
package mindustry.io.legacy;
import java.io.*;
public class Save1 extends LegacySaveVersion{
public Save1(){
super(1);
}
@Override
public void readEntities(DataInput stream) throws IOException{
readLegacyEntities(stream);
}
}
+15
View File
@@ -0,0 +1,15 @@
package mindustry.io.legacy;
import java.io.*;
public class Save2 extends LegacySaveVersion{
public Save2(){
super(2);
}
@Override
public void readEntities(DataInput stream) throws IOException{
readLegacyEntities(stream);
}
}
+29
View File
@@ -0,0 +1,29 @@
package mindustry.io.legacy;
import mindustry.game.*;
import mindustry.game.Teams.*;
import java.io.*;
import static mindustry.Vars.content;
public class Save3 extends LegacySaveVersion{
public Save3(){
super(3);
}
@Override
public void readEntities(DataInput stream) throws IOException{
int teamc = stream.readInt();
for(int i = 0; i < teamc; i++){
Team team = Team.get(stream.readInt());
TeamData data = team.data();
int blocks = stream.readInt();
for(int j = 0; j < blocks; j++){
data.brokenBlocks.addLast(new BrokenBlock(stream.readShort(), stream.readShort(), stream.readShort(), content.block(stream.readShort()).id, stream.readInt()));
}
}
readLegacyEntities(stream);
}
}
-30
View File
@@ -1,30 +0,0 @@
package mindustry.io.versions;
import java.io.*;
public class Save1 extends Save2{
public Save1(){
version = 1;
}
@Override
public void readEntities(DataInput stream) throws IOException{
//TODO implement
//Prov[] table = LegacyTypeTable.getTable(lastReadBuild);
byte groups = stream.readByte();
for(int i = 0; i < groups; i++){
int amount = stream.readInt();
for(int j = 0; j < amount; j++){
readChunk(stream, true, in -> {
byte typeid = in.readByte();
byte version = in.readByte();
//SaveTrait trait = (SaveTrait)table[typeid].get();
//trait.readSave(in, version);
});
}
}
}
}
-31
View File
@@ -1,31 +0,0 @@
package mindustry.io.versions;
import mindustry.io.*;
import java.io.*;
public class Save2 extends SaveVersion{
public Save2(){
super(2);
}
@Override
public void readEntities(DataInput stream) throws IOException{
//TODO implement
byte groups = stream.readByte();
for(int i = 0; i < groups; i++){
int amount = stream.readInt();
for(int j = 0; j < amount; j++){
//TODO throw exception on read fail
readChunk(stream, true, in -> {
byte typeid = in.readByte();
byte version = in.readByte();
//SaveTrait trait = (SaveTrait)content.<TypeID>getByID(ContentType.typeid, typeid).constructor.get();
//trait.readSave(in, version);
});
}
}
}
}
@@ -1,9 +0,0 @@
package mindustry.io.versions;
import mindustry.io.*;
public class Save3 extends SaveVersion{
public Save3(){
super(3);
}
}
+10
View File
@@ -0,0 +1,10 @@
package mindustry.io.versions;
import mindustry.io.*;
public class Save4 extends SaveVersion{
public Save4(){
super(4);
}
}