Skip to content

Commit

Permalink
Add updater
Browse files Browse the repository at this point in the history
  • Loading branch information
LionZXY committed Apr 30, 2019
1 parent 8b0237c commit 3272afe
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 65 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ task fatJar(type: Jar) {
with jar
}

mainClassName = "ru.lionzxy.tplauncher.MainKt"

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ru/lionzxy/tplauncher/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package ru.lionzxy.tplauncher.config
data class Config(
var profile: Profile? = null,
var lastUpdate: Long? = null,
var jrePath: String? = null
var lastUpdateFromChangeLog: Long? = null
)

This file was deleted.

54 changes: 54 additions & 0 deletions src/main/kotlin/ru/lionzxy/tplauncher/downloader/Updater.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.lionzxy.tplauncher.downloader

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.google.gson.reflect.TypeToken
import ru.lionzxy.tplauncher.utils.ConfigHelper
import sk.tomsik68.mclauncher.api.ui.IProgressMonitor
import sk.tomsik68.mclauncher.util.FileUtils
import sk.tomsik68.mclauncher.util.HttpUtils
import java.io.File

const val UPDATER_JSON_URL = "http://download.glitchless.ru/minecraft_dist/first_server/changelog.json"
const val HOST_URL = "http://download.glitchless.ru/minecraft_dist/first_server/"

class Updater {
val changes = HashMap<String, Action>()
val gson = Gson()
var lastChangeTimestamp = 0L

fun initUpdater() {
val lastUpdateTimestamp = ConfigHelper.config.lastUpdateFromChangeLog ?: 0
val json = HttpUtils.httpGet(UPDATER_JSON_URL)
val type = object : TypeToken<Map<String, Map<String, Action>>>() {}.type
val map = gson.fromJson<Map<String, Map<String, Action>>>(json, type)
val changeLog = map.map { it.key.toLong() to it.value }
.filter { it.first > lastUpdateTimestamp }.sortedBy { it.first }
lastChangeTimestamp = changeLog.lastOrNull()?.first ?: 0
changeLog.forEach { changes.putAll(it.second) }
}

fun update(monitor: IProgressMonitor) {
val base = ConfigHelper.getDefaultDirectory()
changes.forEach {
val file = File(base, it.key)
if (it.value == Action.REMOVE) {
file.delete()
} else if (it.value == Action.ADD) {
file.delete()
monitor.setStatus("Downloading ${it.key}")
FileUtils.downloadFileWithProgress(HOST_URL + it.key, file, monitor)
}
}
ConfigHelper.writeToConfig {
lastUpdateFromChangeLog = lastChangeTimestamp
}
}
}

enum class Action(code: Int) {
@SerializedName("0")
REMOVE(0),
@SerializedName("1")
ADD(1)
}
14 changes: 12 additions & 2 deletions src/main/kotlin/ru/lionzxy/tplauncher/utils/MinecraftLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.lionzxy.tplauncher.utils
import ru.lionzxy.tplauncher.downloader.DefaultLaunchSettings
import sk.tomsik68.mclauncher.api.common.mc.MinecraftInstance
import sk.tomsik68.mclauncher.api.login.ISession
import sk.tomsik68.mclauncher.api.servers.ServerInfo
import sk.tomsik68.mclauncher.api.versions.IVersion
import sk.tomsik68.mclauncher.impl.versions.mcdownload.MCDownloadVersionList
import java.io.File
Expand All @@ -13,7 +14,14 @@ object MinecraftLauncher {
fun launch(minecraft: MinecraftInstance, session: ISession, java: File? = null) {
val version = getVersion(minecraft)
val launchCommands =
version.launcher.getLaunchCommand(session, minecraft, null, version, DefaultLaunchSettings(java), null)
version.launcher.getLaunchCommand(
session,
minecraft,
ServerInfo("glitchless.ru", "Glitchless Server", null, 25565),
version,
DefaultLaunchSettings(java),
null
)
launchCommands.forEach { println(it) }

val pb = ProcessBuilder(launchCommands)
Expand All @@ -37,4 +45,6 @@ object MinecraftLauncher {
cacheVersion = versionList.retrieveVersionInfo("1.12.2-forge1.12.2-14.23.5.2836")
return cacheVersion!!
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.lionzxy.tplauncher.view.controllers
import net.lingala.zip4j.core.ZipFile
import ru.lionzxy.tplauncher.config.Profile
import ru.lionzxy.tplauncher.downloader.OfflineSession
import ru.lionzxy.tplauncher.downloader.Updater
import ru.lionzxy.tplauncher.utils.ConfigHelper
import ru.lionzxy.tplauncher.utils.LocalizationHelper
import ru.lionzxy.tplauncher.utils.MinecraftLauncher
Expand Down Expand Up @@ -43,7 +44,6 @@ class MainController(val mainWindow: MainWindow) {
}

fun downloadAndLaunch() = runAsync {
var jrePathLocal = ConfigHelper.config.jrePath
if (ConfigHelper.config.lastUpdate == null) {
mainWindow.showProgress(true)
mainWindow.showDownloadAndPlayButton(false)
Expand All @@ -54,20 +54,29 @@ class MainController(val mainWindow: MainWindow) {
}
mainWindow.showProgress(false)
}

checkAndDownloadUpdate()

runOnUi {
mainWindow.close()
}
if (jrePathLocal != null) {
if (ConfigHelper.getJREPathFile().exists()) {
MinecraftLauncher.launch(minecraftInstance, session!!, File(ConfigHelper.getJREPathFile().readText()))
} else {
MinecraftLauncher.launch(minecraftInstance, session!!)
}
}

private fun checkAndDownloadUpdate() {
val updater = Updater()
updater.initUpdater()
updater.update(mainWindow)
}

private fun startDownloadZip() {
val dist = File(ConfigHelper.getTemporaryDirectory(), "minecraft.zip")
mainWindow.setStatus(LocalizationHelper.getString("download_mods"))
FileUtils.downloadFileWithProgress("http://download.glitchless.ru/0.0.1_minecraft.zip", dist, mainWindow)
FileUtils.downloadFileWithProgress("http://download.glitchless.ru/0.0.3_minecraft.zip", dist, mainWindow)
val zipFile = ZipFile(dist)
mainWindow.setStatus(LocalizationHelper.getString("unzip_mods"))
mainWindow.setProgress(-1)
Expand Down

0 comments on commit 3272afe

Please sign in to comment.