From 8c9309b66fc73dc08b88dd09c387ccc4295b33dd Mon Sep 17 00:00:00 2001 From: Thalia Nero Date: Sat, 12 Mar 2022 22:29:25 -0600 Subject: [PATCH] Some fixes, warnings, and optimization. --- .../colormatic/properties/ColormapProperties.java | 10 +++++++--- .../kvverti/colormatic/properties/PropertyUtil.java | 10 +++++----- .../properties/adapter/GridEntryAdapter.java | 5 ++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/kvverti/colormatic/properties/ColormapProperties.java b/src/main/java/io/github/kvverti/colormatic/properties/ColormapProperties.java index 61c3c2d..6df06a7 100644 --- a/src/main/java/io/github/kvverti/colormatic/properties/ColormapProperties.java +++ b/src/main/java/io/github/kvverti/colormatic/properties/ColormapProperties.java @@ -339,8 +339,9 @@ private static ColormapProperties loadFromJson(Reader json, Identifier id, boole if(settings == null) { settings = new Settings(); } - } catch(JsonSyntaxException e) { - log.error("Error parsing {}: {}", id, e.getMessage()); + } catch(Exception e) { + // any one of a number of exceptions could have been thrown during deserialization + log.error("Error loading {}: {}", id, e.getMessage()); settings = new Settings(); } if(settings.format == null) { @@ -361,7 +362,10 @@ private static ColormapProperties loadFromJson(Reader json, Identifier id, boole } } } else { - // disable `blocks`, `grid`, and `biomes` for non-custom colormaps + // disable `blocks`, `grid`, and `biomes` for non-custom colormaps, warn if they are present + if(settings.biomes != null || settings.grid != null || settings.blocks != null) { + log.warn("{}: found `biomes`, `grid`, or `blocks` properties in a provided colormap; these will be ignored", id); + } settings.biomes = null; settings.grid = null; settings.blocks = Collections.emptyList(); diff --git a/src/main/java/io/github/kvverti/colormatic/properties/PropertyUtil.java b/src/main/java/io/github/kvverti/colormatic/properties/PropertyUtil.java index 0873d9b..9da594e 100644 --- a/src/main/java/io/github/kvverti/colormatic/properties/PropertyUtil.java +++ b/src/main/java/io/github/kvverti/colormatic/properties/PropertyUtil.java @@ -177,11 +177,11 @@ public static PropertyImage loadColormap(ResourceManager manager, Identifier id, } try(Resource rsc = manager.getResource(props.getSource()); InputStream in = rsc.getInputStream()) { NativeImage image = NativeImage.read(in); - for(int x = 0; x < image.getWidth(); x++) { - for(int y = 0; y < image.getHeight(); y++) { - if(ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) { - // swap the red and blue channels of every pixel, because the biome - // colormap expects ARGB, but NativeImage is ABGR + if(ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) { + // swap the red and blue channels of every pixel, because the biome + // colormap expects ARGB, but NativeImage is ABGR + for(int x = 0; x < image.getWidth(); x++) { + for(int y = 0; y < image.getHeight(); y++) { int pix = image.getColor(x, y); int tmp = (pix & 0xff0000) >> 16; tmp |= (pix & 0x0000ff) << 16; diff --git a/src/main/java/io/github/kvverti/colormatic/properties/adapter/GridEntryAdapter.java b/src/main/java/io/github/kvverti/colormatic/properties/adapter/GridEntryAdapter.java index 8b68a9e..7bd1d24 100644 --- a/src/main/java/io/github/kvverti/colormatic/properties/adapter/GridEntryAdapter.java +++ b/src/main/java/io/github/kvverti/colormatic/properties/adapter/GridEntryAdapter.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import com.google.common.collect.ImmutableList; +import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; @@ -43,7 +44,8 @@ public void write(JsonWriter jsonWriter, GridEntry gridEntry) { public GridEntry read(JsonReader in) throws IOException { switch(in.peek()) { case NULL -> { - return null; + in.nextNull(); + throw new JsonSyntaxException("required nonnull"); } case STRING -> { var biomeId = this.idAdapter.read(in); @@ -66,6 +68,7 @@ public GridEntry read(JsonReader in) throws IOException { } case "column" -> gridEntry.column = in.nextInt(); case "width" -> gridEntry.width = in.nextInt(); + default -> in.skipValue(); } } in.endObject();