DEADSOFTWARE

Save with BufferedOutputStream
[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 ru.deadsoftware.cavecraft.CaveGame;
7 import java.io.BufferedOutputStream;
8 import java.io.IOException;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.nio.ByteBuffer;
13 public class GameSaver {
15 private static final int VERSION = 0;
17 private static int[][] fMap, bMap;
18 private static int readIndex;
20 private static int bytesToInt(byte[] bytes) {
21 ByteBuffer wrapped = ByteBuffer.wrap(bytes);
22 int res = wrapped.getInt(readIndex);
23 readIndex+=4;
24 return res;
25 }
27 private static byte[] intToBytes(int i) {
28 return ByteBuffer.allocate(4).putInt(i).array();
29 }
31 private static void saveMap(FileHandle file, int[][] map) throws IOException{
32 int rl,bl;
33 int width = map.length;
34 int height = map[0].length;
35 BufferedOutputStream out = new BufferedOutputStream(file.write(false));
36 out.write(intToBytes(VERSION));
37 out.write(intToBytes(width));
38 out.write(intToBytes(height));
39 for (int y=0; y<map[0].length; y++) {
40 bl = map[0][y];
41 rl=0;
42 for (int x=0; x<map.length; x++) {
43 if (map[x][y]!=bl) {
44 out.write(intToBytes(rl));
45 out.write(intToBytes(bl));
46 rl=0;
47 bl=map[x][y];
48 }
49 rl++;
50 }
51 out.write(intToBytes(rl));
52 out.write(intToBytes(bl));
53 }
54 out.flush();
55 out.close();
56 }
58 private static int[][] loadMap(FileHandle file) throws Exception {
59 int[][] map = null;
60 int ver, width, height;
61 int rl,bl;
62 byte[] data = file.readBytes();
63 readIndex = 0;
64 ver = bytesToInt(data);
65 if (VERSION == ver) {
66 width = bytesToInt(data);
67 height = bytesToInt(data);
68 map = new int[width][height];
69 for (int y=0; y<height; y++) {
70 for (int x=0; x<width; x+=rl) {
71 rl = bytesToInt(data);
72 bl = bytesToInt(data);
73 for (int i=x; i<x+rl; i++) map[i][y] = bl;
74 }
75 }
76 } else throw new Exception("version mismatch");
77 return map;
78 }
80 public static int[][] getLoadedForeMap() {
81 return fMap;
82 }
84 public static int[][] getLoadedBackMap() {
85 return bMap;
86 }
88 public static void loadWorld() {
89 try {
90 fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"));
91 bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"));
92 } catch (Exception e) {
93 Gdx.app.error("GameSaver",e.getMessage(),e);
94 }
95 }
97 public static GameProc load() {
98 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
99 GameProc gameProc = null;
100 try {
101 ObjectInputStream in = new ObjectInputStream(file.read());
102 int ver = in.readInt();
103 if (VERSION == ver) gameProc = (GameProc)in.readObject();
104 else throw new Exception("version mismatch");
105 in.close();
106 } catch (Exception e) {
107 Gdx.app.error("GameSaver",e.getMessage(),e);
109 gameProc.world = new GameWorld();
110 gameProc.world.load();
111 gameProc.physics = new GamePhysics(gameProc);
112 gameProc.resetRenderer();
113 fMap = null;
114 bMap = null;
115 return gameProc;
118 public static void save(GameProc gameProc) {
119 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/");
120 file.mkdirs();
121 file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
122 try {
123 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
124 out.writeInt(VERSION);
125 out.writeObject(gameProc);
126 out.close();
127 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap());
128 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap());
129 } catch (Exception e) {
130 e.printStackTrace();
134 public static boolean exists() {
135 return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav").exists() &&
136 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
137 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());