comment and refactor LodDimensionFildHandler and ReflectionHandler

This commit is contained in:
James Seibel
2021-09-25 23:51:14 -05:00
parent fb5aceb44d
commit 391c99fd21
2 changed files with 118 additions and 92 deletions
@@ -52,23 +52,16 @@ import com.seibel.lod.util.LodUtil;
*/
public class LodDimensionFileHandler
{
private LodDimension loadedDimension = null;
public long regionLastWriteTime[][];
/** This is the dimension that owns this file handler */
private LodDimension lodDimension = null;
private File dimensionDataSaveFolder;
/**
* lod
*/
/** lod */
private static final String FILE_NAME_PREFIX = "lod";
/**
* .txt
*/
/** .txt */
private static final String FILE_EXTENSION = ".dat";
/**
* detail-#
*/
/** detail- */
private static final String DETAIL_FOLDER_NAME_PREFIX = "detail-";
/**
@@ -94,35 +87,33 @@ public class LodDimensionFileHandler
private ExecutorService fileWritingThreadPool = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName()));
public LodDimensionFileHandler(File newSaveFolder, LodDimension newLoadedDimension)
public LodDimensionFileHandler(File newSaveFolder, LodDimension newLodDimension)
{
if (newSaveFolder == null)
throw new IllegalArgumentException("LodDimensionFileHandler requires a valid File location to read and write to.");
dimensionDataSaveFolder = newSaveFolder;
loadedDimension = newLoadedDimension;
// these two variable are used in sync with the LodDimension
regionLastWriteTime = new long[loadedDimension.getWidth()][loadedDimension.getWidth()];
for (int i = 0; i < loadedDimension.getWidth(); i++)
for (int j = 0; j < loadedDimension.getWidth(); j++)
regionLastWriteTime[i][j] = -1;
lodDimension = newLodDimension;
}
//================//
// read from file //
//================//
/**
* Return the LodRegion region at the given coordinates.
* (null if the file doesn't exist)
* Returns the LodRegion at the given coordinates.
* Returns an empty region if the file doesn't exist.
*/
public LodRegion loadRegionFromFile(byte detailLevel, RegionPos regionPos, DistanceGenerationMode generationMode, VerticalQuality verticalQuality)
{
int regionX = regionPos.x;
int regionZ = regionPos.z;
LodRegion region = new LodRegion(LodUtil.REGION_DETAIL_LEVEL,regionPos, generationMode, verticalQuality);
LodRegion region = new LodRegion(LodUtil.REGION_DETAIL_LEVEL, regionPos, generationMode, verticalQuality);
for (byte tempDetailLevel = LodUtil.REGION_DETAIL_LEVEL; tempDetailLevel >= detailLevel; tempDetailLevel--)
{
String fileName = getFileNameAndPathForRegion(regionX, regionZ, generationMode, tempDetailLevel, verticalQuality);
@@ -135,71 +126,94 @@ public class LodDimensionFileHandler
throw new IllegalArgumentException("Unable to read region [" + regionX + ", " + regionZ + "] file, no fileName.");
File f = new File(fileName);
File file = new File(fileName);
if (!f.exists())
if (!file.exists())
{
// there wasn't a file, don't
// return anything
continue;
}
byte[] data = {0};
long dataSize = f.length();
// don't try parsing empty files
long dataSize = file.length();
dataSize -= 1;
if (dataSize > 0) {
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(f))) {
if (dataSize > 0)
{
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file)))
{
int fileVersion = -1;
fileVersion = inputStream.read();
// check if this file can be read by this file handler
if (fileVersion < LOD_SAVE_FILE_VERSION) {
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.");
file.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) {
}
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.");
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];
// this file is a readable version,
// read the file
byte[] data = new byte[(int) dataSize];
inputStream.read(data);
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
// add the data to our region
switch (region.getLodQualityMode())
{
default:
case HEIGHTMAP:
region.addLevel(new SingleLevelContainer(data));
break;
case MULTI_LOD:
region.addLevel(new VerticalLevelContainer(data));
break;
}
}
}
switch (region.getLodQualityMode()){
default:
case HEIGHTMAP:
region.addLevel(new SingleLevelContainer(data));
break;
case MULTI_LOD:
region.addLevel(new VerticalLevelContainer(data));
break;
}
//region.addLevel(new SingleLevelContainer(data));
} catch (Exception e)
catch (IOException ioEx)
{
ClientProxy.LOGGER.error("LOD file read error. Unable to read to [" + fileName + "] error [" + ioEx.getMessage() + "]: ");
ioEx.printStackTrace();
}
}// file datasize > 0
}
catch (Exception e)
{
// the buffered reader encountered a
// problem reading the file
ClientProxy.LOGGER.error("LOD file read error. Unable to read to [" + fileName + "] error [" + e.getMessage() + "]: ");
e.printStackTrace();
}
}
}// for each detail level
if (region.getMinDetailLevel() >= detailLevel)
region.expand(detailLevel);
return region;
}
@@ -220,18 +234,19 @@ public class LodDimensionFileHandler
{
try
{
for (int i = 0; i < loadedDimension.getWidth(); i++)
for (int i = 0; i < lodDimension.getWidth(); i++)
{
for (int j = 0; j < loadedDimension.getWidth(); j++)
for (int j = 0; j < lodDimension.getWidth(); j++)
{
if (loadedDimension.isRegionToRegen(i,j) && loadedDimension.getRegionByArrayIndex(i,j) != null)
if (lodDimension.isRegionToRegen(i,j) && lodDimension.getRegionByArrayIndex(i,j) != null)
{
saveRegionToFile(loadedDimension.getRegionByArrayIndex(i,j));
loadedDimension.setRegenByArrayIndex(i, j,false);
saveRegionToFile(lodDimension.getRegionByArrayIndex(i,j));
lodDimension.setRegenByArrayIndex(i, j,false);
}
}
}
} catch (Exception e)
}
catch (Exception e)
{
e.printStackTrace();
}
@@ -247,19 +262,16 @@ public class LodDimensionFileHandler
*/
private void saveRegionToFile(LodRegion region)
{
// convert to region coordinates
int x = region.regionPosX;
int z = region.regionPosZ;
for (byte detailLevel = region.getMinDetailLevel(); detailLevel <= LodUtil.REGION_DETAIL_LEVEL; detailLevel++)
{
String fileName = getFileNameAndPathForRegion(x, z, region.getGenerationMode(), detailLevel, region.getLodQualityMode());
String fileName = getFileNameAndPathForRegion(region.regionPosX, region.regionPosZ, region.getGenerationMode(), detailLevel, region.getLodQualityMode());
File oldFile = new File(fileName);
// if the fileName was null that means the folder is inaccessible
// for some reason
if (fileName == null)
{
ClientProxy.LOGGER.warn("Unable to save region [" + x + ", " + z + "] to file, no fileName.");
ClientProxy.LOGGER.warn("Unable to save region [" + region.regionPosX + ", " + region.regionPosZ + "] to file, file is inaccessible.");
return;
}
@@ -274,36 +286,41 @@ public class LodDimensionFileHandler
if (!oldFile.getParentFile().exists())
oldFile.getParentFile().mkdirs();
oldFile.createNewFile();
} else
}
else
{
// the file exists, make sure it
// is the correct version.
// (to make sure we don't overwrite a newer
// version file if it exists)
int fileVersion = LOD_SAVE_FILE_VERSION;
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(oldFile))) {
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(oldFile)))
{
fileVersion = inputStream.read();
inputStream.close();
} catch (IOException ex) {
}
catch (IOException ex)
{
ex.printStackTrace();
}
// check if this file can be written to by the file handler
if (fileVersion > LOD_SAVE_FILE_VERSION) {
if (fileVersion > LOD_SAVE_FILE_VERSION)
{
// the file we are reading is a newer version,
// don't write anything, we don't want to accidently
// delete anything the user may want.
return;
} // if(fileVersion > LOD_SAVE_FILE_VERSION)
//else {
// we are good to continue and overwrite the old file
//}
}
// if we got this far then we are good
// to overwrite the old file
}
// the old file is good, now create a new save file
// the old file is good, now create a new temporary save file
File newFile = new File(fileName + TMP_FILE_EXTENSION);
try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(newFile))) {
try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(newFile)))
{
// add the version of this file
outputStream.write(LOD_SAVE_FILE_VERSION);
@@ -313,10 +330,13 @@ public class LodDimensionFileHandler
// overwrite the old file with the new one
Files.move(newFile.toPath(), oldFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
}catch (IOException ex) {
}
catch (IOException ex)
{
ex.printStackTrace();
}
} catch (Exception e)
}
catch (Exception e)
{
ClientProxy.LOGGER.error("LOD file write error. Unable to write to [" + fileName + "] error [" + e.getMessage() + "]: ");
e.printStackTrace();
@@ -325,6 +345,9 @@ public class LodDimensionFileHandler
}
//================//
// helper methods //
//================//
@@ -333,26 +356,27 @@ public class LodDimensionFileHandler
/**
* Return the name of the file that should contain the
* region at the given x and z. <br>
* Returns null if this object isn't ready to read and write. <br><br>
* Returns null if this object isn't available to read and write. <br><br>
* <p>
* example: "lod.0.0.txt" <br><br>
* example: "lod.0.0.txt" <br>
* <p>
* Returns null if there is an IO Exception.
* Returns null if there is an IO or security Exception.
*/
private String getFileNameAndPathForRegion(int regionX, int regionZ, DistanceGenerationMode generationMode, byte detailLevel, VerticalQuality verticalQuality)
{
try
{
// saveFolder is something like
// ".\Super Flat\DIM-1\data"
// ".\Super Flat\DIM-1\data\"
// or
// ".\Super Flat\data"
// ".\Super Flat\data\"
return dimensionDataSaveFolder.getCanonicalPath() + File.separatorChar +
verticalQuality + File.separatorChar +
generationMode.toString() + File.separatorChar +
DETAIL_FOLDER_NAME_PREFIX + detailLevel + File.separatorChar +
FILE_NAME_PREFIX + "." + regionX + "." + regionZ + FILE_EXTENSION;
} catch (IOException | SecurityException e)
}
catch (IOException | SecurityException e)
{
ClientProxy.LOGGER.warn("Unable to get the filename for the region [" + regionX + ", " + regionZ + "], error: [" + e.getMessage() + "], stacktrace: ");
e.printStackTrace();
@@ -21,6 +21,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.seibel.lod.enums.FogQuality;
import com.seibel.lod.proxy.ClientProxy;
import com.seibel.lod.wrappers.MinecraftWrapper;
/**
@@ -29,7 +30,7 @@ import com.seibel.lod.wrappers.MinecraftWrapper;
* in Optifine.
*
* @author James Seibel
* @version 9-7-2021
* @version 9-25-2021
*/
public class ReflectionHandler
{
@@ -68,6 +69,7 @@ public class ReflectionHandler
// we didn't find the field,
// either optifine isn't installed, or
// optifine changed the name of the variable
ClientProxy.LOGGER.info(ReflectionHandler.class.getSimpleName() + ": unable to find the Optifine fog field. If Optifine isn't installed this can be ignored.");
}
@@ -91,18 +93,21 @@ public class ReflectionHandler
try
{
returnNum = (int) ofFogField.get(mc.getOptions());
} catch (IllegalArgumentException | IllegalAccessException e)
}
catch (IllegalArgumentException | IllegalAccessException e)
{
e.printStackTrace();
}
switch (returnNum)
{
// optifine's "default" option,
// it should never be called in this case
default:
case 0:
// optifine's "default" option,
// it should never be called in this case
return FogQuality.FAST;
// normal options
case 1:
return FogQuality.FAST;
@@ -110,9 +115,6 @@ public class ReflectionHandler
return FogQuality.FANCY;
case 3:
return FogQuality.OFF;
default:
return FogQuality.FAST;
}
}
}