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

Customizable langkey pattern config annotation #68

Merged
merged 4 commits into from
Sep 11, 2024
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
19 changes: 19 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@
String value();
}

/**
* Defines a pattern for generating lang keys for fields and categories in the annotated class.
* <p>
* Placeholders: <br>
* {@code %mod} - mod id <br>
* {@code %file} - file name <br>
* {@code %cat} - category name <br>
* {@code %field} - field name <b>(required)</b> <br>
* </p>
* Default pattern: {@code %mod.%cat.%field}. Categories use the pattern without {@code %field}. Can be overridden
* for fields with {@link Config.LangKey}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface LangKeyPattern {

String pattern() default "%mod.%cat.%field";
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Comment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,15 @@ public class ConfigFieldParser {
PARSERS.put(Enum.class, new EnumParser());
}

public static void loadField(Object instance, Field field, Configuration config, String category)
public static void loadField(Object instance, Field field, Configuration config, String category, String key)
throws ConfigException {
try {
Parser parser = getParser(field);
var comment = Optional.ofNullable(field.getAnnotation(Config.Comment.class)).map(Config.Comment::value)
.map((lines) -> String.join("\n", lines)).orElse("");
val name = getFieldName(field);
val langKey = Optional.ofNullable(field.getAnnotation(Config.LangKey.class)).map(Config.LangKey::value)
.orElse(name);
val defValueString = getModDefault(field);

parser.load(instance, defValueString, field, config, category, name, comment, langKey);
parser.load(instance, defValueString, field, config, category, name, comment, key);
} catch (Exception e) {
throw new ConfigException(
"Failed to load field " + field.getName()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gtnewhorizon.gtnhlib.config;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -39,6 +40,7 @@ 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 String[] langKeyPlaceholders = new String[] { "%mod", "%file", "%cat", "%field" };

private static final ConfigurationManager instance = new ConfigurationManager();

Expand Down Expand Up @@ -131,10 +133,13 @@ private static void processSubCategory(Object instance, Configuration config, Fi
var comment = Optional.ofNullable(subCategoryField.getAnnotation(Config.Comment.class))
.map(Config.Comment::value).map((lines) -> String.join("\n", lines)).orElse("");
val name = ConfigFieldParser.getFieldName(subCategoryField);
val langKey = Optional.ofNullable(subCategoryField.getAnnotation(Config.LangKey.class))
.map(Config.LangKey::value).orElse(name);
val cat = (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + name.toLowerCase();
ConfigCategory subCat = config.getCategory(cat);
val langKey = getLangKey(
subCategoryField.getType(),
subCategoryField.getAnnotation(Config.LangKey.class),
null,
subCat.getName());

subCat.setComment(comment);
subCat.setLanguageKey(langKey);
Expand All @@ -158,6 +163,12 @@ private static void processConfigInternal(Class<?> configClass, String category,
@Nullable Object instance) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException,
NoSuchFieldException, ConfigException {
boolean foundCategory = !category.isEmpty();
ConfigCategory cat = foundCategory ? rawConfig.getCategory(category) : null;
boolean requiresMcRestart = getClassOrBaseAnnotation(configClass, Config.RequiresMcRestart.class) != null
|| foundCategory && cat.requiresMcRestart();
boolean requiresWorldRestart = getClassOrBaseAnnotation(configClass, Config.RequiresWorldRestart.class) != null
|| foundCategory && cat.requiresWorldRestart();

for (val field : configClass.getDeclaredFields()) {
if (instance != null && Modifier.isStatic(field.getModifiers())) {
throw new ConfigException(
Expand Down Expand Up @@ -191,20 +202,31 @@ private static void processConfigInternal(Class<?> configClass, String category,

if (category.isEmpty()) continue;

ConfigFieldParser.loadField(instance, field, rawConfig, category);
val langKey = getLangKey(
configClass,
field.getAnnotation(Config.LangKey.class),
ConfigFieldParser.getFieldName(field),
category);
ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey);

val cat = rawConfig.getCategory(category);
if (field.isAnnotationPresent(Config.RequiresMcRestart.class)) {
cat.setRequiresMcRestart(true);
if (!requiresMcRestart) {
requiresMcRestart = field.isAnnotationPresent(Config.RequiresMcRestart.class);
}
if (field.isAnnotationPresent(Config.RequiresWorldRestart.class)) {
cat.setRequiresWorldRestart(true);

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;
val langKey = getLangKey(configClass, configClass.getAnnotation(Config.LangKey.class), null, cat.getName());
cat.setLanguageKey(langKey);
cat.setRequiresMcRestart(requiresMcRestart);
cat.setRequiresWorldRestart(requiresWorldRestart);
}

/**
Expand Down Expand Up @@ -322,7 +344,65 @@ private static IConfigElementProxy<?> getProxyElement(IConfigElement<?> element,
});
}

private static boolean isFieldSubCategory(Field field) {
private static String getLangKey(Class<?> configClass, @Nullable Config.LangKey langKey, @Nullable String fieldName,
String categoryName) throws ConfigException {
if (langKey != null) return langKey.value();

Config.LangKeyPattern pattern = getClassOrBaseAnnotation(configClass, Config.LangKeyPattern.class);
String name = Optional.ofNullable(fieldName).orElse(categoryName);
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;

return buildKeyFromPattern(patternStr, cfg.modid(), cfg.filename(), categoryName, name);
}

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);
if (annotation != null || !clazz.isMemberClass()) return annotation;

while (clazz.isMemberClass()) {
clazz = clazz.getDeclaringClass();
}

return clazz.getAnnotation(annotationClass);
}

private static boolean isFieldSubCategory(@Nullable Field field) {
if (field == null) return false;

Class<?> fieldClass = field.getType();
return !ConfigFieldParser.canParse(field) && fieldClass.getSuperclass() != null
&& fieldClass.getSuperclass().equals(Object.class);
Expand Down