From a5b09d772ce98ce0dd913bd4e7a59083b8bafb64 Mon Sep 17 00:00:00 2001 From: ya-ilya Date: Sat, 30 Sep 2023 15:57:35 +0300 Subject: [PATCH] 0.1.1 --- README.md | 4 +-- gradle.properties | 2 +- .../src/main/kotlin/org/kraftia/api/Api.kt | 16 ++++++++++++ .../kotlin/org/kraftia/api/account/Account.kt | 8 +++--- .../api/account/container/AccountContainer.kt | 5 +--- .../kraftia/api/config/AbstractConfigClass.kt | 14 +++++----- .../api/config/configs/AccountConfig.kt | 6 ++--- .../api/config/configs/JavaVersionConfig.kt | 6 ++--- .../downloaders/VersionDownloader.kt | 1 + .../kotlin/org/kraftia/headless/Kraftia.kt | 26 ++----------------- ...JavaArgument.kt => JavaVersionArgument.kt} | 8 +++--- .../command/commands/AccountCommand.kt | 4 +-- .../headless/command/commands/JavaCommand.kt | 10 +++---- .../command/commands/LaunchCommand.kt | 9 ++----- 14 files changed, 53 insertions(+), 66 deletions(-) rename kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/{JavaArgument.kt => JavaVersionArgument.kt} (75%) diff --git a/README.md b/README.md index b917bf7..873559c 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,5 @@ Simple and lightweight launcher for [Minecraft](https://www.minecraft.net/) ### Installation -- Download the latest release from [releases](https://github.com/ya-ilya/kraftia/releases) or dev build from [actions](https://github.com/ya-ilya/kraftia/actions) -- Run `java -jar kraftia-headless-VERSION.jar` from command line \ No newline at end of file +- Latest release is located on the [releases page](https://github.com/ya-ilya/kraftia/releases) +- Dev build can be downloaded from [actions page](https://github.com/ya-ilya/kraftia/actions) \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 565c0b6..72c5dfa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ kotlin.experimental.tryK2=true kotlinVersion=1.9.10 # kraftia -kraftiaVersion=0.1 +kraftiaVersion=0.1.1 # other gsonVersion=2.10.1 diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/Api.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/Api.kt index 9b645b5..6de6323 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/Api.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/Api.kt @@ -6,6 +6,12 @@ import com.google.gson.JsonObject import me.liuli.elixir.account.MicrosoftAccount import okhttp3.OkHttpClient import org.kraftia.api.account.Account +import org.kraftia.api.config.configs.AccountConfig +import org.kraftia.api.config.configs.AccountConfig.Companion.apply +import org.kraftia.api.config.configs.AccountConfig.Companion.write +import org.kraftia.api.config.configs.JavaVersionConfig +import org.kraftia.api.config.configs.JavaVersionConfig.Companion.apply +import org.kraftia.api.config.configs.JavaVersionConfig.Companion.write import org.kraftia.api.extensions.checkRules import org.kraftia.api.extensions.path import org.kraftia.api.extensions.resourceJson @@ -69,6 +75,16 @@ object Api { VersionManager } + fun configs() { + AccountConfig.read().apply() + JavaVersionConfig.read().apply() + + Runtime.getRuntime().addShutdownHook(Thread { + AccountConfig.create().write() + JavaVersionConfig.create().write() + }) + } + fun launch( version: Version, account: Account, diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/account/Account.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/account/Account.kt index 15f1481..da7d9c9 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/account/Account.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/account/Account.kt @@ -8,11 +8,11 @@ import okhttp3.Request import org.kraftia.api.Api sealed class Account( - val uuid: String? = null, - val name: String? = null + val name: String? = null, + val uuid: String? = null ) { - class Offline(name: String, uuid: String? = null) : Account(uuid ?: uuid(name), name) - class Microsoft(name: String, uuid: String, val token: String) : Account(uuid, name) + class Offline(name: String, uuid: String? = null) : Account(name, uuid ?: uuid(name)) + class Microsoft(name: String, uuid: String, val token: String) : Account(name, uuid) @JsonAdapter(Account::class) object TypeAdapter : com.google.gson.TypeAdapter() { diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/account/container/AccountContainer.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/account/container/AccountContainer.kt index c1f38c4..8b1f988 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/account/container/AccountContainer.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/account/container/AccountContainer.kt @@ -14,10 +14,7 @@ interface AccountContainer { } fun addAccount(account: Account) { - if (accounts.any { it.name == account.name }) { - throw IllegalArgumentException("Account with same name already exists") - } - + accounts.removeIf { it.name == account.name || it.uuid == account.uuid } accounts.add(account) } diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/config/AbstractConfigClass.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/config/AbstractConfigClass.kt index 48e0f60..d5c12b1 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/config/AbstractConfigClass.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/config/AbstractConfigClass.kt @@ -9,23 +9,23 @@ import kotlin.io.path.writeText import kotlin.reflect.KClass abstract class AbstractConfigClass( - fileName: String, + name: String, private val clazz: KClass ) { - private val path = path(Api.launcherDirectory, fileName) + private val path = path(Api.launcherDirectory, "$name.json") - fun readConfig(): T { + fun read(): T { path.createParentDirectories() return if (path.exists()) Api.GSON.fromJson(path.readText(), clazz.java) - else createConfig() + else create() } - fun T.writeConfig() { + fun T.write() { path.createParentDirectories() path.writeText(Api.GSON.toJson(this, clazz.java)) } - abstract fun createConfig(): T - abstract fun T.applyConfig() + abstract fun create(): T + abstract fun T.apply() } \ No newline at end of file diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/AccountConfig.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/AccountConfig.kt index f593280..533c26c 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/AccountConfig.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/AccountConfig.kt @@ -9,15 +9,15 @@ class AccountConfig( private val accounts: Set = emptySet(), private val current: String? = null ) : AbstractConfig("account") { - companion object : AbstractConfigClass("accounts.json", AccountConfig::class) { - override fun createConfig(): AccountConfig { + companion object : AbstractConfigClass("accounts", AccountConfig::class) { + override fun create(): AccountConfig { return AccountConfig( AccountManager.accounts, AccountManager.current?.name ) } - override fun AccountConfig.applyConfig() { + override fun AccountConfig.apply() { for (account in accounts) { AccountManager.addAccount(account) } diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/JavaVersionConfig.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/JavaVersionConfig.kt index 3ec160b..cb37b33 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/JavaVersionConfig.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/config/configs/JavaVersionConfig.kt @@ -9,15 +9,15 @@ class JavaVersionConfig( private val javaVersions: Set = emptySet(), private val current: Int? = null ) : AbstractConfig("java") { - companion object : AbstractConfigClass("java.json", JavaVersionConfig::class) { - override fun createConfig(): JavaVersionConfig { + companion object : AbstractConfigClass("java", JavaVersionConfig::class) { + override fun create(): JavaVersionConfig { return JavaVersionConfig( JavaVersionManager.javaVersions, JavaVersionManager.current?.versionNumber ) } - override fun JavaVersionConfig.applyConfig() { + override fun JavaVersionConfig.apply() { for (javaVersion in javaVersions .filter { JavaVersionManager.getJavaVersionByNumberOrNull(it.versionNumber) == null } ) { diff --git a/kraftia-api/src/main/kotlin/org/kraftia/api/version/downloader/downloaders/VersionDownloader.kt b/kraftia-api/src/main/kotlin/org/kraftia/api/version/downloader/downloaders/VersionDownloader.kt index 572b517..d358355 100644 --- a/kraftia-api/src/main/kotlin/org/kraftia/api/version/downloader/downloaders/VersionDownloader.kt +++ b/kraftia-api/src/main/kotlin/org/kraftia/api/version/downloader/downloaders/VersionDownloader.kt @@ -52,6 +52,7 @@ class VersionDownloader( } init { + versionsDirectory.createDirectories() librariesDirectory.createDirectories() binDirectory.createDirectories() assetsDirectory.createDirectories() diff --git a/kraftia-headless/src/main/kotlin/org/kraftia/headless/Kraftia.kt b/kraftia-headless/src/main/kotlin/org/kraftia/headless/Kraftia.kt index e029787..1b35455 100644 --- a/kraftia-headless/src/main/kotlin/org/kraftia/headless/Kraftia.kt +++ b/kraftia-headless/src/main/kotlin/org/kraftia/headless/Kraftia.kt @@ -1,17 +1,13 @@ package org.kraftia.headless import org.kraftia.api.Api -import org.kraftia.api.config.configs.AccountConfig -import org.kraftia.api.config.configs.AccountConfig.Companion.applyConfig -import org.kraftia.api.config.configs.AccountConfig.Companion.writeConfig -import org.kraftia.api.config.configs.JavaVersionConfig -import org.kraftia.api.config.configs.JavaVersionConfig.Companion.applyConfig -import org.kraftia.api.config.configs.JavaVersionConfig.Companion.writeConfig import org.kraftia.api.extensions.resourceText import org.kraftia.headless.command.commands.* import org.kraftia.headless.managers.CommandManager fun main() { + Api.configs() + CommandManager.addCommand(AccountCommand) CommandManager.addCommand(ExitCommand) CommandManager.addCommand(FabricCommand) @@ -21,24 +17,6 @@ fun main() { CommandManager.addCommand(LaunchCommand) CommandManager.addCommand(VersionCommand) - AccountConfig - .readConfig() - .applyConfig() - - JavaVersionConfig - .readConfig() - .applyConfig() - - Runtime.getRuntime().addShutdownHook(Thread { - AccountConfig - .createConfig() - .writeConfig() - - JavaVersionConfig - .createConfig() - .writeConfig() - }) - println(resourceText("ascii_logo.txt")) println("Welcome to the Kraftia v${Api.VERSION}, simple and lightweight launcher for Minecraft.") diff --git a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/JavaArgument.kt b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/JavaVersionArgument.kt similarity index 75% rename from kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/JavaArgument.kt rename to kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/JavaVersionArgument.kt index 2492b64..507ce06 100644 --- a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/JavaArgument.kt +++ b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/arguments/JavaVersionArgument.kt @@ -8,10 +8,10 @@ import com.mojang.brigadier.exceptions.DynamicCommandExceptionType import org.kraftia.api.java.JavaVersion import org.kraftia.api.managers.JavaVersionManager -class JavaArgument : ArgumentType { +class JavaVersionArgument : ArgumentType { companion object { - private val NO_SUCH_JAVA = DynamicCommandExceptionType { name: Any -> - Message { "Java $name not found" } + private val NO_SUCH_JAVA_VERSION = DynamicCommandExceptionType { name: Any -> + Message { "Java version $name not found" } } operator fun get(context: CommandContext): JavaVersion { @@ -23,6 +23,6 @@ class JavaArgument : ArgumentType { val argument = reader.readInt() return JavaVersionManager.javaVersions.firstOrNull { it.versionNumber == argument } - ?: throw NO_SUCH_JAVA.create(argument) + ?: throw NO_SUCH_JAVA_VERSION.create(argument) } } \ No newline at end of file diff --git a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/AccountCommand.kt b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/AccountCommand.kt index f9335db..1d95a24 100644 --- a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/AccountCommand.kt +++ b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/AccountCommand.kt @@ -56,7 +56,7 @@ object AccountCommand : AbstractCommand("account", "Manage accounts") { println("Accounts:") for (account in AccountManager.accounts) { - println("- [${account.javaClass.simpleName}] ${account.name} (${account.uuid})") + println("- [${account.javaClass.simpleName.first()}] ${account.name} (${account.uuid})") } } ) @@ -64,7 +64,7 @@ object AccountCommand : AbstractCommand("account", "Manage accounts") { builder.execute { AccountManager.current.also { if (it != null) { - println("Current account: [${it.javaClass.simpleName}] ${it.name} (${it.uuid}") + println("Current account: [${it.javaClass.simpleName.first()}] ${it.name} (${it.uuid}") } else { println("You are not logged in") } diff --git a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/JavaCommand.kt b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/JavaCommand.kt index 4b07298..95ca985 100644 --- a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/JavaCommand.kt +++ b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/JavaCommand.kt @@ -3,7 +3,7 @@ package org.kraftia.headless.command.commands import com.mojang.brigadier.arguments.StringArgumentType import org.kraftia.api.managers.JavaVersionManager import org.kraftia.headless.command.AbstractCommand -import org.kraftia.headless.command.arguments.JavaArgument +import org.kraftia.headless.command.arguments.JavaVersionArgument import java.nio.file.Paths import kotlin.io.path.exists @@ -28,8 +28,8 @@ object JavaCommand : AbstractCommand("java", "Manage java versions") { builder.then( literal("set").then( - argument("java", JavaArgument()).execute { context -> - JavaVersionManager.current = JavaArgument[context] + argument("java", JavaVersionArgument()).execute { context -> + JavaVersionManager.current = JavaVersionArgument[context] println("Java version switched to ${JavaVersionManager.current!!.versionNumber}") } ) @@ -37,8 +37,8 @@ object JavaCommand : AbstractCommand("java", "Manage java versions") { builder.then( literal("remove").then( - argument("java", JavaArgument()).execute { context -> - val javaVersion = JavaArgument[context] + argument("java", JavaVersionArgument()).execute { context -> + val javaVersion = JavaVersionArgument[context] JavaVersionManager.removeJavaVersion(javaVersion) println("Removed ${javaVersion.versionNumber} (${javaVersion.executable}) java version") diff --git a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/LaunchCommand.kt b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/LaunchCommand.kt index 7e3a49f..417dde5 100644 --- a/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/LaunchCommand.kt +++ b/kraftia-headless/src/main/kotlin/org/kraftia/headless/command/commands/LaunchCommand.kt @@ -1,17 +1,11 @@ package org.kraftia.headless.command.commands -import com.mojang.brigadier.Message -import com.mojang.brigadier.exceptions.DynamicCommandExceptionType import org.kraftia.api.Api import org.kraftia.api.managers.AccountManager import org.kraftia.headless.command.AbstractCommand import org.kraftia.headless.command.arguments.VersionArgument object LaunchCommand : AbstractCommand("launch", "Launch specified version") { - private val ACCOUNT_ERROR = DynamicCommandExceptionType { _: Any -> - Message { "Login in account before launching game" } - } - var process: Process? = null init { @@ -23,7 +17,8 @@ object LaunchCommand : AbstractCommand("launch", "Launch specified version") { process = Api.launch( VersionArgument[context], - AccountManager.current ?: throw ACCOUNT_ERROR.create(Any()) + AccountManager.current + ?: throw IllegalArgumentException("Login in account before launching game") ) println("Minecraft exited with code: ${process!!.waitFor()}")