Add comments to config, make entries static and fix VersionInfo cloning

This commit is contained in:
Johannes Frohnmeyer 2022-09-07 18:06:34 +02:00
parent 165cad8e6b
commit 5cc2b416be
Signed by: Johannes
GPG Key ID: E76429612C2929F4
14 changed files with 167 additions and 94 deletions

View File

@ -1,35 +0,0 @@
package io.gitlab.jfronny.inceptum.common;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.model.inceptum.Config;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
public class ConfigHolder {
public static Config CONFIG;
public static void load() throws IOException {
if (!Files.exists(MetaHolder.CONFIG_PATH.getParent()))
Files.createDirectories(MetaHolder.CONFIG_PATH.getParent());
if (!Files.exists(MetaHolder.CONFIG_PATH)) {
Path gLaunch2 = MetaHolder.BASE_PATH.resolve("glaunch2.json");
if (Files.exists(gLaunch2)) {
Files.move(gLaunch2, MetaHolder.CONFIG_PATH);
} else {
JFiles.writeObject(MetaHolder.CONFIG_PATH, new Config());
}
}
CONFIG = JFiles.readObject(MetaHolder.CONFIG_PATH, Config.class);
}
public static void saveConfig() {
try {
JFiles.writeObject(MetaHolder.CONFIG_PATH, CONFIG);
} catch (IOException e) {
Utils.LOGGER.error("Could not save config", e);
}
}
}

View File

@ -0,0 +1,118 @@
package io.gitlab.jfronny.inceptum.common;
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder;
import io.gitlab.jfronny.gson.stream.*;
import io.gitlab.jfronny.inceptum.common.model.inceptum.UpdateChannel;
import java.io.*;
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 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.getParent()))
Files.createDirectories(MetaHolder.CONFIG_PATH.getParent());
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);
} if (Files.exists(json)) {
Files.move(json, MetaHolder.CONFIG_PATH);
} else {
saveConfig();
}
}
try (Reader reader = Files.newBufferedReader(MetaHolder.CONFIG_PATH);
JsonReader jr = GsonHolder.getGson().newJsonReader(reader)) {
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 "enforceAccount" -> enforceAccount = jr.nextBoolean();
case "lastAccount" -> lastAccount = jr.nextString();
case "offlineAccountLastName" -> offlineAccountLastName = jr.nextString();
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);
skipValue(jr);
}
}
} 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);
}
}
jr.endObject();
}
}
private static void skipValue(JsonReader jr) throws IOException {
switch (jr.peek()) {
case BEGIN_ARRAY -> {
jr.beginArray();
while (jr.peek() != JsonToken.END_ARRAY) skipValue(jr);
jr.endArray();
}
case BEGIN_OBJECT -> {
jr.beginObject();
while (jr.peek() != JsonToken.END_OBJECT) {
jr.nextName();
skipValue(jr);
}
jr.endObject();
}
case STRING -> jr.nextString();
case NUMBER -> jr.nextDouble();
case BOOLEAN -> jr.nextBoolean();
case NULL -> jr.nextNull();
}
}
public static void saveConfig() {
try (Writer writer = Files.newBufferedWriter(MetaHolder.CONFIG_PATH);
JsonWriter jw = GsonHolder.getGson().newJsonWriter(writer)) {
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 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);
}
}
}

View File

