diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigHolder.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigHolder.java index 46df2666b..2643f7647 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigHolder.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigHolder.java @@ -39,6 +39,7 @@ public final class ConfigHolder { private static Config configInstance; private static boolean initialized; + private static boolean newlyCreated; public static Config config() { if (configInstance == null) { @@ -51,33 +52,49 @@ public final class ConfigHolder { return initialized; } + public static boolean isNewlyCreated() { + if (configInstance == null) { + throw new IllegalStateException("Configuration hasn't been loaded"); + } + return newlyCreated; + } + public synchronized static void init() { if (configInstance != null) { throw new IllegalStateException("Configuration is already loaded"); } - configInstance = initSettings(); + + configInstance = loadConfig(); + configInstance.addListener(source -> saveConfig()); + Settings.init(); + + if (newlyCreated) { + saveConfig(); + } + initialized = true; } - private static Config initSettings() { - Config config = new Config(); + private static Config loadConfig() { if (Files.exists(CONFIG_PATH)) { try { - String json = new String(Files.readAllBytes(CONFIG_PATH), UTF_8); - Map raw = new Gson().fromJson(json, Map.class); - Config deserialized = Config.fromJson(json); + String content = new String(Files.readAllBytes(CONFIG_PATH), UTF_8); + Config deserialized = Config.fromJson(content); if (deserialized == null) { - LOG.finer("Config file is empty, use the default config."); + LOG.info("Config is empty"); } else { - config = upgradeConfig(deserialized, raw); + Map raw = new Gson().fromJson(content, Map.class); + return upgradeConfig(deserialized, raw); } - LOG.finest("Initialized settings."); } catch (IOException | JsonParseException e) { LOG.log(Level.WARNING, "Something went wrong when loading config.", e); } } - return config; + + LOG.info("Creating an empty config"); + newlyCreated = true; + return new Config(); } static void saveConfig() { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Decorator.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Decorator.java index 29542487d..966fd3e38 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Decorator.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Decorator.java @@ -55,6 +55,7 @@ import javafx.stage.StageStyle; import javafx.util.Duration; import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDnD; +import org.jackhuang.hmcl.setting.ConfigHolder; import org.jackhuang.hmcl.setting.EnumBackgroundImage; import org.jackhuang.hmcl.setting.Settings; import org.jackhuang.hmcl.setting.Theme; @@ -69,7 +70,6 @@ import org.jackhuang.hmcl.ui.wizard.*; import org.jackhuang.hmcl.util.Lang; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; @@ -200,7 +200,7 @@ public final class Decorator extends StackPane implements TaskExecutorDialogWiza ); nowAnimation.play(); }); - if (/* TODO: is not first launch or */Settings.instance().getLocale().getLocale() != Locale.CHINA) + if (!ConfigHolder.isNewlyCreated() || Settings.instance().getLocale().getLocale() != Locale.CHINA) drawerWrapper.getChildren().remove(welcomeView); if (!min) buttonsContainer.getChildren().remove(btnMin);