mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-26 05:50:08 +00:00
implement PackMetaContext
This commit is contained in:
1 parent
587cb8755f
commit
1e43365bbf
3 files changed
+51
-15
No files matched your search
@@ -1,18 +1,10 @@
|
|||||||
package com.dfsek.terra.api.config.meta;
|
package com.dfsek.terra.api.config.meta;
|
||||||
|
|
||||||
|
import com.dfsek.tectonic.exception.ConfigException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context from which to pull {@link MetaValue}s
|
* Context from which to pull {@link MetaValue}s
|
||||||
*/
|
*/
|
||||||
public interface MetaContext {
|
public interface MetaContext {
|
||||||
<T> T load(Object in, Class<T> clazz);
|
<T> T load(String meta, Class<T> clazz) throws ConfigException;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+1
-4
@@ -8,8 +8,6 @@ import com.dfsek.terra.api.util.collections.ProbabilityCollection;
|
|||||||
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class GenericMetaValueLoader extends MetaValueLoader<MetaValue<Object>, Object> {
|
public class GenericMetaValueLoader extends MetaValueLoader<MetaValue<Object>, Object> {
|
||||||
protected GenericMetaValueLoader(MetaContext context) {
|
protected GenericMetaValueLoader(MetaContext context) {
|
||||||
@@ -24,8 +22,7 @@ public class GenericMetaValueLoader extends MetaValueLoader<MetaValue<Object>, O
|
|||||||
ParameterizedType pType = (ParameterizedType) type;
|
ParameterizedType pType = (ParameterizedType) type;
|
||||||
Type generic = pType.getActualTypeArguments()[0];
|
Type generic = pType.getActualTypeArguments()[0];
|
||||||
if(c instanceof String) {
|
if(c instanceof String) {
|
||||||
String possibleMeta = ((String) c).trim();
|
|
||||||
// TODO: parse meta string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return MetaValue.of(loader.loadType(generic, c));
|
return MetaValue.of(loader.loadType(generic, c));
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user