Files
GTNHLib/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java
T

630 lines
27 KiB
Java

package com.gtnewhorizon.gtnhlib.config;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import net.minecraft.launchwrapper.Launch;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import cpw.mods.fml.client.config.IConfigElement;
import cpw.mods.fml.common.FMLCommonHandler;
/**
* Class for controlling the loading of configuration files.
*/
public class ConfigurationManager {
static final Logger LOGGER = LogManager.getLogger("GTNHLibConfig");
private static final Map<String, Configuration> configs = new HashMap<>();
private static final Map<Configuration, Map<String, Set<Class<?>>>> configToCategoryClassMap = new HashMap<>();
private static final Map<String, Set<Class<?>>> modIdToConfigClasses = new HashMap<>();
private static final String[] langKeyPlaceholders = new String[] { "%mod", "%file", "%cat", "%field" };
private static final boolean PRINT_KEYS = Boolean.getBoolean("gtnhlib.printkeys");
private static final boolean DUMP_KEYS = Boolean.getBoolean("gtnhlib.dumpkeys");
private static final Map<String, List<String>> generatedLangKeys = new HashMap<>();
private static final Map<Configuration, Set<String>> observedCategories = new HashMap<>();
private static final Map<ConfigCategory, Class<?>> configEntries = new HashMap<>();
private static final Map<Class<?>, ConfigNode> configNode = new HashMap<>();
private final static Path configDir;
static {
configDir = minecraftHome().toPath().resolve("config");
}
private ConfigurationManager() {}
/**
* Registers a configuration class to be loaded. This should be done before or during preInit.
*
* @param configClass The class to register.
*/
public static void registerConfig(Class<?> configClass) throws ConfigException {
final var cfg = Optional.ofNullable(configClass.getAnnotation(Config.class)).orElseThrow(
() -> new ConfigException("Class " + configClass.getName() + " does not have a @Config annotation!"));
final var category = cfg.category().trim().toLowerCase();
final var modid = cfg.modid();
final var filename = Optional.of(cfg.filename().trim()).filter(s -> !s.isEmpty()).orElse(modid);
if (!configClass.isAnnotationPresent(Config.ExcludeFromAutoGui.class)) {
modIdToConfigClasses.computeIfAbsent(modid, (ignored) -> new HashSet<>()).add(configClass);
}
Configuration rawConfig = configs.computeIfAbsent(getConfigKey(cfg), (ignored) -> {
Path newConfigDir = configDir;
if (!cfg.configSubDirectory().trim().isEmpty()) {
newConfigDir = newConfigDir.resolve(cfg.configSubDirectory().trim());
}
final var configFile = newConfigDir.resolve(filename + ".cfg").toFile();
final var config = new Configuration(configFile);
config.load();
return config;
});
configToCategoryClassMap.computeIfAbsent(rawConfig, (ignored) -> new HashMap<>())
.computeIfAbsent(category, (ignored) -> new HashSet<>()).add(configClass);
configNode.put(configClass, new ConfigNode(configClass));
try {
processConfigInternal(configClass, category, rawConfig, null);
rawConfig.save();
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException e) {
throw new ConfigException(e);
}
}
public static void save(Class<?>... configClasses) {
if (configClasses.length == 0) return;
Set<Configuration> savedConfigs = new HashSet<>();
for (final var clazz : configClasses) {
try {
final var cfg = Optional.ofNullable(clazz.getAnnotation(Config.class)).orElseThrow(
() -> new ConfigException("Class " + clazz.getName() + " does not have a @Config annotation!"));
final var category = cfg.category().trim().toLowerCase();
Configuration rawConfig = configs.get(getConfigKey(cfg));
save(clazz, null, rawConfig, category);
savedConfigs.add(rawConfig);
} catch (IllegalAccessException | ConfigException e) {
LOGGER.error("Failed to save config for class {}", clazz.getName(), e);
}
}
savedConfigs.forEach(Configuration::save);
}
private static void save(Class<?> configClass, Object instance, Configuration rawConfig, String category)
throws IllegalAccessException, ConfigException {
for (final var field : configClass.getDeclaredFields()) {
if (shouldSkipField(field)) {
continue;
}
if (ConfigSyncHandler.hasSyncedValues()) {
SyncedConfigElement element = ConfigSyncHandler.syncedElements.get(field.toString());
if (element != null && element.isSynced()) continue;
}
field.setAccessible(true);
if (!ConfigFieldParser.canParse(field)) {
if (isFieldSubCategory(field)) {
final var cat = (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER)
+ ConfigFieldParser.getFieldName(field).toLowerCase();
Object subInstance = field.get(instance);
save(field.getType(), subInstance, rawConfig, cat);
}
continue;
}
if (category.isEmpty()) continue;
ConfigFieldParser.saveField(instance, field, rawConfig, category);
}
}
private static void processSubCategory(Object instance, Configuration config, Field subCategoryField,
String category) throws ConfigException {
final var name = ConfigFieldParser.getFieldName(subCategoryField);
final var cat = (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + name.toLowerCase();
ConfigCategory subCat = config.getCategory(cat);
Optional.ofNullable(subCategoryField.getAnnotation(Config.Comment.class)).map(Config.Comment::value)
.map((lines) -> String.join("\n", lines)).ifPresent(subCat::setComment);
Optional.ofNullable(subCategoryField.getAnnotation(Config.LangKey.class)).map(Config.LangKey::value)
.ifPresent(subCat::setLanguageKey);
if (subCategoryField.isAnnotationPresent(Config.RequiresMcRestart.class)) {
subCat.setRequiresMcRestart(true);
}
if (subCategoryField.isAnnotationPresent(Config.RequiresWorldRestart.class)) {
subCat.setRequiresWorldRestart(true);
}
try {
Object subInstance = subCategoryField.get(instance);
processConfigInternal(subCategoryField.getType(), cat, config, subInstance);
config.save();
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException e) {
throw new ConfigException(e);
}
}
private static void processConfigInternal(Class<?> configClass, String category, Configuration rawConfig,
@Nullable Object instance) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException,
NoSuchFieldException, ConfigException {
boolean foundCategory = !category.isEmpty();
ConfigCategory cat = foundCategory ? rawConfig.getCategory(category) : null;
Config.Sync sync = getClassOrBaseAnnotation(configClass, Config.Sync.class);
boolean syncCategory = sync != null && sync.value();
boolean requiresMcRestart = getClassOrBaseAnnotation(configClass, Config.RequiresMcRestart.class) != null
|| foundCategory && cat.requiresMcRestart();
boolean requiresWorldRestart = syncCategory
|| getClassOrBaseAnnotation(configClass, Config.RequiresWorldRestart.class) != null
|| foundCategory && cat.requiresWorldRestart();
List<String> observedValues = new ArrayList<>();
for (final var field : configClass.getDeclaredFields()) {
if (shouldSkipField(field)) {
continue;
}
if (instance != null && Modifier.isStatic(field.getModifiers())) {
throw new ConfigException(
"Illegal config field: " + field.getName()
+ " in "
+ configClass.getName()
+ ": Static field in instance context! Did you forget an @Config.Ignore annotation?");
}
field.setAccessible(true);
if (!ConfigFieldParser.canParse(field)) {
if (isFieldSubCategory(field)) {
processSubCategory(instance, rawConfig, field, category);
foundCategory = true;
continue;
}
throw new ConfigException(
"Illegal config field: " + field.getName()
+ " in "
+ configClass.getName()
+ ": Unsupported type "
+ field.getType().getName()
+ "! Did you forget an @Ignore annotation?");
}
if (category.isEmpty()) continue;
final var fieldName = ConfigFieldParser.getFieldName(field);
final var langKey = getLangKey(configClass, field.getAnnotation(Config.LangKey.class), fieldName, cat);
ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey);
observedValues.add(fieldName);
Config.Sync fieldSync = field.getAnnotation(Config.Sync.class);
if (fieldSync != null && fieldSync.value() || syncCategory && fieldSync == null) {
SyncedConfigElement element = new SyncedConfigElement(instance, field, () -> {
try {
ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey);
} catch (ConfigException e) {
LOGGER.error(
"Failed to restore synced field {} in class {}",
fieldName,
field.getDeclaringClass(),
e);
}
});
ConfigSyncHandler.syncedElements.put(field.toString(), element);
if (!requiresWorldRestart) {
cat.get(fieldName).setRequiresWorldRestart(true);
}
}
if (!requiresMcRestart) {
requiresMcRestart = field.isAnnotationPresent(Config.RequiresMcRestart.class);
}
if (!requiresWorldRestart) {
requiresWorldRestart = field.isAnnotationPresent(Config.RequiresWorldRestart.class);
}
}
if (!foundCategory) {
throw new ConfigException("No category found for config class " + configClass.getName() + "!");
}
if (category.isEmpty()) return;
if (cat.size() != observedValues.size()) {
final var properties = new ArrayList<>(cat.keySet());
properties.removeIf(observedValues::contains);
properties.forEach(cat::remove);
}
if (cat.getLanguagekey().equals(category)) {
final var langKey = getLangKey(configClass, configClass.getAnnotation(Config.LangKey.class), null, cat);
cat.setLanguageKey(langKey);
}
cat.setRequiresMcRestart(requiresMcRestart);
cat.setRequiresWorldRestart(requiresWorldRestart);
observedCategories.computeIfAbsent(rawConfig, k -> new HashSet<>()).add(cat.getQualifiedName());
Optional.ofNullable(configClass.getAnnotation(Config.Comment.class)).map(Config.Comment::value)
.map((lines) -> String.join("\n", lines)).ifPresent(cat::setComment);
configEntries.put(cat, configClass);
}
/**
* Process the configuration into a list of config elements usable in config GUI code.
*
* @param configClass The class to process.
*
* @return The configuration elements.
*/
@SuppressWarnings("rawtypes")
public static List<IConfigElement> getConfigElements(Class<?> configClass) throws ConfigException {
return getConfigElements(configClass, false);
}
/**
* Process the configuration into a list of config elements usable in config GUI code.
*
* @param configClass The class to process.
* @param categorized Whether to return the elements split by category.
* @return The configuration elements.
*/
@SuppressWarnings("rawtypes")
public static List<IConfigElement> getConfigElements(Class<?> configClass, boolean categorized)
throws ConfigException {
final var cfg = Optional.ofNullable(configClass.getAnnotation(Config.class)).orElseThrow(
() -> new ConfigException("Class " + configClass.getName() + " does not have a @Config annotation!"));
final var rawConfig = Optional.ofNullable(configs.get(getConfigKey(cfg))).map(
(conf) -> Optional.ofNullable(configToCategoryClassMap.get(conf))
.map((map) -> map.get(cfg.category().trim().toLowerCase()).contains(configClass)).orElse(false)
? conf
: null)
.orElseThrow(
() -> new ConfigException("Tried to get config elements for non-registered config class!"));
final var category = cfg.category().toLowerCase();
List<IConfigElement> result = new ArrayList<>();
if (categorized) {
if (category.isEmpty()) return getSubcategoryElements(configClass, category, rawConfig, true);
if (category.indexOf('.') != -1) return Collections.emptyList();
result.add(
getProxyElement(
new ConfigElement<>(rawConfig.getCategory(category)),
configClass,
rawConfig,
category));
} else {
final var elements = category.isEmpty() ? getSubcategoryElements(configClass, category, rawConfig, false)
: new ConfigElement<>(rawConfig.getCategory(category)).getChildElements();
elements.stream().map((element) -> getProxyElement(element, configClass, rawConfig, category))
.forEach(result::add);
}
return result;
}
@SuppressWarnings({ "rawtypes" })
public static List<IConfigElement> getConfigElementsMulti(Class<?>... configClasses) throws ConfigException {
return getConfigElementsMulti(false, configClasses);
}
@SuppressWarnings({ "rawtypes" })
public static List<IConfigElement> getConfigElementsMulti(boolean categorized, Class<?>... configClasses)
throws ConfigException {
switch (configClasses.length) {
case 0:
return Collections.emptyList();
case 1:
return getConfigElements(configClasses[0], categorized);
default:
final var result = new ArrayList<IConfigElement>();
for (final var configClass : configClasses) {
List<IConfigElement> elements = getConfigElements(configClass, categorized);
result.addAll(elements);
}
return result;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static List<IConfigElement> getSubcategoryElements(Class<?> configClass, String category,
Configuration rawConfig, boolean categorized) {
List<IConfigElement> elements = new ArrayList<>();
for (final var field : configClass.getDeclaredFields()) {
if (shouldSkipField(field)) {
continue;
}
if (isFieldSubCategory(field)) {
final var name = ConfigFieldParser.getFieldName(field).toLowerCase();
elements.add(
getProxyElement(
new ConfigElement(rawConfig.getCategory(name)),
configClass,
rawConfig,
category));
}
}
if (categorized) return elements;
return (List<IConfigElement>) elements.stream().flatMap(element -> element.getChildElements().stream())
.collect(Collectors.toList());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static IConfigElementProxy<?> getProxyElement(IConfigElement<?> element, Class<?> configClass,
Configuration rawConfig, String category) {
if (element instanceof IConfigElementProxy<?>proxy) {
return proxy;
}
ConfigNode rootNode = configNode.get(configClass);
ConfigNode node = rootNode.children.getOrDefault(element.getName().toLowerCase(), rootNode);
Config.Order orderAnn = configClass.getAnnotation(Config.Order.class);
int order = orderAnn != null ? orderAnn.value() : Integer.MAX_VALUE;
return new IConfigElementProxy(element, () -> {
try {
processConfigInternal(configClass, category, rawConfig, null);
rawConfig.save();
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException
| ConfigException e) {
e.printStackTrace();
}
}, node, order);
}
private static String getLangKey(Class<?> configClass, @Nullable Config.LangKey langKey, @Nullable String fieldName,
@NotNull ConfigCategory category) throws ConfigException {
if (langKey != null) return langKey.value();
Config.LangKeyPattern pattern = getClassOrBaseAnnotation(configClass, Config.LangKeyPattern.class);
String name = Optional.ofNullable(fieldName).orElse(category.getQualifiedName());
if (pattern == null) return name;
String patternStr = pattern.pattern();
if (!patternStr.contains("%field") || !patternStr.contains(".")) {
throw new ConfigException("Invalid pattern for class " + configClass.getName() + ": " + patternStr);
}
Config cfg = getClassOrBaseAnnotation(configClass, Config.class);
// Config annotation can't be null at this point
assert cfg != null;
String categoryName = pattern.fullyQualified() ? category.getQualifiedName() : category.getName();
if (fieldName == null) name = categoryName;
String key = buildKeyFromPattern(patternStr, cfg.modid(), cfg.filename(), categoryName, name);
if (PRINT_KEYS) {
if (fieldName != null) {
LOGGER.info("Lang key for field {} in category {}: {}", fieldName, category.getName(), key);
} else {
LOGGER.info("Lang key for category {}: {}", category.getName(), key);
}
}
if (DUMP_KEYS) {
generatedLangKeys.computeIfAbsent(cfg.modid(), k -> new ArrayList<>()).add(key + "=");
}
return key;
}
private static String buildKeyFromPattern(String pattern, String modId, String fileName, String categoryName,
String fieldName) {
StringBuilder s = new StringBuilder(pattern);
String[] replacements = new String[] { modId, fileName, categoryName, fieldName };
boolean isCategory = categoryName.equals(fieldName);
for (int i = 0; i < langKeyPlaceholders.length; i++) {
String placeholder = langKeyPlaceholders[i];
int index = s.indexOf(placeholder);
if (index == -1) continue;
int nextIndex = index + placeholder.length();
if (isCategory && "%field".equals(placeholder)) {
if (nextIndex + 1 <= s.length() && s.charAt(nextIndex + 1) == '.') {
s.delete(index, nextIndex + 1);
} else {
s.delete(index - 1, nextIndex);
}
continue;
}
s.replace(index, nextIndex, replacements[i].toLowerCase());
}
return s.toString();
}
private static @Nullable <A extends Annotation> A getClassOrBaseAnnotation(Class<?> clazz,
Class<A> annotationClass) {
A annotation = clazz.getAnnotation(annotationClass);
while (annotation == null && clazz.isMemberClass()) {
clazz = clazz.getDeclaringClass();
annotation = clazz.getAnnotation(annotationClass);
}
return annotation;
}
private static boolean isFieldSubCategory(@Nullable Field field) {
if (shouldSkipField(field)) return false;
Class<?> fieldClass = field.getType();
return !ConfigFieldParser.canParse(field) && fieldClass.getSuperclass() != null
&& fieldClass.getSuperclass().equals(Object.class);
}
private static boolean shouldSkipField(@Nullable Field field) {
return field == null || field.isSynthetic() || field.isAnnotationPresent(Config.Ignore.class);
}
public static Configuration getConfig(Class<?> configClass) {
final var cfg = configClass.getAnnotation(Config.class);
return configs.get(getConfigKey(cfg));
}
private static String getConfigKey(Config cfg) {
return cfg.modid() + "|" + cfg.configSubDirectory() + "|" + cfg.filename();
}
private static File minecraftHome() {
return Launch.minecraftHome != null ? Launch.minecraftHome : new File(".");
}
public static boolean isModRegistered(String modid) {
return modIdToConfigClasses.containsKey(modid);
}
static Class<?>[] getConfigClasses(String modid) {
return modIdToConfigClasses.getOrDefault(modid, Collections.emptySet()).toArray(new Class<?>[0]);
}
public static void onInit() {
FMLCommonHandler.instance().bus().register(new ConfigSyncHandler());
cullDeadCategories();
if (DUMP_KEYS) {
writeLangKeysToFile();
}
}
public static void applyConfigEntries() {
for (Map.Entry<ConfigCategory, Class<?>> entry : configEntries.entrySet()) {
ConfigCategory cat = entry.getKey();
Class<?> configClass = entry.getValue();
for (final var field : configClass.getDeclaredFields()) {
if (shouldSkipField(field)) {
continue;
}
final var fieldName = ConfigFieldParser.getFieldName(field);
Config.Entry fieldEntry = field.getAnnotation(Config.Entry.class);
if (fieldEntry != null && fieldEntry.value() != null) {
if (cat.get(fieldName) != null) {
cat.get(fieldName).setConfigEntryClass(fieldEntry.value());
}
}
}
Config.Entry classEntry = getClassOrBaseAnnotation(configClass, Config.Entry.class);
if (classEntry != null && classEntry.value() != null) {
cat.setConfigEntryClass(classEntry.value());
}
}
}
private static void writeLangKeysToFile() {
for (Map.Entry<String, List<String>> entry : generatedLangKeys.entrySet()) {
final var langFile = new File("generated-lang-keys", entry.getKey() + ".txt");
try {
FileUtils.writeLines(langFile, entry.getValue());
} catch (IOException e) {
LOGGER.error(
"Failed to write lang keys for {} to file {}",
entry.getKey(),
langFile.getAbsolutePath(),
e);
}
}
}
private static void cullDeadCategories() {
for (final var entry : observedCategories.entrySet()) {
final var config = entry.getKey();
if (!isCategoryCullingEnabled(config)) {
LOGGER.info("Skipping category culling for config {}", config);
continue;
}
final var observedCategoryNames = entry.getValue();
if (config.getCategoryNames().size() == observedCategoryNames.size()) {
continue;
}
final var savedCategoryNames = new ArrayList<>(config.getCategoryNames());
savedCategoryNames.removeIf(observedCategoryNames::contains);
// Only keep the top-level category that needs removal otherwise getCategory() for any subcategory will
// recreate it.
List<String> parentsToRemove = savedCategoryNames.stream()
.filter(s -> savedCategoryNames.stream().noneMatch(existing -> s.startsWith(existing + ".")))
.collect(Collectors.toList());
for (final var category : parentsToRemove) {
ConfigCategory cat = config.getCategory(category);
config.removeCategory(cat);
}
config.save();
}
observedCategories.clear();
}
private static boolean isCategoryCullingEnabled(Configuration config) {
Map<String, Set<Class<?>>> map = configToCategoryClassMap.get(config);
if (map == null) return true;
for (Set<Class<?>> classes : map.values()) {
for (Class<?> clazz : classes) {
Config cfg = clazz.getAnnotation(Config.class);
if (cfg != null && !cfg.categoryCulling()) {
return false;
}
}
}
return true;
}
public static class ConfigNode {
final Map<String, Integer> fieldOrder = new HashMap<>();
final Map<String, ConfigNode> children = new HashMap<>();
public ConfigNode(Class<?> configClass) {
for (Field field : configClass.getDeclaredFields()) {
if (shouldSkipField(field)) {
continue;
}
String fieldName = ConfigFieldParser.getFieldName(field).toLowerCase();
Config.Order ann = field.getAnnotation(Config.Order.class);
if (ann != null) fieldOrder.put(fieldName, ann.value());
if (ConfigurationManager.isFieldSubCategory(field)) {
children.put(fieldName, new ConfigNode(field.getType()));
}
}
}
}
}