1
1
package mynameisjeff.skyblockclientupdater.gui
2
2
3
3
import kotlinx.coroutines.Dispatchers
4
- import kotlinx.coroutines.job
5
4
import kotlinx.coroutines.launch
6
5
import kotlinx.coroutines.runBlocking
7
6
import mynameisjeff.skyblockclientupdater.SkyClientUpdater
7
+ import mynameisjeff.skyblockclientupdater.utils.TickTask
8
8
import mynameisjeff.skyblockclientupdater.utils.UpdateChecker
9
9
import net.minecraft.client.gui.GuiButton
10
10
import net.minecraft.client.gui.GuiScreen
@@ -22,13 +22,13 @@ import kotlin.concurrent.thread
22
22
* https://github.com/Skytils/SkytilsMod/blob/1.x/LICENSE
23
23
*/
24
24
class UpdateScreen (private val updatingMods : List <Triple <File , String , String >>) : GuiScreen() {
25
- private var failed = false
26
25
private var complete = false
27
26
private var exited = false
28
27
private var backButton: GuiButton = GuiButton (0 , 0 , 0 , 200 , 20 , " " )
29
28
private var progress = 0f
30
29
31
- private var completedDownloads = 0
30
+ private var downloadedMods = arrayListOf<File >()
31
+ private var failedDownloadingMods = arrayListOf<File >()
32
32
33
33
override fun initGui () {
34
34
backButton.xPosition = width / 2 - 100
@@ -47,12 +47,12 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
47
47
launch(Dispatchers .IO ) {
48
48
val jarName = update.second
49
49
val url = update.third
50
- downloadUpdate(url, File (directory, jarName))
51
- if (! failed) {
50
+ val file = File (directory, jarName)
51
+ downloadUpdate(url, file)
52
+ if (! failedDownloadingMods.contains(file)) {
53
+ downloadedMods.add(file)
52
54
UpdateChecker .deleteFileOnShutdown(update.first, jarName)
53
55
}
54
- }.invokeOnCompletion {
55
- completedDownloads++
56
56
}
57
57
}
58
58
} catch (ex: Exception ) {
@@ -63,7 +63,7 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
63
63
}
64
64
65
65
private fun updateText () {
66
- backButton.displayString = if (failed || complete) " Back" else " Cancel"
66
+ backButton.displayString = if (exited || complete) " Back" else " Cancel"
67
67
}
68
68
69
69
private fun downloadUpdate (url : String , location : File ) {
@@ -75,14 +75,14 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
75
75
)
76
76
st.connect()
77
77
if (st.responseCode != HttpURLConnection .HTTP_OK ) {
78
- failed = true
78
+ failedDownloadingMods.add(location)
79
79
updateText()
80
80
println (url + " returned status code " + st.responseCode)
81
81
return
82
82
}
83
83
location.parentFile.mkdirs()
84
84
if (! location.exists() && ! location.createNewFile()) {
85
- failed = true
85
+ failedDownloadingMods.add(location)
86
86
updateText()
87
87
println (" Couldn't create update file directory" )
88
88
return
@@ -98,7 +98,6 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
98
98
// Cancelled
99
99
fos.close()
100
100
fis.close()
101
- failed = true
102
101
return
103
102
}
104
103
total + = count.toLong()
@@ -109,12 +108,11 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
109
108
fos.close()
110
109
fis.close()
111
110
if (exited) {
112
- failed = true
113
111
return
114
112
}
115
113
} catch (ex: Exception ) {
116
114
ex.printStackTrace()
117
- failed = true
115
+ failedDownloadingMods.add(location)
118
116
updateText()
119
117
}
120
118
}
@@ -128,9 +126,9 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
128
126
override fun drawScreen (mouseX : Int , mouseY : Int , partialTicks : Float ) {
129
127
drawDefaultBackground()
130
128
when {
131
- failed -> drawCenteredString(
129
+ exited -> drawCenteredString(
132
130
mc.fontRendererObj,
133
- EnumChatFormatting .RED .toString() + " Update download failed " ,
131
+ EnumChatFormatting .RED .toString() + " Update download exited " ,
134
132
width / 2 ,
135
133
height / 2 ,
136
134
- 0x1
@@ -142,13 +140,15 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
142
140
height / 2 ,
143
141
0xFFFFFF
144
142
)
143
+ downloadedMods.size + failedDownloadingMods.size == updatingMods.size -> {
144
+ complete = true
145
+ updateText()
146
+ TickTask (1 ) {
147
+ mc.displayGuiScreen(UpdateSummaryScreen (downloadedMods, failedDownloadingMods))
148
+ }
149
+ }
145
150
else -> {
146
151
drawCenteredString(mc.fontRendererObj," Downloading... ${UpdateChecker .needsDelete.size} / ${updatingMods.size} " , width / 2 , height / 2 , 0xFFFFFF )
147
- if (! failed && completedDownloads == updatingMods.size) {
148
- mc.shutdown()
149
- complete = true
150
- updateText()
151
- }
152
152
}
153
153
}
154
154
super .drawScreen(mouseX, mouseY, partialTicks)
0 commit comments