Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Failing downloads no longer suspend the updater + added a summary screen
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed May 30, 2021
1 parent 079b605 commit 28b0c48
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package mynameisjeff.skyblockclientupdater.gui

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import mynameisjeff.skyblockclientupdater.SkyClientUpdater
import mynameisjeff.skyblockclientupdater.utils.TickTask
import mynameisjeff.skyblockclientupdater.utils.UpdateChecker
import net.minecraft.client.gui.GuiButton
import net.minecraft.client.gui.GuiScreen
Expand All @@ -22,13 +22,13 @@ import kotlin.concurrent.thread
* https://github.com/Skytils/SkytilsMod/blob/1.x/LICENSE
*/
class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>) : GuiScreen() {
private var failed = false
private var complete = false
private var exited = false
private var backButton: GuiButton = GuiButton(0, 0, 0, 200, 20, "")
private var progress = 0f

private var completedDownloads = 0
private var downloadedMods = arrayListOf<File>()
private var failedDownloadingMods = arrayListOf<File>()

override fun initGui() {
backButton.xPosition = width / 2 - 100
Expand All @@ -47,12 +47,12 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
launch(Dispatchers.IO) {
val jarName = update.second
val url = update.third
downloadUpdate(url, File(directory, jarName))
if (!failed) {
val file = File(directory, jarName)
downloadUpdate(url, file)
if (!failedDownloadingMods.contains(file)) {
downloadedMods.add(file)
UpdateChecker.deleteFileOnShutdown(update.first, jarName)
}
}.invokeOnCompletion {
completedDownloads++
}
}
} catch (ex: Exception) {
Expand All @@ -63,7 +63,7 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
}

private fun updateText() {
backButton.displayString = if (failed || complete) "Back" else "Cancel"
backButton.displayString = if (exited || complete) "Back" else "Cancel"
}

private fun downloadUpdate(url: String, location: File) {
Expand All @@ -75,14 +75,14 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
)
st.connect()
if (st.responseCode != HttpURLConnection.HTTP_OK) {
failed = true
failedDownloadingMods.add(location)
updateText()
println(url + " returned status code " + st.responseCode)
return
}
location.parentFile.mkdirs()
if (!location.exists() && !location.createNewFile()) {
failed = true
failedDownloadingMods.add(location)
updateText()
println("Couldn't create update file directory")
return
Expand All @@ -98,7 +98,6 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
// Cancelled
fos.close()
fis.close()
failed = true
return
}
total += count.toLong()
Expand All @@ -109,12 +108,11 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
fos.close()
fis.close()
if (exited) {
failed = true
return
}
} catch (ex: Exception) {
ex.printStackTrace()
failed = true
failedDownloadingMods.add(location)
updateText()
}
}
Expand All @@ -128,9 +126,9 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
drawDefaultBackground()
when {
failed -> drawCenteredString(
exited -> drawCenteredString(
mc.fontRendererObj,
EnumChatFormatting.RED.toString() + "Update download failed",
EnumChatFormatting.RED.toString() + "Update download exited",
width / 2,
height / 2,
-0x1
Expand All @@ -142,13 +140,15 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
height / 2,
0xFFFFFF
)
downloadedMods.size + failedDownloadingMods.size == updatingMods.size -> {
complete = true
updateText()
TickTask(1) {
mc.displayGuiScreen(UpdateSummaryScreen(downloadedMods, failedDownloadingMods))
}
}
else -> {
drawCenteredString(mc.fontRendererObj,"Downloading... ${UpdateChecker.needsDelete.size} / ${updatingMods.size}", width / 2, height / 2, 0xFFFFFF)
if (!failed && completedDownloads == updatingMods.size) {
mc.shutdown()
complete = true
updateText()
}
}
}
super.drawScreen(mouseX, mouseY, partialTicks)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mynameisjeff.skyblockclientupdater.gui

import net.minecraft.client.gui.GuiButton
import net.minecraft.client.gui.GuiScreen
import java.awt.Color
import java.io.File

class UpdateSummaryScreen(val downloadedMods: ArrayList<File>, val failedDownloadingMods: ArrayList<File>) : GuiScreen() {

override fun initGui() {
buttonList.add(GuiButton(0, width / 2 - 100, height - 75, 200, 20, "Quit Game"))
}

override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
drawDefaultBackground()

drawCenteredString(fontRendererObj, "${downloadedMods.size + failedDownloadingMods.size} updates requested", width / 2, 20, 0xFFFFFF)
drawCenteredString(fontRendererObj, "${downloadedMods.size} updates successful", width / 4, 30, Color.GREEN.rgb)
drawCenteredString(fontRendererObj, "${failedDownloadingMods.size} updates failed", 3 * (width / 4), 30, Color.RED.rgb)

for (i in 0 until downloadedMods.size) drawCenteredString(fontRendererObj,
downloadedMods[i].name, width / 4, 30 + i.inc() * fontRendererObj.FONT_HEIGHT, Color.GREEN.rgb)
for (i in 0 until failedDownloadingMods.size) drawCenteredString(fontRendererObj,
failedDownloadingMods[i].name, 3 * (width / 4), 30 + i.inc() * fontRendererObj.FONT_HEIGHT, Color.RED.rgb)
super.drawScreen(mouseX, mouseY, partialTicks)
}

override fun actionPerformed(button: GuiButton) {
mc.shutdown()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Skytils - Hypixel Skyblock Quality of Life Mod
* Copyright (C) 2021 Skytils
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package mynameisjeff.skyblockclientupdater.utils

import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class TickTask(private var ticks: Int = 0, private val task: () -> Unit) {

@SubscribeEvent
fun onTick(event: TickEvent.ClientTickEvent) {
if (event.phase != TickEvent.Phase.START) return
if (ticks <= 0) {
task()
MinecraftForge.EVENT_BUS.unregister(this)
}
ticks--
}

init {
MinecraftForge.EVENT_BUS.register(this)
}
}

0 comments on commit 28b0c48

Please sign in to comment.