From b12704d5079eae21931c1159325c825e627fe421 Mon Sep 17 00:00:00 2001 From: NikkyAI Date: Tue, 11 Dec 2018 15:02:12 +1300 Subject: [PATCH] remove gson dependency, update dependency checks --- 1.7.10/build.gradle | 1 + core/build.gradle | 3 +- .../kotlin/matterlink/jenkins/Artifact.kt | 4 +- .../main/kotlin/matterlink/jenkins/Build.kt | 11 ++-- .../matterlink/jenkins/BuildWithDetails.kt | 4 +- .../matterlink/jenkins/JenkinsServer.kt | 11 ++-- .../src/main/kotlin/matterlink/jenkins/Job.kt | 4 +- .../kotlin/matterlink/update/CurseFile.kt | 6 +- .../kotlin/matterlink/update/UpdateChecker.kt | 61 ++++++++----------- scripts/start.sh | 2 +- 10 files changed, 51 insertions(+), 56 deletions(-) diff --git a/1.7.10/build.gradle b/1.7.10/build.gradle index 62bf528..125840b 100644 --- a/1.7.10/build.gradle +++ b/1.7.10/build.gradle @@ -40,6 +40,7 @@ dependencies { shade(group: "com.github.kittinunf.Fuel", name: "fuel", version: fuelVersion) shade(group: "com.github.kittinunf.Fuel", name: "fuel-coroutines", version: fuelVersion) + shade(group: "com.github.kittinunf.Fuel", name: "fuel-kotlinx-serialization", version: fuelVersion) shade(group: 'com.github.kittinunf.result', name: 'result', version: resultVersion) // shade group: 'com.github.kittinunf.fuel', name: 'fuel', version: fuelVersion diff --git a/core/build.gradle b/core/build.gradle index 03a4241..76022de 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -20,13 +20,14 @@ dependencies { compile project(":Jankson") shadow(project(':Jankson')) { transitive = false } - compile group: 'com.google.code.gson', name: 'gson', version: '+' compile group: 'com.google.guava', name: 'guava', version: '+' compile(group: "com.github.kittinunf.Fuel", name: "fuel", version: fuelVersion) shadow(group: "com.github.kittinunf.Fuel", name: "fuel", version: fuelVersion) { transitive = true } compile(group: "com.github.kittinunf.Fuel", name: "fuel-coroutines", version: fuelVersion) shadow(group: "com.github.kittinunf.Fuel", name: "fuel-coroutines", version: fuelVersion) { transitive = true } + compile(group: "com.github.kittinunf.Fuel", name: "fuel-kotlinx-serialization", version: fuelVersion) + shadow(group: "com.github.kittinunf.Fuel", name: "fuel-kotlinx-serialization", version: fuelVersion) { transitive = true } // compile(group: 'com.github.kittinunf.result', name: 'result', version: resultVersion) // shadow(group: 'com.github.kittinunf.result', name: 'result', version: resultVersion) { transitive = false } diff --git a/core/src/main/kotlin/matterlink/jenkins/Artifact.kt b/core/src/main/kotlin/matterlink/jenkins/Artifact.kt index 7341ac3..a46e2b9 100644 --- a/core/src/main/kotlin/matterlink/jenkins/Artifact.kt +++ b/core/src/main/kotlin/matterlink/jenkins/Artifact.kt @@ -1,11 +1,13 @@ package matterlink.jenkins +import kotlinx.serialization.Serializable + /** * Created by nikky on 03/02/18. * @author Nikky */ -//@JsonIgnoreProperties(ignoreUnknown = true) +@Serializable data class Artifact( val displayPath: String, val fileName: String, diff --git a/core/src/main/kotlin/matterlink/jenkins/Build.kt b/core/src/main/kotlin/matterlink/jenkins/Build.kt index c3cc095..fd7f432 100644 --- a/core/src/main/kotlin/matterlink/jenkins/Build.kt +++ b/core/src/main/kotlin/matterlink/jenkins/Build.kt @@ -1,8 +1,9 @@ package matterlink.jenkins import com.github.kittinunf.fuel.httpGet +import com.github.kittinunf.fuel.serialization.kotlinxDeserializerOf import com.github.kittinunf.result.Result -import com.google.gson.Gson +import kotlinx.serialization.json.JSON import matterlink.logger @@ -20,10 +21,10 @@ data class Build( val (request, response, result) = "$url/api/json" .httpGet() .header("User-Agent" to userAgent) - .responseString() + .responseObject(kotlinxDeserializerOf(loader = BuildWithDetails.serializer(), json = JSON.nonstrict)) return when (result) { is Result.Success -> { - gson.fromJson(result.value, BuildWithDetails::class.java) + result.value } is Result.Failure -> { logger.error(result.error.toString()) @@ -31,9 +32,5 @@ data class Build( } } } - - companion object { - val gson = Gson() - } } diff --git a/core/src/main/kotlin/matterlink/jenkins/BuildWithDetails.kt b/core/src/main/kotlin/matterlink/jenkins/BuildWithDetails.kt index f308412..d1c9240 100644 --- a/core/src/main/kotlin/matterlink/jenkins/BuildWithDetails.kt +++ b/core/src/main/kotlin/matterlink/jenkins/BuildWithDetails.kt @@ -1,12 +1,12 @@ package matterlink.jenkins +import kotlinx.serialization.Serializable import java.util.Date -//@JsonIgnoreProperties(ignoreUnknown = true) +@Serializable data class BuildWithDetails( val number: Int, val url: String, val artifacts: List, -// @JsonFormat(shape=JsonFormat.Shape.NUMBER, pattern="s") val timestamp: Date ) \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/jenkins/JenkinsServer.kt b/core/src/main/kotlin/matterlink/jenkins/JenkinsServer.kt index 8da7a8c..60a719a 100644 --- a/core/src/main/kotlin/matterlink/jenkins/JenkinsServer.kt +++ b/core/src/main/kotlin/matterlink/jenkins/JenkinsServer.kt @@ -2,8 +2,9 @@ package matterlink.jenkins import com.github.kittinunf.fuel.httpGet +import com.github.kittinunf.fuel.serialization.kotlinxDeserializerOf import com.github.kittinunf.result.Result -import com.google.gson.Gson +import kotlinx.serialization.json.JSON import matterlink.logger /** @@ -21,10 +22,10 @@ class JenkinsServer(val url: String) { val (_, _, result) = requestURL .httpGet() .header("User-Agent" to userAgent) - .responseString() + .responseObject(kotlinxDeserializerOf(loader = Job.serializer(), json = JSON.nonstrict)) return when (result) { is Result.Success -> { - gson.fromJson(result.value, Job::class.java) + result.value } is Result.Failure -> { logger.error(result.error.toString()) @@ -33,8 +34,4 @@ class JenkinsServer(val url: String) { } } - companion object { - val gson = Gson() - } - } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/jenkins/Job.kt b/core/src/main/kotlin/matterlink/jenkins/Job.kt index fba7d3e..7277509 100644 --- a/core/src/main/kotlin/matterlink/jenkins/Job.kt +++ b/core/src/main/kotlin/matterlink/jenkins/Job.kt @@ -1,11 +1,13 @@ package matterlink.jenkins +import kotlinx.serialization.Serializable + /** * Created by nikky on 03/02/18. * @author Nikky */ -//@JsonIgnoreProperties(ignoreUnknown = true) +@Serializable data class Job( val url: String, val name: String, diff --git a/core/src/main/kotlin/matterlink/update/CurseFile.kt b/core/src/main/kotlin/matterlink/update/CurseFile.kt index 15d9d64..83cf43e 100644 --- a/core/src/main/kotlin/matterlink/update/CurseFile.kt +++ b/core/src/main/kotlin/matterlink/update/CurseFile.kt @@ -1,8 +1,12 @@ package matterlink.update +import kotlinx.serialization.Serializable + +@Serializable data class CurseFile( - val downloadURL: String, + val downloadUrl: String, val fileName: String, + val fileNameOnDisk: String, val gameVersion: List, val releaseType: String, val fileStatus: String diff --git a/core/src/main/kotlin/matterlink/update/UpdateChecker.kt b/core/src/main/kotlin/matterlink/update/UpdateChecker.kt index 686d5ae..d4fbcb1 100644 --- a/core/src/main/kotlin/matterlink/update/UpdateChecker.kt +++ b/core/src/main/kotlin/matterlink/update/UpdateChecker.kt @@ -1,9 +1,11 @@ package matterlink.update -import com.google.gson.GsonBuilder +import com.github.kittinunf.fuel.core.extensions.cUrlString +import com.github.kittinunf.fuel.httpGet import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlinx.serialization.list import matterlink.api.ApiMessage import matterlink.bridge.MessageHandlerInst import matterlink.config.cfg @@ -12,9 +14,9 @@ import matterlink.handlers.LocationHandler import matterlink.instance import matterlink.jenkins.JenkinsServer import matterlink.logger -import java.io.BufferedReader -import java.net.HttpURLConnection -import java.net.URL +import com.github.kittinunf.fuel.serialization.kotlinxDeserializerOf +import com.github.kittinunf.result.Result +import kotlinx.serialization.json.JSON object UpdateChecker : CoroutineScope { override val coroutineContext = Job() + CoroutineName("UpdateChacker") @@ -61,41 +63,30 @@ object UpdateChecker : CoroutineScope { return } - val gson = GsonBuilder() -// .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) - .create() - logger.info("Checking for new versions...") - - val url = URL("https://staging_cursemeta.dries007.net/api/v3/direct/addon/287323/files") - val con = url.openConnection() as HttpURLConnection - - with(instance) { + val (request, response, result) = with(instance) { val useragent = "MatterLink/$modVersion MinecraftForge/$mcVersion-$forgeVersion (https://github.com/elytra/MatterLink)" logger.debug("setting User-Agent: '$useragent'") - con.setRequestProperty("User-Agent", useragent) + + "https://curse.nikky.moe/api/addon/287323/files".httpGet() + .header("User-Agent" to useragent) + .responseObject(kotlinxDeserializerOf(loader = CurseFile.serializer().list, json = JSON.nonstrict)) } - con.connect() - - val apiUpdateList = if (200 == con.responseCode) { //HTTP 200 OK - val buffer: BufferedReader = con.inputStream.bufferedReader() - - //put all of the buffer content onto the string - val content = buffer.readText() - logger.trace("updateData: $content") - - gson.fromJson(content, Array::class.java) - .filter { - it.fileStatus == "SemiNormal" && it.gameVersion.contains(instance.mcVersion) - } - .sortedByDescending { it.fileName.substringAfterLast(" ") } - - } else { - logger.error("Could not check for updates!") - return + val apiUpdateList = when(result) { + is Result.Success -> { + result.value + } + is Result.Failure -> { + logger.error("Could not check for updates!") + logger.error("cUrl: ${request.cUrlString()}") + logger.error("request: $request") + logger.error("response: $response") + return + } } + .filter { it.fileStatus == "SemiNormal" && it.gameVersion.contains(instance.mcVersion) } val modVersionChunks = instance.modVersion .substringBefore("-dev") @@ -133,13 +124,13 @@ object UpdateChecker : CoroutineScope { logger.info("Matterlink out of date! You are $count $version behind") possibleUpdates.forEach { - logger.info("version: ${it.fileName} download: ${it.downloadURL}") + logger.info("version: ${it.fileName} download: ${it.downloadUrl}") } - logger.warn("Mod out of date! New $version available at ${latest.downloadURL}") + logger.warn("Mod out of date! New $version available at ${latest.downloadUrl}") MessageHandlerInst.transmit( ApiMessage( - text = "MatterLink out of date! You are $count $version behind! Please download new version from ${latest.downloadURL}" + text = "MatterLink out of date! You are $count $version behind! Please download new version from ${latest.downloadUrl}" ) ) } diff --git a/scripts/start.sh b/scripts/start.sh index 948269e..f6aea17 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -4,7 +4,7 @@ java -jar forge-installer.jar --installServer FORGE_FILE=`grep "The server installed successfully, you should now be able to run the file " forge-installer.jar.log | tail -1` FORGE_FILE=${FORGE_FILE#"The server installed successfully, you should now be able to run the file "} -echo FORGE_FILE +echo $FORGE_FILE cp -f "$FORGE_FILE" forge.jar if [ ! $? -eq 0 ]; then