Skip to content

Commit

Permalink
Add Gson and use it for resource pack loading. (#1311)
Browse files Browse the repository at this point in the history
* Add GSON

* Add licensing info for gson.

* Switch resource pack loading to use GSON.

* Add gson copyright notice.

Co-authored-by: Maik Marschner <[email protected]>
  • Loading branch information
ThatRedox and leMaik authored Jun 17, 2022
1 parent 258fdfc commit 2e7143b
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 379 deletions.
460 changes: 236 additions & 224 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions chunky/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ configurations {
dependencies {
implementation 'it.unimi.dsi:fastutil:8.4.4'
implementation 'org.apache.commons:commons-math3:3.2'
implementation 'com.google.code.gson:gson:2.9.0'
implementation project(':lib')

testImplementation 'com.google.truth:truth:1.1.3'
Expand Down
Binary file added chunky/lib/gson-2.9.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,42 @@
*/
package se.llbit.chunky.resources;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import se.llbit.chunky.world.biome.Biome;
import se.llbit.chunky.world.biome.BiomeBuilder;
import se.llbit.chunky.world.biome.Biomes;
import se.llbit.json.JsonObject;
import se.llbit.json.JsonParser;
import se.llbit.json.JsonValue;
import se.llbit.log.Log;
import se.llbit.util.JsonPreprocessor;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Stream;

public class ResourcePackBiomeLoader implements ResourcePackLoader.PackLoader {
public ResourcePackBiomeLoader() {}

protected static final Gson GSON = new GsonBuilder()
.disableJdkUnsafe()
.setLenient()
.create();

protected static class BiomeJson {
public double temperature = 0.5;
public double downfall = 0.5;
public BiomeEffects effects = new BiomeEffects();
}

protected static class BiomeEffects {
public Integer foliage_color = null;
public Integer grass_color = null;
public Integer water_color = null;
public String grass_color_modifier = null;
}

@Override
public boolean load(Path pack, String baseName) {
Path data = pack.resolve("data");
Expand All @@ -56,42 +71,31 @@ public boolean load(Path pack, String baseName) {
String resourceLocation = namespace + ":" + biomeName;

if (!Biomes.contains(resourceLocation)) {
try (InputStream f = new BufferedInputStream(Files.newInputStream(biome))) {
JsonValue v = JsonPreprocessor.parse(f);
JsonObject root = v.asObject();

double temperature = root.get("temperature").doubleValue(0.5);
double rain = root.get("downfall").doubleValue(0.5);
BiomeBuilder builder = Biome.create(resourceLocation, biomeName, temperature, rain);
try (Reader f = Files.newBufferedReader(biome)) {
BiomeJson json = GSON.fromJson(f, BiomeJson.class);

JsonObject effects = root.get("effects").asObject();
JsonValue t;
if (!(t = effects.get("foliage_color")).isUnknown()) {
builder.foliageColor(t.intValue(0));
}
if (!(t = effects.get("grass_color")).isUnknown()) {
builder.grassColor(t.intValue(0));
}
if (!(t = effects.get("water_color")).isUnknown()) {
builder.waterColor(t.intValue(0));
}
if (!(t = effects.get("grass_color_modifier")).isUnknown()) {
String modifier = t.asString("none").toLowerCase();
switch (modifier) {
BiomeBuilder builder = Biome.create(resourceLocation, biomeName, json.temperature, json.downfall);
Optional.ofNullable(json.effects.foliage_color).ifPresent(builder::foliageColor);
Optional.ofNullable(json.effects.grass_color).ifPresent(builder::grassColor);
Optional.ofNullable(json.effects.water_color).ifPresent(builder::waterColor);
Optional.ofNullable(json.effects.grass_color_modifier).ifPresent(modifier -> {
switch (modifier.toLowerCase()) {
case "none":
break;
case "dark_forest":
builder.darkForest();
break;
case "swamp":
builder.swamp();
break;
default:
Log.warnf("Unsupported biome `grass_color_modifier`: %s", modifier);
Log.warnf("Unsupported biome `grass_modifier_color`: %s", modifier);
}
}
});
// TODO Custom fog colors

Biomes.register(builder);
} catch (IOException | JsonParser.SyntaxError ignored) {
} catch (IOException ignored) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public class CreditsController implements Initializable {
@FXML
private Hyperlink fastutilLicense;
@FXML
private Hyperlink gson;
@FXML
private Hyperlink gsonLicense;
@FXML
private Hyperlink simplexnoise;
@FXML
private Hyperlink simplexnoiseLicense;
Expand Down Expand Up @@ -159,6 +163,11 @@ public void initialize(URL location, ResourceBundle resources) {
fastutilLicense.setBorder(Border.EMPTY);
fastutilLicense.setOnAction(e -> launchAndReset(fastutilLicense, "https://www.apache.org/licenses/LICENSE-2.0"));

gson.setBorder(Border.EMPTY);
gson.setOnAction(e -> launchAndReset(gson, "https://github.com/google/gson"));
gsonLicense.setBorder(Border.EMPTY);
gsonLicense.setOnAction(e -> launchAndReset(gsonLicense, "https://www.apache.org/licenses/LICENSE-2.0.txt"));

simplexnoise.setBorder(Border.EMPTY);
simplexnoise.setOnAction(e -> launchAndReset(simplexnoise, "https://github.com/keijiro/sketches2016/blob/master/Simplex2/SimplexNoise.java"));
simplexnoiseLicense.setBorder(Border.EMPTY);
Expand Down
14 changes: 14 additions & 0 deletions chunky/src/res/se/llbit/chunky/ui/dialogs/Credits.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@
</padding>
</Hyperlink>

<Hyperlink fx:id="gson" text="Gson">
<padding>
<Insets/>
</padding>
</Hyperlink>
<Hyperlink fx:id="gsonLicense" text="Apache License 2.0">
<font>
<Font size="10.0"/>
</font>
<padding>
<Insets left="20.0"/>
</padding>
</Hyperlink>

<Hyperlink fx:id="simplexnoise" text="Simplex noise">
<padding>
<Insets/>
Expand Down
33 changes: 0 additions & 33 deletions chunky/src/test/se/llbit/util/PreProcessorTest.java

This file was deleted.

91 changes: 0 additions & 91 deletions lib/src/se/llbit/util/JsonPreprocessor.java

This file was deleted.

13 changes: 13 additions & 0 deletions licenses/gson.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

0 comments on commit 2e7143b

Please sign in to comment.