Skip to content

Commit

Permalink
fix incompatible old overlays crashing the program on startup in some…
Browse files Browse the repository at this point in the history
… cases
  • Loading branch information
Querz committed Jun 9, 2023
1 parent 370834f commit 441a971
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
24 changes: 18 additions & 6 deletions src/main/java/net/querz/mcaselector/config/ConfigProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ public final class ConfigProvider {
private ConfigProvider() {}

public static void loadGlobalConfig() {
GLOBAL = GlobalConfig.load();
LOGGER.debug("loaded global config: {}", GLOBAL);
try {
GLOBAL = GlobalConfig.load();
LOGGER.debug("loaded global config: {}", GLOBAL);
} catch (Exception ex) {
LOGGER.warn("failed to load global config", ex);
}
}

public static void loadWorldConfig(WorldDirectories worldDirectories, List<File> dimensionDirectories) {
WORLD = WorldConfig.load(worldDirectories, dimensionDirectories);
LOGGER.debug("loaded world config: {}", WORLD);
try {
WORLD = WorldConfig.load(worldDirectories, dimensionDirectories);
LOGGER.debug("loaded world config: {}", WORLD);
} catch (Exception ex) {
LOGGER.warn("failed to load world config", ex);
}
}

public static void loadOverlayConfig() {
OVERLAY = OverlayConfig.load();
LOGGER.debug("loaded overlay config: {}", OVERLAY);
try {
OVERLAY = OverlayConfig.load();
LOGGER.debug("loaded overlay config: {}", OVERLAY);
} catch (Exception ex) {
LOGGER.warn("failed to load overlay config", ex);
}
}

public static void saveAll() {
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/net/querz/mcaselector/config/OverlayConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.querz.mcaselector.config.adapter.OverlayAdapter;
import net.querz.mcaselector.logging.GsonNamingStrategy;
import net.querz.mcaselector.overlay.Overlay;
import java.util.Arrays;
import java.util.List;

public class OverlayConfig extends Config {
Expand Down Expand Up @@ -31,11 +33,34 @@ public void save() {
save(gsonInstance, BASE_OVERLAYS_FILE);
}

@Override
protected String save(Gson gson) {
return gson.toJson(overlays);
}

public static OverlayConfig load() {
String json = loadString(BASE_OVERLAYS_FILE);
if (json == null) {
return new OverlayConfig();
}
return gsonInstance.fromJson(json, OverlayConfig.class);
Overlay[] overlays = gsonInstance.fromJson(json, Overlay[].class);
OverlayConfig cfg = new OverlayConfig();
cfg.overlays = Arrays.asList(overlays);
return cfg;
}

private static final Gson toStringGsonInstance;

static {
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
builder.setFieldNamingStrategy(new GsonNamingStrategy());
builder.registerTypeAdapter(Overlay.class, new OverlayAdapter());
toStringGsonInstance = builder.create();
}

@Override
public String toString() {
return toStringGsonInstance.toJson(overlays);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ public void write(JsonWriter out, Overlay value) throws IOException {
out.name("max").value(value.max());
out.name("rawMin").value(value.getRawMin());
out.name("rawMax").value(value.getRawMax());
out.name("multiValues");
if (value.multiValues() != null) {
out.name("multiValues");
out.beginArray();
for (String multiValue : value.multiValues()) {
out.value(multiValue);
}
out.endArray();
} else {
out.nullValue();
}
out.name("rawMultiValues").value(value.getRawMultiValues());
out.name("minHue").value(value.getMinHue());
Expand All @@ -46,11 +44,12 @@ public Overlay read(JsonReader in) throws IOException {
OverlayType type = OverlayType.valueOf((String) map.get("type"));
Overlay overlay = type.instance();
overlay.setActive(get(map, "active", false, v -> (Boolean) v));
overlay.setMinInt(get(map, "min", null, v -> Integer.parseInt((String) v)));
overlay.setMaxInt(get(map, "max", null, v -> Integer.parseInt((String) v)));
overlay.setMinInt(get(map, "min", null, v -> ((Double) v).intValue()));
overlay.setMaxInt(get(map, "max", null, v -> ((Double) v).intValue()));
overlay.setRawMin(get(map, "rawMin", null, v -> (String) v));
overlay.setRawMax(get(map, "rawMax", null, v -> (String) v));
overlay.setMultiValues(get(map, "multiValues", null, v -> {
@SuppressWarnings("unchecked")
ArrayList<Object> list = (ArrayList<Object>) v;
String[] multiValues = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
Expand All @@ -59,10 +58,10 @@ public Overlay read(JsonReader in) throws IOException {
return multiValues;
}));
overlay.setRawMultiValues(get(map, "rawMultiValues", null, v -> (String) v));
overlay.setMinHue(get(map, "minHue", 0.0f, v -> Float.parseFloat((String) v)));
overlay.setMaxHue(get(map, "maxHue", 0.0f, v -> Float.parseFloat((String) v)));
overlay.setMinHue(get(map, "minHue", 0.0f, v -> ((Double) v).floatValue()));
overlay.setMaxHue(get(map, "maxHue", 0.0f, v -> ((Double) v).floatValue()));
overlay.readCustomJSON(map);
return null;
return overlay;
}

private <T> T get(Map<String, Object> map, String name, T def, Function<Object, T> parser) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/net/querz/mcaselector/overlay/Overlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class Overlay implements Cloneable {
private float minHue = 0.66666667f; // blue
private float maxHue = 0f; // red

private String multiValuesID = "";
private transient String multiValuesID = "";

public Overlay(OverlayType type) {
this.type = type;
Expand Down Expand Up @@ -128,7 +128,11 @@ public void setMaxHue(float maxHue) {

public void setMultiValues(String[] multiValues) {
this.multiValues = multiValues;
multiValuesID = UUID.nameUUIDFromBytes(String.join("", multiValues).getBytes()).toString().replace("-", "");
if (multiValues == null) {
multiValuesID = "";
} else {
multiValuesID = UUID.nameUUIDFromBytes(String.join("", multiValues).getBytes()).toString().replace("-", "");
}
}

public String getMultiValuesID() {
Expand Down

0 comments on commit 441a971

Please sign in to comment.