Initialize ConfigHolder & Settings manually
This commit is contained in:
@@ -32,12 +32,16 @@ import javax.net.ssl.TrustManagerFactory;
|
|||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
|
import org.jackhuang.hmcl.setting.ConfigHolder;
|
||||||
|
|
||||||
public final class Main {
|
public final class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
checkJavaFX();
|
checkJavaFX();
|
||||||
checkDirectoryPath();
|
checkDirectoryPath();
|
||||||
checkDSTRootCAX3();
|
checkDSTRootCAX3();
|
||||||
|
|
||||||
|
ConfigHolder.init();
|
||||||
Launcher.main(args);
|
Launcher.main(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,9 @@ public final class ConfigHolder {
|
|||||||
|
|
||||||
public static final String CONFIG_FILENAME = "hmcl.json";
|
public static final String CONFIG_FILENAME = "hmcl.json";
|
||||||
public static final Path CONFIG_PATH = Paths.get(CONFIG_FILENAME).toAbsolutePath();
|
public static final Path CONFIG_PATH = Paths.get(CONFIG_FILENAME).toAbsolutePath();
|
||||||
private static Config configInstance = initSettings();
|
|
||||||
|
private static Config configInstance;
|
||||||
|
private static boolean initialized;
|
||||||
|
|
||||||
public static Config config() {
|
public static Config config() {
|
||||||
if (configInstance == null) {
|
if (configInstance == null) {
|
||||||
@@ -45,10 +47,17 @@ public final class ConfigHolder {
|
|||||||
return configInstance;
|
return configInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Config upgradeSettings(Config deserialized, Map<?, ?> rawJson) {
|
public static boolean isInitialized() {
|
||||||
if (!rawJson.containsKey("commonDirType"))
|
return initialized;
|
||||||
deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM);
|
}
|
||||||
return deserialized;
|
|
||||||
|
public synchronized static void init() {
|
||||||
|
if (configInstance != null) {
|
||||||
|
throw new IllegalStateException("Configuration is already loaded");
|
||||||
|
}
|
||||||
|
configInstance = initSettings();
|
||||||
|
Settings.init();
|
||||||
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Config initSettings() {
|
private static Config initSettings() {
|
||||||
@@ -59,13 +68,13 @@ public final class ConfigHolder {
|
|||||||
Map<?, ?> raw = new Gson().fromJson(json, Map.class);
|
Map<?, ?> raw = new Gson().fromJson(json, Map.class);
|
||||||
Config deserialized = Config.fromJson(json);
|
Config deserialized = Config.fromJson(json);
|
||||||
if (deserialized == null) {
|
if (deserialized == null) {
|
||||||
LOG.finer("Settings file is empty, use the default settings.");
|
LOG.finer("Config file is empty, use the default config.");
|
||||||
} else {
|
} else {
|
||||||
config = upgradeSettings(deserialized, raw);
|
config = upgradeConfig(deserialized, raw);
|
||||||
}
|
}
|
||||||
LOG.finest("Initialized settings.");
|
LOG.finest("Initialized settings.");
|
||||||
} catch (IOException | JsonParseException e) {
|
} catch (IOException | JsonParseException e) {
|
||||||
LOG.log(Level.WARNING, "Something happened wrongly when load settings.", e);
|
LOG.log(Level.WARNING, "Something went wrong when loading config.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
@@ -80,4 +89,9 @@ public final class ConfigHolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Config upgradeConfig(Config deserialized, Map<?, ?> rawJson) {
|
||||||
|
if (!rawJson.containsKey("commonDirType"))
|
||||||
|
deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM);
|
||||||
|
return deserialized;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,12 +34,22 @@ import static org.jackhuang.hmcl.setting.ConfigHolder.config;
|
|||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
|
|
||||||
private static Settings instance = new Settings();
|
private static Settings instance;
|
||||||
|
|
||||||
public static Settings instance() {
|
public static Settings instance() {
|
||||||
|
if (instance == null) {
|
||||||
|
throw new IllegalStateException("Settings hasn't been initialized");
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should be called from {@link ConfigHolder#init()}.
|
||||||
|
*/
|
||||||
|
static void init() {
|
||||||
|
instance = new Settings();
|
||||||
|
}
|
||||||
|
|
||||||
private final boolean firstLaunch;
|
private final boolean firstLaunch;
|
||||||
|
|
||||||
private Settings() {
|
private Settings() {
|
||||||
|
|||||||
@@ -23,24 +23,19 @@ import java.util.MissingResourceException;
|
|||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import org.jackhuang.hmcl.setting.ConfigHolder;
|
||||||
import org.jackhuang.hmcl.setting.Settings;
|
import org.jackhuang.hmcl.setting.Settings;
|
||||||
|
|
||||||
public final class I18n {
|
public final class I18n {
|
||||||
|
|
||||||
private I18n() {}
|
private I18n() {}
|
||||||
|
|
||||||
private static ResourceBundle RESOURCE_BUNDLE = null;
|
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
RESOURCE_BUNDLE = Settings.instance().getLocale().getResourceBundle();
|
|
||||||
} catch (Throwable e) {
|
|
||||||
LOG.log(Level.SEVERE, "Settings cannot be initialized", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ResourceBundle getResourceBundle() {
|
public static ResourceBundle getResourceBundle() {
|
||||||
return RESOURCE_BUNDLE == null ? Locales.DEFAULT.getResourceBundle() : RESOURCE_BUNDLE;
|
if (ConfigHolder.isInitialized()) {
|
||||||
|
return Settings.instance().getLocale().getResourceBundle();
|
||||||
|
} else {
|
||||||
|
return Locales.DEFAULT.getResourceBundle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String i18n(String key, Object... formatArgs) {
|
public static String i18n(String key, Object... formatArgs) {
|
||||||
|
|||||||
Reference in New Issue
Block a user