diff --git a/src/main/java/com/backsun/lod/handlers/LodDimensionFileHandler.java b/src/main/java/com/backsun/lod/handlers/LodDimensionFileHandler.java index 5ec179e84..6790f2638 100644 --- a/src/main/java/com/backsun/lod/handlers/LodDimensionFileHandler.java +++ b/src/main/java/com/backsun/lod/handlers/LodDimensionFileHandler.java @@ -18,7 +18,7 @@ import com.backsun.lod.objects.LodRegion; * to file. * * @author James Seibel - * @version 03-31-2021 + * @version 05-19-2021 */ public class LodDimensionFileHandler { @@ -30,6 +30,13 @@ public class LodDimensionFileHandler private final String FILE_NAME_PREFIX = "lod"; private final String FILE_EXTENSION = ".txt"; + /** This is the file version currently accepted by this + * file handler, older versions (smaller numbers) will be deleted and overwritten, + * newer versions (larger numbers) will be ignored and won't be read. */ + public static final int LOD_SAVE_FILE_VERSION = 1; + /** This is the string written before the file version */ + private static final String LOD_FILE_VERSION_PREFIX = "lod_save_file_version"; + /** Allow saving asynchronously, but never try to save multiple regions * at a time */ private ExecutorService fileWritingThreadPool = Executors.newSingleThreadExecutor(); @@ -83,7 +90,41 @@ public class LodDimensionFileHandler { BufferedReader br = new BufferedReader(new FileReader(f)); String s = br.readLine(); + int fileVersion = -1; + // try to get the file version + try + { + fileVersion = Integer.parseInt(s.substring(s.indexOf(' ')).trim()); + } + catch(NumberFormatException | StringIndexOutOfBoundsException e) + { + // this file doesn't have a version + // keep the version as -1 + } + + // check if this file can be read by this file handler + if(fileVersion < LOD_SAVE_FILE_VERSION) + { + // the file we are reading is an older version, + // close the reader and delete the file. + br.close(); + f.delete(); + + return null; + } + else if(fileVersion > LOD_SAVE_FILE_VERSION) + { + // the file we are reading is an newer version, + // close the reader and ignore the file, we don't + // want to accidently delete anything the user may want. + br.close(); + + return null; + } + + + // this file is a readable version, begin reading the file while(s != null && !s.isEmpty()) { try @@ -98,6 +139,8 @@ public class LodDimensionFileHandler // we were unable to create this chunk // for whatever reason. // skip to the next chunk + + // TODO write this to the log } s = br.readLine(); @@ -107,9 +150,7 @@ public class LodDimensionFileHandler } catch (IOException e) { - // File not found - - // or the buffered reader encountered a + // the buffered reader encountered a // problem reading the file return null; } @@ -175,6 +216,10 @@ public class LodDimensionFileHandler FileWriter fw = new FileWriter(f); + // add the version of this file + fw.write(LOD_FILE_VERSION_PREFIX + " " + LOD_SAVE_FILE_VERSION + "\n"); + + // add each LodChunk to the file for(LodChunk[] chunkArray : region.getAllLods()) for(LodChunk chunk : chunkArray) if(chunk != null && !chunk.isPlaceholder())