Respackopts/src/main/java/io/gitlab/jfronny/respackopts/data/entry/ConfigBranch.java

116 lines
3.7 KiB
Java
Raw Normal View History

2021-06-10 16:07:22 +02:00
package io.gitlab.jfronny.respackopts.data.entry;
2021-06-10 13:10:12 +02:00
2021-06-10 14:55:03 +02:00
import com.google.common.collect.ImmutableMap;
import io.gitlab.jfronny.respackopts.Respackopts;
2021-06-10 16:07:22 +02:00
import io.gitlab.jfronny.respackopts.data.RpoError;
2021-06-10 13:10:12 +02:00
import java.util.HashMap;
import java.util.Map;
public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
2021-06-10 14:55:03 +02:00
public ConfigBranch() {
setValue(new HashMap<>());
}
2021-06-10 13:10:12 +02:00
2021-06-10 16:07:22 +02:00
public boolean getBoolean(String name) throws RpoError {
2021-06-10 13:10:12 +02:00
String[] sp = name.split("\\.");
2021-06-10 14:55:03 +02:00
if (!super.getValue().containsKey(sp[0]))
2021-06-10 16:07:22 +02:00
throw new RpoError("Invalid path to key");
ConfigEntry<?> e = super.getValue().get(sp[0]);
2021-06-10 13:10:12 +02:00
if (sp.length == 1) {
if (e instanceof ConfigBooleanEntry b)
2021-06-10 14:55:03 +02:00
return b.getValue();
2021-06-10 16:07:22 +02:00
throw new RpoError("Not a boolean");
2021-06-10 13:10:12 +02:00
}
if (sp.length == 2 && e instanceof ConfigEnumEntry en) {
for (String entry : en.values) {
if (entry.equals(sp[1]))
return entry.equals(en.getValue());
2021-06-10 13:10:12 +02:00
}
2021-06-10 16:07:22 +02:00
throw new RpoError("Could not find enum entry");
2021-06-10 13:10:12 +02:00
}
if (e instanceof ConfigBranch b)
return b.getBoolean(name.substring(name.indexOf('.') + 1));
2021-06-10 16:07:22 +02:00
throw new RpoError("Invalid path to key");
2021-06-10 13:10:12 +02:00
}
2021-06-10 14:55:03 +02:00
@Override
public void sync(ConfigEntry<Map<String, ConfigEntry<?>>> source, SyncMode mode) {
for (Map.Entry<String, ConfigEntry<?>> e : source.getValue().entrySet()) {
2021-06-10 14:55:03 +02:00
if (!has(e.getKey())) {
if (mode == SyncMode.RESPACK_LOAD)
2021-06-10 19:26:59 +02:00
add(e.getKey(), e.getValue().clone());
2021-06-10 14:55:03 +02:00
} else {
ConfigEntry<?> current = get(e.getKey());
2021-06-10 14:55:03 +02:00
if (e.getValue().typeMatches(current)) {
syncSub(current, (ConfigEntry)e.getValue(), mode);
2021-06-10 13:10:12 +02:00
}
else {
2021-06-10 14:55:03 +02:00
Respackopts.LOGGER.warn("Type mismatch in config, ignoring");
2021-06-10 13:10:12 +02:00
}
}
}
}
2021-06-10 14:55:03 +02:00
@Override
public boolean typeMatches(ConfigEntry<?> val) {
2021-06-10 14:55:03 +02:00
return val instanceof ConfigBranch;
}
private <T> void syncSub(ConfigEntry<T> current, ConfigEntry<T> next, SyncMode mode) {
2021-06-10 14:55:03 +02:00
current.sync(next, mode);
}
public <T> void add(String name, ConfigEntry<T> val) {
val.setVersion(version);
2021-06-10 14:55:03 +02:00
super.getValue().put(name, val);
2021-06-10 13:10:12 +02:00
}
public ConfigEntry<?> get(String key) {
2021-06-10 14:55:03 +02:00
return super.getValue().get(key);
2021-06-10 13:10:12 +02:00
}
public boolean has(String key) {
2021-06-10 14:55:03 +02:00
return super.getValue().containsKey(key);
2021-06-10 13:10:12 +02:00
}
2021-06-10 14:55:03 +02:00
@Override
public Map<String, ConfigEntry<?>> getValue() {
2021-06-10 14:55:03 +02:00
return ImmutableMap.copyOf(super.getValue());
2021-06-10 13:10:12 +02:00
}
2021-06-10 14:55:03 +02:00
@Override
public void buildShader(StringBuilder sb, String valueName) throws RpoError {
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
e.getValue().buildShader(sb, valueName + "_" + e.getKey());
2021-06-10 13:10:12 +02:00
}
}
2021-06-10 14:55:03 +02:00
@Override
public void appendString(StringBuilder sb) {
for (Map.Entry<String, ConfigEntry<?>> e : getValue().entrySet()) {
2021-06-10 14:55:03 +02:00
sb.append("\n");
sb.append(e.getKey());
sb.append(": ");
sb.append(e.getValue());
}
}
2021-06-10 19:26:59 +02:00
@Override
public ConfigEntry<Map<String, ConfigEntry<?>>> clone() {
2021-06-10 19:26:59 +02:00
ConfigBranch branch = new ConfigBranch();
for (Map.Entry<String, ConfigEntry<?>> e : getValue().entrySet()) {
2021-06-10 19:26:59 +02:00
branch.add(e.getKey(), e.getValue().clone());
}
return branch;
}
@Override
public void setVersion(int version) {
super.setVersion(version);
for (ConfigEntry<?> value : getValue().values()) {
value.setVersion(version);
}
}
2021-06-10 13:10:12 +02:00
}