-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable using multiple fetchers for downloading server.jar Currently these are the fetchers: LauncherManifest - Probably what you want, downloads the server.jar from Mojang servers DownloadURL - Downloads server.jar from the internet CopyFile - Copies server.jar from a location on your PC Also added a test to verify that the main config is always correct, wouldn't want any bugs from that! Signed-off-by: Kozova1 <mug66kk@gmail.com>
- Loading branch information
Showing
15 changed files
with
255 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package net.vogman.mcdeploy | ||
|
||
sealed interface Command { | ||
suspend fun run(args: Array<String>): Result | ||
suspend fun run(args: Array<String>): Result<Unit, Error> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.vogman.mcdeploy | ||
|
||
fun ServerJarFetcher.CopyFile.fetchImpl(config: Config): Result<ByteArray, Error> { | ||
assert(config.Server.JarSource is ServerJarFetcher.CopyFile) | ||
if (!CopyFrom.exists()) { | ||
logErr("File ${CopyFrom.canonicalPath} does not exist!") | ||
return Result.Err(Error.User) | ||
} | ||
println("Copying server.jar from ${CopyFrom.canonicalPath}") | ||
val bytes = CopyFrom.readBytes() | ||
logOk("server.jar read successfully") | ||
return Result.Ok(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.vogman.mcdeploy | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
|
||
suspend fun ServerJarFetcher.DownloadURL.fetchImpl(config: Config): Result<ByteArray, Error> = | ||
HttpClient().use { client -> | ||
assert(config.Server.JarSource is ServerJarFetcher.DownloadURL) | ||
println("Downloading server.jar from $ServerJarURL") | ||
val response: HttpResponse = client.get(ServerJarURL) | ||
val serverJar: ByteArray = response.receive() | ||
return Result.Ok(serverJar) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package net.vogman.mcdeploy | ||
|
||
interface Fetcher { | ||
suspend fun fetch(config: Config): Result<ByteArray, Error> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package net.vogman.mcdeploy | ||
|
||
object HelpPrinter : Command { | ||
override suspend fun run(args: Array<String>): Result { | ||
override suspend fun run(args: Array<String>): Result<Unit, Error> { | ||
if (args.isNotEmpty()) { | ||
logErr("'help' subcommand accepts exactly zero arguments") | ||
return Result.Err(Error.User) | ||
} | ||
println(HELP) | ||
return Result.Ok | ||
return Result.Ok(Unit) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package net.vogman.mcdeploy | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
|
||
suspend fun ServerJarFetcher.LauncherManifest.fetchImpl(config: Config): Result<ByteArray, Error> = | ||
HttpClient().use { client -> | ||
assert(config.Server.JarSource is ServerJarFetcher.LauncherManifest) | ||
println("Downloading launcher manifest...") | ||
val versionResponse: HttpResponse = client.get(LauncherManifestURL) | ||
val versions: Versions = Json { ignoreUnknownKeys = true }.decodeFromString(versionResponse.receive()) | ||
val versionUrl = versions.findURI( | ||
when { | ||
Version.equals("latest-release", ignoreCase = true) -> versions.latest.release | ||
Version.equals("latest-snapshot", ignoreCase = true) -> versions.latest.snapshot | ||
else -> Version | ||
} | ||
) | ||
|
||
if (versionUrl == null) { | ||
logErr("No manifest URL found for version $Version (Are you sure this is the correct version?)") | ||
return Result.Err(Error.Server) | ||
} | ||
|
||
println() | ||
|
||
println("Downloading manifest for selected version...") | ||
val responseManifest: HttpResponse = client.get(versionUrl) | ||
val manifest: VersionManifest = | ||
Json { ignoreUnknownKeys = true }.decodeFromString(responseManifest.receive()) | ||
logOk("Received manifest for version $Version") | ||
|
||
if (manifest.downloads.server == null) { | ||
logErr("No server jar for version $Version") | ||
return Result.Err(Error.Server) | ||
} | ||
|
||
// Download server.jar for the selected version | ||
println("Downloading server.jar...") | ||
val serverResponse: HttpResponse = client.get(manifest.downloads.server.url) | ||
val serverJar: ByteArray = serverResponse.receive() | ||
logOk("Downloaded server.jar") | ||
println("Verifying server.jar...") | ||
|
||
val serverJarHash = sha1sum(serverJar) | ||
println("Downloaded: $serverJarHash") | ||
println("Manifest: ${manifest.downloads.server.sha1}") | ||
return if (manifest.downloads.server.sha1 == serverJarHash) { | ||
logOk("SHA-1 Match! Continuing") | ||
Result.Ok(serverJar) | ||
} else { | ||
logErr("SHA-1 Mismatch! Exiting") | ||
Result.Err(Error.Hash) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.