DEADSOFTWARE

Fix json load
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameSaver.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.files.FileHandle;
5 import ru.deadsoftware.cavedroid.CaveGame;
7 import java.io.*;
8 import java.nio.ByteBuffer;
10 public class GameSaver {
12 private static final int VERSION = 0;
14 private static byte[] intToBytes(int i) {
15 return ByteBuffer.allocate(4).putInt(i).array();
16 }
18 private static void saveMap(FileHandle file, int[][] map) throws IOException {
19 int rl, bl;
20 int width = map.length;
21 int height = map[0].length;
22 BufferedOutputStream out = new BufferedOutputStream(file.write(false));
23 out.write(intToBytes(VERSION));
24 out.write(intToBytes(width));
25 out.write(intToBytes(height));
26 for (int y = 0; y < height; y++) {
27 bl = map[0][y];
28 rl = 0;
29 for (int x = 0; x < width; x++) {
30 if (map[x][y] != bl) {
31 out.write(intToBytes(rl));
32 out.write(intToBytes(bl));
33 rl = 0;
34 bl = map[x][y];
35 }
36 rl++;
37 }
38 out.write(intToBytes(rl));
39 out.write(intToBytes(bl));
40 }
41 out.flush();
42 out.close();
43 }
45 private static int[][] loadMap(FileHandle file) throws Exception {
46 int[][] map;
47 int ver, width, height;
48 int rl, bl;
49 DataInputStream in = new DataInputStream(file.read());
50 ver = in.readInt();
51 if (VERSION == ver) {
52 width = in.readInt();
53 height = in.readInt();
54 map = new int[width][height];
55 for (int y = 0; y < height; y++) {
56 for (int x = 0; x < width; x += rl) {
57 rl = in.readInt();
58 bl = in.readInt();
59 for (int i = x; i < x + rl; i++) map[i][y] = bl;
60 }
61 }
62 in.close();
63 } else throw new Exception("version mismatch");
64 return map;
65 }
67 public static GameProc load() {
68 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
69 GameProc gameProc = null;
70 try {
71 ObjectInputStream in = new ObjectInputStream(file.read());
72 int ver = in.readInt();
73 if (VERSION == ver) gameProc = (GameProc) in.readObject();
74 else throw new Exception("version mismatch");
75 in.close();
76 gameProc.world = new GameWorld();
77 gameProc.world.setMaps(
78 loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
79 loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
80 );
81 gameProc.physics = new GamePhysics();
82 gameProc.resetRenderer();
83 } catch (Exception e) {
84 Gdx.app.error("GameSaver", e.getMessage(), e);
85 Gdx.app.exit();
86 }
87 return gameProc;
88 }
90 public static void save(GameProc gp) {
91 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
92 file.mkdirs();
93 file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
94 try {
95 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
96 out.writeInt(VERSION);
97 out.writeObject(gp);
98 out.close();
99 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
100 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
101 } catch (Exception e) {
102 e.printStackTrace();
106 public static boolean exists() {
107 return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
108 Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
109 Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());