Some more work, still doesn't work

This commit is contained in:
JFronny 2021-06-10 14:55:03 +02:00
parent 68a3bf2d54
commit a3721174d9
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
14 changed files with 233 additions and 107 deletions

View File

@ -23,7 +23,7 @@ import java.util.Optional;
public class GuiFactory {
public void buildCategory(ConfigBranch source, String screenId, JfConfigCategory config, ConfigEntryBuilder entryBuilder, String namePrefix) {
String b = "respackopts.field." + screenId;
for (Map.Entry<String, Entry<?>> in : source.getEntries()) {
for (Map.Entry<String, Entry<?>> in : source.getValue().entrySet()) {
Entry<?> entry = in.getValue();
String n = ("".equals(namePrefix) ? "" : namePrefix + ".") + in.getKey();
if (entry instanceof ConfigBranch e) {
@ -33,50 +33,36 @@ public class GuiFactory {
config.addEntry(sc.build());
}
else if (entry instanceof ConfigBooleanEntry e) {
config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), e.value)
.setDefaultValue(e.defaultValue)
.setSaveConsumer(v -> {
/*ConfigBooleanEntry cb = (ConfigBooleanEntry)source.get(e.name);
cb.value = v;
Respackopts.LOGGER.info(cb == e);
Respackopts.LOGGER.info(cb == entry);*/
e.value = v;
})
config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), e.getValue())
.setDefaultValue(e.getDefault())
.setSaveConsumer(e::setValue)
.setTooltipSupplier(() -> getTooltip(n, screenId))
.build());
}
else if (entry instanceof ConfigEnumEntry e) {
String selected = "";
String def = "";
for (Map.Entry<String, Integer> en : e.values.entrySet()) {
if (en.getValue().equals(e.value))
selected = en.getKey();
if (en.getValue().equals(e.defaultValue))
def = en.getKey();
}
config.addEntry(entryBuilder.startDropdownMenu(getText(n, b),
DropdownMenuBuilder.TopCellElementBuilder.of(selected, LiteralText::new),
DropdownMenuBuilder.TopCellElementBuilder.of(e.getValueName(), LiteralText::new),
new DropdownBoxEntry.DefaultSelectionCellCreator())
.setSuggestionMode(false)
.setDefaultValue(def)
.setDefaultValue(e.getDefaultName())
.setSelections(() -> e.values.keySet().iterator())
.setSaveConsumer(v -> e.value = e.values.get(v))
.setSaveConsumer(v -> e.setValue(e.values.get(v)))
.setTooltipSupplier(() -> getTooltip(n, screenId))
.build());
}
else if (entry instanceof ConfigNumericEntry e) {
if (e.min != null && e.max != null) {
config.addEntry(entryBuilder.startIntSlider(getText(n, b),
e.value.intValue(), e.min.intValue(), e.max.intValue())
.setDefaultValue(e.defaultValue.intValue())
.setSaveConsumer(v -> e.value = v.doubleValue())
e.getValue().intValue(), e.min.intValue(), e.max.intValue())
.setDefaultValue(e.getDefault().intValue())
.setSaveConsumer(v -> e.setValue(v.doubleValue()))
.setTooltipSupplier(() -> getTooltip(n, screenId))
.build());
}
else {
config.addEntry(entryBuilder.startDoubleField(getText(n, b), e.value)
.setDefaultValue(e.defaultValue)
.setSaveConsumer(v -> e.value = v)
config.addEntry(entryBuilder.startDoubleField(getText(n, b), e.getValue())
.setDefaultValue(e.getDefault())
.setSaveConsumer(e::setValue)
.setTooltipSupplier(() -> getTooltip(n, screenId))
.build());
}

View File

@ -3,10 +3,7 @@ package io.gitlab.jfronny.respackopts;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.gitlab.jfronny.respackopts.data.ConfigBooleanEntry;
import io.gitlab.jfronny.respackopts.data.ConfigBranch;
import io.gitlab.jfronny.respackopts.data.ConfigEnumEntry;
import io.gitlab.jfronny.respackopts.data.ConfigNumericEntry;
import io.gitlab.jfronny.respackopts.data.*;
import io.gitlab.jfronny.respackopts.data.in.Respackmeta;
import io.gitlab.jfronny.respackopts.filters.conditions.SyntaxError;
import io.gitlab.jfronny.respackopts.gson.BooleanEntrySerializer;
@ -67,6 +64,7 @@ public class Respackopts implements ClientModInitializer {
public static void save() {
for (String s : resPackMetas.keySet()) {
s = resPackMetas.get(s).id;
LOGGER.info(s + ": " + CONFIG_BRANCH.get(s));
try (Writer writer = Files.newBufferedWriter(CONF_DIR.resolve(s + ".json"))) {
GSON.toJson(CONFIG_BRANCH.get(s), writer);
writer.flush();
@ -85,10 +83,10 @@ public class Respackopts implements ClientModInitializer {
try (Reader reader = Files.newBufferedReader(q)) {
ConfigBranch b = GSON.fromJson(reader, ConfigBranch.class);
if (CONFIG_BRANCH.containsKey(id))
CONFIG_BRANCH.get(id).loadValues(b, false);
CONFIG_BRANCH.get(id).sync(b, SyncMode.CONF_LOAD);
else
CONFIG_BRANCH.put(id, b);
} catch (IOException | SyntaxError e) {
} catch (IOException e) {
LOGGER.error("Failed to load " + id, e);
}
}

View File

@ -2,6 +2,11 @@ package io.gitlab.jfronny.respackopts.data;
public class ConfigBooleanEntry extends Entry<Boolean> {
public ConfigBooleanEntry(boolean v) {
value = v;
setValue(v);
}
@Override
public boolean typeMatches(Entry<?> val) {
return val instanceof ConfigBooleanEntry;
}
}

View File

@ -1,30 +1,31 @@
package io.gitlab.jfronny.respackopts.data;
import com.google.common.collect.ImmutableMap;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.filters.conditions.SyntaxError;
import java.io.InvalidClassException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class ConfigBranch extends Entry<Object> {
private final Map<String, Entry<?>> entries = new HashMap<>();
public class ConfigBranch extends Entry<Map<String, Entry<?>>> {
public ConfigBranch() {
setValue(new HashMap<>());
}
public boolean getBoolean(String name) throws SyntaxError {
String[] sp = name.split("\\.");
if (!entries.containsKey(sp[0]))
if (!super.getValue().containsKey(sp[0]))
throw new SyntaxError("Invalid path to key");
Entry<?> e = entries.get(sp[0]);
Entry<?> e = super.getValue().get(sp[0]);
if (sp.length == 1) {
if (e instanceof ConfigBooleanEntry b)
return b.value;
return b.getValue();
throw new SyntaxError("Not a boolean");
}
if (sp.length == 2 && e instanceof ConfigEnumEntry en) {
for (Map.Entry<String, Integer> entry : en.values.entrySet()) {
if (entry.getKey().equals(sp[1]))
return entry.getValue().equals(en.value);
return entry.getValue().equals(en.getValue());
}
throw new SyntaxError("Could not find enum entry");
}
@ -33,73 +34,62 @@ public class ConfigBranch extends Entry<Object> {
throw new SyntaxError("Invalid path to key");
}
public void loadValues(ConfigBranch newBranch, boolean additionOnly) throws SyntaxError {
for (Map.Entry<String, Entry<?>> e : newBranch.entries.entrySet()) {
if (!entries.containsKey(e.getKey()))
add(e.getKey(), e.getValue());
else if (!additionOnly) {
if (e.getValue() instanceof ConfigEnumEntry ne) {
ConfigEnumEntry ol = (ConfigEnumEntry)entries.get(e.getKey());
if (ne.value != null)
ol.value = ne.value;
if (ne.values != null && !ne.values.isEmpty())
ol.values = ne.values;
}
else if (e.getValue() instanceof ConfigBooleanEntry ne) {
ConfigBooleanEntry ol = (ConfigBooleanEntry)entries.get(e.getKey());
if (ne.value != null)
ol.value = ne.value;
}
else if (e.getValue() instanceof ConfigNumericEntry ne) {
ConfigNumericEntry ol = (ConfigNumericEntry)entries.get(e.getKey());
if (ne.value != null)
ol.value = ne.value;
if (ne.min != null)
ol.min = ne.min;
if (ne.max != null)
ol.max = ne.max;
}
else if (e.getValue() instanceof ConfigBranch ne) {
ConfigBranch ol = (ConfigBranch)entries.get(e.getKey());
ol.loadValues(ne, false);
@Override
public void sync(Entry<Map<String, Entry<?>>> source, SyncMode mode) {
for (Map.Entry<String, Entry<?>> e : source.getValue().entrySet()) {
if (!has(e.getKey())) {
if (mode.isAdd)
add(e.getKey(), e.getValue());
} else {
Entry<?> current = get(e.getKey());
if (e.getValue().typeMatches(current)) {
syncSub(current, (Entry)e.getValue(), mode);
}
else {
throw new SyntaxError("Invalid type");
Respackopts.LOGGER.warn("Type mismatch in config, ignoring");
}
}
}
}
public <T extends Entry<T2>, T2> T add(String name, T val) {
val.name = name;
val.defaultValue = val.value;
entries.put(name, val);
return val;
@Override
public boolean typeMatches(Entry<?> val) {
return val instanceof ConfigBranch;
}
private <T> void syncSub(Entry<T> current, Entry<T> next, SyncMode mode) {
current.sync(next, mode);
}
public <T> void add(String name, Entry<T> val) {
val.setDefault(val.getValue());
super.getValue().put(name, val);
}
public Entry<?> get(String key) {
return entries.get(key);
return super.getValue().get(key);
}
public boolean has(String key) {
return entries.containsKey(key);
return super.getValue().containsKey(key);
}
public Set<Map.Entry<String, Entry<?>>> getEntries() {
return entries.entrySet();
@Override
public Map<String, Entry<?>> getValue() {
return ImmutableMap.copyOf(super.getValue());
}
public void buildShader(StringBuilder sb, String valuePrefix) throws SyntaxError {
for (Map.Entry<String, Entry<?>> e : entries.entrySet()) {
for (Map.Entry<String, Entry<?>> e : super.getValue().entrySet()) {
if (e.getValue() instanceof ConfigNumericEntry n) {
sb.append("\n#define ");
sb.append(valuePrefix);
sb.append(e.getKey());
sb.append(' ');
sb.append(n.value.toString());
sb.append(n.getValue().toString());
}
else if (e.getValue() instanceof ConfigBooleanEntry n) {
if (n.value) {
if (n.getValue()) {
sb.append("\n#define ");
sb.append(valuePrefix);
sb.append(e.getKey());
@ -110,7 +100,7 @@ public class ConfigBranch extends Entry<Object> {
sb.append(valuePrefix);
sb.append(e.getKey());
sb.append(' ');
sb.append(n.value.toString());
sb.append(n.getValue().toString());
for (Map.Entry<String, Integer> e2 : n.values.entrySet()) {
sb.append("\n#define ");
sb.append(valuePrefix);
@ -129,4 +119,14 @@ public class ConfigBranch extends Entry<Object> {
}
}
}
@Override
public void appendString(StringBuilder sb) {
for (Map.Entry<String, Entry<?>> e : getValue().entrySet()) {
sb.append("\n");
sb.append(e.getKey());
sb.append(": ");
sb.append(e.getValue());
}
}
}

View File

@ -5,8 +5,57 @@ import java.util.Map;
public class ConfigEnumEntry extends Entry<Integer> {
public Map<String, Integer> values = new HashMap<>();
private String fieldName;
public ConfigEnumEntry() {
value = 0;
setValue(0);
}
@Override
public Integer getValue() {
Integer v = super.getValue();
if (v == null)
throw new NullPointerException();
return v;
}
@Override
public void sync(Entry<Integer> source, SyncMode mode) {
super.sync(source, mode);
ConfigEnumEntry n = (ConfigEnumEntry) source;
if (mode.modifyDefault) {
if (n.values != null && !n.values.isEmpty())
values = n.values;
}
if (mode.modifyValue) {
if (n.fieldName != null)
setValue(values.get(n.fieldName));
}
}
@Override
public boolean typeMatches(Entry<?> val) {
return val instanceof ConfigEnumEntry;
}
public void setFieldName(String s) {
if (s != null)
fieldName = s;
}
public String getValueName() {
for (Map.Entry<String, Integer> en : values.entrySet()) {
if (en.getValue().equals(getValue()))
return en.getKey();
}
return getDefaultName();
}
public String getDefaultName() {
for (Map.Entry<String, Integer> en : values.entrySet()) {
if (en.getValue().equals(getDefault()))
return en.getKey();
}
throw new NullPointerException();
}
}

View File

@ -5,6 +5,35 @@ public class ConfigNumericEntry extends Entry<Double> {
public Double max;
public ConfigNumericEntry() {
value = 0d;
setValue(0d);
}
@Override
public void sync(Entry<Double> source, SyncMode mode) {
super.sync(source, mode);
ConfigNumericEntry n = (ConfigNumericEntry) source;
if (mode.modifyDefault) {
if (n.min != null)
min = n.min;
if (n.max != null)
max = n.max;
}
}
@Override
public boolean typeMatches(Entry<?> val) {
return val instanceof ConfigNumericEntry;
}
@Override
public void appendString(StringBuilder sb) {
sb.append(getValue())
.append(" (")
.append(getDefault())
.append(", ")
.append(min)
.append("-")
.append(max)
.append(")");
}
}

View File

@ -1,7 +1,44 @@
package io.gitlab.jfronny.respackopts.data;
public abstract class Entry<T> {
public String name;
public T defaultValue;
public T value;
private T defaultValue;
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
if (value != null)
this.value = value;
}
public T getDefault() {
return defaultValue;
}
public void setDefault(T value) {
if (value != null)
defaultValue = value;
}
public void sync(Entry<T> source, SyncMode mode) {
if (mode.modifyDefault)
setDefault(source.getDefault());
if (mode.modifyValue)
setValue(source.getValue());
}
public abstract boolean typeMatches(Entry<?> val);
public void appendString(StringBuilder sb) {
sb.append(value + " (" + defaultValue + ")");
}
@Override
public String toString() {
StringBuilder log = new StringBuilder();
appendString(log);
return log.toString();
}
}

View File

@ -0,0 +1,16 @@
package io.gitlab.jfronny.respackopts.data;
public class SyncMode {
public static final SyncMode RESPACK_LOAD = new SyncMode(false, true, true);
public static final SyncMode CONF_LOAD = new SyncMode(true, false, false);
public final boolean modifyValue;
public final boolean modifyDefault;
public final boolean isAdd;
private SyncMode(boolean modifyValue, boolean modifyDefault, boolean isAdd) {
this.modifyValue = modifyValue;
this.modifyDefault = modifyDefault;
this.isAdd = isAdd;
}
}

View File

@ -13,6 +13,6 @@ public class BooleanEntrySerializer implements JsonSerializer<ConfigBooleanEntry
@Override
public JsonElement serialize(ConfigBooleanEntry src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.value);
return new JsonPrimitive(src.getValue());
}
}

View File

@ -10,7 +10,7 @@ public class ConfigBranchSerializer implements JsonSerializer<ConfigBranch>, Jso
@Override
public JsonElement serialize(ConfigBranch src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject o = new JsonObject();
for (Map.Entry<String, Entry<?>> entry : src.getEntries()) {
for (Map.Entry<String, Entry<?>> entry : src.getValue().entrySet()) {
o.add(entry.getKey(), context.serialize(entry.getValue()));
}
return o;
@ -34,11 +34,10 @@ public class ConfigBranchSerializer implements JsonSerializer<ConfigBranch>, Jso
JsonPrimitive p = j.getAsJsonPrimitive();
if (p.isBoolean())
cbNew.add(s, new ConfigBooleanEntry(p.getAsBoolean()));
else if (p.isNumber()) {
//TODO identify saved enum values - use string?
else if (p.isNumber())
cbNew.add(s, context.deserialize(j, ConfigNumericEntry.class));
} else if (p.isString())
throw new JsonSyntaxException("String primitives are not currently supported");
else if (p.isString())
cbNew.add(s, context.deserialize(j, ConfigEnumEntry.class));
}
else if (j.isJsonArray())
cbNew.add(s, context.deserialize(j, ConfigEnumEntry.class));

View File

@ -9,7 +9,7 @@ import java.util.HashMap;
public class EnumEntrySerializer implements JsonSerializer<ConfigEnumEntry>, JsonDeserializer<ConfigEnumEntry> {
@Override
public JsonElement serialize(ConfigEnumEntry src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.value == null ? 0 : src.value);
return new JsonPrimitive(src.getValueName());
}
@Override
@ -18,7 +18,12 @@ public class EnumEntrySerializer implements JsonSerializer<ConfigEnumEntry>, Jso
if (json.isJsonPrimitive()) {
JsonPrimitive jp = json.getAsJsonPrimitive();
if (jp.isNumber()) {
result.value = jp.getAsInt();
result.setValue(jp.getAsInt());
result.setDefault(jp.getAsInt());
return result;
}
else if (jp.isString()) {
result.setFieldName(jp.getAsString());
return result;
}
else

View File

@ -28,7 +28,8 @@ public class LegacyConfigDeserializer {
}
for (Map.Entry<String, Double> e : lc.doubles.entrySet()) {
ConfigNumericEntry ne = new ConfigNumericEntry();
ne.value = e.getValue();
ne.setValue(e.getValue());
ne.setDefault(e.getValue());
int i = e.getKey().lastIndexOf('.');
if (i == -1)
cb.add(e.getKey(), ne);

View File

@ -8,13 +8,14 @@ import java.lang.reflect.Type;
public class NumericEntrySerializer implements JsonSerializer<ConfigNumericEntry>, JsonDeserializer<ConfigNumericEntry> {
@Override
public JsonElement serialize(ConfigNumericEntry src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.value);
return new JsonPrimitive(src.getValue());
}
@Override
public ConfigNumericEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ConfigNumericEntry result = new ConfigNumericEntry();
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
result.value = json.getAsDouble();
result.setValue(json.getAsDouble());
result.setDefault(json.getAsDouble());
return result;
}
else if (isSlider(json)) {
@ -32,7 +33,8 @@ public class NumericEntrySerializer implements JsonSerializer<ConfigNumericEntry
throw new JsonSyntaxException("Default value out of range in slider definition");
}
if (isWhole(defV) && isWhole(minV) && isWhole(maxV)) {
result.value = defV;
result.setValue(defV);
result.setDefault(defV);
result.min = minV;
result.max = maxV;
return result;

View File

@ -2,7 +2,7 @@ package io.gitlab.jfronny.respackopts.mixin;
import com.google.gson.*;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.data.ConfigBranch;
import io.gitlab.jfronny.respackopts.data.SyncMode;
import io.gitlab.jfronny.respackopts.data.in.Respackmeta;
import net.minecraft.resource.ResourcePackManager;
import net.minecraft.resource.ResourcePackProfile;
@ -16,7 +16,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import java.util.Map;
@Mixin(ResourcePackManager.class)
@ -36,7 +35,7 @@ public class ResourcePackManagerMixin {
if (!Respackopts.CONFIG_BRANCH.containsKey(conf.id))
Respackopts.CONFIG_BRANCH.put(conf.id, conf.conf);
else
Respackopts.CONFIG_BRANCH.get(conf.id).loadValues(conf.conf, true);
Respackopts.CONFIG_BRANCH.get(conf.id).sync(conf.conf, SyncMode.RESPACK_LOAD);
Respackopts.resPackMetas.put(v.getDisplayName().asString(), conf);
Respackopts.load(conf.id);
} catch (Throwable e) {