Synchronise config reloading

In the future, config will be writable by both client & server
and so synchronisation should be happening.
This commit is contained in:
Spottedleaf
2024-07-08 05:54:22 -07:00
parent bd3f32c944
commit e73a2443c7
3 changed files with 27 additions and 18 deletions

View File

@@ -35,7 +35,7 @@ public final class YamlConfig<T> {
private final Class<? extends T> clazz;
public T config;
public volatile T config;
private final Yaml yaml;

View File

@@ -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=<file> - Override the config file location. Maybe useful for multiple game versions.
-DMoonrise.ConfigFile=<file> - Override the config file location. Might be useful for multiple game versions.
-DMoonrise.WorkerThreadCount=<number> - Override the auto configured worker thread counts (worker-threads).
""";
@@ -38,31 +38,39 @@ public final class MoonriseCommon {
reloadConfig();
}
public static YamlConfig<MoonriseConfig> 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;
}
}
}

View File

@@ -16,7 +16,8 @@
"breaks": {
"notenoughcrashes": "*",
"starlight": "*",
"lithium": "*"
"lithium": "*",
"c2me": "*"
},
"license": "GPL-3.0-only",
"icon": "assets/moonrise/icon.png",