Skip to content

Commit

Permalink
Undo unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed Jul 3, 2024
1 parent c80d87a commit f9f1e22
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
4 changes: 2 additions & 2 deletions loader/src/main/java/net/neoforged/fml/ModContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void addConfig(final ModConfig modConfig) {
* @param configSpec A config spec
*/
public void registerConfig(ModConfig.Type type, IConfigSpec configSpec) {
if (configSpec.getDefaultConfig().isEmpty()) {
if (configSpec.isEmpty()) {
// This handles the case where a mod tries to register a config, without any options configured inside it.
LOGGER.debug("Attempted to register an empty config for type {} on mod {}", type, modId);
return;
Expand All @@ -128,7 +128,7 @@ public void registerConfig(ModConfig.Type type, IConfigSpec configSpec) {
* @param configSpec A config spec
*/
public void registerConfig(ModConfig.Type type, IConfigSpec configSpec, String fileName) {
if (configSpec.getDefaultConfig().isEmpty()) {
if (configSpec.isEmpty()) {
// This handles the case where a mod tries to register a config, without any options configured inside it.
LOGGER.debug("Attempted to register an empty config for type {} on mod {} using file name {}", type, modId, fileName);
return;
Expand Down
16 changes: 11 additions & 5 deletions loader/src/main/java/net/neoforged/fml/config/ConfigTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static void loadConfig(ModConfig modConfig, CommentedFileConfig config) {
throw new RuntimeException("Failed to recreate config file " + modConfig.getFileName() + " of type " + modConfig.getType() + " for modid " + modConfig.getModId(), ex);
}
}
modConfig.getSpec().load(config);
modConfig.getSpec().acceptConfig(config);
}

public void acceptSyncedConfig(ModConfig modConfig, byte[] bytes) {
Expand All @@ -168,7 +168,7 @@ public void acceptSyncedConfig(ModConfig modConfig, byte[] bytes) {
}
modConfig.config = TomlFormat.instance().createParser().parse(new ByteArrayInputStream(bytes));
// TODO: do we want to do any validation? (what do we do if it fails?)
modConfig.getSpec().load(modConfig.config);
modConfig.getSpec().acceptConfig(modConfig.config);
modConfig.postConfigEvent(ModConfigEvent.Reloading::new); // TODO: should maybe be Loading on the first load?
}

Expand All @@ -177,12 +177,18 @@ public void loadDefaultServerConfigs() {
if (modConfig.config != null) {
LOGGER.warn("Overwriting non-null config {} at path {} with default server config", modConfig.config, modConfig.getFileName());
}
modConfig.config = CommentedConfig.copy(modConfig.getSpec().getDefaultConfig());
modConfig.getSpec().load(modConfig.config);
modConfig.config = createDefaultConfig(modConfig.getSpec());
modConfig.getSpec().acceptConfig(modConfig.config);
modConfig.postConfigEvent(ModConfigEvent.Loading::new);
});
}

private static CommentedConfig createDefaultConfig(IConfigSpec spec) {
var commentedConfig = CommentedConfig.inMemory();
spec.correct(commentedConfig);
return commentedConfig;
}

private void closeConfig(final ModConfig config) {
if (config.config != null) {
if (config.config instanceof CommentedFileConfig) {
Expand Down Expand Up @@ -214,7 +220,7 @@ private static boolean setupConfigFile(final ModConfig modConfig, final Path fil
LOGGER.info(CONFIG, "Loading default config file from path {}", p);
Files.copy(p, file);
} else {
conf.createWriter().write(modConfig.getSpec().getDefaultConfig(), file, WritingMode.REPLACE_ATOMIC);
conf.createWriter().write(createDefaultConfig(modConfig.getSpec()), file, WritingMode.REPLACE_ATOMIC);
}
return true;
}
Expand Down
17 changes: 13 additions & 4 deletions loader/src/main/java/net/neoforged/fml/config/IConfigSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
package net.neoforged.fml.config;

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.UnmodifiableCommentedConfig;

/**
* A config spec is responsible for interpreting (loading, correcting) raw {@link CommentedConfig}s from NightConfig.
*
* <p>NeoForge provides {@code ModConfigSpec} for the most common cases.
*
* <h3>Thread-safety</h3>
* <p>The {@link Config} objects themselves are thread-safe, but mod config code should not be assumed to be thread-safe in general.
* FML will guard event dispatches behind a lock when necessary.
* A spec can safely mutate {@code Config} objects, but should let FML fire events,
* for example using {@link ModConfig#onConfigChanged()}.
*/
public interface IConfigSpec {
/**
* Returns the default content of the config.
* Returns {@code true} if this spec is empty.
*/
UnmodifiableCommentedConfig getDefaultConfig();
boolean isEmpty();

/**
* Checks that a config is correct.
Expand All @@ -32,7 +39,9 @@ public interface IConfigSpec {
* <p>This can be used to fix broken entries, add back missing entries or comments, etc...
* The returned config will be saved to disk.
*
* <p>The config should not be loaded into the spec yet. A call to {@link #load} will be made for that.
* <p>This function is also used to construct the default instance of a config, by passing in an empty config.
*
* <p>The config should not be loaded into the spec yet. A call to {@link #acceptConfig} will be made for that.
*
* <p>The config should not be saved yet. FML will take care of that after this method.
*/
Expand All @@ -43,5 +52,5 @@ public interface IConfigSpec {
* This is called on loading and on reloading.
* The config is guaranteed to be valid according to {@link #isCorrect}.
*/
void load(CommentedConfig config);
void acceptConfig(CommentedConfig config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Path getFullPath() {

/**
* To be called when the config changed in code, for example via {@code ModConfigSpec.ConfigValue#set}.
* This function will update the config on disk, and fire the reloading event.
* This function will update the config on disk, and fire the reloading event, taking the appropriate lock first.
*/
public void onConfigChanged() {
if (!(this.config instanceof FileConfig fileConfig)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,11 @@ private void waitUntil(Runnable assertion) throws InterruptedException {
}

private static class SimpleConfigSpec implements IConfigSpec {
private final UnmodifiableCommentedConfig defaultConfig;
private int loadedValue = 4;

public SimpleConfigSpec() {
var defaultConfig = CommentedConfig.inMemory();
defaultConfig.set("configEntry", 4);
defaultConfig.setComment("configEntry", "Test comment:");
this.defaultConfig = defaultConfig.unmodifiable();
}

@Override
public UnmodifiableCommentedConfig getDefaultConfig() {
return defaultConfig;
public boolean isEmpty() {
return false;
}

@Override
Expand Down Expand Up @@ -227,7 +219,7 @@ private boolean correct(UnmodifiableCommentedConfig config, boolean dryRun) {
}

@Override
public void load(CommentedConfig config) {
public void acceptConfig(CommentedConfig config) {
loadedValue = config.getInt("configEntry");
}
}
Expand Down

0 comments on commit f9f1e22

Please sign in to comment.