DEADSOFTWARE

Fix NPE after loading game
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameSaver.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.files.FileHandle;
5 import com.badlogic.gdx.utils.ArrayMap;
6 import ru.deadsoftware.cavecraft.CaveGame;
8 import java.io.*;
9 import java.nio.ByteBuffer;
11 public class GameSaver {
13 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<map[0].length; y++) {
28 bl = map[0][y];
29 rl=0;
30 for (int x=0; x<map.length; x++) {
31 if (map[x][y]!=bl) {
32 out.write(intToBytes(rl));
33 out.write(intToBytes(bl));
34 rl=0;
35 bl=map[x][y];
36 }
37 rl++;
38 }
39 out.write(intToBytes(rl));
40 out.write(intToBytes(bl));
41 }
42 out.flush();
43 out.close();
44 }
46 private static int[][] loadMap(FileHandle file) throws Exception {
47 int[][] map;
48 int ver, width, height;
49 int rl,bl;
50 DataInputStream in = new DataInputStream(file.read());
51 ver = in.readInt();
52 if (VERSION == ver) {
53 width = in.readInt();
54 height = in.readInt();
55 map = new int[width][height];
56 for (int y=0; y<height; y++) {
57 for (int x=0; x<width; x+=rl) {
58 rl = in.readInt();
59 bl = in.readInt();
60 for (int i=x; i<x+rl; i++) map[i][y] = bl;
61 }
62 }
63 in.close();
64 } else throw new Exception("version mismatch");
65 return map;
66 }
68 public static GameProc load() {
69 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
70 GameProc gameProc = null;
71 try {
72 ObjectInputStream in = new ObjectInputStream(file.read());
73 int ver = in.readInt();
74 if (VERSION == ver) gameProc = (GameProc)in.readObject();
75 else throw new Exception("version mismatch");
76 in.close();
77 gameProc.world = new GameWorld();
78 gameProc.world.setMaps(
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.world.metaMap = new ArrayMap<String, Integer>();
83 gameProc.physics = new GamePhysics(gameProc);
84 gameProc.resetRenderer();
85 } catch (Exception e) {
86 Gdx.app.error("GameSaver",e.getMessage(),e);
87 Gdx.app.exit();
88 }
89 return gameProc;
90 }
92 public static void save(GameProc gameProc) {
93 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/");
94 file.mkdirs();
95 file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
96 try {
97 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
98 out.writeInt(VERSION);
99 out.writeObject(gameProc);
100 out.close();
101 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap());
102 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap());
103 } catch (Exception e) {
104 e.printStackTrace();
108 public static boolean exists() {
109 return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav").exists() &&
110 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
111 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());