This commit is contained in:
yuhuihuang 2020-07-19 16:36:24 +08:00
parent 55d78aeba7
commit 00c350cd82
2 changed files with 26 additions and 1 deletions

View File

@ -40,6 +40,7 @@ import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import static org.jackhuang.hmcl.ui.FXUtils.runInFX;
import static org.jackhuang.hmcl.util.Logging.LOG;
@ -55,6 +56,7 @@ public final class Launcher extends Application {
try {
ConfigHolder.init();
} catch (IOException e) {
LOG.log(Level.SEVERE, "Failed to load config", e);
Main.showErrorAndExit(i18n("fatal.config_loading_failure", Paths.get("").toAbsolutePath().normalize()));
}

View File

@ -62,6 +62,9 @@ public final class ConfigHolder {
}
configLocation = locateConfig();
LOG.log(Level.INFO, "Config location: " + configLocation);
configInstance = loadConfig();
configInstance.addListener(source -> markConfigDirty());
@ -88,6 +91,26 @@ public final class ConfigHolder {
}
private static Path locateConfig() {
Path exePath = Paths.get("");
try {
Path jarPath = Paths.get(ConfigHolder.class.getProtectionDomain().getCodeSource().getLocation()
.toURI()).toAbsolutePath();
if (Files.isRegularFile(jarPath)) {
jarPath = jarPath.getParent();
exePath = jarPath;
Path config = jarPath.resolve(CONFIG_FILENAME);
if (Files.isRegularFile(config))
return config;
Path dotConfig = jarPath.resolve(CONFIG_FILENAME_LINUX);
if (Files.isRegularFile(dotConfig))
return dotConfig;
}
} catch (Throwable ignore) {
}
Path config = Paths.get(CONFIG_FILENAME);
if (Files.isRegularFile(config))
return config;
@ -97,7 +120,7 @@ public final class ConfigHolder {
return dotConfig;
// create new
return OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? config : dotConfig;
return exePath.resolve(OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? CONFIG_FILENAME : CONFIG_FILENAME_LINUX);
}
private static Config loadConfig() throws IOException {