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" group = "dev.frogmc"
version = "0.0.1-alpha.6" version = "0.0.1-alpha.7"
repositories { repositories {
maven { maven {

View file

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

View file

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

View file

@ -21,7 +21,7 @@ abstract class PhytotelmaGradleExtensionImpl : PhytotelmaGradleExtension {
abstract fun getProject(): Project abstract fun getProject(): Project
override fun minecraft(version: String, parchmentGameVersion: String, parchmentVersion: String?) { 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...") println("Setting up Minecraft...")
val parchment = val parchment =
parchmentVersion ?: kotlin.runCatching { ParchmentProvider.findForMinecraftVersion(version) } parchmentVersion ?: kotlin.runCatching { ParchmentProvider.findForMinecraftVersion(version) }

View file

@ -46,6 +46,6 @@ object AssetDownloader {
private fun get(localDir: Path, hash: String) { private fun get(localDir: Path, hash: String) {
val shortHash = hash.substring(0, 2) val shortHash = hash.substring(0, 2)
val path = localDir.resolve("$shortHash/$hash") 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)
} }
} }