Move ConfigurationPaths into type based paths, add some custom exceptions, ...

This commit is contained in:
Bixilon 2020-11-03 19:43:25 +01:00
parent 202bd35075
commit ae685e9068
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
11 changed files with 83 additions and 51 deletions

View File

@ -51,9 +51,6 @@ public final class Minosoft {
public static Configuration config;
public static void main(String[] args) {
// init log thread
Log.initThread();
Log.info("Starting...");
AsyncTaskWorker taskWorker = new AsyncTaskWorker("StartUp");
@ -108,9 +105,9 @@ public final class Minosoft {
e.printStackTrace();
return;
}
Log.info(String.format("Loaded config file (version=%s)", config.getInt(ConfigurationPaths.CONFIG_VERSION)));
Log.info(String.format("Loaded config file (version=%s)", config.getInt(ConfigurationPaths.IntegerPaths.GENERAL_CONFIG_VERSION)));
// set log level from config
Log.setLevel(LogLevels.valueOf(config.getString(ConfigurationPaths.GENERAL_LOG_LEVEL)));
Log.setLevel(LogLevels.valueOf(config.getString(ConfigurationPaths.StringPaths.GENERAL_LOG_LEVEL)));
Log.info(String.format("Logging info with level: %s", Log.getLevel()));
serverList = config.getServers();
@ -123,7 +120,7 @@ public final class Minosoft {
taskWorker.addTask(new Task(progress -> {
progress.countUp();
LocaleManager.load(config.getString(ConfigurationPaths.GENERAL_LANGUAGE));
LocaleManager.load(config.getString(ConfigurationPaths.StringPaths.GENERAL_LANGUAGE));
progress.countDown();
}, "Minosoft Language", "Load minosoft language files", Priorities.HIGH, TaskImportance.REQUIRED, "Configuration"));
@ -140,7 +137,7 @@ public final class Minosoft {
Log.debug("Refreshing account token...");
checkClientToken();
accountList = config.getMojangAccounts();
selectAccount(accountList.get(config.getString(ConfigurationPaths.ACCOUNT_SELECTED)));
selectAccount(accountList.get(config.getString(ConfigurationPaths.StringPaths.ACCOUNT_SELECTED)));
}, "Token refresh", "Refresh selected account token", Priorities.LOW, TaskImportance.OPTIONAL, "Configuration"));
taskWorker.addTask(new Task(progress -> {
@ -157,7 +154,7 @@ public final class Minosoft {
taskWorker.addTask(new Task(progress -> {
progress.countUp();
MinecraftLocaleManager.load(config.getString(ConfigurationPaths.GENERAL_LANGUAGE));
MinecraftLocaleManager.load(config.getString(ConfigurationPaths.StringPaths.GENERAL_LANGUAGE));
progress.countDown();
}, "Mojang language", "Load minecraft language files", Priorities.HIGH, TaskImportance.REQUIRED, "Assets"));
@ -171,8 +168,8 @@ public final class Minosoft {
}
public static void checkClientToken() {
if (config.getString(ConfigurationPaths.CLIENT_TOKEN).isBlank()) {
config.putString(ConfigurationPaths.CLIENT_TOKEN, UUID.randomUUID().toString());
if (config.getString(ConfigurationPaths.StringPaths.CLIENT_TOKEN).isBlank()) {
config.putString(ConfigurationPaths.StringPaths.CLIENT_TOKEN, UUID.randomUUID().toString());
config.saveToFile();
}
}
@ -180,7 +177,7 @@ public final class Minosoft {
public static void selectAccount(MojangAccount account) {
if (account == null) {
selectedAccount = null;
config.putString(ConfigurationPaths.ACCOUNT_SELECTED, "");
config.putString(ConfigurationPaths.StringPaths.ACCOUNT_SELECTED, "");
config.saveToFile();
return;
}
@ -192,7 +189,7 @@ public final class Minosoft {
selectedAccount = null;
return;
}
config.putString(ConfigurationPaths.ACCOUNT_SELECTED, account.getUserId());
config.putString(ConfigurationPaths.StringPaths.ACCOUNT_SELECTED, account.getUserId());
selectedAccount = account;
MainWindow.selectAccount();
account.saveToConfig();

View File

@ -0,0 +1,35 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.config;
public class ConfigMigrationException extends Exception {
public ConfigMigrationException() {
}
public ConfigMigrationException(String message) {
super(message);
}
public ConfigMigrationException(String message, Throwable cause) {
super(message, cause);
}
public ConfigMigrationException(Throwable cause) {
super(cause);
}
public ConfigMigrationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -32,7 +32,7 @@ public class Configuration {
final JsonObject config;
private final Object lock = new Object();
public Configuration() throws IOException {
public Configuration() throws IOException, ConfigMigrationException {
File file = new File(StaticConfiguration.HOME_DIR + "config/" + StaticConfiguration.CONFIG_FILENAME);
if (!file.exists()) {
// no configuration file
@ -48,9 +48,9 @@ public class Configuration {
file = new File(StaticConfiguration.HOME_DIR + "config/" + StaticConfiguration.CONFIG_FILENAME);
}
config = Util.readJsonFromFile(file.getAbsolutePath());
int configVersion = getInt(ConfigurationPaths.CONFIG_VERSION);
int configVersion = getInt(ConfigurationPaths.IntegerPaths.GENERAL_CONFIG_VERSION);
if (configVersion > LATEST_CONFIG_VERSION) {
throw new RuntimeException(String.format("Configuration was migrated to newer config format (version=%d, expected=%d). Downgrading the config file is unsupported!", configVersion, LATEST_CONFIG_VERSION));
throw new ConfigMigrationException(String.format("Configuration was migrated to newer config format (version=%d, expected=%d). Downgrading the config file is unsupported!", configVersion, LATEST_CONFIG_VERSION));
}
if (configVersion < LATEST_CONFIG_VERSION) {
migrateConfiguration();
@ -96,57 +96,51 @@ public class Configuration {
}, "IO").start();
}
public boolean getBoolean(ConfigurationPaths path) {
public boolean getBoolean(ConfigurationPaths.BooleanPaths path) {
return switch (path) {
case NETWORK_FAKE_CLIENT_BRAND -> config.getAsJsonObject("network").get("fake-network-brand").getAsBoolean();
case DEBUG_VERIFY_ASSETS -> config.getAsJsonObject("debug").get("verify-assets").getAsBoolean();
default -> throw new RuntimeException(String.format("Illegal boolean value: %s", path));
};
}
public void putBoolean(ConfigurationPaths path, boolean value) {
public void putBoolean(ConfigurationPaths.BooleanPaths path, boolean value) {
switch (path) {
case NETWORK_FAKE_CLIENT_BRAND -> config.getAsJsonObject("network").addProperty("fake-network-brand", value);
case DEBUG_VERIFY_ASSETS -> config.getAsJsonObject("debug").addProperty("verify-assets", value);
default -> throw new RuntimeException(String.format("Illegal boolean value: %s", path));
}
}
public int getInt(ConfigurationPaths path) {
public int getInt(ConfigurationPaths.IntegerPaths path) {
return switch (path) {
case CONFIG_VERSION -> config.getAsJsonObject("general").get("version").getAsInt();
case GENERAL_CONFIG_VERSION -> config.getAsJsonObject("general").get("version").getAsInt();
case GAME_RENDER_DISTANCE -> config.getAsJsonObject("game").get("render-distance").getAsInt();
default -> throw new RuntimeException(String.format("Illegal int value: %s", path));
};
}
public void putInt(ConfigurationPaths path, int value) {
public void putInt(ConfigurationPaths.IntegerPaths path, int value) {
switch (path) {
case CONFIG_VERSION -> config.getAsJsonObject("general").addProperty("version", value);
case GENERAL_CONFIG_VERSION -> config.getAsJsonObject("general").addProperty("version", value);
case GAME_RENDER_DISTANCE -> config.getAsJsonObject("game").addProperty("render-distance", value);
default -> throw new RuntimeException(String.format("Illegal int value: %s", path));
}
}
public String getString(ConfigurationPaths path) {
public String getString(ConfigurationPaths.StringPaths path) {
return switch (path) {
case ACCOUNT_SELECTED -> config.getAsJsonObject("accounts").get("selected").getAsString();
case GENERAL_LOG_LEVEL -> config.getAsJsonObject("general").get("log-level").getAsString();
case GENERAL_LANGUAGE -> config.getAsJsonObject("general").get("language").getAsString();
case MAPPINGS_URL -> config.getAsJsonObject("download").getAsJsonObject("urls").get("mappings").getAsString();
case CLIENT_TOKEN -> config.getAsJsonObject("accounts").get("client-token").getAsString();
default -> throw new RuntimeException(String.format("Illegal String value: %s", path));
};
}
public void putString(ConfigurationPaths path, String value) {
public void putString(ConfigurationPaths.StringPaths path, String value) {
switch (path) {
case ACCOUNT_SELECTED -> config.getAsJsonObject("accounts").addProperty("selected", value);
case GENERAL_LOG_LEVEL -> config.getAsJsonObject("general").addProperty("log-level", value);
case GENERAL_LANGUAGE -> config.getAsJsonObject("general").addProperty("language", value);
case MAPPINGS_URL -> config.getAsJsonObject("download").getAsJsonObject("urls").addProperty("mappings", value);
case CLIENT_TOKEN -> config.getAsJsonObject("accounts").addProperty("client-token", value);
default -> throw new RuntimeException(String.format("Illegal String value: %s", path));
}
}
@ -194,12 +188,12 @@ public class Configuration {
}
private void migrateConfiguration() {
int configVersion = getInt(ConfigurationPaths.CONFIG_VERSION);
int configVersion = getInt(ConfigurationPaths.IntegerPaths.GENERAL_CONFIG_VERSION);
Log.info(String.format("Migrating config from version %d to %d", configVersion, LATEST_CONFIG_VERSION));
for (int nextVersion = configVersion + 1; nextVersion <= LATEST_CONFIG_VERSION; nextVersion++) {
migrateConfiguration(nextVersion);
}
putInt(ConfigurationPaths.CONFIG_VERSION, LATEST_CONFIG_VERSION);
putInt(ConfigurationPaths.IntegerPaths.GENERAL_CONFIG_VERSION, LATEST_CONFIG_VERSION);
saveToFile();
Log.info("Finished migrating config!");

View File

@ -13,14 +13,21 @@
package de.bixilon.minosoft.config;
public enum ConfigurationPaths {
CONFIG_VERSION,
GAME_RENDER_DISTANCE,
NETWORK_FAKE_CLIENT_BRAND,
GENERAL_LOG_LEVEL,
CLIENT_TOKEN,
MAPPINGS_URL,
ACCOUNT_SELECTED,
GENERAL_LANGUAGE,
DEBUG_VERIFY_ASSETS,
public abstract class ConfigurationPaths {
public enum StringPaths {
GENERAL_LOG_LEVEL,
CLIENT_TOKEN,
MAPPINGS_URL,
ACCOUNT_SELECTED,
GENERAL_LANGUAGE,
}
public enum BooleanPaths {
NETWORK_FAKE_CLIENT_BRAND, DEBUG_VERIFY_ASSETS
}
public enum IntegerPaths {
GENERAL_CONFIG_VERSION,
GAME_RENDER_DISTANCE
}
}

View File

@ -152,7 +152,7 @@ public class AssetsManager {
if (getAssetSize(hash) == -1) {
return false;
}
if (!Minosoft.config.getBoolean(ConfigurationPaths.DEBUG_VERIFY_ASSETS)) {
if (!Minosoft.config.getBoolean(ConfigurationPaths.BooleanPaths.DEBUG_VERIFY_ASSETS)) {
return true;
}
try {

View File

@ -145,7 +145,7 @@ public class Versions {
} catch (FileNotFoundException e) {
long downloadStartTime = System.currentTimeMillis();
Log.info(String.format("Mappings for %s are not available on disk. Downloading them...", version.getVersionName()));
Util.downloadFile(String.format(Minosoft.getConfig().getString(ConfigurationPaths.MAPPINGS_URL), version.getVersionName()), fileName);
Util.downloadFile(String.format(Minosoft.getConfig().getString(ConfigurationPaths.StringPaths.MAPPINGS_URL), version.getVersionName()), fileName);
try {
files = Util.readJsonTarGzFile(fileName);
} catch (ZipException e2) {

View File

@ -190,7 +190,6 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
setErrorMotd(String.format("%s", server.getLastPing().getLastConnectionException().getLocalizedMessage()));
}
}));
}
@Override

View File

@ -45,7 +45,7 @@ public class SettingsWindow implements Initializable {
return;
}
Log.setLevel(newLevel);
Minosoft.getConfig().putString(ConfigurationPaths.GENERAL_LOG_LEVEL, newLevel.name());
Minosoft.getConfig().putString(ConfigurationPaths.StringPaths.GENERAL_LOG_LEVEL, newLevel.name());
Minosoft.getConfig().saveToFile();
}));

View File

@ -24,10 +24,10 @@ import java.util.concurrent.LinkedBlockingQueue;
public class Log {
final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
final static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
final static long startTime = System.currentTimeMillis();
public final static long MINOSOFT_START_TIME = System.currentTimeMillis();
static LogLevels level = LogLevels.PROTOCOL;
public static void initThread() {
static {
new Thread(() -> {
while (true) {
// something to print
@ -69,7 +69,7 @@ public class Log {
StringBuilder builder = new StringBuilder();
builder.append("[");
if (StaticConfiguration.LOG_RELATIVE_TIME) {
builder.append(System.currentTimeMillis() - startTime);
builder.append(System.currentTimeMillis() - MINOSOFT_START_TIME);
} else {
builder.append(timeFormat.format(System.currentTimeMillis()));
}

View File

@ -199,7 +199,7 @@ public class PacketHandler {
if (pkg.getChannel().equals(DefaultPluginChannels.MC_BRAND.getChangeableIdentifier().get(connection.getVersion().getVersionId()))) {
InByteBuffer data = pkg.getDataAsBuffer();
String serverVersion;
String clientVersion = (Minosoft.getConfig().getBoolean(ConfigurationPaths.NETWORK_FAKE_CLIENT_BRAND) ? "vanilla" : "Minosoft");
String clientVersion = (Minosoft.getConfig().getBoolean(ConfigurationPaths.BooleanPaths.NETWORK_FAKE_CLIENT_BRAND) ? "vanilla" : "Minosoft");
OutByteBuffer toSend = new OutByteBuffer(connection);
if (connection.getVersion().getVersionId() < 29) {
// no length prefix

View File

@ -26,7 +26,7 @@ import java.net.http.HttpResponse;
public final class MojangAuthentication {
public static MojangAccountAuthenticationAttempt login(String username, String password) {
return login(Minosoft.getConfig().getString(ConfigurationPaths.CLIENT_TOKEN), username, password);
return login(Minosoft.getConfig().getString(ConfigurationPaths.StringPaths.CLIENT_TOKEN), username, password);
}
public static MojangAccountAuthenticationAttempt login(String clientToken, String username, String password) {
@ -81,7 +81,7 @@ public final class MojangAuthentication {
}
public static String refresh(String accessToken) {
return refresh(Minosoft.getConfig().getString(ConfigurationPaths.CLIENT_TOKEN), accessToken);
return refresh(Minosoft.getConfig().getString(ConfigurationPaths.StringPaths.CLIENT_TOKEN), accessToken);
}
public static String refresh(String clientToken, String accessToken) {