Inceptum/common/src/main/java/io/gitlab/jfronny/inceptum/common/InceptumConfig.java

109 lines
5.1 KiB
Java

package io.gitlab.jfronny.inceptum.common;
import io.gitlab.jfronny.gson.stream.*;
import io.gitlab.jfronny.inceptum.common.model.inceptum.UpdateChannel;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class InceptumConfig {
public static boolean snapshots = false;
public static boolean darkTheme = false;
public static boolean listView = false;
public static boolean enforceAccount = true;
public static String lastAccount = null;
public static String offlineAccountLastName = null;
public static UpdateChannel channel = UpdateChannel.Stable;
public static String authorName = "Inceptum";
public static void load() throws IOException {
if (!Files.exists(MetaHolder.CONFIG_PATH.parent))
Files.createDirectories(MetaHolder.CONFIG_PATH.parent);
if (!Files.exists(MetaHolder.CONFIG_PATH)) {
Path gLaunch2 = MetaHolder.BASE_PATH.resolve("glaunch2.json");
Path json = MetaHolder.BASE_PATH.resolve("inceptum.json");
if (Files.exists(gLaunch2)) {
Files.move(gLaunch2, MetaHolder.CONFIG_PATH);
} else if (Files.exists(json)) {
Files.move(json, MetaHolder.CONFIG_PATH);
} else {
saveConfig();
}
}
try (JsonReader jr = new JsonReader(Files.newBufferedReader(MetaHolder.CONFIG_PATH))) {
GsonPreset.Config.configure(jr);
jr.beginObject();
while (jr.peek() != JsonToken.END_OBJECT) {
String name = null;
try {
name = jr.nextName();
switch (name) {
case "snapshots" -> snapshots = jr.nextBoolean();
case "darkTheme" -> darkTheme = jr.nextBoolean();
case "listView" -> listView = jr.nextBoolean();
case "enforceAccount" -> enforceAccount = jr.nextBoolean();
case "lastAccount" -> lastAccount = nullableString(jr);
case "offlineAccountLastName" -> offlineAccountLastName = nullableString(jr);
case "channel" -> {
try {
channel = UpdateChannel.valueOf(jr.nextString());
} catch (IllegalArgumentException e) {
Utils.LOGGER.error("Could not read channel", e);
}
}
case "authorName" -> authorName = jr.nextString();
default -> {
Utils.LOGGER.error("Unexpected entry name: " + name);
jr.skipValue();
}
}
} catch (Throwable t) {
if (name == null) Utils.LOGGER.error("Could not read config entry", t);
else Utils.LOGGER.error("Could not read config entry: " + name, t);
return;
}
}
jr.endObject();
}
}
public static void saveConfig() {
try (JsonWriter jw = new JsonWriter(Files.newBufferedWriter(MetaHolder.CONFIG_PATH))) {
GsonPreset.Config.configure(jw);
jw.beginObject()
.comment("Whether to show snapshots in the version selector for new instances")
.name("snapshots").value(snapshots)
.comment("Whether to launch the GUI in dark mode")
.comment("Configurable in Settings->Dark Theme")
.name("darkTheme").value(darkTheme)
.comment("Whether the GTK UI should default to a list view instead of a grid")
.name("listView").value(listView)
.comment("Whether to require an account to launch the game")
.comment("Intended to allow running the game from USB sticks on constrained networks")
.name("enforceAccount").value(enforceAccount)
.comment("The currently selected account")
.comment("Used to launch the game")
.name("lastAccount").value(lastAccount)
.comment("The last name used for an offline session")
.name("offlineAccountLastName").value(offlineAccountLastName)
.comment("The update channel. Either \"CI\" or \"Stable\"")
.comment("I personally recommend the CI channel as it gets the latest fixes and features quicker")
.name("channel").value(channel.toString())
.comment("The author name to add to packs where the metadata format requires specifying one")
.name("authorName").value(authorName)
.endObject();
} catch (IOException e) {
Utils.LOGGER.error("Could not save config", e);
}
}
private static String nullableString(JsonReader jr) throws IOException {
if (jr.peek() == JsonToken.NULL) {
jr.nextNull();
return null;
}
return jr.nextString();
}
}