Add ConfigHolder.isNewlyCreated()

This commit is contained in:
yushijinhun 2018-07-29 14:02:32 +08:00
parent f8abebf110
commit 49e70e7195
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
2 changed files with 29 additions and 12 deletions

View File

@ -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() {

View File

@ -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);