Also use config with lowres model compression

This commit is contained in:
Blue (Lukas Rieger) 2020-01-18 16:42:34 +01:00
parent 5ea1fbb4c5
commit aaaaf7e18a
4 changed files with 28 additions and 19 deletions

View File

@ -116,7 +116,8 @@ public void renderMaps() throws IOException {
LowresModelManager lowresModelManager = new LowresModelManager(
config.getWebDataPath().resolve(mapConfig.getId()).resolve("lowres"),
new Vector2i(mapConfig.getLowresPointsPerLowresTile(), mapConfig.getLowresPointsPerLowresTile()),
new Vector2i(mapConfig.getLowresPointsPerHiresTile(), mapConfig.getLowresPointsPerHiresTile())
new Vector2i(mapConfig.getLowresPointsPerHiresTile(), mapConfig.getLowresPointsPerHiresTile()),
mapConfig.useGzipCompression()
);
TileRenderer tileRenderer = new TileRenderer(hiresModelManager, lowresModelManager);

View File

@ -190,7 +190,8 @@ public synchronized void load() throws IOException, ParseResourceException {
LowresModelManager lowresModelManager = new LowresModelManager(
config.getWebDataPath().resolve(id).resolve("lowres"),
new Vector2i(mapConfig.getLowresPointsPerLowresTile(), mapConfig.getLowresPointsPerLowresTile()),
new Vector2i(mapConfig.getLowresPointsPerHiresTile(), mapConfig.getLowresPointsPerHiresTile())
new Vector2i(mapConfig.getLowresPointsPerHiresTile(), mapConfig.getLowresPointsPerHiresTile()),
mapConfig.useGzipCompression()
);
TileRenderer tileRenderer = new TileRenderer(hiresModelManager, lowresModelManager);

View File

@ -27,6 +27,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
@ -95,7 +96,7 @@ public void update(Vector2i point, float height, Vector3f color){
* Saves this model to its file
* @param force if this is false, the model is only saved if it has any changes
*/
public void save(File file, boolean force) throws IOException {
public void save(File file, boolean force, boolean useGzip) throws IOException {
if (!force && !hasUnsavedChanges) return;
this.hasUnsavedChanges = false;
@ -118,9 +119,9 @@ public void save(File file, boolean force) throws IOException {
throw new IOException("Failed to get write-access to file: " + file, e);
}
FileOutputStream fos = new FileOutputStream(file);
GZIPOutputStream zos = new GZIPOutputStream(fos);
OutputStreamWriter osw = new OutputStreamWriter(zos, StandardCharsets.UTF_8);
OutputStream os = new FileOutputStream(file);
if (useGzip) os = new GZIPOutputStream(os);
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
try (
PrintWriter pw = new PrintWriter(osw);
){

View File

@ -27,6 +27,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
@ -58,14 +59,18 @@ public class LowresModelManager {
private Vector2i pointsPerHiresTile;
private Map<File, CachedModel> models;
private boolean useGzip;
public LowresModelManager(Path fileRoot, Vector2i gridSize, Vector2i pointsPerHiresTile) {
public LowresModelManager(Path fileRoot, Vector2i gridSize, Vector2i pointsPerHiresTile, boolean useGzip) {
this.fileRoot = fileRoot;
this.gridSize = gridSize;
this.pointsPerHiresTile = pointsPerHiresTile;
models = new ConcurrentHashMap<>();
this.useGzip = useGzip;
}
/**
@ -166,13 +171,13 @@ public void update(UUID world, Vector2i point, float height, Vector3f color) thr
/**
* Returns the file for a tile
*/
public File getFile(Vector2i tile){
return FileUtils.coordsToFile(fileRoot, tile, "json.gz");
public File getFile(Vector2i tile, boolean useGzip){
return FileUtils.coordsToFile(fileRoot, tile, "json" + (useGzip ? ".gz" : ""));
}
private LowresModel getModel(UUID world, Vector2i tile) throws IOException {
File modelFile = getFile(tile);
File modelFile = getFile(tile, useGzip);
CachedModel model = models.get(modelFile);
if (model == null){
@ -181,11 +186,10 @@ private LowresModel getModel(UUID world, Vector2i tile) throws IOException {
if (model == null){
if (modelFile.exists()){
FileInputStream fis = new FileInputStream(modelFile);
try(
GZIPInputStream zis = new GZIPInputStream(fis);
){
String json = IOUtils.toString(zis, StandardCharsets.UTF_8);
InputStream is = new FileInputStream(modelFile);
if (useGzip) is = new GZIPInputStream(is);
try {
String json = IOUtils.toString(is, StandardCharsets.UTF_8);
try {
model = new CachedModel(world, tile, BufferGeometry.fromJson(json));
@ -194,6 +198,8 @@ private LowresModel getModel(UUID world, Vector2i tile) throws IOException {
//gridFile.renameTo(gridFile.toPath().getParent().resolve(gridFile.getName() + ".broken").toFile());
modelFile.delete();
}
} finally {
is.close();
}
}
@ -238,10 +244,10 @@ public synchronized void tidyUpModelCache() {
}
private synchronized void saveAndRemoveModel(CachedModel model) {
File modelFile = getFile(model.getTile());
File modelFile = getFile(model.getTile(), useGzip);
models.remove(modelFile);
try {
model.save(modelFile, false);
model.save(modelFile, false, useGzip);
//logger.logDebug("Saved and unloaded lowres tile: " + model.getTile());
} catch (IOException ex) {
Logger.global.logError("Failed to save and unload lowres-model: " + modelFile, ex);
@ -249,9 +255,9 @@ private synchronized void saveAndRemoveModel(CachedModel model) {
}
private void saveModel(CachedModel model) {
File modelFile = getFile(model.getTile());
File modelFile = getFile(model.getTile(), useGzip);
try {
model.save(modelFile, false);
model.save(modelFile, false, useGzip);
//logger.logDebug("Saved lowres tile: " + model.getTile());
} catch (IOException ex) {
Logger.global.logError("Failed to save lowres-model: " + modelFile, ex);