Update checker

This commit is contained in:
DaMachinator 2018-02-16 15:09:47 -05:00
parent d93e0249b2
commit 7ea258fdc4
6 changed files with 110 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import matterlink.command.CommandMatterlink
import matterlink.command.IMinecraftCommandSender import matterlink.command.IMinecraftCommandSender
import matterlink.command.MatterlinkCommandSender import matterlink.command.MatterlinkCommandSender
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.update.UpdateChecker
import net.minecraft.util.text.TextComponentString import net.minecraft.util.text.TextComponentString
import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.FMLCommonHandler
import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.Mod
@ -51,6 +52,7 @@ object MatterLink : IMatterLink() {
event.registerServerCommand(CommandMatterlink()) event.registerServerCommand(CommandMatterlink())
serverStartTime = System.currentTimeMillis() serverStartTime = System.currentTimeMillis()
connect() connect()
Thread(UpdateChecker()).start()
} }
@Mod.EventHandler @Mod.EventHandler
@ -72,4 +74,7 @@ object MatterLink : IMatterLink() {
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO),formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender
override val mcVersion: String = "1.10.2"
override val modVersion: String = VERSION
} }

View File

@ -4,6 +4,7 @@ import matterlink.command.CommandMatterlink
import matterlink.command.IMinecraftCommandSender import matterlink.command.IMinecraftCommandSender
import matterlink.command.MatterlinkCommandSender import matterlink.command.MatterlinkCommandSender
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.update.UpdateChecker
import net.minecraft.util.text.TextComponentString import net.minecraft.util.text.TextComponentString
import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.FMLCommonHandler
import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.Mod
@ -51,6 +52,7 @@ object MatterLink : IMatterLink() {
event.registerServerCommand(CommandMatterlink()) event.registerServerCommand(CommandMatterlink())
serverStartTime = System.currentTimeMillis() serverStartTime = System.currentTimeMillis()
connect() connect()
Thread(UpdateChecker()).start()
} }
@Mod.EventHandler @Mod.EventHandler
@ -72,4 +74,7 @@ object MatterLink : IMatterLink() {
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO),formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender
override val mcVersion: String = "1.11.2"
override val modVersion: String = VERSION
} }

View File

@ -4,6 +4,7 @@ import matterlink.command.CommandMatterlink
import matterlink.command.IMinecraftCommandSender import matterlink.command.IMinecraftCommandSender
import matterlink.command.MatterlinkCommandSender import matterlink.command.MatterlinkCommandSender
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.update.UpdateChecker
import net.minecraft.util.text.TextComponentString import net.minecraft.util.text.TextComponentString
import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.FMLCommonHandler
import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.Mod
@ -51,6 +52,7 @@ object MatterLink : IMatterLink() {
event.registerServerCommand(CommandMatterlink()) event.registerServerCommand(CommandMatterlink())
serverStartTime = System.currentTimeMillis() serverStartTime = System.currentTimeMillis()
connect() connect()
Thread(UpdateChecker()).start()
} }
@Mod.EventHandler @Mod.EventHandler
@ -72,4 +74,6 @@ object MatterLink : IMatterLink() {
logger.log(Level.toLevel(level, Level.INFO),formatString, *data) logger.log(Level.toLevel(level, Level.INFO),formatString, *data)
override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender
override val mcVersion: String = "1.12.2"
override val modVersion: String = VERSION
} }

View File

@ -11,9 +11,11 @@ import matterlink.config.cfg
lateinit var instance: IMatterLink lateinit var instance: IMatterLink
abstract class IMatterLink { abstract class IMatterLink {
// var interrupted: Boolean = false abstract val mcVersion : String
abstract var commandSender: IMinecraftCommandSender abstract val modVersion : String
abstract var commandSender: IMinecraftCommandSender
//var interrupted: Boolean = false
abstract fun wrappedSendToPlayers(msg: String) abstract fun wrappedSendToPlayers(msg: String)

View File

@ -0,0 +1,19 @@
package matterlink.update
import com.google.gson.Gson
data class ApiUpdate (
val downloadURL: String,
val fileName: String,
val gameVersion: Array<String>,
val releaseType: String
) {
companion object {
val gson = Gson()
fun decode(json: String): ApiUpdate {
return gson.fromJson(json, ApiUpdate::class.java)
}
}
}

View File

@ -0,0 +1,73 @@
package matterlink.update
import com.google.gson.Gson
import matterlink.bridge.ApiMessage
import matterlink.bridge.MessageHandler
import matterlink.config.cfg
import matterlink.instance
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import java.io.BufferedReader
import java.util.regex.Pattern
class UpdateChecker : Runnable {
override fun run() {
instance.info("Checking for new versions...")
val ApiUpdateList : Array<ApiUpdate>
val client : HttpClient = HttpClients.createDefault()
val response : HttpResponse = client.execute(HttpGet("https://cursemeta.nikky.moe/api/addon/287323/files"))
if (200 == response.statusLine.statusCode) { //HTTP 200 OK
val buffer : BufferedReader = response.entity.content.bufferedReader()
//put all of the buffer content onto the string
var content : String = ""
var line : String? = buffer.readLine()
while (line != null) {
instance.debug(line)
content += line
line = buffer.readLine()
}
instance.debug("updateData: $content")
val gson = Gson()
ApiUpdateList = gson.fromJson<Array<ApiUpdate>>(content,Array<ApiUpdate>::class.java)
} else {
instance.error("Could not check for updates!")
return
}
val possibleUpdates = HashMap<String,ApiUpdate>()
var maxVersion : String = ""
ApiUpdateList.forEach {
//TODO: fix this if we ever release jars that support multiple versions
if (it.gameVersion[0] == instance.mcVersion) {
if (Pattern.matches("[mM]atter[lL]ink \\d+\\.\\d+\\.\\d+-\\d+\\.\\d+\\.?\\d*",it.fileName) ) {
val version : String = it.fileName.split("-")[1]
instance.debug(version)
possibleUpdates.set(version,it)
if (version.compareTo(maxVersion)>0 || maxVersion.equals("")) maxVersion = version
}
}
}
if (maxVersion.isEmpty()) return
if (maxVersion.compareTo(instance.modVersion)<0) {
val latest : ApiUpdate? = possibleUpdates[maxVersion]
if (latest != null) {
instance.warn("Mod out of date! New version available at ${latest.downloadURL}")
MessageHandler.transmit(ApiMessage(
username = cfg!!.relay.systemUser,
text = "Matterlink out of date! Please download new version from ${latest.downloadURL}"
))
} else {
instance.fatal("Severe error in update checker!")
}
}
}
}