DEADSOFTWARE

Nonnull by default
[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;
15 private static byte[] intToBytes(int i) {
16 return ByteBuffer.allocate(4).putInt(i).array();
17 }
19 private static void saveMap(FileHandle file, int[][] map) throws IOException {
20 int rl, bl;
21 int width = map.length;
22 int height = map[0].length;
23 BufferedOutputStream out = new BufferedOutputStream(file.write(false));
24 out.write(intToBytes(VERSION));
25 out.write(intToBytes(width));
26 out.write(intToBytes(height));
27 for (int y = 0; y < height; y++) {
28 bl = map[0][y];
29 rl = 0;
30 for (int[] ints : map) {
31 if (ints[y] != bl) {
32 out.write(intToBytes(rl));
33 out.write(intToBytes(bl));
34 rl = 0;
35 bl = ints[y];
36 }
37 rl++;
38 }
39 out.write(intToBytes(rl));
40 out.write(intToBytes(bl));
41 }
42 out.flush();
43 out.close();
44 }
47 private static int[][] loadMap(FileHandle file) throws Exception {
48 int[][] map;
49 int ver, width, height;
50 int rl, bl;
51 DataInputStream in = new DataInputStream(file.read());
52 ver = in.readInt();
53 if (VERSION == ver) {
54 width = in.readInt();
55 height = in.readInt();
56 map = new int[width][height];
57 for (int y = 0; y < height; y++) {
58 for (int x = 0; x < width; x += rl) {
59 rl = in.readInt();
60 bl = in.readInt();
61 for (int i = x; i < x + rl; i++) map[i][y] = bl;
62 }
63 }
64 in.close();
65 } else throw new Exception("version mismatch");
66 return map;
67 }
69 public static GameProc load() {
70 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
71 GameProc gameProc = null;
72 try {
73 ObjectInputStream in = new ObjectInputStream(file.read());
74 int ver = in.readInt();
75 if (VERSION == ver) gameProc = (GameProc) in.readObject();
76 else throw new Exception("version mismatch");
77 in.close();
78 gameProc.world = new GameWorld(
79 loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
80 loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
81 );
82 gameProc.physics = new GamePhysics();
83 gameProc.input = new GameInput();
84 } catch (Exception e) {
85 Gdx.app.error("GameSaver", e.getMessage(), e);
86 Gdx.app.exit();
87 }
88 return gameProc;
89 }
91 public static void save(GameProc gp) {
92 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
93 file.mkdirs();
94 file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
95 try {
96 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
97 out.writeInt(VERSION);
98 out.writeObject(gp);
99 out.close();
100 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
101 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
102 } catch (Exception e) {
103 e.printStackTrace();
107 public static boolean exists() {
108 return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
109 Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
110 Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());