Better save validation

This commit is contained in:
Anuken
2020-10-29 19:03:58 -04:00
parent cd6516786b
commit 1663497e6c
4 changed files with 17 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package mindustry.io;
import arc.struct.*;
import arc.struct.ObjectMap.*;
import arc.util.*;
import arc.util.io.*;
import mindustry.world.*;
@@ -60,9 +61,11 @@ public abstract class SaveFileReader{
protected final DataOutputStream dataBytesSmall = new DataOutputStream(byteOutputSmall);
protected int lastRegionLength;
protected @Nullable CounterInputStream currCounter;
protected void region(String name, DataInput stream, CounterInputStream counter, IORunner<DataInput> cons) throws IOException{
counter.resetCount();
this.currCounter = counter;
int length;
try{
length = readChunk(stream, cons);
@@ -70,8 +73,8 @@ public abstract class SaveFileReader{
throw new IOException("Error reading region \"" + name + "\".", e);
}
if(length != counter.count() - 4){
throw new IOException("Error reading region \"" + name + "\": read length mismatch. Expected: " + length + "; Actual: " + (counter.count() - 4));
if(length != counter.count - 4){
throw new IOException("Error reading region \"" + name + "\": read length mismatch. Expected: " + length + "; Actual: " + (counter.count - 4));
}
}
@@ -114,8 +117,11 @@ public abstract class SaveFileReader{
/** Reads a chunk of some length. Use the runner for reading to catch more descriptive errors. */
public int readChunk(DataInput input, boolean isShort, IORunner<DataInput> runner) throws IOException{
int length = isShort ? input.readUnsignedShort() : input.readInt();
int pos = currCounter.count;
lastRegionLength = length;
runner.accept(input);
if(pos + length != currCounter.count) throw new IOException("Read length mismatch. Expected: " + length + ", Actual: " + (currCounter.count - pos));
return length;
}