mirror of
https://gitlab.com/2009scape/09launcher.git
synced 2025-12-09 16:45:54 -07:00
Initial push
This commit is contained in:
commit
dee36e793b
27 changed files with 554 additions and 0 deletions
86
src/main/kotlin/Updater.kt
Normal file
86
src/main/kotlin/Updater.kt
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import java.awt.Image
|
||||
import java.io.File
|
||||
import java.io.RandomAccessFile
|
||||
import java.net.URL
|
||||
import java.util.concurrent.Executors
|
||||
import javax.swing.ImageIcon
|
||||
|
||||
object Updater {
|
||||
|
||||
private var status = UpdateStatus.DOWNLOADING
|
||||
|
||||
fun checkUpdate(): Boolean{
|
||||
val localMD5 = Checksum.getLocalChecksum(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME)
|
||||
val remoteMD5 = Checksum.getRemoteChecksum(Settings.DOWNLOAD_URL)
|
||||
|
||||
return localMD5 != remoteMD5
|
||||
}
|
||||
|
||||
fun runUpdate(){
|
||||
val t = Thread() {
|
||||
var oldText = MainWindow.loadingLabel.text
|
||||
MainWindow.loadingLabel.text = "Updating client..."
|
||||
MainWindow.playButton.isEnabled = false
|
||||
var downloaded = 0
|
||||
var lastBarUpdate = 0
|
||||
|
||||
val connection = URL(Settings.DOWNLOAD_URL).openConnection()
|
||||
connection.connect()
|
||||
|
||||
val length = connection.contentLength
|
||||
|
||||
if (!File(Settings.SAVE_DIR).exists()) {
|
||||
File(Settings.SAVE_DIR).mkdir()
|
||||
}
|
||||
if (File(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME).exists()) {
|
||||
File(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME).delete()
|
||||
}
|
||||
|
||||
File(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME).createNewFile()
|
||||
|
||||
val file = RandomAccessFile(Settings.SAVE_DIR + File.separator + Settings.SAVE_NAME, "rw")
|
||||
|
||||
val stream = connection.getInputStream()
|
||||
|
||||
while(status == UpdateStatus.DOWNLOADING){
|
||||
val buffer = if(length - downloaded > 1024){
|
||||
ByteArray(1024)
|
||||
} else {
|
||||
ByteArray(length - downloaded)
|
||||
}
|
||||
|
||||
val read = stream.read(buffer)
|
||||
|
||||
if(read == -1) break
|
||||
|
||||
val progress = ((downloaded / length.toFloat()) * 100).toInt()
|
||||
val barLength = 1 + ((progress / 100.0) * 695.0).toInt()
|
||||
|
||||
MainWindow.loadingLabel.text = "Updating client... $progress%"
|
||||
|
||||
if(barLength - lastBarUpdate > 5){
|
||||
MainWindow.loadingBar.icon = ImageIcon((MainWindow.loadingBar.icon as ImageIcon).image.getScaledInstance(barLength, 31, Image.SCALE_FAST))
|
||||
MainWindow.loadingBar.placeAt(103, MainWindow.height - 33, barLength, 31)
|
||||
lastBarUpdate = barLength
|
||||
}
|
||||
|
||||
file.write(buffer, 0, read)
|
||||
downloaded += read
|
||||
|
||||
if(downloaded >= length){
|
||||
MainWindow.loadingLabel.text = oldText
|
||||
MainWindow.playButton.isEnabled = true
|
||||
file.close()
|
||||
status = UpdateStatus.COMPLETE
|
||||
}
|
||||
}
|
||||
MainWindow.loadingBar.icon = ImageIcon((MainWindow.loadingBar.icon as ImageIcon).image.getScaledInstance(695, 31, Image.SCALE_FAST))
|
||||
MainWindow.loadingBar.placeAt(103, MainWindow.height - 33, 695, 31)
|
||||
}.start()
|
||||
}
|
||||
|
||||
internal enum class UpdateStatus{
|
||||
DOWNLOADING,
|
||||
COMPLETE
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue