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
@@ -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);
}
}
}
}
@@ -0,0 +1,139 @@
package mindustry.io.legacy;
/*
Latest data: [build 81]
0 = Player
1 = Fire
2 = Puddle
3 = MinerDrone
4 = RepairDrone
5 = BuilderDrone
6 = GroundUnit
7 = GroundUnit
8 = GroundUnit
9 = GroundUnit
10 = GroundUnit
11 = FlyingUnit
12 = FlyingUnit
13 = Revenant
Before removal of lightining/bullet: [build 80]
0 = Player
1 = Fire
2 = Puddle
3 = Bullet
4 = Lightning
5 = MinerDrone
6 = RepairDrone
7 = BuilderDrone
8 = GroundUnit
9 = GroundUnit
10 = GroundUnit
11 = GroundUnit
12 = GroundUnit
13 = FlyingUnit
14 = FlyingUnit
15 = Revenant
Before addition of new units: [build 79 and below]
0 = Player
1 = Fire
2 = Puddle
3 = Bullet
4 = Lightning
5 = RepairDrone
6 = GroundUnit
7 = GroundUnit
8 = GroundUnit
9 = GroundUnit
10 = GroundUnit
11 = FlyingUnit
12 = FlyingUnit
13 = BuilderDrone
14 = Revenant
*/
public class LegacyTypeTable{
/*
0 = Player
1 = Fire
2 = Puddle
3 = Draug
4 = Spirit
5 = Phantom
6 = Dagger
7 = Crawler
8 = Titan
9 = Fortress
10 = Eruptor
11 = Wraith
12 = Ghoul
13 = Revenant
private static final Prov[] build81Table = {
Playerc::new,
Fire::new,
Puddle::new,
MinerDrone::new,
RepairDrone::new,
BuilderDrone::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
FlyingUnit::new,
FlyingUnit::new,
HoverUnit::new
};
private static final Prov[] build80Table = {
Playerc::new,
Fire::new,
Puddle::new,
Bullet::new,
Lightning::new,
MinerDrone::new,
RepairDrone::new,
BuilderDrone::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
FlyingUnit::new,
FlyingUnit::new,
HoverUnit::new
};
private static final Prov[] build79Table = {
Playerc::new,
Fire::new,
Puddle::new,
Bullet::new,
Lightning::new,
RepairDrone::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
GroundUnit::new,
FlyingUnit::new,
FlyingUnit::new,
BuilderDrone::new,
HoverUnit::new
};
public static Prov[] getTable(int build){
if(build == -1 || build == 81){
//return most recent one since that's probably it; not guaranteed
return build81Table;
}else if(build == 80){
return build80Table;
}else{
return build79Table;
}
}*/
}
+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);
}
}