blob: ef1e3767078e70eee655257c5a26c619a0ba57dd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package cc.woverflow.chatting.updater
import cc.woverflow.chatting.Chatting
import cc.woverflow.chatting.config.ChattingConfig
import gg.essential.api.EssentialAPI
import gg.essential.api.utils.Multithreading
import gg.essential.api.utils.WebUtil.downloadToFile
import gg.essential.api.utils.WebUtil.fetchJSON
import gg.essential.universal.UDesktop
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.versioning.DefaultArtifactVersion
import java.io.File
import java.io.IOException
object Updater {
var updateUrl = ""
var latestTag: String? = null
var shouldUpdate = false
fun update() {
Multithreading.runAsync {
try {
val latestRelease =
fetchJSON("https://api.github.com/repos/W-OVERFLOW/${Chatting.ID}/releases/latest").getObject()
latestTag = latestRelease["tag_name"].asString
val currentVersion =
DefaultArtifactVersion(Chatting.VER.substringBefore("-"))
val latestVersion = DefaultArtifactVersion(latestTag!!.substringAfter("v").substringBefore("-"))
if (currentVersion >= latestVersion) {
if (currentVersion != latestVersion || !Chatting.VER.contains("-")) {
return@runAsync
}
}
updateUrl =
latestRelease["assets"].asJsonArray[0].asJsonObject["browser_download_url"]
.asString
if (updateUrl.isNotEmpty()) {
if (ChattingConfig.showUpdate) {
EssentialAPI.getNotifications().push(
Chatting.NAME,
"${Chatting.NAME} has a new update ($latestTag)! Click here to download it automatically!"
) { EssentialAPI.getGuiUtil().openScreen(DownloadGui()) }
}
shouldUpdate = true
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun download(url: String, file: File): Boolean {
var url = url
if (file.exists()) return true
url = url.replace(" ", "%20")
try {
downloadToFile(url, file, "${Chatting.NAME}/${Chatting.VER}")
} catch (e: Exception) {
e.printStackTrace()
return false
}
return file.exists()
}
fun addShutdownHook() {
EssentialAPI.getShutdownHookUtil().register {
println("Opening Deleter task...")
try {
val runtime = javaRuntime
if (Minecraft.isRunningOnMac) {
UDesktop.open(Chatting.jarFile.parentFile)
}
val file = File(Chatting.modDir.parentFile, "Deleter-1.2.jar")
Runtime.getRuntime()
.exec("\"" + runtime + "\" -jar \"" + file.absolutePath + "\" \"" + Chatting.jarFile.absolutePath + "\"")
} catch (e: Exception) {
e.printStackTrace()
}
}
}
/**
* Gets the current Java runtime being used.
*
* @link https://stackoverflow.com/a/47925649
*/
@get:Throws(IOException::class)
val javaRuntime: String
get() {
val os = System.getProperty("os.name")
val java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
if (os != null && os.lowercase().startsWith("windows")) "java.exe" else "java"
if (!File(java).isFile) {
throw IOException("Unable to find suitable java runtime at $java")
}
return java
}
}
|