DEADSOFTWARE

Read with DataInputStream
authorfred-boy <fred-boy@protonmail.com>
Tue, 24 Apr 2018 18:45:09 +0000 (01:45 +0700)
committerfred-boy <fred-boy@protonmail.com>
Tue, 24 Apr 2018 18:45:09 +0000 (01:45 +0700)
core/src/ru/deadsoftware/cavecraft/game/GameSaver.java
core/src/ru/deadsoftware/cavecraft/game/GameWorld.java

index 1bf6684682e6e6af212e4d158cb5699e36118a7b..aab81f54ecb26d101c315e0fbe8c98bd4dab1e8f 100644 (file)
@@ -4,26 +4,13 @@ import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.files.FileHandle;
 import ru.deadsoftware.cavecraft.CaveGame;
 
-import java.io.BufferedOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
+import java.io.*;
 import java.nio.ByteBuffer;
 
 public class GameSaver {
 
     private static final int VERSION = 0;
 
-    private static int[][] fMap, bMap;
-    private static int readIndex;
-
-    private static int bytesToInt(byte[] bytes) {
-        ByteBuffer wrapped = ByteBuffer.wrap(bytes);
-        int res = wrapped.getInt(readIndex);
-        readIndex+=4;
-        return res;
-    }
-
     private static byte[] intToBytes(int i) {
         return ByteBuffer.allocate(4).putInt(i).array();
     }
@@ -56,44 +43,27 @@ public class GameSaver {
     }
 
     private static int[][] loadMap(FileHandle file) throws Exception {
-        int[][] map = null;
+        int[][] map;
         int ver, width, height;
         int rl,bl;
-        byte[] data = file.readBytes();
-        readIndex = 0;
-        ver = bytesToInt(data);
+        DataInputStream in = new DataInputStream(file.read());
+        ver = in.readInt();
         if (VERSION == ver) {
-            width = bytesToInt(data);
-            height = bytesToInt(data);
+            width = in.readInt();
+            height = in.readInt();
             map = new int[width][height];
             for (int y=0; y<height; y++) {
                 for (int x=0; x<width; x+=rl) {
-                    rl = bytesToInt(data);
-                    bl = bytesToInt(data);
+                    rl = in.readInt();
+                    bl = in.readInt();
                     for (int i=x; i<x+rl; i++) map[i][y] = bl;
                 }
             }
+            in.close();
         } else throw new Exception("version mismatch");
         return map;
     }
 
-    public static int[][] getLoadedForeMap() {
-        return fMap;
-    }
-
-    public static int[][] getLoadedBackMap() {
-        return bMap;
-    }
-
-    public static void loadWorld() {
-        try {
-            fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"));
-            bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"));
-        } catch (Exception e) {
-            Gdx.app.error("GameSaver",e.getMessage(),e);
-        }
-    }
-
     public static GameProc load() {
         FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
         GameProc gameProc = null;
@@ -103,15 +73,17 @@ public class GameSaver {
             if (VERSION == ver) gameProc = (GameProc)in.readObject();
                 else throw new Exception("version mismatch");
             in.close();
+            gameProc.world = new GameWorld();
+            gameProc.world.setMaps(
+                    loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
+                    loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
+            );
+            gameProc.physics = new GamePhysics(gameProc);
+            gameProc.resetRenderer();
         } catch (Exception e) {
             Gdx.app.error("GameSaver",e.getMessage(),e);
+            Gdx.app.exit();
         }
-        gameProc.world = new GameWorld();
-        gameProc.world.load();
-        gameProc.physics = new GamePhysics(gameProc);
-        gameProc.resetRenderer();
-        fMap = null;
-        bMap = null;
         return gameProc;
     }
 
index 63169fbb7d5d6b8de1e753398add86cd095fb2e0..e86fb3a5e5a5ecbf0c5b2ada365a7ef61c4aee3f 100644 (file)
@@ -103,10 +103,9 @@ public class GameWorld {
         WorldGen.clear();
     }
 
-    public void load() {
-        GameSaver.loadWorld();
-        foreMap = GameSaver.getLoadedForeMap();
-        backMap = GameSaver.getLoadedBackMap();
+    public void setMaps(int[][] foreMap, int[][] backMap) {
+        this.foreMap = foreMap.clone();
+        this.backMap = backMap.clone();
         WIDTH = foreMap.length;
         HEIGHT = foreMap[0].length;
     }