mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 01:16:46 -04:00
config: fix config creation bug
This commit is contained in:
parent
4c305adf13
commit
4dc509efd8
@ -105,7 +105,7 @@ public class Minosoft {
|
||||
public static void checkClientToken() {
|
||||
if (config.getString(GameConfiguration.CLIENT_TOKEN) == null || config.getString(GameConfiguration.CLIENT_TOKEN).equals("randomGenerated")) {
|
||||
config.putString(GameConfiguration.CLIENT_TOKEN, UUID.randomUUID().toString());
|
||||
config.saveToFile(Config.configFileName);
|
||||
config.saveToFile();
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,7 +125,7 @@ public class Minosoft {
|
||||
if (account == null) {
|
||||
selectedAccount = null;
|
||||
config.putString(GameConfiguration.ACCOUNT_SELECTED, null);
|
||||
config.saveToFile(Config.configFileName);
|
||||
config.saveToFile();
|
||||
return;
|
||||
}
|
||||
MojangAccount.RefreshStates refreshState = account.refreshToken();
|
||||
|
@ -30,6 +30,7 @@ import java.util.UUID;
|
||||
|
||||
public class Configuration {
|
||||
final LinkedHashMap<String, Object> config;
|
||||
final Thread thread;
|
||||
|
||||
public Configuration(String filename) throws IOException {
|
||||
File file = new File(Config.homeDir + "config/" + filename);
|
||||
@ -50,6 +51,45 @@ public class Configuration {
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
config = yml.load(inputStream);
|
||||
inputStream.close();
|
||||
|
||||
final File finalFile = file;
|
||||
thread = new Thread(() -> {
|
||||
while (true) {
|
||||
// wait for interrupt
|
||||
try {
|
||||
Thread.sleep(Integer.MAX_VALUE);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
// write config to temp file, delete original config, rename temp file to original file to avoid conflicts if minosoft gets closed while saving the config
|
||||
File tempFile = new File(Config.homeDir + "config/" + filename + ".tmp");
|
||||
Yaml yaml = new Yaml();
|
||||
FileWriter writer;
|
||||
try {
|
||||
writer = new FileWriter(tempFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
synchronized (config) {
|
||||
yaml.dump(config, writer);
|
||||
}
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (finalFile.exists()) {
|
||||
finalFile.delete();
|
||||
}
|
||||
if (!tempFile.renameTo(finalFile)) {
|
||||
Log.fatal("An error occurred while saving the config file");
|
||||
} else {
|
||||
Log.verbose(String.format("Configuration saved to file %s", filename));
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.setName("IO-Thread");
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public boolean getBoolean(String path) {
|
||||
@ -173,35 +213,8 @@ public class Configuration {
|
||||
config.remove(path);
|
||||
}
|
||||
|
||||
public void saveToFile(String filename) {
|
||||
Thread thread = new Thread(() -> {
|
||||
// write config to temp file, delete original config, rename temp file to original file to avoid conflicts if minosoft gets closed while saving the config
|
||||
File tempFile = new File(Config.homeDir + "config/" + filename + ".tmp");
|
||||
File file = new File(Config.homeDir + "config/" + filename);
|
||||
Yaml yaml = new Yaml();
|
||||
FileWriter writer;
|
||||
try {
|
||||
writer = new FileWriter(tempFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
synchronized (config) {
|
||||
yaml.dump(config, writer);
|
||||
}
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!file.delete() || !tempFile.renameTo(file)) {
|
||||
Log.fatal("An error occurred while saving the config file");
|
||||
} else {
|
||||
Log.verbose(String.format("Configuration saved to file %s", filename));
|
||||
}
|
||||
});
|
||||
thread.setName("IO-Thread");
|
||||
thread.start();
|
||||
public void saveToFile() {
|
||||
thread.interrupt();
|
||||
}
|
||||
|
||||
public HashBiMap<String, MojangAccount> getMojangAccounts() {
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
package de.bixilon.minosoft.gui.main;
|
||||
|
||||
import de.bixilon.minosoft.Config;
|
||||
import de.bixilon.minosoft.Minosoft;
|
||||
import de.bixilon.minosoft.protocol.network.Connection;
|
||||
import de.bixilon.minosoft.protocol.protocol.ConnectionReasons;
|
||||
@ -95,12 +94,12 @@ public class Server {
|
||||
|
||||
public void saveToConfig() {
|
||||
Minosoft.getConfig().putServer(this);
|
||||
Minosoft.getConfig().saveToFile(Config.configFileName);
|
||||
Minosoft.getConfig().saveToFile();
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
Minosoft.getConfig().removeServer(this);
|
||||
Minosoft.getConfig().saveToFile(Config.configFileName);
|
||||
Minosoft.getConfig().saveToFile();
|
||||
}
|
||||
|
||||
public Connection getLastPing() {
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
package de.bixilon.minosoft.gui.main;
|
||||
|
||||
import de.bixilon.minosoft.Config;
|
||||
import de.bixilon.minosoft.Minosoft;
|
||||
import de.bixilon.minosoft.config.GameConfiguration;
|
||||
import de.bixilon.minosoft.logging.Log;
|
||||
@ -43,7 +42,7 @@ public class SettingsWindow implements Initializable {
|
||||
}
|
||||
Log.setLevel(newLevel);
|
||||
Minosoft.getConfig().putString(GameConfiguration.GENERAL_LOG_LEVEL, newLevel.name());
|
||||
Minosoft.getConfig().saveToFile(Config.configFileName);
|
||||
Minosoft.getConfig().saveToFile();
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,6 @@
|
||||
package de.bixilon.minosoft.util.mojang.api;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import de.bixilon.minosoft.Config;
|
||||
import de.bixilon.minosoft.Minosoft;
|
||||
import de.bixilon.minosoft.util.Util;
|
||||
|
||||
@ -92,7 +91,7 @@ public class MojangAccount {
|
||||
|
||||
public void saveToConfig() {
|
||||
Minosoft.getConfig().putMojangAccount(this);
|
||||
Minosoft.getConfig().saveToFile(Config.configFileName);
|
||||
Minosoft.getConfig().saveToFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -103,7 +102,7 @@ public class MojangAccount {
|
||||
public void delete() {
|
||||
Minosoft.getAccountList().remove(this.getUserId());
|
||||
Minosoft.getConfig().removeAccount(this);
|
||||
Minosoft.getConfig().saveToFile(Config.configFileName);
|
||||
Minosoft.getConfig().saveToFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user