Skip to content

Commit

Permalink
0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Sep 30, 2023
1 parent 675791b commit a5b09d7
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- 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)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions kraftia-api/src/main/kotlin/org/kraftia/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Account>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import kotlin.io.path.writeText
import kotlin.reflect.KClass

abstract class AbstractConfigClass<T : AbstractConfig>(
fileName: String,
name: String,
private val clazz: KClass<T>
) {
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class AccountConfig(
private val accounts: Set<Account> = emptySet(),
private val current: String? = null
) : AbstractConfig("account") {
companion object : AbstractConfigClass<AccountConfig>("accounts.json", AccountConfig::class) {
override fun createConfig(): AccountConfig {
companion object : AbstractConfigClass<AccountConfig>("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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class JavaVersionConfig(
private val javaVersions: Set<JavaVersion> = emptySet(),
private val current: Int? = null
) : AbstractConfig("java") {
companion object : AbstractConfigClass<JavaVersionConfig>("java.json", JavaVersionConfig::class) {
override fun createConfig(): JavaVersionConfig {
companion object : AbstractConfigClass<JavaVersionConfig>("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 }
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class VersionDownloader(
}

init {
versionsDirectory.createDirectories()
librariesDirectory.createDirectories()
binDirectory.createDirectories()
assetsDirectory.createDirectories()
Expand Down
26 changes: 2 additions & 24 deletions kraftia-headless/src/main/kotlin/org/kraftia/headless/Kraftia.kt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaVersion> {
class JavaVersionArgument : ArgumentType<JavaVersion> {
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<Any>): JavaVersion {
Expand All @@ -23,6 +23,6 @@ class JavaArgument : ArgumentType<JavaVersion> {
val argument = reader.readInt()

return JavaVersionManager.javaVersions.firstOrNull { it.versionNumber == argument }
?: throw NO_SUCH_JAVA.create(argument)
?: throw NO_SUCH_JAVA_VERSION.create(argument)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ 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})")
}
}
)

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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,17 +28,17 @@ 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}")
}
)
)

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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()}")
Expand Down

0 comments on commit a5b09d7

Please sign in to comment.