Load default configs from the defaultconfig directory by default. (Can

be overridden in the fml.toml config file if desired).

Will load any type of config tracked file from there - client, common, server
or custom tracked file. This means you can ship defaults for your pack
in a defaultconfig directory. I am thinking about ways to force a specific
config override for an existing config.

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
This commit is contained in:
cpw 2019-07-28 13:42:49 -04:00
parent 283ece29e8
commit cd826f7bcf
No known key found for this signature in database
GPG key ID: 8EB3DF749553B1B7
3 changed files with 36 additions and 5 deletions

View file

@ -27,6 +27,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -41,6 +42,7 @@ public class FMLConfig
configSpec.define("splashscreen", Boolean.TRUE); configSpec.define("splashscreen", Boolean.TRUE);
configSpec.define("maxThreads", -1); configSpec.define("maxThreads", -1);
configSpec.define("versionCheck", Boolean.TRUE); configSpec.define("versionCheck", Boolean.TRUE);
configSpec.define("defaultConfigPath", "defaultconfigs");
} }
private CommentedFileConfig configData; private CommentedFileConfig configData;
@ -69,6 +71,8 @@ public class FMLConfig
LOGGER.trace(CORE, "Splash screen is {}", FMLConfig::splashScreenEnabled); LOGGER.trace(CORE, "Splash screen is {}", FMLConfig::splashScreenEnabled);
LOGGER.trace(CORE, "Max threads for mod loading computed at {}", FMLConfig::loadingThreadCount); LOGGER.trace(CORE, "Max threads for mod loading computed at {}", FMLConfig::loadingThreadCount);
LOGGER.trace(CORE, "Version check is {}", FMLConfig::runVersionCheck); LOGGER.trace(CORE, "Version check is {}", FMLConfig::runVersionCheck);
LOGGER.trace(CORE, "Default config paths at {}", FMLConfig::defaultConfigPath);
FMLPaths.getOrCreateGameRelativePath(Paths.get(FMLConfig.defaultConfigPath()), "default config directory");
} }
public static boolean splashScreenEnabled() { public static boolean splashScreenEnabled() {
@ -84,4 +88,8 @@ public class FMLConfig
public static boolean runVersionCheck() { public static boolean runVersionCheck() {
return INSTANCE.configData.<Boolean>getOptional("versionCheck").orElse(Boolean.TRUE); return INSTANCE.configData.<Boolean>getOptional("versionCheck").orElse(Boolean.TRUE);
} }
public static String defaultConfigPath() {
return INSTANCE.configData.<String>getOptional("defaultConfigPath").orElse("defaultconfigs");
}
} }

View file

@ -50,16 +50,16 @@ public enum FMLPaths
this.isDirectory = true; this.isDirectory = true;
} }
private Path computePath(String... path)
{
return Paths.get(path[0], Arrays.copyOfRange(path, 1, path.length));
}
FMLPaths(boolean isDir, FMLPaths parent, String... path) { FMLPaths(boolean isDir, FMLPaths parent, String... path) {
this.relativePath = parent.relativePath.resolve(computePath(path)); this.relativePath = parent.relativePath.resolve(computePath(path));
this.isDirectory = isDir; this.isDirectory = isDir;
} }
private Path computePath(String... path)
{
return Paths.get(path[0], Arrays.copyOfRange(path, 1, path.length));
}
public static void setup(IEnvironment env) { public static void setup(IEnvironment env) {
final Path rootPath = env.getProperty(IEnvironment.Keys.GAMEDIR.get()).orElseThrow(() -> new RuntimeException("No game path found")); final Path rootPath = env.getProperty(IEnvironment.Keys.GAMEDIR.get()).orElseThrow(() -> new RuntimeException("No game path found"));
@ -79,6 +79,10 @@ public enum FMLPaths
} }
} }
public static Path getOrCreateGameRelativePath(Path path, String name) {
return FileUtils.getOrCreateDirectory(FMLPaths.GAMEDIR.get().resolve(path), name);
}
public Path relative() { public Path relative() {
return relativePath; return relativePath;
} }

View file

@ -19,13 +19,18 @@
package net.minecraftforge.fml.config; package net.minecraftforge.fml.config;
import com.electronwill.nightconfig.core.ConfigFormat;
import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
import com.electronwill.nightconfig.core.file.FileWatcher; import com.electronwill.nightconfig.core.file.FileWatcher;
import com.electronwill.nightconfig.core.io.WritingMode; import com.electronwill.nightconfig.core.io.WritingMode;
import net.minecraftforge.fml.loading.FMLConfig;
import net.minecraftforge.fml.loading.FMLPaths;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.function.Function; import java.util.function.Function;
@ -34,6 +39,7 @@ import static net.minecraftforge.fml.config.ConfigTracker.CONFIG;
public class ConfigFileTypeHandler { public class ConfigFileTypeHandler {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
static ConfigFileTypeHandler TOML = new ConfigFileTypeHandler(); static ConfigFileTypeHandler TOML = new ConfigFileTypeHandler();
private static final Path defaultConfigPath = FMLPaths.GAMEDIR.get().resolve(FMLConfig.defaultConfigPath());
public Function<ModConfig, CommentedFileConfig> reader(Path configBasePath) { public Function<ModConfig, CommentedFileConfig> reader(Path configBasePath) {
return (c) -> { return (c) -> {
@ -41,6 +47,7 @@ public class ConfigFileTypeHandler {
final CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync(). final CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync().
preserveInsertionOrder(). preserveInsertionOrder().
autosave(). autosave().
onFileNotFound((newfile, configFormat)-> setupConfigFile(c, newfile, configFormat)).
writingMode(WritingMode.REPLACE). writingMode(WritingMode.REPLACE).
build(); build();
LOGGER.debug(CONFIG, "Built TOML config for {}", configPath.toString()); LOGGER.debug(CONFIG, "Built TOML config for {}", configPath.toString());
@ -56,6 +63,18 @@ public class ConfigFileTypeHandler {
}; };
} }
private boolean setupConfigFile(final ModConfig modConfig, final Path file, final ConfigFormat<?> conf) throws IOException {
Path p = defaultConfigPath.resolve(modConfig.getFileName());
if (Files.exists(p)) {
LOGGER.info(CONFIG, "Loading default config file from path {}", p);
Files.copy(p, file);
} else {
Files.createFile(file);
conf.initEmptyFile(file);
}
return true;
}
private static class ConfigWatcher implements Runnable { private static class ConfigWatcher implements Runnable {
private final ModConfig modConfig; private final ModConfig modConfig;
private final CommentedFileConfig commentedFileConfig; private final CommentedFileConfig commentedFileConfig;