@ -12,7 +12,7 @@ public class InceptumEnvironmentInitializer {
Logger.registerFactory(InceptumEnvironmentInitializer::defaultFactory);
HttpUtils.setUserAgent("jfmods/inceptum/" + BuildMetadata.VERSION);
GsonHolder.register();
ConfigHolder.load();
InceptumConfig.load();
}
public static Logger defaultFactory(String name) {

View File

@ -19,7 +19,7 @@ public class MetaHolder {
}
public static final Path ACCOUNTS_PATH = BASE_PATH.resolve("accounts.json");
public static final Path CONFIG_PATH = BASE_PATH.resolve("inceptum.json");
public static final Path CONFIG_PATH = BASE_PATH.resolve("inceptum.json5");
public static final Path WRAPPER_CONFIG_PATH = BASE_PATH.resolve("wrapper.json");
public static final Path NATIVES_DIR = BASE_PATH.resolve("natives");
public static final Path FORCE_LOAD_PATH = NATIVES_DIR.resolve("forceload");

View File

@ -27,10 +27,10 @@ public class Updater {
private static final String PROJECT_MAVEN = "https://gitlab.com/api/v4/projects/" + PROJECT_ID + "/packages/maven/";
public static UpdateMetadata getUpdate() {
return Updater.check(ConfigHolder.CONFIG.channel, BuildMetadata.VERSION, channel -> {
return Updater.check(InceptumConfig.channel, BuildMetadata.VERSION, channel -> {
Utils.LOGGER.error("No stable version was found, switching to experimental channel");
ConfigHolder.CONFIG.channel = channel;
ConfigHolder.saveConfig();
InceptumConfig.channel = channel;
InceptumConfig.saveConfig();
});
}

View File

@ -1,13 +0,0 @@
package io.gitlab.jfronny.inceptum.common.model.inceptum;
import java.util.Map;
public class Config {
public boolean snapshots = false;
public boolean darkTheme = false;
public boolean enforceAccount = true;
public String lastAccount;
public String offlineAccountLastName = null;
public UpdateChannel channel = UpdateChannel.Stable;
public String authorName = "Inceptum";
}

View File

@ -9,16 +9,18 @@ All of these are subject to change, though automatic migrations will likely be p
{
// Whether to show snapshots in the version selector for new instances
"snapshots": false,
// Whether to launch the GUI in dark mode.
// Whether to launch the GUI in dark mode
// Configurable in Settings->Dark Theme
"darkTheme": true,
// Whether to require an account to launch the game.
// Whether to require an account to launch the game
// Intended to allow running the game from USB sticks on constrained networks
"enforceAccount": false,
// The currently selected account.
// The currently selected account
// Used to launch the game
"lastAccount": "some UUID",
// The update channel. Either "CI" or "Stable".
// The last name used for an offline session
"offlineAccountLastName": "some name",
// The update channel. Either "CI" or "Stable"
// I personally recommend the CI channel as it gets the latest fixes and features quicker
"channel": "CI",
// The author name to add to packs where the metadata format requires specifying one

View File

@ -237,7 +237,7 @@ public class GuiMain {
}
public static void applyTheme() {
if (ConfigHolder.CONFIG.darkTheme) ImGui.styleColorsDark();
if (InceptumConfig.darkTheme) ImGui.styleColorsDark();
else ImGui.styleColorsLight();
}
}

View File

@ -27,7 +27,7 @@ public class InstanceManageControls {
private final ImInt version = new ImInt(0);
private final ImString name = new ImString("", GuiUtil.INPUT_FIELD_LENGTH);
private final ImInt fabricVersion = new ImInt(0);
private final ImBoolean snapshots = new ImBoolean(ConfigHolder.CONFIG.snapshots);
private final ImBoolean snapshots = new ImBoolean(InceptumConfig.snapshots);
private final ImBoolean fabric = new ImBoolean(true);
private VersionsListInfo selected;
@ -70,22 +70,22 @@ public class InstanceManageControls {
public void snapshotsBox() {
if (ImGui.checkbox("Show snapshots", snapshots)) {
boolean prev = ConfigHolder.CONFIG.snapshots;
ConfigHolder.CONFIG.snapshots = snapshots.get();
ConfigHolder.saveConfig();
boolean prev = InceptumConfig.snapshots;
InceptumConfig.snapshots = snapshots.get();
InceptumConfig.saveConfig();
//fix version index
int i = getVersions(ConfigHolder.CONFIG.snapshots).indexOf(getVersions(prev).get(version.get()));
int i = getVersions(InceptumConfig.snapshots).indexOf(getVersions(prev).get(version.get()));
if (i == -1) version.set(0);
else version.set(i);
}
}
public void versionBox(Consumer<String> modifiedVersion) {
List<VersionsListInfo> vil = getVersions(ConfigHolder.CONFIG.snapshots);
List<VersionsListInfo> vil = getVersions(InceptumConfig.snapshots);
String originalStr = null;
try {
originalStr = getVersionInfo().id;
} catch (IOException e) {
} catch (Throwable e) {
Utils.LOGGER.error("Could not get version string", e);
}
if (ImGui.combo("Version", version, vil.stream().map(info -> info.id).toArray(String[]::new))) {

View File

@ -17,7 +17,7 @@ import java.nio.file.Path;
import java.util.List;
public class MainWindow extends Window {
private final ImBoolean darkTheme = new ImBoolean(ConfigHolder.CONFIG.darkTheme);
private final ImBoolean darkTheme = new ImBoolean(InceptumConfig.darkTheme);
private final ImBoolean debugTools = new ImBoolean(false);
private final ImInt accountIndex = new ImInt(AccountManager.getSelectedIndex());
@ -70,8 +70,8 @@ public class MainWindow extends Window {
}
if (ImGui.beginMenu("Settings")) {
if (ImGui.checkbox("Dark Theme", darkTheme)) {
ConfigHolder.CONFIG.darkTheme = darkTheme.get();
ConfigHolder.saveConfig();
InceptumConfig.darkTheme = darkTheme.get();
InceptumConfig.saveConfig();
GuiMain.applyTheme();
}
ImGui.endMenu();

View File

@ -8,6 +8,7 @@ import io.gitlab.jfronny.inceptum.common.*;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@ -58,7 +59,7 @@ public class AccountManager {
}
for (MicrosoftAccount account : ACCOUNTS) {
account.refreshAccessToken();
if (account.accountId.equalsIgnoreCase(ConfigHolder.CONFIG.lastAccount)) {
if (account.accountId.equalsIgnoreCase(InceptumConfig.lastAccount)) {
SELECTED_ACCOUNT = account;
}
}
@ -88,13 +89,13 @@ public class AccountManager {
if (account == null) {
Utils.LOGGER.info("Logging out");
SELECTED_ACCOUNT = null;
ConfigHolder.CONFIG.lastAccount = null;
InceptumConfig.lastAccount = null;
} else {
Utils.LOGGER.info("Changed account to " + account);
SELECTED_ACCOUNT = account;
ConfigHolder.CONFIG.lastAccount = account.accountId;
InceptumConfig.lastAccount = account.accountId;
}
ConfigHolder.saveConfig();
InceptumConfig.saveConfig();
}
public static Object getAccountByName(String username) {

View File

@ -17,13 +17,13 @@ public class VersionInfo extends VersionsListInfo implements Cloneable {
@Override
public VersionInfo clone() {
VersionInfo clone = new VersionInfo();
clone.arguments = arguments.clone();
clone.assetIndex = assetIndex.clone();
if (arguments != null) clone.arguments = arguments.clone();
if (assetIndex != null) clone.assetIndex = assetIndex.clone();
clone.assets = assets;
clone.downloads = downloads.clone();
clone.javaVersion = javaVersion.clone();
if (downloads != null) clone.downloads = downloads.clone();
if (javaVersion != null) clone.javaVersion = javaVersion.clone();
clone.libraries = new LinkedList<>();
for (Library library : libraries) clone.libraries.add(library.clone());
if (libraries != null) libraries.forEach(library -> clone.libraries.add(library.clone()));
clone.mainClass = mainClass;
clone.minecraftArguments = minecraftArguments;
clone.minimumLauncherVersion = minimumLauncherVersion;
@ -38,9 +38,9 @@ public class VersionInfo extends VersionsListInfo implements Cloneable {
public Arguments clone() {
Arguments clone = new Arguments();
clone.game = new LinkedList<>();
for (MinecraftArgument argument : game) clone.game.add(argument.clone());
if (game != null) game.forEach(argument -> clone.game.add(argument.clone()));
clone.jvm = new LinkedList<>();
for (MinecraftArgument argument : jvm) clone.jvm.add(argument.clone());
if (jvm != null) jvm.forEach(argument -> clone.jvm.add(argument.clone()));
return clone;
}
}
@ -69,9 +69,9 @@ public class VersionInfo extends VersionsListInfo implements Cloneable {
public Downloads clone() {
Downloads clone = new Downloads();
clone.client = client.clone();
clone.client_mappings = client_mappings.clone();
if (client_mappings != null) clone.client_mappings = client_mappings.clone();
clone.server = server.clone();
clone.server_mappings = server_mappings.clone();
if (server_mappings != null) clone.server_mappings = server_mappings.clone();
return clone;
}
}
@ -98,11 +98,11 @@ public class VersionInfo extends VersionsListInfo implements Cloneable {
@Override
public Library clone() {
Library clone = new Library();
clone.downloads = downloads.clone();
if (downloads != null) clone.downloads = downloads.clone();
clone.name = name;
clone.natives = new LinkedHashMap<>();
clone.natives.putAll(natives);
clone.rules = rules.clone();
if (natives != null) clone.natives.putAll(natives);
if (rules != null) clone.rules = rules.clone();
return clone;
}
@ -113,9 +113,9 @@ public class VersionInfo extends VersionsListInfo implements Cloneable {
@Override
public Downloads clone() {
Downloads clone = new Downloads();
clone.artifact = artifact.clone();
if (artifact != null) clone.artifact = artifact.clone();
clone.classifiers = new LinkedHashMap<>();
classifiers.forEach((key, value) -> clone.classifiers.put(key, value.clone()));
if (classifiers != null) classifiers.forEach((key, value) -> clone.classifiers.put(key, value.clone()));
return clone;
}

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.inceptum.launcher.system.export;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.ConfigHolder;
import io.gitlab.jfronny.inceptum.common.InceptumConfig;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.launcher.model.curseforge.CurseforgeModpackManifest;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
@ -46,7 +46,7 @@ public class InstanceExporter {
manifest.manifestVersion = 1;
manifest.name = instanceDir.getFileName().toString();
manifest.version = version;
manifest.author = ConfigHolder.CONFIG.authorName;
manifest.author = InceptumConfig.authorName;
manifest.overrides = OVERRIDES_DIR_DEFAULT;
Path overrides = fs.getPath(OVERRIDES_DIR_DEFAULT);
Files.createDirectories(overrides);

View File

@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class InstanceLauncher {
public static void launchClient(Path path, InstanceMeta instance) {
if (AccountManager.accountMissing() && ConfigHolder.CONFIG.enforceAccount) {
if (AccountManager.accountMissing() && InceptumConfig.enforceAccount) {
LauncherEnv.showError("You have not set up an account.\nDoing so is required to play Minecraft", "Not authenticated");
return;
}
@ -32,11 +32,11 @@ public class InstanceLauncher {
if (authInfo.equals(AccountManager.NULL_AUTH)) {
try {
String sysUser = System.getProperty("user.name");
if (ConfigHolder.CONFIG.offlineAccountLastName == null)
ConfigHolder.CONFIG.offlineAccountLastName = sysUser;
LauncherEnv.getInput("User name", "Please enter the username to use for this session", ConfigHolder.CONFIG.offlineAccountLastName, name -> {
ConfigHolder.CONFIG.offlineAccountLastName = name.equals(sysUser) ? null : name;
ConfigHolder.saveConfig();
if (InceptumConfig.offlineAccountLastName == null)
InceptumConfig.offlineAccountLastName = sysUser;
LauncherEnv.getInput("User name", "Please enter the username to use for this session", InceptumConfig.offlineAccountLastName, name -> {
InceptumConfig.offlineAccountLastName = name.equals(sysUser) ? null : name;
InceptumConfig.saveConfig();
AuthInfo infoNew = new AuthInfo(name, authInfo.uuid(), authInfo.accessToken(), authInfo.userType());
launchClient(path, instance, infoNew);
}, R::nop);