1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.files
.FileHandle
;
5 import ru
.deadsoftware
.cavedroid
.MainConfig
;
6 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
7 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
9 import javax
.annotation
.CheckForNull
;
11 import java
.nio
.ByteBuffer
;
13 public class GameSaver
{
15 public static class Data
{
17 private MobsController mMobsController
;
19 private DropController mDropController
;
21 private int[][] mForeMap
, mBackMap
;
23 public Data(MobsController mobsController
, DropController dropController
, int[][] foreMap
, int[][] backMap
) {
24 mMobsController
= mobsController
;
25 mDropController
= dropController
;
30 public MobsController
retrieveMobsController() {
31 assert mMobsController
!= null;
32 MobsController mobsController
= mMobsController
;
33 mMobsController
= null;
34 return mobsController
;
37 public DropController
retrieveDropController() {
38 assert mDropController
!= null;
39 DropController dropController
= mDropController
;
40 mDropController
= null;
41 return dropController
;
44 public int[][] retrieveForeMap() {
45 assert mForeMap
!= null;
46 int[][] foreMap
= mForeMap
;
51 public int[][] retrieveBackMap() {
52 assert mBackMap
!= null;
53 int[][] backMap
= mBackMap
;
58 public boolean isEmpty() {
59 return mMobsController
== null && mDropController
== null && mForeMap
== null && mBackMap
== null;
63 private static final int SAVE_VERSION
= 1;
65 private static byte[] intToBytes(int i
) {
66 return ByteBuffer
.allocate(4).putInt(i
).array();
69 private static void saveMap(FileHandle file
, int[][] map
) throws IOException
{
71 int width
= map
.length
;
72 int height
= map
[0].length
;
74 BufferedOutputStream out
= new BufferedOutputStream(file
.write(false));
76 out
.write(intToBytes(SAVE_VERSION
));
77 out
.write(intToBytes(width
));
78 out
.write(intToBytes(height
));
80 for (int y
= 0; y
< height
; y
++) {
83 for (int[] ints
: map
) {
84 if (ints
[y
] != block
) {
85 out
.write(intToBytes(run
));
86 out
.write(intToBytes(block
));
92 out
.write(intToBytes(run
));
93 out
.write(intToBytes(block
));
100 private static int[][] loadMap(FileHandle file
) throws Exception
{
102 int version
, width
, height
;
105 DataInputStream in
= new DataInputStream(file
.read());
107 version
= in
.readInt();
109 if (SAVE_VERSION
== version
) {
110 width
= in
.readInt();
111 height
= in
.readInt();
112 map
= new int[width
][height
];
113 for (int y
= 0; y
< height
; y
++) {
114 for (int x
= 0; x
< width
; x
+= run
) {
116 block
= in
.readInt();
117 for (int i
= x
; i
< x
+ run
; i
++) {
123 throw new Exception("version mismatch");
131 public static Data
load(MainConfig mainConfig
) {
132 String folder
= mainConfig
.getGameFolder();
133 FileHandle file
= Gdx
.files
.absolute(folder
+ "/saves/game.sav");
136 ObjectInputStream in
= new ObjectInputStream(file
.read());
137 int version
= in
.readInt();
138 DropController dropController
;
139 MobsController mobsController
;
141 if (SAVE_VERSION
== version
) {
142 dropController
= (DropController
) in
.readObject();
143 mobsController
= (MobsController
) in
.readObject();
145 throw new Exception("version mismatch");
150 int[][] foreMap
= loadMap(Gdx
.files
.absolute(mainConfig
.getGameFolder() + "/saves/foremap.sav"));
151 int[][] backMap
= loadMap(Gdx
.files
.absolute(mainConfig
.getGameFolder() + "/saves/backmap.sav"));
153 if (dropController
== null || mobsController
== null) {
154 throw new Exception("couldn't load");
157 return new Data(mobsController
, dropController
, foreMap
, backMap
);
158 } catch (Exception e
) {
159 Gdx
.app
.error("GameSaver", e
.getMessage());
165 public static void save(MainConfig mainConfig
,
166 DropController dropController
,
167 MobsController mobsController
,
168 GameWorld gameWorld
) {
170 String folder
= mainConfig
.getGameFolder();
171 FileHandle file
= Gdx
.files
.absolute(folder
+ "/saves/");
173 file
= Gdx
.files
.absolute(folder
+ "/saves/game.sav");
176 ObjectOutputStream out
= new ObjectOutputStream(file
.write(false));
177 out
.writeInt(SAVE_VERSION
);
178 out
.writeObject(dropController
);
179 out
.writeObject(mobsController
);
181 saveMap(Gdx
.files
.absolute(folder
+ "/saves/foremap.sav"), gameWorld
.getFullForeMap());
182 saveMap(Gdx
.files
.absolute(folder
+ "/saves/backmap.sav"), gameWorld
.getFullBackMap());
183 } catch (Exception e
) {
188 public static boolean exists(MainConfig mainConfig
) {
189 String folder
= mainConfig
.getGameFolder();
190 return (Gdx
.files
.absolute(folder
+ "/saves/game.sav").exists() &&
191 Gdx
.files
.absolute(folder
+ "/saves/foremap.sav").exists() &&
192 Gdx
.files
.absolute(folder
+ "/saves/backmap.sav").exists());