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;
|
|
|
|
|
2021-06-14 16:33:15 +02:00
|
|
|
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");
|
2021-06-14 16:33:15 +02:00
|
|
|
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) {
|
2021-06-14 16:33:15 +02:00
|
|
|
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
|
2021-06-14 16:33:15 +02:00
|
|
|
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())) {
|
2021-06-14 16:33:15 +02:00
|
|
|
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 {
|
2021-06-14 16:33:15 +02:00
|
|
|
ConfigEntry<?> current = get(e.getKey());
|
2021-06-10 14:55:03 +02:00
|
|
|
if (e.getValue().typeMatches(current)) {
|
2021-06-14 16:33:15 +02:00
|
|
|
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
|
2021-06-14 16:33:15 +02:00
|
|
|
public boolean typeMatches(ConfigEntry<?> val) {
|
2021-06-10 14:55:03 +02:00
|
|
|
return val instanceof ConfigBranch;
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:33:15 +02:00
|
|
|
private <T> void syncSub(ConfigEntry<T> current, ConfigEntry<T> next, SyncMode mode) {
|
2021-06-10 14:55:03 +02:00
|
|
|
current.sync(next, mode);
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:33:15 +02:00
|
|
|
public <T> void add(String name, ConfigEntry<T> val) {
|
2021-06-10 14:55:03 +02:00
|
|
|
super.getValue().put(name, val);
|
2021-06-10 13:10:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:33:15 +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
|
2021-06-14 16:33:15 +02:00
|
|
|
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
|
|
|
|
2021-06-10 16:07:22 +02:00
|
|
|
public void buildShader(StringBuilder sb, String valuePrefix) throws RpoError {
|
2021-06-14 16:33:15 +02:00
|
|
|
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
|
2021-06-10 13:10:12 +02:00
|
|
|
if (e.getValue() instanceof ConfigNumericEntry n) {
|
|
|
|
sb.append("\n#define ");
|
|
|
|
sb.append(valuePrefix);
|
|
|
|
sb.append(e.getKey());
|
|
|
|
sb.append(' ');
|
2021-06-10 14:55:03 +02:00
|
|
|
sb.append(n.getValue().toString());
|
2021-06-10 13:10:12 +02:00
|
|
|
}
|
|
|
|
else if (e.getValue() instanceof ConfigBooleanEntry n) {
|
2021-06-10 14:55:03 +02:00
|
|
|
if (n.getValue()) {
|
2021-06-10 13:10:12 +02:00
|
|
|
sb.append("\n#define ");
|
|
|
|
sb.append(valuePrefix);
|
|
|
|
sb.append(e.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (e.getValue() instanceof ConfigEnumEntry n) {
|
|
|
|
sb.append("\n#define ");
|
|
|
|
sb.append(valuePrefix);
|
|
|
|
sb.append(e.getKey());
|
2021-06-14 16:33:15 +02:00
|
|
|
for (String e2 : n.values) {
|
|
|
|
if (e2.equals(n.getValue())) {
|
|
|
|
sb.append("\n#define ");
|
|
|
|
sb.append(valuePrefix);
|
|
|
|
sb.append(e.getKey());
|
|
|
|
sb.append('_');
|
|
|
|
sb.append(e2);
|
|
|
|
}
|
2021-06-10 13:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (e.getValue() instanceof ConfigBranch n) {
|
|
|
|
n.buildShader(sb, valuePrefix + e.getKey() + "_");
|
|
|
|
}
|
|
|
|
else {
|
2021-06-10 16:07:22 +02:00
|
|
|
throw new RpoError("Invalid type");
|
2021-06-10 13:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 14:55:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void appendString(StringBuilder sb) {
|
2021-06-14 16:33:15 +02:00
|
|
|
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
|
2021-06-14 16:33:15 +02:00
|
|
|
public ConfigEntry<Map<String, ConfigEntry<?>>> clone() {
|
2021-06-10 19:26:59 +02:00
|
|
|
ConfigBranch branch = new ConfigBranch();
|
2021-06-14 16:33:15 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-06-10 13:10:12 +02:00
|
|
|
}
|