diff --git a/android/assets/.klooni.sav b/android/assets/.klooni.sav index 4382609..2b2abd0 100644 Binary files a/android/assets/.klooni.sav and b/android/assets/.klooni.sav differ diff --git a/core/src/io/github/lonamiwebs/klooni/serializer/BinSerializer.java b/core/src/io/github/lonamiwebs/klooni/serializer/BinSerializer.java index d58b665..a56af99 100644 --- a/core/src/io/github/lonamiwebs/klooni/serializer/BinSerializer.java +++ b/core/src/io/github/lonamiwebs/klooni/serializer/BinSerializer.java @@ -5,12 +5,23 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Arrays; public class BinSerializer { + + // ascii (klooni) and binary (1010b) + private static byte[] HEADER = { 0x6B, 0x6C, 0x6F, 0x6F, 0x6E, 0x69, 0xa }; + + // MODIFY THIS VALUE EVERY TIME A BinSerializable IMPLEMENTATION CHANGES + // Or unwanted results will happen and corrupt the game in an unknown way. + private static int VERSION = 1; + public static void serialize(final BinSerializable serializable, final OutputStream output) throws IOException { DataOutputStream out = new DataOutputStream(output); try { + out.write(HEADER); + out.writeInt(VERSION); serializable.write(out); } finally { try { @@ -23,6 +34,19 @@ public class BinSerializer { throws IOException { DataInputStream in = new DataInputStream(input); try { + // Read the HEADER and the VERSION (checks) + byte[] savedBuffer = new byte[HEADER.length]; + in.readFully(savedBuffer); + if (!Arrays.equals(savedBuffer, HEADER)) + throw new IOException("Invalid saved header found."); + + int savedVersion = in.readInt(); + if (savedVersion != VERSION) { + throw new IOException( + "Invalid saved version found. Should be " + VERSION + ", not " + savedVersion); + } + + // Read the saved data if the checks passed serializable.read(in); } finally { try {