implement PackMetaContext

This commit is contained in:
dfsek
2021-06-14 00:54:26 -07:00
parent 587cb8755f
commit 1e43365bbf
3 changed files with 51 additions and 15 deletions

View File

@@ -1,18 +1,10 @@
package com.dfsek.terra.api.config.meta;
import com.dfsek.tectonic.exception.ConfigException;
/**
* Context from which to pull {@link MetaValue}s
*/
public interface MetaContext {
<T> T load(Object in, Class<T> clazz);
/**
* Load method for when class is unknown.
*
* @param in Object to load/load metavalue for
* @return Loaded object
*/
default Object load(Object in) {
return load(in, in.getClass());
}
<T> T load(String meta, Class<T> clazz) throws ConfigException;
}

View File

@@ -8,8 +8,6 @@ import com.dfsek.terra.api.util.collections.ProbabilityCollection;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class GenericMetaValueLoader extends MetaValueLoader<MetaValue<Object>, Object> {
protected GenericMetaValueLoader(MetaContext context) {
@@ -24,8 +22,7 @@ public class GenericMetaValueLoader extends MetaValueLoader<MetaValue<Object>, O
ParameterizedType pType = (ParameterizedType) type;
Type generic = pType.getActualTypeArguments()[0];
if(c instanceof String) {
String possibleMeta = ((String) c).trim();
// TODO: parse meta string
}
return MetaValue.of(loader.loadType(generic, c));

View File

@@ -0,0 +1,47 @@
package com.dfsek.terra.config.pack.meta;
import com.dfsek.tectonic.config.Configuration;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.terra.api.config.meta.MetaContext;
import com.dfsek.terra.config.fileloaders.Loader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class PackMetaContext implements MetaContext {
private final Map<String, Configuration> configs = new HashMap<>(); // Lazy init configs
private final Loader fileAccess;
private final ConfigLoader loader;
public PackMetaContext(Loader fileAccess, ConfigLoader loader) {
this.fileAccess = fileAccess;
this.loader = loader;
}
@Override
public <T> T load(String meta, Class<T> clazz) throws ConfigException {
if(meta.indexOf(":") != meta.lastIndexOf(":")) { // We just need to know if there are >1.
throw new LoadException("Malformed metavalue string: " + meta);
}
String file = "/pack.yml";
String key;
if(meta.contains(":")) {
file = meta.substring(0, meta.indexOf(":"));
key = meta.substring(meta.indexOf(":") + 1);
} else {
key = meta;
}
Configuration configuration;
try {
configuration = new Configuration(fileAccess.get(file), file);
} catch(IOException e) {
throw new LoadException("Failed to load config file \"" + file + "\":", e);
}
return loader.loadClass(clazz, configuration.get(key));
}
}