create MetaValuePreprocessor

This commit is contained in:
dfsek
2021-07-20 21:01:37 -07:00
parent c71445d3e5
commit fe60e12f16
5 changed files with 60 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.config.Loader;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.event.events.config.ConfigurationDiscoveryEvent;
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
@@ -40,6 +41,7 @@ import com.dfsek.terra.config.fileloaders.FolderLoader;
import com.dfsek.terra.config.fileloaders.ZIPLoader;
import com.dfsek.terra.config.loaders.GenericTemplateSupplierLoader;
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
import com.dfsek.terra.config.preprocessor.MetaValuePreprocessor;
import com.dfsek.terra.config.prototype.ProtoConfig;
import com.dfsek.terra.registry.CheckedRegistryImpl;
import com.dfsek.terra.registry.OpenRegistryImpl;
@@ -234,13 +236,16 @@ public class ConfigPackImpl implements ConfigPack {
varScope.create(var.getKey(), var.getValue());
}
List<Configuration> configurations = new ArrayList<>();
Map<String, Configuration> configurations = new HashMap<>();
main.getEventManager().callEvent(new ConfigurationDiscoveryEvent(this, loader, configurations::add)); // Create all the configs.
main.getEventManager().callEvent(new ConfigurationDiscoveryEvent(this, loader, configurations::put)); // Create all the configs.
selfLoader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurations));
abstractConfigLoader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurations));
Map<ConfigType<? extends ConfigTemplate, ?>, List<Configuration>> configs = new HashMap<>();
for(Configuration configuration : configurations) { // Sort the configs
for(Configuration configuration : configurations.values()) { // Sort the configs
ProtoConfig config = new ProtoConfig();
selfLoader.load(config, configuration);
configs.computeIfAbsent(config.getType(), configType -> new ArrayList<>()).add(configuration);

View File

@@ -0,0 +1,44 @@
package com.dfsek.terra.config.preprocessor;
import com.dfsek.tectonic.config.Configuration;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.preprocessor.Result;
import com.dfsek.tectonic.preprocessor.ValuePreprocessor;
import com.dfsek.terra.api.config.meta.Meta;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.AnnotatedType;
import java.util.Map;
public class MetaValuePreprocessor implements ValuePreprocessor<Meta> {
private final Map<String, Configuration> configs;
public MetaValuePreprocessor(Map<String, Configuration> configs) {
this.configs = configs;
}
@SuppressWarnings("unchecked")
@Override
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader configLoader, Meta annotation) {
if(c instanceof String) {
String value = ((String) c).trim();
if(value.startsWith("$")) { // it's a meta value.
String raw = value.substring(1);
int sep = raw.indexOf(':');
String file = raw.substring(0, sep);
String key = raw.substring(sep + 1);
if(!configs.containsKey(file)) throw new LoadException("Cannot fetch metavalue: No such config: " + file);
Configuration config = configs.get(file);
if(!config.contains(key))
throw new LoadException("Cannot fetch metavalue: No such key " + key + " in configuration " + config.getName());
return (Result<T>) Result.overwrite(config.get(key));
}
}
return Result.noOp();
}
}