Imports System.IO Imports System.Net Imports System.Net.Http Imports ModpackUpdater.Model Public Class ModpackInstaller Public Event InstallProgessUpdated(result As UpdateCheckResult, processedSyncs As Integer) Public Event CheckingProgressUpdated(toCheck As Integer, processed As Integer) Private ReadOnly updateConfig As ModpackConfig Private ReadOnly localPath As String Private httpClient As New HttpClient Public Sub New(updateConfig As ModpackConfig, localPath As String) Me.updateConfig = updateConfig Me.localPath = localPath End Sub Protected Overrides Sub Finalize() httpClient.Dispose() MyBase.Finalize() End Sub 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 End If End If Return result End Function Public Async Function InstallUpdates(checkResult As UpdateCheckResult) As Task(Of Boolean?) Dim modpackInfo As ModpackInfo If ModpackInfo.HasModpackInfo(localPath) Then modpackInfo = ModpackInfo.Load(localPath) Else modpackInfo = New ModpackInfo End If 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 Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) 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 action.IsDirectory Then If Directory.Exists(destFilePath) Then Directory.Delete(destFilePath, True) End If Else If File.Exists(destFilePath) Then File.Delete(destFilePath) End If End If Case UpdateActionType.Copy Dim srcFilePath As String = Path.Combine(localPath, action.SrcPath) If File.Exists(srcFilePath) Then Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) File.Copy(srcFilePath, destFilePath, True) End If Case UpdateActionType.Move Dim srcFilePath As String = Path.Combine(localPath, action.SrcPath) If File.Exists(srcFilePath) Then Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) File.Move(srcFilePath, destFilePath, True) End If End Select processed += 1 RaiseEvent InstallProgessUpdated(checkResult, processed) Next modpackInfo.Version = checkResult.LatestVersion modpackInfo.Save(localPath) Return True End Function End Class