Skip to content

Commit

Permalink
Merge branch 'main' into dep-overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Nov 25, 2024
2 parents 927c30d + be57775 commit 6e16b9b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
82 changes: 57 additions & 25 deletions loader/src/main/java/net/neoforged/fml/loading/FMLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.ConfigSpec;
import com.electronwill.nightconfig.core.InMemoryFormat;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
import com.electronwill.nightconfig.core.io.ParsingException;
import com.electronwill.nightconfig.core.io.ParsingMode;
import com.electronwill.nightconfig.core.io.WritingMode;
import com.electronwill.nightconfig.toml.TomlFormat;
import com.electronwill.nightconfig.toml.TomlParser;
import com.electronwill.nightconfig.toml.TomlWriter;
import com.mojang.logging.LogUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Unmodifiable;
Expand Down Expand Up @@ -72,11 +77,20 @@ void buildConfigEntry(ConfigSpec spec, CommentedConfig commentedConfig) {
}

@SuppressWarnings("unchecked")
private <T> T getConfigValue(CommentedFileConfig config) {
private <T> T getConfigValue(CommentedConfig config) {
return (T) this.entryFunction.apply(config != null ? config.get(this.entry) : this.defaultValue);
}

/**
* @deprecated Use {@link FMLConfig#updateConfig(ConfigValue, Object)} instead.
*/
@Deprecated(forRemoval = true)
public <T> void updateValue(final CommentedFileConfig configData, final T value) {
setConfigValue(configData, value);
FMLConfig.INSTANCE.saveConfig(FMLPaths.FMLCONFIG.get());
}

private <T> void setConfigValue(CommentedConfig configData, final T value) {
configData.set(this.entry, value);
}
}
Expand All @@ -90,7 +104,9 @@ private static Object maxThreads(final Object value) {
private static final Logger LOGGER = LogUtils.getLogger();
private static final FMLConfig INSTANCE = new FMLConfig();
private static Map<String, List<DependencyOverride>> dependencyOverrides = Map.of();
private static final ConfigSpec configSpec = new ConfigSpec();
private static final ConfigSpec configSpec = new ConfigSpec(
// Make sure the values are written in the same order as the enum.
InMemoryFormat.withUniversalSupport().createConfig(LinkedHashMap::new));
private static final CommentedConfig configComments = CommentedConfig.inMemory();
static {
for (ConfigValue cv : ConfigValue.values()) {
Expand All @@ -99,27 +115,43 @@ private static Object maxThreads(final Object value) {

// Make sure that we don't end up "correcting" the config and removing dependency overrides
// Since we're not writing them by default, the default value can be null and we accept any objects (parsing and validation is done when the config is loaded)
configSpec.define("dependencyOverrides", () -> null, object -> true);
configSpec.define("dependencyOverrides", Map::of, object -> true);
configComments.set("dependencyOverrides", configComments.createSubConfig());
configComments.setComment("dependencyOverrides", """
Define dependency overrides below
Dependency overrides can be used to forcibly remove a dependency constraint from a mod or to force a mod to load AFTER another mod
Using dependency overrides can cause issues. Use at your own risk.
Example dependency override for the mod with the id 'targetMod': dependency constraints (incompatibility clauses or restrictive version ranges) against mod 'dep1' are removed, and the mod will now load after the mod 'dep2'
dependencyOverrides.targetMod = ["-dep1", "+dep2"]""");
}

private CommentedFileConfig configData;

private void loadFrom(final Path configFile) {
configData = CommentedFileConfig.builder(configFile).sync()
.onFileNotFound(FileNotFoundAction.copyData(Objects.requireNonNull(getClass().getResourceAsStream("/META-INF/defaultfmlconfig.toml"))))
.writingMode(WritingMode.REPLACE)
.build();
try {
configData.load();
} catch (ParsingException e) {
throw new RuntimeException("Failed to load FML config from " + configFile, e);
}
if (!configSpec.isCorrect(configData)) {
LOGGER.warn(LogMarkers.CORE, "Configuration file {} is not correct. Correcting", configFile);
configSpec.correct(configData, (action, path, incorrectValue, correctedValue) -> LOGGER.info(LogMarkers.CORE, "Incorrect key {} was corrected from {} to {}", path, incorrectValue, correctedValue));
private CommentedConfig configData;

private void loadFrom(Path configFile) {
this.configData = CommentedConfig.of(LinkedHashMap::new, TomlFormat.instance());

if (Files.exists(configFile)) {
try (var configStream = Files.newInputStream(configFile)) {
new TomlParser().parse(configStream, configData, ParsingMode.REPLACE);
} catch (IOException e) {
throw new RuntimeException("Failed to read FML config", e);
}

if (!configSpec.isCorrect(this.configData)) {
LOGGER.warn(LogMarkers.CORE, "Configuration file {} is not correct. Correcting", configFile);
configSpec.correct(this.configData, (action, path, incorrectValue, correctedValue) -> LOGGER.warn(LogMarkers.CORE, "Incorrect key {} was corrected from {} to {}", path, incorrectValue, correctedValue));
}
} else {
// This populates the config with the default values.
configSpec.correct(this.configData);
}
configData.putAllComments(configComments);
configData.save();

this.configData.putAllComments(configComments);
saveConfig(configFile);
}

private void saveConfig(Path configFile) {
new TomlWriter().write(this.configData, configFile, WritingMode.REPLACE);
}

public static void load() {
Expand Down Expand Up @@ -189,8 +221,8 @@ public static <A> List<A> getListConfigValue(ConfigValue v) {

public static <T> void updateConfig(ConfigValue v, T value) {
if (INSTANCE.configData != null) {
v.updateValue(INSTANCE.configData, value);
INSTANCE.configData.save();
v.setConfigValue(INSTANCE.configData, value);
INSTANCE.saveConfig(FMLPaths.FMLCONFIG.get());
}
}

Expand Down
27 changes: 0 additions & 27 deletions loader/src/main/resources/META-INF/defaultfmlconfig.toml

This file was deleted.

0 comments on commit 6e16b9b

Please sign in to comment.