mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-10 04:16:02 -04:00
Fix config file is checked before created
This commit is contained in:
parent
4c9cb99a94
commit
08a7c27353
@ -28,10 +28,10 @@ import java.util.Map;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import org.jackhuang.hmcl.util.InvocationDispatcher;
|
import org.jackhuang.hmcl.util.InvocationDispatcher;
|
||||||
|
import org.jackhuang.hmcl.util.Lang;
|
||||||
import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
import static org.jackhuang.hmcl.util.Lang.thread;
|
|
||||||
import static org.jackhuang.hmcl.util.Logging.LOG;
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
|
||||||
public final class ConfigHolder {
|
public final class ConfigHolder {
|
||||||
@ -45,19 +45,6 @@ public final class ConfigHolder {
|
|||||||
private static Config configInstance;
|
private static Config configInstance;
|
||||||
private static boolean newlyCreated;
|
private static boolean newlyCreated;
|
||||||
|
|
||||||
private static InvocationDispatcher<String> configWriter = new InvocationDispatcher<>(content -> {
|
|
||||||
thread(() -> {
|
|
||||||
LOG.info("Saving config");
|
|
||||||
try {
|
|
||||||
synchronized (configLocation) {
|
|
||||||
Files.write(configLocation, content.get().getBytes(UTF_8));
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.log(Level.SEVERE, "Failed to save config", e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
public static Config config() {
|
public static Config config() {
|
||||||
if (configInstance == null) {
|
if (configInstance == null) {
|
||||||
throw new IllegalStateException("Configuration hasn't been loaded");
|
throw new IllegalStateException("Configuration hasn't been loaded");
|
||||||
@ -84,7 +71,7 @@ public final class ConfigHolder {
|
|||||||
Settings.init();
|
Settings.init();
|
||||||
|
|
||||||
if (newlyCreated) {
|
if (newlyCreated) {
|
||||||
markConfigDirty();
|
saveConfigSync();
|
||||||
|
|
||||||
// hide the config file on windows
|
// hide the config file on windows
|
||||||
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
|
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
|
||||||
@ -138,7 +125,26 @@ public final class ConfigHolder {
|
|||||||
return new Config();
|
return new Config();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static InvocationDispatcher<String> configWriter = InvocationDispatcher.runOn(Lang::thread, content -> {
|
||||||
|
try {
|
||||||
|
writeToConfig(content);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.log(Level.SEVERE, "Failed to save config", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
private static void writeToConfig(String content) throws IOException {
|
||||||
|
LOG.info("Saving config");
|
||||||
|
synchronized (configLocation) {
|
||||||
|
Files.write(configLocation, content.getBytes(UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void markConfigDirty() {
|
static void markConfigDirty() {
|
||||||
configWriter.accept(configInstance.toJson());
|
configWriter.accept(configInstance.toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void saveConfigSync() throws IOException {
|
||||||
|
writeToConfig(configInstance.toJson());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user