From 1e92902724e4f7e2000a01fe366f2ed8e88c6a7e Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Thu, 25 Dec 2025 12:14:26 +1000 Subject: [PATCH] move config stuff --- .../voxy/client/VoxyClientInstance.java | 57 +-------------- .../cortex/voxy/common/StorageConfigUtil.java | 71 +++++++++++++++++++ 2 files changed, 74 insertions(+), 54 deletions(-) create mode 100644 src/main/java/me/cortex/voxy/common/StorageConfigUtil.java diff --git a/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java b/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java index 4bddc125..d141e303 100644 --- a/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java +++ b/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java @@ -4,6 +4,7 @@ import me.cortex.voxy.client.compat.FlashbackCompat; import me.cortex.voxy.client.config.VoxyConfig; import me.cortex.voxy.client.mixin.sodium.AccessorSodiumWorldRenderer; import me.cortex.voxy.common.Logger; +import me.cortex.voxy.common.StorageConfigUtil; import me.cortex.voxy.common.config.ConfigBuildCtx; import me.cortex.voxy.common.config.Serialization; import me.cortex.voxy.common.config.compressors.ZSTDCompressor; @@ -35,7 +36,7 @@ public class VoxyClientInstance extends VoxyInstance { path = getBasePath(); } this.basePath = path; - this.storageConfig = getCreateStorageConfig(path); + this.storageConfig = StorageConfigUtil.getCreateStorageConfig(Config.class, c->c.version==1&&c.sectionStorageConfig!=null, ()->DEFAULT_STORAGE_CONFIG, path).sectionStorageConfig; this.updateDedicatedThreads(); } @@ -69,44 +70,6 @@ public class VoxyClientInstance extends VoxyInstance { return this.storageConfig.build(ctx); } - public static SectionStorageConfig getCreateStorageConfig(Path path) { - try { - Files.createDirectories(path); - } catch (Exception e) { - throw new RuntimeException(e); - } - var json = path.resolve("config.json"); - Config config = null; - if (Files.exists(json)) { - try { - config = Serialization.GSON.fromJson(Files.readString(json), Config.class); - if (config == null) { - Logger.error("Config deserialization null, reverting to default"); - } else { - if (config.sectionStorageConfig == null) { - Logger.error("Config section storage null, reverting to default"); - config = null; - } - } - } catch (Exception e) { - Logger.error("Failed to load the storage configuration file, resetting it to default, this will probably break your save if you used a custom storage config", e); - } - } - - if (config == null) { - config = DEFAULT_STORAGE_CONFIG; - } - try { - Files.writeString(json, Serialization.GSON.toJson(config)); - } catch (Exception e) { - throw new RuntimeException("Failed write the config, aborting!", e); - } - if (config == null) { - throw new IllegalStateException("Config is still null\n"); - } - return config.sectionStorageConfig; - } - public Path getStorageBasePath() { return this.basePath; } @@ -124,21 +87,7 @@ public class VoxyClientInstance extends VoxyInstance { private static final Config DEFAULT_STORAGE_CONFIG; static { var config = new Config(); - - //Load the default config - var baseDB = new RocksDBStorageBackend.Config(); - - var compressor = new ZSTDCompressor.Config(); - compressor.compressionLevel = 1; - - var compression = new CompressionStorageAdaptor.Config(); - compression.delegate = baseDB; - compression.compressor = compressor; - - var serializer = new SectionSerializationStorage.Config(); - serializer.storage = compression; - config.sectionStorageConfig = serializer; - + config.sectionStorageConfig = StorageConfigUtil.createDefaultSerializer(); DEFAULT_STORAGE_CONFIG = config; } diff --git a/src/main/java/me/cortex/voxy/common/StorageConfigUtil.java b/src/main/java/me/cortex/voxy/common/StorageConfigUtil.java new file mode 100644 index 00000000..78054ed7 --- /dev/null +++ b/src/main/java/me/cortex/voxy/common/StorageConfigUtil.java @@ -0,0 +1,71 @@ +package me.cortex.voxy.common; + +import me.cortex.voxy.common.config.Serialization; +import me.cortex.voxy.common.config.compressors.ZSTDCompressor; +import me.cortex.voxy.common.config.section.SectionSerializationStorage; +import me.cortex.voxy.common.config.section.SectionStorageConfig; +import me.cortex.voxy.common.config.storage.other.CompressionStorageAdaptor; +import me.cortex.voxy.common.config.storage.rocksdb.RocksDBStorageBackend; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Predicate; +import java.util.function.Supplier; + +public class StorageConfigUtil { + + public static T getCreateStorageConfig(Class clz, Predicate verifier, Supplier defaultConfig, Path path) { + try { + Files.createDirectories(path); + } catch (Exception e) { + throw new RuntimeException(e); + } + var json = path.resolve("config.json"); + T config = null; + if (Files.exists(json)) { + try { + config = Serialization.GSON.fromJson(Files.readString(json), clz); + if (config == null) { + Logger.error("Config deserialization null, reverting to default"); + } else { + if (!verifier.test(config)) { + Logger.error("Config section storage null, reverting to default"); + config = null; + } + } + } catch (Exception e) { + Logger.error("Failed to load the storage configuration file, resetting it to default, this will probably break your save if you used a custom storage config", e); + } + } + + if (config == null) { + config = defaultConfig.get(); + } + try { + Files.writeString(json, Serialization.GSON.toJson(config)); + } catch (Exception e) { + throw new RuntimeException("Failed write the config, aborting!", e); + } + if (config == null) { + throw new IllegalStateException("Config is still null\n"); + } + return config; + } + + public static SectionSerializationStorage.Config createDefaultSerializer() { + //Create the default config + var baseDB = new RocksDBStorageBackend.Config(); + + var compressor = new ZSTDCompressor.Config(); + compressor.compressionLevel = 1; + + var compression = new CompressionStorageAdaptor.Config(); + compression.delegate = baseDB; + compression.compressor = compressor; + + var serializer = new SectionSerializationStorage.Config(); + serializer.storage = compression; + + return serializer; + } +}