From 26301bfe1b259123fa993dd8596f2a19efe9d68b Mon Sep 17 00:00:00 2001 From: mcrcortex <{ID}+{username}@users.noreply.github.com> Date: Wed, 10 Apr 2024 07:52:17 +1000 Subject: [PATCH] Serialization changes --- src/main/java/me/cortex/voxy/client/Voxy.java | 3 +- .../cortex/voxy/client/config/VoxyConfig.java | 9 ++++- .../core/rendering/Gl46FarWorldRenderer.java | 1 + .../client/saver/ContextSelectionSystem.java | 40 ++++++++++++------- .../voxy/common/config/Serialization.java | 12 ++++++ 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/main/java/me/cortex/voxy/client/Voxy.java b/src/main/java/me/cortex/voxy/client/Voxy.java index 92dfe56d..7fb667ad 100644 --- a/src/main/java/me/cortex/voxy/client/Voxy.java +++ b/src/main/java/me/cortex/voxy/client/Voxy.java @@ -16,11 +16,12 @@ public class Voxy implements ClientModInitializer { static { ModContainer mod = (ModContainer) FabricLoader.getInstance().getModContainer("voxy").orElseThrow(NullPointerException::new); VERSION = mod.getMetadata().getVersion().getFriendlyString(); + + Serialization.init(); } @Override public void onInitializeClient() { - Serialization.init(); ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { dispatcher.register(WorldImportCommand.register()); diff --git a/src/main/java/me/cortex/voxy/client/config/VoxyConfig.java b/src/main/java/me/cortex/voxy/client/config/VoxyConfig.java index c8f1bb21..72066a1a 100644 --- a/src/main/java/me/cortex/voxy/client/config/VoxyConfig.java +++ b/src/main/java/me/cortex/voxy/client/config/VoxyConfig.java @@ -3,6 +3,7 @@ package me.cortex.voxy.client.config; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import me.cortex.voxy.client.saver.ContextSelectionSystem; import net.fabricmc.loader.api.FabricLoader; import org.lwjgl.opengl.GL; @@ -30,13 +31,19 @@ public class VoxyConfig { public int savingThreads = 4; public int renderThreads = 5; public boolean useMeshShaderIfPossible = true; + public String defaultSaveConfig; public static VoxyConfig loadOrCreate() { var path = getConfigPath(); if (Files.exists(path)) { try (FileReader reader = new FileReader(path.toFile())) { - return GSON.fromJson(reader, VoxyConfig.class); + var cfg = GSON.fromJson(reader, VoxyConfig.class); + if (cfg.defaultSaveConfig == null) { + //Shitty gson being a pain TODO: replace with a proper fix + cfg.defaultSaveConfig = ContextSelectionSystem.DEFAULT_STORAGE_CONFIG; + } + return cfg; } catch (IOException e) { System.err.println("Could not parse config"); e.printStackTrace(); diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java b/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java index afa491a2..36a9c3b7 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java @@ -145,6 +145,7 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer //glPointSize(10); //TODO: replace glMultiDrawElementsIndirectCountARB with glMultiDrawElementsIndirect on intel gpus, since it performs so much better //glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, drawCnt, 0); + //glLineWidth(2); glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, 0, (int) (this.geometry.getSectionCount()*4.4), 0); glEnable(GL_CULL_FACE); diff --git a/src/main/java/me/cortex/voxy/client/saver/ContextSelectionSystem.java b/src/main/java/me/cortex/voxy/client/saver/ContextSelectionSystem.java index 9d997bd7..e793a29e 100644 --- a/src/main/java/me/cortex/voxy/client/saver/ContextSelectionSystem.java +++ b/src/main/java/me/cortex/voxy/client/saver/ContextSelectionSystem.java @@ -27,6 +27,23 @@ public class ContextSelectionSystem { public int maxYOverride = Integer.MIN_VALUE; public StorageConfig storageConfig; } + public static final String DEFAULT_STORAGE_CONFIG; + static { + var config = new WorldConfig(); + + //Load the default config + var baseDB = new RocksDBStorageBackend.Config(); + + var compressor = new ZSTDCompressor.Config(); + compressor.compressionLevel = 7; + + var compression = new CompressionStorageAdaptor.Config(); + compression.delegate = baseDB; + compression.compressor = compressor; + + config.storageConfig = compression; + DEFAULT_STORAGE_CONFIG = Serialization.GSON.toJson(config); + } public static class Selection { private final Path selectionFolder; @@ -46,27 +63,22 @@ public class ContextSelectionSystem { if (Files.exists(json)) { try { this.config = Serialization.GSON.fromJson(Files.readString(json), WorldConfig.class); + if (this.config == null) { + throw new IllegalStateException("Config deserialization null, reverting to default"); + } return; } catch (Exception e) { System.err.println("Failed to load the storage configuration file, resetting it to default"); e.printStackTrace(); } } - this.config = new WorldConfig(); - //Load the default config - var baseDB = new RocksDBStorageBackend.Config(); - - var compressor = new ZSTDCompressor.Config(); - compressor.compressionLevel = 7; - - var compression = new CompressionStorageAdaptor.Config(); - compression.delegate = baseDB; - compression.compressor = compressor; - - this.config.storageConfig = compression; - - this.save(); + try { + this.config = Serialization.GSON.fromJson(VoxyConfig.CONFIG.defaultSaveConfig, WorldConfig.class); + this.save(); + } catch (Exception e) { + throw new RuntimeException("Failed to deserialize the default config, aborting!", e); + } } public StorageBackend createStorageBackend() { diff --git a/src/main/java/me/cortex/voxy/common/config/Serialization.java b/src/main/java/me/cortex/voxy/common/config/Serialization.java index 19fcc86a..49beb9ae 100644 --- a/src/main/java/me/cortex/voxy/common/config/Serialization.java +++ b/src/main/java/me/cortex/voxy/common/config/Serialization.java @@ -4,6 +4,8 @@ import com.google.gson.*; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; +import me.cortex.voxy.common.storage.config.CompressorConfig; +import me.cortex.voxy.common.storage.config.StorageConfig; import net.fabricmc.loader.api.FabricLoader; import java.io.BufferedReader; @@ -89,6 +91,13 @@ public class Serialization { } static { + try { + Class.forName(CompressorConfig.class.getName()); + Class.forName(StorageConfig.class.getName()); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + String BASE_SEARCH_PACKAGE = "me.cortex.voxy"; Map, GsonConfigSerialization> serializers = new HashMap<>(); @@ -109,6 +118,9 @@ public class Serialization { if (clzName.contains("VoxyConfigScreenFactory")) { continue;//Dont want to modmenu incase it doesnt exist } + if (clzName.endsWith("VoxyConfig")) { + continue;//Special case to prevent recursive loading pain + } if (clzName.equals(Serialization.class.getName())) { continue;//Dont want to load ourselves