diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 02fcb83..0e61870 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -11,32 +11,6 @@ import java.util.Arrays; public class BinaryParser implements FileParser { - /** - * check if the file is a binary file suited for parsing - * @return true if it is a binary File - */ - public static boolean isBinaryFile(File file) throws IOException { - if (Files.probeContentType(file.toPath()) != null) - return false ; - - FileInputStream r_file = new FileInputStream(file); - byte[] content = r_file.readAllBytes(); - - boolean header_found = false; - for (int i = 0; i < content.length; i++) { - if (!header_found) { - if (content[i] == 'S' && content[i + 1] == 'M' && content[i + 2] == 'S') { - header_found = true; - } - } else { - if (content[i] == 'S' && content[i + 1] == 'M' && content[i + 2] == 'E') { - return true; - } - } - } - return false; - } - @Override public Map getLevel(File file, boolean saved_data) throws IOException { Map ret; diff --git a/app/src/main/java/school_project/Parsers/FileParser.java b/app/src/main/java/school_project/Parsers/FileParser.java index e590194..c20d0bc 100644 --- a/app/src/main/java/school_project/Parsers/FileParser.java +++ b/app/src/main/java/school_project/Parsers/FileParser.java @@ -3,6 +3,7 @@ package school_project.Parsers; import school_project.Map; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; public interface FileParser { @@ -14,6 +15,8 @@ public interface FileParser { * @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" + * @throws FileNotFoundException if the file was not found or was not accessible + * @throws IOException if an I/O occurs */ Map getLevel(File file, boolean saved_data) throws IOException; @@ -23,6 +26,8 @@ public interface FileParser { * @param file the file where to save * @param levelData the map to save * @param save_data should save the map data (need to be false only in development I think) + * @throws FileNotFoundException The file could not be created + * @throws IOException if an I/O occurs */ void saveLevel(File file, Map levelData, boolean save_data) throws IOException; } diff --git a/app/src/main/java/school_project/Parsers/FileParserFactory.java b/app/src/main/java/school_project/Parsers/FileParserFactory.java index a5501b1..a7f4f68 100644 --- a/app/src/main/java/school_project/Parsers/FileParserFactory.java +++ b/app/src/main/java/school_project/Parsers/FileParserFactory.java @@ -1,27 +1,83 @@ package school_project.Parsers; +import javafx.util.Pair; +import school_project.Map; + import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.NotSerializableException; /** * This is used to find the right parser to parser a save/level file. + * This should be the only right way to save/load a file! you can just use `Map loadMapFromFile(File)` to load a file + * and `void saveFileFromMap(File, Map)` to save a file * *
- * Mor file format can be added in the future by adding a new class that implement parser and making it recognisable by - * the CreateParser class + * there is currently 2 file format with 2 variation each (save file or level file) + * - BinaryParser + * - ".level" + * - ".slevel" + * - SerializeParser + * - ".serialized" + * - ".sserialized" + *
+ * + *+ * More file format can be added in the future by adding a new class that implement parser + * and adding it to this file *
* * @author tonitch */ public class FileParserFactory { - public static FileParser createParser(File file) throws IOException { - if(BinaryParser.isBinaryFile(file)) - return new BinaryParser(); - else if (JsonParser.isJsonFile(file)) - return new JsonParser(); - else - throw new NotSerializableException("This file format is not supported"); + /** + * Load a file and return a map + * If this is a save map, return the map with its save data + * @param file file to get data from + * @return Map generated from the file + * @throws FileNotFoundException if the file was not found or was not accessible + * @throws IOException if an I/O occurs + */ + public static Map loadMapFromFile(File file) throws IOException { + Pair