From ee1af448097848ba6e5ad5ca865c9141756e7641 Mon Sep 17 00:00:00 2001 From: Tang <1024830255@qq.com> Date: Mon, 28 Mar 2022 09:36:42 -0500 Subject: [PATCH] Update UncivServer: (#6427) - Add `clikt` lib. - Add custom multiplayer file's folder. --- build.gradle.kts | 13 +++--- .../src/com/unciv/app/desktop/UncivServer.kt | 40 ++++++++++++------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0fd76680d4..af25100881 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,7 +21,7 @@ buildscript { mavenCentral() maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots/") } gradlePluginPortal() - maven{ url = uri("https://jitpack.io") } // for the anuken packr + maven { url = uri("https://jitpack.io") } // for the anuken packr } dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${com.unciv.build.BuildConfig.kotlinVersion}") @@ -33,10 +33,10 @@ buildscript { classpath("com.github.anuken:packr:-SNAPSHOT") } } - + allprojects { - apply(plugin = "eclipse") - apply(plugin = "idea") + apply(plugin = "eclipse") + apply(plugin = "idea") version = "1.0.1" @@ -53,7 +53,7 @@ allprojects { mavenCentral() maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots/") } maven { url = uri("https://oss.sonatype.org/content/repositories/releases/") } - maven{ url = uri("https://jitpack.io") } // for java-discord-rpc + maven { url = uri("https://jitpack.io") } // for java-discord-rpc } } @@ -70,12 +70,13 @@ project(":desktop") { } "implementation"("com.github.MinnDevelopment:java-discord-rpc:v2.0.1") - + // For server-side "implementation"("io.ktor:ktor-server-core:1.6.8") "implementation"("io.ktor:ktor-server-netty:1.6.8") "implementation"("ch.qos.logback:logback-classic:1.2.5") + "implementation"("com.github.ajalt.clikt:clikt:3.4.0") } } diff --git a/desktop/src/com/unciv/app/desktop/UncivServer.kt b/desktop/src/com/unciv/app/desktop/UncivServer.kt index 892aad0672..e53f4ef446 100644 --- a/desktop/src/com/unciv/app/desktop/UncivServer.kt +++ b/desktop/src/com/unciv/app/desktop/UncivServer.kt @@ -1,5 +1,10 @@ package com.unciv.app.desktop +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.int +import com.github.ajalt.clikt.parameters.types.restrictTo import io.ktor.application.* import io.ktor.response.* import io.ktor.routing.* @@ -12,24 +17,29 @@ import java.io.File internal object UncivServer { - private var serverPort = 8080 - @JvmStatic - fun main(args: Array) { - args.forEach { arg -> - when { - arg.startsWith("-port=") -> with(arg.removePrefix("-port=").toIntOrNull() ?: 0) { - if (this in 1024..49151) serverPort = this - else println("'port' must be between 1024 and 49151") - } - } - } + fun main(args: Array) = UncivServerRunner().main(args) +} - println("Server will run on $serverPort port, you can use '-port=XXXX' custom port.") +private class UncivServerRunner : CliktCommand() { + private val port by option( + "-p", "-port", + envvar = "UncivServerPort", + help = "Server port" + ).int().restrictTo(1024..49151).default(8080) - val fileFolderName = "MultiplayerFiles" - File(fileFolderName).mkdir() - println(File(fileFolderName).absolutePath) + private val folder by option( + "-f", "-folder", + envvar = "UncivServerFolder", + help = "Multiplayer file's folder" + ).default("MultiplayerFiles") + + override fun run() { + serverRun(port, folder) + } + + private fun serverRun(serverPort: Int, fileFolderName: String) { + echo("Starting UncivServer for ${File(fileFolderName).absolutePath} on port $serverPort") embeddedServer(Netty, port = serverPort) { routing { get("/isalive") {