Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading configs from a specified override path #48

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading