From 6bf9e187e0793dbe1961f01bcda833ca4492a715 Mon Sep 17 00:00:00 2001 From: cola98765 Date: Thu, 16 Sep 2021 11:41:53 +0200 Subject: [PATCH] this breaks old file version system by simply ignoring the old files. --- .../lod/handlers/LodDimensionFileHandler.java | 148 +++++++----------- .../seibel/lod/objects/LevelContainer.java | 8 +- .../lod/objects/SingleLevelContainer.java | 61 ++++---- .../lod/objects/VerticalLevelContainer.java | 5 +- 4 files changed, 90 insertions(+), 132 deletions(-) diff --git a/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java b/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java index bab05d836..95536420c 100644 --- a/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java +++ b/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java @@ -17,11 +17,7 @@ */ package com.seibel.lod.handlers; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.concurrent.ExecutorService; @@ -57,7 +53,7 @@ public class LodDimensionFileHandler /** * .txt */ - private static final String FILE_EXTENSION = ".txt"; + private static final String FILE_EXTENSION = ".dat"; /** * detail-# */ @@ -79,11 +75,6 @@ public class LodDimensionFileHandler */ public static final int LOD_SAVE_FILE_VERSION = 6; - /** - * 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 @@ -140,66 +131,50 @@ public class LodDimensionFileHandler // return anything continue; } - String data = ""; - BufferedReader bufferedReader = new BufferedReader(new FileReader(f)); - data = bufferedReader.readLine(); - int fileVersion = -1; + byte data[] = {0}; + long dataSize = f.length(); + dataSize -= 1; + if (dataSize > 0) { + try (InputStream inputStream = new BufferedInputStream(new FileInputStream(f))) { + int fileVersion = -1; + fileVersion = inputStream.read(); + // 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. + inputStream.close(); + f.delete(); + ClientProxy.LOGGER.info("Outdated LOD region file for region: (" + regionX + "," + regionZ + ") version found: " + fileVersion + + ", version requested: " + LOD_SAVE_FILE_VERSION + + " File was been deleted."); - if (data != null && !data.isEmpty()) - { - // try to get the file version - try - { - fileVersion = Integer.parseInt(data.substring(data.indexOf(' ')).trim()); - } catch (NumberFormatException | StringIndexOutOfBoundsException e) - { - // this file doesn't have a version - // keep the version as -1 - fileVersion = -1; + break; + } else if (fileVersion > LOD_SAVE_FILE_VERSION) { + // the file we are reading is a newer version, + // close the reader and ignore the file, we don't + // want to accidently delete anything the user may want. + inputStream.close(); + ClientProxy.LOGGER.info("Newer LOD region file for region: (" + regionX + "," + regionZ + ") version found: " + fileVersion + + ", version requested: " + LOD_SAVE_FILE_VERSION + + " this region will not be written to in order to protect the newer file."); + + break; + } + // this file is a readable version, begin reading the file + data = new byte[(int) dataSize]; + inputStream.read(data); + inputStream.close(); + } catch (IOException ex) { + ex.printStackTrace(); } - - // 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. - bufferedReader.close(); - f.delete(); - ClientProxy.LOGGER.info("Outdated LOD region file for region: (" + regionX + "," + regionZ + ") version found: " + fileVersion + - ", version requested: " + LOD_SAVE_FILE_VERSION + - " File was been deleted."); - - break; - } else if (fileVersion > LOD_SAVE_FILE_VERSION) - { - // the file we are reading is a newer version, - // close the reader and ignore the file, we don't - // want to accidently delete anything the user may want. - bufferedReader.close(); - ClientProxy.LOGGER.info("Newer LOD region file for region: (" + regionX + "," + regionZ + ") version found: " + fileVersion + - ", version requested: " + LOD_SAVE_FILE_VERSION + - " this region will not be written to in order to protect the newer file."); - - break; - } - } else - { - // there is no data in this file - bufferedReader.close(); - break; } - - // this file is a readable version, begin reading the file - data = bufferedReader.readLine(); - - bufferedReader.close(); switch (region.getLodQualityMode()){ default: case HEIGHTMAP: region.addLevel(new SingleLevelContainer(data)); break; case MULTI_LOD: - region.addLevel(new VerticalLevelContainer(data)); + //region.addLevel(new VerticalLevelContainer(data)); break; } //region.addLevel(new SingleLevelContainer(data)); @@ -293,25 +268,13 @@ public class LodDimensionFileHandler // is the correct version. // (to make sure we don't overwrite a newer // version file if it exists) - - BufferedReader br = new BufferedReader(new FileReader(oldFile)); - String s = br.readLine(); int fileVersion = LOD_SAVE_FILE_VERSION; - - if (s != null && !s.isEmpty()) - { - // 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 correctly formated version - // just overwrite the file - } + try (InputStream inputStream = new BufferedInputStream(new FileInputStream(oldFile))) { + fileVersion = inputStream.read(); + inputStream.close(); + } catch (IOException ex) { + ex.printStackTrace(); } - br.close(); - // check if this file can be written to by the file handler if (fileVersion <= LOD_SAVE_FILE_VERSION) { @@ -327,17 +290,20 @@ public class LodDimensionFileHandler // the old file is good, now create a new save file File newFile = new File(fileName + TMP_FILE_EXTENSION); - FileWriter fw = new FileWriter(newFile); + try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(newFile));) { - // add the version of this file - fw.write(LOD_FILE_VERSION_PREFIX + " " + LOD_SAVE_FILE_VERSION + "\n"); + // add the version of this file + outputStream.write(LOD_SAVE_FILE_VERSION); - // add each LodChunk to the file - fw.write(region.getLevel(detailLevel).toDataString()); - fw.close(); + // add each LodChunk to the file + outputStream.write(region.getLevel(detailLevel).toDataString()); + outputStream.close(); - // overwrite the old file with the new one - Files.move(newFile.toPath(), oldFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + // overwrite the old file with the new one + Files.move(newFile.toPath(), oldFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + }catch (IOException ex) { + ex.printStackTrace(); + } } catch (Exception e) { ClientProxy.LOGGER.error("LOD file write error. Unable to write to [" + fileName + "] error [" + e.getMessage() + "]: "); @@ -370,10 +336,10 @@ public class LodDimensionFileHandler // or // ".\Super Flat\data" return dimensionDataSaveFolder.getCanonicalPath() + File.separatorChar + - lodQualityMode + File.separatorChar + - generationMode.toString() + File.separatorChar + - DETAIL_FOLDER_NAME_PREFIX + detailLevel + File.separatorChar + - FILE_NAME_PREFIX + "." + regionX + "." + regionZ + FILE_EXTENSION; + lodQualityMode + File.separatorChar + + generationMode.toString() + File.separatorChar + + DETAIL_FOLDER_NAME_PREFIX + detailLevel + File.separatorChar + + FILE_NAME_PREFIX + "." + regionX + "." + regionZ + FILE_EXTENSION; } catch (IOException | SecurityException e) { ClientProxy.LOGGER.warn("Unable to get the filename for the region [" + regionX + ", " + regionZ + "], error: [" + e.getMessage() + "], stacktrace: "); diff --git a/src/main/java/com/seibel/lod/objects/LevelContainer.java b/src/main/java/com/seibel/lod/objects/LevelContainer.java index 75a1c28e9..b03a293e7 100644 --- a/src/main/java/com/seibel/lod/objects/LevelContainer.java +++ b/src/main/java/com/seibel/lod/objects/LevelContainer.java @@ -1,11 +1,5 @@ package com.seibel.lod.objects; -import com.seibel.lod.util.LevelPosUtil; -import com.seibel.lod.util.LodUtil; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - public interface LevelContainer { public static final char VERTICAL_DATA_DELIMITER = '\t'; @@ -74,5 +68,5 @@ public interface LevelContainer * This will give the data to save in the file * @return data as a String */ - public String toDataString(); + public byte[] toDataString(); } diff --git a/src/main/java/com/seibel/lod/objects/SingleLevelContainer.java b/src/main/java/com/seibel/lod/objects/SingleLevelContainer.java index e85405f09..9fc1a2390 100644 --- a/src/main/java/com/seibel/lod/objects/SingleLevelContainer.java +++ b/src/main/java/com/seibel/lod/objects/SingleLevelContainer.java @@ -1,8 +1,7 @@ package com.seibel.lod.objects; -import com.seibel.lod.builders.LodBuilder; -import com.seibel.lod.enums.DistanceGenerationMode; import com.seibel.lod.util.*; +import java.util.Arrays; public class SingleLevelContainer implements LevelContainer { @@ -57,17 +56,13 @@ public class SingleLevelContainer implements LevelContainer return new SingleLevelContainer((byte) (getDetailLevel() - 1)); } - public SingleLevelContainer(String inputString) + public SingleLevelContainer(byte inputData[]) { int tempIndex; - int shift = 0; int index = 0; - int digit; - char currentChar; long newData; - currentChar = inputString.charAt(index); - digit = Character.digit(currentChar,16); - detailLevel = (byte) digit; + detailLevel = inputData[index]; + index++; size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel); this.data = new long[size][size]; for (int x = 0; x < size; x++) @@ -75,21 +70,17 @@ public class SingleLevelContainer implements LevelContainer for (int z = 0; z < size; z++) { newData = 0; - for(tempIndex = 0; tempIndex < 16; tempIndex++) - { - if(index+tempIndex >= inputString.length()) - break; - currentChar = inputString.charAt(index+tempIndex); - if(currentChar == DATA_DELIMITER){ - break; - } - shift = (15-tempIndex)*4; - digit = Character.digit(currentChar,16); - newData += ((((long) digit & 0xf)) << shift); - } - newData = newData >>> (shift); + if(inputData[index] == 0) + index++; + else if(index + 7 >= inputData.length) + break; + else { + for (tempIndex = 0; tempIndex < 8; tempIndex++) + newData += (((long) inputData[index + tempIndex]) & 0xff) << (8 * tempIndex); + index = index + 8; + } data[x][z] = newData; - index = index + tempIndex; + } } } @@ -125,22 +116,28 @@ public class SingleLevelContainer implements LevelContainer return DataPointUtil.doesItExist(getSingleData(posX, posZ)); } - public String toDataString() + public byte[] toDataString() { - StringBuilder stringBuilder = new StringBuilder(); - int size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel); - stringBuilder.append(detailLevel); - stringBuilder.append(DATA_DELIMITER); + int index = 0; + int tempIndex; + byte[] tempData = new byte[1 + (size * size * 8)]; + tempData[index] = detailLevel; + index++; for (int x = 0; x < size; x++) { for (int z = 0; z < size; z++) { - //Converting the dataToHex - stringBuilder.append(Long.toHexString(data[x][z])); - stringBuilder.append(DATA_DELIMITER); + if(data[x][z] == 0){ + tempData[index] = 0; + index++; + } else { + for (tempIndex = 0; tempIndex < 8; tempIndex++) + tempData[index + tempIndex] = (byte) (data[x][z] >>> (8 * tempIndex)); + index += 8; + } } } - return stringBuilder.toString(); + return Arrays.copyOfRange(tempData, 0, index); } @Override diff --git a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java index 9a845780d..6c91f1dcc 100644 --- a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java +++ b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java @@ -114,9 +114,10 @@ public class VerticalLevelContainer implements LevelContainer addData(data,posX,posZ); } - public String toDataString() + public byte[] toDataString() { - return toString(); + byte[] temp = {}; + return temp; } @Override