fix version validation with outdated version manifest
All checks were successful
Publish to snapshot maven / build (push) Successful in 26s

This commit is contained in:
moehreag 2024-06-13 20:21:31 +02:00
parent d8ddcd0176
commit 44217c379f
5 changed files with 22 additions and 16 deletions

View file

@ -7,7 +7,7 @@ plugins {
}
group = "dev.frogmc"
version = "0.0.1-alpha.6"
version = "0.0.1-alpha.7"
repositories {
maven {

View file

@ -48,9 +48,15 @@ object VersionChecker {
}
}
fun validateVersion(version: String): Boolean {
if (validVersions.isEmpty()) getValidVersions()
return validVersions.any { it.id == version }
fun validateVersion(version: String, ignoreCache: Boolean = false, offlineMode: Boolean): Boolean {
if (validVersions.isEmpty()) getValidVersions(ignoreCache)
if (!validVersions.any { it.id == version }) {
if (!offlineMode) {
return validateVersion(version, ignoreCache = true, offlineMode = false)
}
return false
}
return true
}
fun getVersionUrl(version: String): String {
@ -86,17 +92,17 @@ object VersionChecker {
return CachingHttpClient.getUncached(URI.create(url)).readAllBytes()
}
private fun getUrl(url: String): String {
return CachingHttpClient.getString(URI.create(url))
private fun getUrl(url: String, ignoreCache: Boolean = false): String {
return CachingHttpClient.getString(URI.create(url), ignoreCache)
}
private fun getValidVersions() {
private fun getValidVersions(ignoreCache: Boolean = false) {
if (validVersions.isNotEmpty()) return
// get json from https://piston-meta.mojang.com/mc/game/version_manifest_v2.json
// make http request
val url = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"
val response = getUrl(url)
val response = getUrl(url, ignoreCache)
// response is in format of {versions: [{id: "1.17.1", type: "release"}, ...]}
// parse json
// get all versions

View file

@ -13,16 +13,16 @@ import kotlin.io.path.notExists
object CachingHttpClient {
fun downloadTo(uri: URI, path: Path) {
if (path.notExists()) {
fun downloadTo(uri: URI, path: Path, allowOverwrite: Boolean) {
if (allowOverwrite || path.notExists()) {
path.createParentDirectories()
Files.copy(getUncached(uri), path)
}
}
fun get(uri: URI): InputStream {
fun get(uri: URI, ignoreCache: Boolean = false): InputStream {
val cache = getCacheFile(uri)
if (cache.notExists()) {
if (ignoreCache || cache.notExists()) {
getUncached(uri).use {
Files.copy(it, cache)
}
@ -30,8 +30,8 @@ object CachingHttpClient {
return cache.inputStream()
}
fun getString(uri: URI): String {
return get(uri).bufferedReader(StandardCharsets.UTF_8).lines().collect(Collectors.joining("\n"))
fun getString(uri: URI, ignoreCache: Boolean = false): String {
return get(uri, ignoreCache).bufferedReader(StandardCharsets.UTF_8).lines().collect(Collectors.joining("\n"))
}
fun getUncached(uri: URI): InputStream {

View file

@ -21,7 +21,7 @@ abstract class PhytotelmaGradleExtensionImpl : PhytotelmaGradleExtension {
abstract fun getProject(): Project
override fun minecraft(version: String, parchmentGameVersion: String, parchmentVersion: String?) {
if (VersionChecker.validateVersion(version)) {
if (VersionChecker.validateVersion(version, offlineMode = getProject().gradle.startParameter.isOffline)) {
println("Setting up Minecraft...")
val parchment =
parchmentVersion ?: kotlin.runCatching { ParchmentProvider.findForMinecraftVersion(version) }

View file

@ -46,6 +46,6 @@ object AssetDownloader {
private fun get(localDir: Path, hash: String) {
val shortHash = hash.substring(0, 2)
val path = localDir.resolve("$shortHash/$hash")
CachingHttpClient.downloadTo(URI.create("$ASSETS_URL/$shortHash/$hash"), path)
CachingHttpClient.downloadTo(URI.create("$ASSETS_URL/$shortHash/$hash"), path, true)
}
}