From 19324c624743ad8cb2dfc7592a3ea89282ac50a4 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 16 May 2024 19:32:08 +0700 Subject: [PATCH] Add cli command arguments --- build.gradle.kts | 3 + gradle.properties | 2 + .../kotlin/ru/dbotthepony/kstarbound/Main.kt | 71 +++++++++++++++---- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2ec6f12f..0af64b6d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,6 +23,7 @@ val log4jVersion: String by project val guavaVersion: String by project val junitVersion: String by project val sqliteVersion: String by project +val picocliVersion: String by project repositories { maven { @@ -91,6 +92,8 @@ dependencies { implementation("com.github.ben-manes.caffeine:caffeine:$caffeineVersion") implementation(project(":luna")) + implementation("info.picocli:picocli:$picocliVersion") + implementation("org.xerial:sqlite-jdbc:$sqliteVersion") implementation("io.netty:netty-transport:$nettyVersion") diff --git a/gradle.properties b/gradle.properties index 702281ea..2bea4cbb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,3 +17,5 @@ log4jVersion=2.17.1 guavaVersion=33.0.0-jre junitVersion=5.8.2 sqliteVersion=3.45.3.0 + +picocliVersion=4.7.6 diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt index 0ff77548..f824194d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt @@ -2,30 +2,73 @@ package ru.dbotthepony.kstarbound import org.apache.logging.log4j.LogManager import org.lwjgl.Version +import picocli.CommandLine +import picocli.CommandLine.Command +import picocli.CommandLine.Option +import picocli.CommandLine.Parameters import ru.dbotthepony.kstarbound.client.StarboundClient import ru.dbotthepony.kstarbound.server.IntegratedStarboundServer import java.io.File import java.net.InetSocketAddress +import java.util.concurrent.Callable +import kotlin.system.exitProcess private val LOGGER = LogManager.getLogger() -fun main() { - Starbound.addArchive(File("J:\\Steam\\steamapps\\common\\Starbound\\assets\\packed.pak")) +@Command( + name = "client", + mixinStandardHelpOptions = true, + description = ["Launches game client"] +) +class StartClientCommand : Callable { + @Option(names = ["-d", "--data"], description = ["Game folder where Starbound is installed.", "Defaults to working directory."]) + var dataFolder: String = "./" - /*for (f in File("J:\\Steam\\steamapps\\workshop\\content\\211820").listFiles()!!) { - for (f2 in f.listFiles()!!) { - if (f2.isFile) { - Starbound.addArchive(f2) - } + @Option(names = ["-s", "--storage"], description = ["Location where to store game files.", "Defaults to game's 'storage' subfolder"]) + var storageFolder: String? = null + + override fun call(): Int { + val data = File(dataFolder) + + if (!data.exists()) { + LOGGER.fatal("No such file ${data.absolutePath}") + return 1 } - }*/ - LOGGER.info("Running LWJGL ${Version.getVersion()}") + val file = File("${dataFolder}/assets/packed.pak") - val client = StarboundClient() + if (!file.exists()) { + LOGGER.fatal("No such file ${file.absolutePath}") + return 1 + } - Starbound.initializeGame().thenApply { - val server = IntegratedStarboundServer(client, File("./storage")) - server.channels.createChannel(InetSocketAddress(21060)) - }.exceptionally { LOGGER.error("what", it); null } + Starbound.addArchive(file) + + /*for (f in File("J:\\Steam\\steamapps\\workshop\\content\\211820").listFiles()!!) { + for (f2 in f.listFiles()!!) { + if (f2.isFile) { + Starbound.addArchive(f2) + } + } + }*/ + + LOGGER.info("Running LWJGL ${Version.getVersion()}") + + val client = StarboundClient() + + Starbound.initializeGame().thenApply { + val server = IntegratedStarboundServer(client, if (storageFolder == null) File(data, "storage") else File(storageFolder!!)) + server.channels.createChannel(InetSocketAddress(21060)) + }.exceptionally { LOGGER.error("what", it); null } + + return 0 + } +} + +fun main(vararg args: String) { + val result = CommandLine(StartClientCommand()).execute(*args) + + if (result != 0) { + exitProcess(result) + } }