Add basic update checking.

This commit is contained in:
Logan Gorence 2021-12-22 08:11:22 +00:00
parent cadfcf2cda
commit 5ad75a2580
No known key found for this signature in database
GPG Key ID: 9743CEF10935949A
7 changed files with 96 additions and 2 deletions

View File

@ -83,6 +83,7 @@ subprojects {
// Serialization
implementation("com.charleskorn.kaml:kaml:0.38.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1")
// Paper API
compileOnly("io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT")

View File

@ -1,2 +1,3 @@
dependencies {
// TODO: might be able to ship all dependencies in core? are we duplicating classes in JARs?
}

View File

@ -3,6 +3,7 @@ package cloud.kubelet.foundation.core
import cloud.kubelet.foundation.core.command.BackupCommand
import cloud.kubelet.foundation.core.command.GamemodeCommand
import cloud.kubelet.foundation.core.command.LeaderboardCommand
import cloud.kubelet.foundation.core.command.UpdateCommand
import net.kyori.adventure.text.Component
import org.bukkit.GameMode
import org.bukkit.command.CommandExecutor
@ -41,6 +42,7 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
// Register commands.
registerCommandExecutor("fbackup", BackupCommand(this, backupPath))
registerCommandExecutor("fupdate", UpdateCommand())
registerCommandExecutor(listOf("survival", "s"), GamemodeCommand(GameMode.SURVIVAL))
registerCommandExecutor(listOf("creative", "c"), GamemodeCommand(GameMode.CREATIVE))
registerCommandExecutor(listOf("adventure", "a"), GamemodeCommand(GameMode.ADVENTURE))

View File

@ -0,0 +1,37 @@
package cloud.kubelet.foundation.core.command
import cloud.kubelet.foundation.core.update.UpdateUtil
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
class UpdateCommand : CommandExecutor {
override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
// TODO: Move to separate thread?
val modules = UpdateUtil.fetchManifest()
val plugins = sender.server.pluginManager.plugins.associateBy { it.name.lowercase() }
sender.sendMessage("Updates:")
modules.forEach { (name, manifest) ->
// Dumb naming problem. Don't want to fix it right now.
val plugin = if (name == "foundation-core") {
plugins["foundation"]
} else {
plugins[name.lowercase()]
}
if (plugin == null) {
sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})")
} else {
sender.sendMessage("${plugin.name}: ${manifest.version} (have ${plugin.description.version})")
}
}
return true
}
}

View File

@ -0,0 +1,9 @@
package cloud.kubelet.foundation.core.update
import kotlinx.serialization.Serializable
@Serializable
data class ModuleManifest(
val version: String,
val artifacts: List<String>,
)

View File

@ -0,0 +1,40 @@
package cloud.kubelet.foundation.core.update
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
object UpdateUtil {
private val client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build()
// TODO: Add environment variable override. Document it.
private const val manifestUrl =
"https://git.gorence.io/lgorence/foundation/-/jobs/artifacts/main/raw/build/manifests/update.json?job=build"
fun fetchManifest() = fetchFile(
manifestUrl, MapSerializer(String.serializer(), ModuleManifest.serializer()),
)
private inline fun <reified T> fetchFile(url: String, strategy: DeserializationStrategy<T>): T {
val request = HttpRequest
.newBuilder()
.GET()
.uri(URI.create(url))
.build()
val response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
)
return Json.decodeFromString(
strategy,
response.body()
)
}
}

View File

@ -8,9 +8,13 @@ authors:
- kubelet
commands:
fbackup:
description: Foundation Backup
description: Back the server up now
usage: /fbackup
permission: foundation.backup
permission: foundation.command.backup
fupdate:
description: List updates to the Foundation plugin
usage: /fupdate
permission: foundation.command.update
survival:
description: Switch to survival gamemode
usage: /survival