mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-18 08:16:58 -04:00
Add ConfigHolder.isNewlyCreated()
This commit is contained in:
parent
f8abebf110
commit
49e70e7195
@ -39,6 +39,7 @@ public final class ConfigHolder {
|
|||||||
|
|
||||||
private static Config configInstance;
|
private static Config configInstance;
|
||||||
private static boolean initialized;
|
private static boolean initialized;
|
||||||
|
private static boolean newlyCreated;
|
||||||
|
|
||||||
public static Config config() {
|
public static Config config() {
|
||||||
if (configInstance == null) {
|
if (configInstance == null) {
|
||||||
@ -51,33 +52,49 @@ public final class ConfigHolder {
|
|||||||
return initialized;
|
return initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isNewlyCreated() {
|
||||||
|
if (configInstance == null) {
|
||||||
|
throw new IllegalStateException("Configuration hasn't been loaded");
|
||||||
|
}
|
||||||
|
return newlyCreated;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized static void init() {
|
public synchronized static void init() {
|
||||||
if (configInstance != null) {
|
if (configInstance != null) {
|
||||||
throw new IllegalStateException("Configuration is already loaded");
|
throw new IllegalStateException("Configuration is already loaded");
|
||||||
}
|
}
|
||||||
configInstance = initSettings();
|
|
||||||
|
configInstance = loadConfig();
|
||||||
|
configInstance.addListener(source -> saveConfig());
|
||||||
|
|
||||||
Settings.init();
|
Settings.init();
|
||||||
|
|
||||||
|
if (newlyCreated) {
|
||||||
|
saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Config initSettings() {
|
private static Config loadConfig() {
|
||||||
Config config = new Config();
|
|
||||||
if (Files.exists(CONFIG_PATH)) {
|
if (Files.exists(CONFIG_PATH)) {
|
||||||
try {
|
try {
|
||||||
String json = new String(Files.readAllBytes(CONFIG_PATH), UTF_8);
|
String content = new String(Files.readAllBytes(CONFIG_PATH), UTF_8);
|
||||||
Map<?, ?> raw = new Gson().fromJson(json, Map.class);
|
Config deserialized = Config.fromJson(content);
|
||||||
Config deserialized = Config.fromJson(json);
|
|
||||||
if (deserialized == null) {
|
if (deserialized == null) {
|
||||||
LOG.finer("Config file is empty, use the default config.");
|
LOG.info("Config is empty");
|
||||||
} else {
|
} 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) {
|
} catch (IOException | JsonParseException e) {
|
||||||
LOG.log(Level.WARNING, "Something went wrong when loading config.", 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() {
|
static void saveConfig() {
|
||||||
|
@ -55,6 +55,7 @@ import javafx.stage.StageStyle;
|
|||||||
import javafx.util.Duration;
|
import javafx.util.Duration;
|
||||||
import org.jackhuang.hmcl.Launcher;
|
import org.jackhuang.hmcl.Launcher;
|
||||||
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDnD;
|
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDnD;
|
||||||
|
import org.jackhuang.hmcl.setting.ConfigHolder;
|
||||||
import org.jackhuang.hmcl.setting.EnumBackgroundImage;
|
import org.jackhuang.hmcl.setting.EnumBackgroundImage;
|
||||||
import org.jackhuang.hmcl.setting.Settings;
|
import org.jackhuang.hmcl.setting.Settings;
|
||||||
import org.jackhuang.hmcl.setting.Theme;
|
import org.jackhuang.hmcl.setting.Theme;
|
||||||
@ -69,7 +70,6 @@ import org.jackhuang.hmcl.ui.wizard.*;
|
|||||||
import org.jackhuang.hmcl.util.Lang;
|
import org.jackhuang.hmcl.util.Lang;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.InvalidPathException;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -200,7 +200,7 @@ public final class Decorator extends StackPane implements TaskExecutorDialogWiza
|
|||||||
);
|
);
|
||||||
nowAnimation.play();
|
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);
|
drawerWrapper.getChildren().remove(welcomeView);
|
||||||
|
|
||||||
if (!min) buttonsContainer.getChildren().remove(btnMin);
|
if (!min) buttonsContainer.getChildren().remove(btnMin);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user