From e54fab4c2f8caaee5cb4d8d0f2305c9e77d95515 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Thu, 23 Mar 2023 12:38:12 +0100 Subject: [PATCH] WIP: adding save data --- JournalDeBord/src/spec/FileParser.md | 3 ++ app/src/main/java/school_project/Map.java | 4 ++ .../school_project/Parsers/BinaryParser.java | 38 +++++++++++++++- .../school_project/Parsers/FileParser.java | 14 +++++- .../school_project/Parsers/JsonParser.java | 10 +++++ .../Parsers/BinaryParserTest.java | 43 +++++++++++++++++++ 6 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/school_project/Parsers/BinaryParserTest.java diff --git a/JournalDeBord/src/spec/FileParser.md b/JournalDeBord/src/spec/FileParser.md index f34d0bc..eed995a 100644 --- a/JournalDeBord/src/spec/FileParser.md +++ b/JournalDeBord/src/spec/FileParser.md @@ -31,6 +31,9 @@ level data at the top of the file. - 4 first bits : width - 4 last bits: height - next bytes -> Width * Height (+1 if Width * Height % 8 is not 0) + - Position ( only for saved file ) + - 'F' if the piece is in float state ( not placed) + - x and y on 2 bytes for position if the piece is placed ## Known Limitation diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index 418a7f2..30568e5 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -54,4 +54,8 @@ public class Map extends Shape{ } return used; } + + public ArrayList getPieces() { + return pieces; + } } diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index fee1c7d..d26a708 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -10,9 +10,14 @@ import java.util.Arrays; public class BinaryParser implements FileParser { + @Override + public Map getSavedData(File file) { + return null; + } + @Override public void saveLevelData(File file, Map levelData) throws IOException { - // TODO: 3/20/23 + } /** @@ -20,7 +25,6 @@ public class BinaryParser implements FileParser { * @return true if it is a binary File */ public static boolean isBinaryFile(File file) throws IOException { - // TODO: 3/20/23 Need more testing if (Files.probeContentType(file.toPath()) != null) return false ; @@ -42,6 +46,7 @@ public class BinaryParser implements FileParser { return false; } + @Override public Map getLevel(File file) throws IOException { Map ret; @@ -57,6 +62,11 @@ public class BinaryParser implements FileParser { return ret; } + @Override + public void saveLevel(File file, Map levelData) throws FileNotFoundException { +// byte[] data = new byte[]; + } + /** * Extract Level data from file content * @param fileStream file stream to read extract data from @@ -147,4 +157,28 @@ public class BinaryParser implements FileParser { } return ret; } + + /** + * give the amount of byte needed to store the given Map + * following the binary file format + * @param level the map to check + * @param data should add save data or only level data + * @return integer of the ammount of byte needed + */ + public static int getByteSizeForMap(Map level, boolean data){ + int ret = 6; // header + footer + ret += 2; //size of the piece + ret += ((level.getWidth() * level.getHeight()) / 8); // size of the map + ret += level.getHeight() * level.getWidth() % 8 == 0 ? 0 : 1; // Add 1 if the size of map is not a mult of 8 + ret += 1; // amount of pieces + for(Piece p: level.getPieces()){ + ret += 1; // size of the piece + ret += p.getHeight() * p.getWidth() / 8; + ret += p.getHeight() * p.getWidth() % 8 == 0 ? 0 : 1; // add 1 if the size of the piece is not mult of 8 + if(data){ + ret += p.getPosition() != null ? 2: 1; // if the piece is not placed, only one byte else 2 + } + } + return ret; + } } \ No newline at end of file diff --git a/app/src/main/java/school_project/Parsers/FileParser.java b/app/src/main/java/school_project/Parsers/FileParser.java index 7df3566..f77ac14 100644 --- a/app/src/main/java/school_project/Parsers/FileParser.java +++ b/app/src/main/java/school_project/Parsers/FileParser.java @@ -10,13 +10,21 @@ public interface FileParser { /** * Parse the file and create a Map with its shape and pieces setup - * Ignore the rest of the file! (ie: save data) * * @param file file to parse + * @param saved_data does the saved data should be added to the map * @return Map Object parsed with file data * @see "TODO: Add Specification when done" */ - Map getLevel(File file) throws IOException; + Map getLevel(File file, boolean saved_data) throws IOException; + + /** + * Save Map to a file without all it's data + * Could be used for generating level file. might not be used in game. + * @param file the file where to save + * @param levelData the map to save + */ + void saveLevel(File file, Map levelData) throws FileNotFoundException; /** * Save Map to a file with all it's data @@ -25,6 +33,8 @@ public interface FileParser { */ void saveLevelData(File file, Map levelData) throws IOException; + + // TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file } diff --git a/app/src/main/java/school_project/Parsers/JsonParser.java b/app/src/main/java/school_project/Parsers/JsonParser.java index 14d56d8..0e15404 100644 --- a/app/src/main/java/school_project/Parsers/JsonParser.java +++ b/app/src/main/java/school_project/Parsers/JsonParser.java @@ -7,6 +7,16 @@ import java.nio.file.Files; public class JsonParser implements FileParser{ + @Override + public Map getSavedData(File file) { + return null; + } + + @Override + public void saveLevel(File file, Map levelData) throws FileNotFoundException { + + } + @Override public void saveLevelData(File file, Map levelData) throws IOException { FileOutputStream fileStream = new FileOutputStream(file); diff --git a/app/src/test/java/school_project/Parsers/BinaryParserTest.java b/app/src/test/java/school_project/Parsers/BinaryParserTest.java new file mode 100644 index 0000000..a34dff2 --- /dev/null +++ b/app/src/test/java/school_project/Parsers/BinaryParserTest.java @@ -0,0 +1,43 @@ +package school_project.Parsers; + +import org.junit.jupiter.api.Test; +import school_project.Map; +import school_project.Piece; +import school_project.Vec2; + +import static org.junit.jupiter.api.Assertions.*; + +class BinaryParserTest { + + @Test + void getByteSizeForMap() { + boolean[][] map_shape = { + { true, true, true }, + { true, false, true }, + { true, true, true }, + + }; + boolean[][] piece1_shape = { + { true, true }, + { true, false }, + { true, true }, + + }; + boolean[][] piece2_shape = { + { true }, + { true }, + { true }, + + }; + Map map = new Map(map_shape); + Piece piece1 = new Piece(piece1_shape); + Piece piece2 = new Piece(piece2_shape); + + map.addPiece(new Piece[]{piece1, piece2}); + piece2.setPosition(new Vec2(0, 2)); + + + assertEquals(15, BinaryParser.getByteSizeForMap(map, false)); + assertEquals(18, BinaryParser.getByteSizeForMap(map, true)); + } +} \ No newline at end of file