Skip to content

Commit

Permalink
Allow loading configs from a specified override path and simplify log…
Browse files Browse the repository at this point in the history
…ic for removing the file watch
  • Loading branch information
pupnewfster committed Dec 11, 2023
1 parent 3447c36 commit 00ec249
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public Function<ModConfig, CommentedFileConfig> 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) {
Expand Down
32 changes: 25 additions & 7 deletions core/src/main/java/net/neoforged/fml/config/ConfigTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 00ec249

Please sign in to comment.