use new system via an update info

This commit is contained in:
2023-05-04 11:12:21 +02:00
parent 6dd6721667
commit 0d7e570676
9 changed files with 209 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports WebDav
@@ -11,12 +12,18 @@ Public Class UpdateInstaller
Private ReadOnly updateConfig As UpdateConfig
Private ReadOnly localPath As String
Private webdavClient As WebDavClient = Nothing
Private httpClient As New HttpClient
Public Sub New(updateConfig As UpdateConfig, localPath As String)
Me.updateConfig = updateConfig
Me.localPath = localPath
End Sub
Protected Overrides Sub Finalize()
httpClient.Dispose()
MyBase.Finalize()
End Sub
Private Function CreateClient() As WebDavClient
If webdavClient Is Nothing Then
Dim params As New WebDavClientParams With {
@@ -29,13 +36,57 @@ Public Class UpdateInstaller
Return webdavClient
End Function
Private Async Function DownloadUpdateInfos() As Task(Of UpdateInfos)
Dim content As String = Await httpClient.GetStringAsync(updateConfig.UpdateUrl)
Return UpdateInfos.Parse(content)
End Function
Public Async Function CheckForUpdates(ignoreRevmoedFiles As Boolean) As Task(Of UpdateCheckResult)
Dim infos As UpdateInfos = Await DownloadUpdateInfos()
Dim result As New UpdateCheckResult
If infos IsNot Nothing AndAlso infos.Updates.Any Then
Dim updatesOrderes = infos.Updates.OrderByDescending(Function(n) n.Version)
result.LatestVersion = updatesOrderes.First.Version
If ModpackInfo.HasModpackInfo(localPath) Then
Dim modpackInfo As ModpackInfo = ModpackInfo.Load(localPath)
result.CurrentVersion = modpackInfo.Version
Dim checkingVersionIndex As Integer = 0
Dim checkingVersion As UpdateInfo = updatesOrderes(checkingVersionIndex)
Do While checkingVersion IsNot Nothing AndAlso checkingVersion.Version > result.CurrentVersion
Dim actionsToAdd As New List(Of UpdateAction)
For Each action In checkingVersion.Actions
If Not result.UpdateActions.Any(Function(n) n.DestPath = action.DestPath) Then
actionsToAdd.Add(action)
End If
Next
result.UpdateActions.InsertRange(0, actionsToAdd)
checkingVersionIndex += 1
checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex)
Loop
Else
Await CheckForUpdatesLegacy(result, ignoreRevmoedFiles)
End If
End If
Return result
End Function
Private Async Function CheckForUpdatesLegacy(result As UpdateCheckResult, ignoreRevmoedFiles As Boolean) As Task
Dim client As WebDavClient = CreateClient()
Dim resTopFolder As WebDavResource = Nothing
Dim checkedFiles = New List(Of String)()
Dim responseSuccessfull As Boolean = False
result.CurrentVersion = New Version("0.0.0.0")
result.IsLegacy = True
'Check for new & updated files
Dim response = Await client.Propfind(String.Empty, New PropfindParameters() With {.ApplyTo = ApplyTo.Propfind.ResourceAndAncestors})
If resTopFolder Is Nothing AndAlso response.IsSuccessful AndAlso response.Resources.Any() Then
@@ -101,11 +152,64 @@ Public Class UpdateInstaller
End If
Next
End If
Return result
End Function
Public Async Function InstallUpdates(checkResult As UpdateCheckResult, Optional ignoreActions As UpdateSyncAction = UpdateSyncAction.None) As Task(Of Boolean?)
Dim isSuccessfully As Boolean
Dim modpackInfo As ModpackInfo
If ModpackInfo.HasModpackInfo(localPath) Then
modpackInfo = ModpackInfo.Load(localPath)
Else
modpackInfo = New ModpackInfo
End If
If checkResult.IsLegacy Then
isSuccessfully = Await InstallUpdatesLegacy(checkResult, ignoreActions)
Else
Dim processed As Integer = 0
For Each action As UpdateAction In checkResult.UpdateActions
Dim destFilePath As String = Path.Combine(localPath, action.DestPath)
Select Case action.Type
Case UpdateActionType.Update
Dim sRemote As Stream = Await httpClient.GetStreamAsync(action.DownloadUrl)
Dim fs As New FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite)
Await sRemote.CopyToAsync(fs)
sRemote.Close()
fs.Close()
Case UpdateActionType.Delete
If File.Exists(destFilePath) Then
File.Delete(destFilePath)
End If
Case UpdateActionType.Copy
Dim srcFilePath As String = Path.Combine(localPath, action.SrcPath)
If File.Exists(srcFilePath) Then
File.Copy(srcFilePath, destFilePath, True)
End If
Case UpdateActionType.Move
Dim srcFilePath As String = Path.Combine(localPath, action.SrcPath)
If File.Exists(srcFilePath) Then
File.Move(srcFilePath, destFilePath, True)
End If
End Select
processed += 1
RaiseEvent InstallProgessUpdated(checkResult, processed)
Next
isSuccessfully = True
End If
If isSuccessfully Then
modpackInfo.Version = checkResult.LatestVersion
modpackInfo.Save(localPath)
End If
Return isSuccessfully
End Function
Private Async Function InstallUpdatesLegacy(checkResult As UpdateCheckResult, Optional ignoreActions As UpdateSyncAction = UpdateSyncAction.None) As Task(Of Boolean?)
Dim client As WebDavClient = CreateClient()
Dim success As Boolean? = False
Dim processed As Integer = 0