Files
minecraft-modpack-updater/ModpackUpdater/AppUpdater.vb
2024-06-17 19:02:04 +02:00

66 lines
2.1 KiB
VB.net

Imports System.IO
Imports System.Net.Http
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters
Public Class AppUpdater
Private Class UpdateInfo
<JsonConverter(GetType(StringEnumConverter))>
Public Property Version As Version
Public Property DownloadUrl As String
End Class
Private Const UPDATE_URL As String = "https://git.pilzinsel64.de/gaming/minecraft/minecraft-modpack-updater/-/snippets/3/raw??????????"
Private ReadOnly httpClient As New HttpClient
Private info As UpdateInfo
Public Async Function Check() As Task(Of Boolean)
Dim appFileName = Pilz.IO.Extensions.GetExecutablePath()
Dim hasUpdate As Boolean = False
Try
Dim result = Await httpClient.GetStringAsync(UPDATE_URL)
info = JsonConvert.DeserializeObject(Of UpdateInfo)(result)
If info IsNot Nothing AndAlso info.Version < New Version(Application.ProductVersion) Then
hasUpdate = True
End If
Catch ex As Exception
End Try
Return hasUpdate
End Function
Public Async Function Install() As Task
Dim client As New HttpClient
Dim tempFileName = Path.GetTempFileName
Dim appFileName = Pilz.IO.Extensions.GetExecutablePath()
Dim oldFileName = appFileName & ".old"
'Delete old file
Try
File.Delete(oldFileName)
Catch ex As Exception
End Try
'Download the new file
Using tempFileStream As New FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite)
Dim downloadStream As Stream = Nothing
Try
downloadStream = Await client.GetStreamAsync(info.DownloadUrl)
Await downloadStream.CopyToAsync(tempFileStream)
Catch
Finally
downloadStream?.Dispose()
End Try
End Using
'Replace current application file with new file
File.Move(appFileName, oldFileName, True)
File.Move(tempFileName, appFileName)
End Function
End Class