From 00ec2493447102041c2e9894d6d9de28e019ab29 Mon Sep 17 00:00:00 2001 From: Sara Freimer Date: Mon, 11 Dec 2023 14:44:46 -0600 Subject: [PATCH] Allow loading configs from a specified override path and simplify logic for removing the file watch --- .../fml/config/ConfigFileTypeHandler.java | 4 +-- .../neoforged/fml/config/ConfigTracker.java | 32 +++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java b/core/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java index c110a5c3f..c188698af 100644 --- a/core/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java +++ b/core/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java @@ -73,10 +73,10 @@ public Function reader(Path configBasePath) { }; } - public void unload(Path configBasePath, ModConfig config) { + public void unload(ModConfig config) { if (FMLConfig.getBoolConfigValue(FMLConfig.ConfigValue.DISABLE_CONFIG_WATCHER)) return; - Path configPath = configBasePath.resolve(config.getFileName()); + Path configPath = config.getFullPath(); try { FileWatcher.defaultInstance().removeWatch(configPath); } catch (RuntimeException e) { diff --git a/core/src/main/java/net/neoforged/fml/config/ConfigTracker.java b/core/src/main/java/net/neoforged/fml/config/ConfigTracker.java index 345e585cb..88ae85256 100644 --- a/core/src/main/java/net/neoforged/fml/config/ConfigTracker.java +++ b/core/src/main/java/net/neoforged/fml/config/ConfigTracker.java @@ -8,6 +8,8 @@ import com.electronwill.nightconfig.core.CommentedConfig; import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.mojang.logging.LogUtils; +import java.nio.file.Files; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.Marker; import org.slf4j.MarkerFactory; @@ -46,28 +48,44 @@ void trackConfig(final ModConfig config) { } public void loadConfigs(ModConfig.Type type, Path configBasePath) { + loadConfigs(type, configBasePath, null); + } + + public void loadConfigs(ModConfig.Type type, Path configBasePath, @Nullable Path configOverrideBasePath) { LOGGER.debug(CONFIG, "Loading configs type {}", type); - this.configSets.get(type).forEach(config -> openConfig(config, configBasePath)); + this.configSets.get(type).forEach(config -> openConfig(config, configBasePath, configOverrideBasePath)); } - public void unloadConfigs(ModConfig.Type type, Path configBasePath) { + public void unloadConfigs(ModConfig.Type type) { LOGGER.debug(CONFIG, "Unloading configs type {}", type); - this.configSets.get(type).forEach(config -> closeConfig(config, configBasePath)); + this.configSets.get(type).forEach(this::closeConfig); + } + + private Path resolveBasePath(ModConfig config, Path configBasePath, @Nullable Path configOverrideBasePath) { + if (configOverrideBasePath != null) { + Path overrideFilePath = configOverrideBasePath.resolve(config.getFileName()); + if (Files.exists(overrideFilePath)) { + LOGGER.info(CONFIG, "Found config file override in path {}", overrideFilePath); + return configOverrideBasePath; + } + } + return configBasePath; } - private void openConfig(final ModConfig config, final Path configBasePath) { + private void openConfig(final ModConfig config, final Path configBasePath, @Nullable Path configOverrideBasePath) { LOGGER.trace(CONFIG, "Loading config file type {} at {} for {}", config.getType(), config.getFileName(), config.getModId()); - final CommentedFileConfig configData = ConfigFileTypeHandler.TOML.reader(configBasePath).apply(config); + final Path basePath = resolveBasePath(config, configBasePath, configOverrideBasePath); + final CommentedFileConfig configData = ConfigFileTypeHandler.TOML.reader(basePath).apply(config); config.setConfigData(configData); IConfigEvent.loading(config).post(); config.save(); } - private void closeConfig(final ModConfig config, final Path configBasePath) { + private void closeConfig(final ModConfig config) { if (config.getConfigData() != null) { LOGGER.trace(CONFIG, "Closing config file type {} at {} for {}", config.getType(), config.getFileName(), config.getModId()); // stop the filewatcher before we save the file and close it, so reload doesn't fire - ConfigFileTypeHandler.TOML.unload(configBasePath, config); + ConfigFileTypeHandler.TOML.unload(config); var unloading = IConfigEvent.unloading(config); if (unloading != null) unloading.post();