From e73a2443c750013c4734c07ec15e631f98ff55b7 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Mon, 8 Jul 2024 05:54:22 -0700 Subject: [PATCH] Synchronise config reloading In the future, config will be writable by both client & server and so synchronisation should be happening. --- .../common/config/config/YamlConfig.java | 2 +- .../moonrise/common/util/MoonriseCommon.java | 40 +++++++++++-------- src/main/resources/fabric.mod.json | 3 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/ca/spottedleaf/moonrise/common/config/config/YamlConfig.java b/src/main/java/ca/spottedleaf/moonrise/common/config/config/YamlConfig.java index 4d3f222..fbfd4c8 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/config/config/YamlConfig.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/config/config/YamlConfig.java @@ -35,7 +35,7 @@ public final class YamlConfig { private final Class clazz; - public T config; + public volatile T config; private final Yaml yaml; diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java b/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java index 7e03310..31b8fd5 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java @@ -30,7 +30,7 @@ public final class MoonriseCommon { Below are the Moonrise startup flags. Note that startup flags must be placed in the JVM arguments, not program arguments. - -DMoonrise.ConfigFile= - Override the config file location. Maybe useful for multiple game versions. + -DMoonrise.ConfigFile= - Override the config file location. Might be useful for multiple game versions. -DMoonrise.WorkerThreadCount= - Override the auto configured worker thread counts (worker-threads). """; @@ -38,31 +38,39 @@ public final class MoonriseCommon { reloadConfig(); } + public static YamlConfig getConfigRaw() { + return CONFIG; + } + public static MoonriseConfig getConfig() { return CONFIG.config; } public static boolean reloadConfig() { - if (CONFIG_FILE.exists()) { - try { - CONFIG.load(CONFIG_FILE); - } catch (final Exception ex) { - LOGGER.error("Failed to load configuration, using defaults", ex); - return false; + synchronized (CONFIG) { + if (CONFIG_FILE.exists()) { + try { + CONFIG.load(CONFIG_FILE); + } catch (final Exception ex) { + LOGGER.error("Failed to load configuration, using defaults", ex); + return false; + } } - } - // write back any changes, or create if needed - return saveConfig(); + // write back any changes, or create if needed + return saveConfig(); + } } public static boolean saveConfig() { - try { - CONFIG.save(CONFIG_FILE, CONFIG_HEADER); - return true; - } catch (final Exception ex) { - LOGGER.error("Failed to save configuration", ex); - return false; + synchronized (CONFIG) { + try { + CONFIG.save(CONFIG_FILE, CONFIG_HEADER); + return true; + } catch (final Exception ex) { + LOGGER.error("Failed to save configuration", ex); + return false; + } } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 72573df..518485e 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -16,7 +16,8 @@ "breaks": { "notenoughcrashes": "*", "starlight": "*", - "lithium": "*" + "lithium": "*", + "c2me": "*" }, "license": "GPL-3.0-only", "icon": "assets/moonrise/icon.png",