120 lines
4.7 KiB
Java
120 lines
4.7 KiB
Java
package io.gitlab.jfronny.libjf.config.impl.legacy;
|
|
|
|
import io.gitlab.jfronny.libjf.LibJf;
|
|
import io.gitlab.jfronny.libjf.config.api.*;
|
|
import io.gitlab.jfronny.libjf.config.api.v1.ConfigCategory;
|
|
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
|
|
import io.gitlab.jfronny.libjf.config.api.v1.dsl.DSL;
|
|
import io.gitlab.jfronny.libjf.config.api.v1.type.Type;
|
|
import io.gitlab.jfronny.libjf.config.impl.AuxiliaryMetadata;
|
|
import io.gitlab.jfronny.libjf.config.impl.dsl.DslEntryInfo;
|
|
import io.gitlab.jfronny.libjf.config.impl.reflect.ReflectiveConfigBuilderImpl;
|
|
import net.fabricmc.loader.api.ModContainer;
|
|
|
|
import java.lang.reflect.*;
|
|
import java.nio.file.Path;
|
|
import java.util.*;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Deprecated
|
|
public record ConfigHolderImpl(io.gitlab.jfronny.libjf.config.api.v1.ConfigHolder base) implements ConfigHolder {
|
|
private static final Map<Class<?>, io.gitlab.jfronny.libjf.config.api.v1.ConfigInstance> klazzToInstance = new HashMap<>();
|
|
@Override
|
|
public void register(String modId, Class<?> config) {
|
|
AtomicReference<ModContainer> mc = new AtomicReference<>();
|
|
base.migrateFiles(modId);
|
|
klazzToInstance.put(config, DSL.create(modId).register(builder -> {
|
|
Optional.ofNullable(AuxiliaryMetadata.forMod(modId)).ifPresent(meta -> meta.applyTo(builder));
|
|
return applyCategory(builder, config);
|
|
}));
|
|
}
|
|
|
|
private <T extends CategoryBuilder<?>> T applyCategory(T builder, Class<?> klazz) {
|
|
for (Field field : klazz.getFields()) {
|
|
if (field.isAnnotationPresent(Entry.class)) {
|
|
Entry entry = field.getAnnotation(Entry.class);
|
|
Object defaultValue = null;
|
|
try {
|
|
defaultValue = field.get(null);
|
|
} catch (IllegalAccessException ignored) {
|
|
}
|
|
//noinspection unchecked,rawtypes
|
|
builder.value(new DslEntryInfo<Object>(
|
|
field.getName(),
|
|
defaultValue,
|
|
() -> field.get(null),
|
|
v -> field.set(null, v),
|
|
(Type) Type.ofClass(field.getGenericType()),
|
|
entry.width(),
|
|
entry.min(),
|
|
entry.max()
|
|
));
|
|
}
|
|
}
|
|
builder.addPreset(ReflectiveConfigBuilderImpl.CONFIG_PRESET_DEFAULT, ConfigCategory::reset);
|
|
for (Method method : klazz.getMethods()) {
|
|
if (method.isAnnotationPresent(Preset.class)) {
|
|
builder.addPreset(builder.getTranslationPrefix() + method.getName(), c -> {
|
|
try {
|
|
method.invoke(null);
|
|
} catch (InvocationTargetException | IllegalAccessException e) {
|
|
LibJf.LOGGER.error("Could not apply preset", e);
|
|
}
|
|
});
|
|
} else if (method.isAnnotationPresent(Verifier.class)) {
|
|
builder.addVerifier(c -> {
|
|
try {
|
|
method.invoke(null);
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
LibJf.LOGGER.error("Could not run verifier", e);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
for (Class<?> category : klazz.getClasses()) {
|
|
if (category.isAnnotationPresent(Category.class)) {
|
|
String name = category.getSimpleName();
|
|
name = Character.toLowerCase(name.charAt(0)) + name.substring(1); // camelCase
|
|
builder.category(name, categoryBuilder -> applyCategory(categoryBuilder, category));
|
|
}
|
|
}
|
|
return builder;
|
|
}
|
|
|
|
@Override
|
|
public Map<String, ConfigInstance> getRegistered() {
|
|
return base.getRegistered().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, s -> ConfigInstanceImpl.of(s.getValue())));
|
|
}
|
|
|
|
@Override
|
|
public ConfigInstance get(Class<?> configClass) {
|
|
return ConfigInstanceImpl.of(klazzToInstance.get(configClass));
|
|
}
|
|
|
|
@Override
|
|
public ConfigInstance get(String modId) {
|
|
return ConfigInstanceImpl.of(base.get(modId));
|
|
}
|
|
|
|
@Override
|
|
public ConfigInstance get(Path configPath) {
|
|
return ConfigInstanceImpl.of(base.get(configPath));
|
|
}
|
|
|
|
@Override
|
|
public boolean isRegistered(Class<?> configClass) {
|
|
return klazzToInstance.containsKey(configClass);
|
|
}
|
|
|
|
@Override
|
|
public boolean isRegistered(String modId) {
|
|
return base.isRegistered(modId);
|
|
}
|
|
|
|
@Override
|
|
public boolean isRegistered(Path configPath) {
|
|
return base.isRegistered(configPath);
|
|
}
|
|
}
|