install: rework ModpackInstaller (incomplete)

This commit is contained in:
2024-06-17 18:30:57 +02:00
parent a4d5fff876
commit 9c43055580
6 changed files with 95 additions and 53 deletions

View File

@@ -1,4 +1,5 @@
Imports System.IO Imports System.IO
Imports System.IO.Compression
Imports System.Net Imports System.Net
Imports System.Net.Http Imports System.Net.Http
@@ -28,17 +29,23 @@ Public Class ModpackInstaller
Return UpdateInfos.Parse(content) Return UpdateInfos.Parse(content)
End Function End Function
Public Async Function CheckForUpdates(ignoreRevmoedFiles As Boolean) As Task(Of UpdateCheckResult) Private Async Function DownloadInstallInfos() As Task(Of InstallInfos)
Dim infos As UpdateInfos = Await DownloadUpdateInfos() Dim content As String = Await httpClient.GetStringAsync(updateConfig.InstallUrl)
Return InstallInfos.Parse(content)
End Function
Public Async Function Check(ignoreRevmoedFiles As Boolean) As Task(Of UpdateCheckResult)
Dim result As New UpdateCheckResult Dim result As New UpdateCheckResult
If infos IsNot Nothing AndAlso infos.Updates.Any Then If ModpackInfo.HasModpackInfo(localPath) Then
Dim updatesOrderes = infos.Updates.OrderByDescending(Function(n) n.Version) Dim infos As UpdateInfos = Await DownloadUpdateInfos()
result.LatestVersion = updatesOrderes.First.Version Dim modpackInfo As ModpackInfo = ModpackInfo.Load(localPath)
If ModpackInfo.HasModpackInfo(localPath) Then If infos IsNot Nothing AndAlso infos.Updates.Count <> 0 Then
Dim modpackInfo As ModpackInfo = ModpackInfo.Load(localPath) Dim updatesOrderes = infos.Updates.OrderByDescending(Function(n) n.Version)
result.LatestVersion = updatesOrderes.First.Version
result.CurrentVersion = modpackInfo.Version result.CurrentVersion = modpackInfo.Version
result.IsInstalled = True
Dim checkingVersionIndex As Integer = 0 Dim checkingVersionIndex As Integer = 0
Dim checkingVersion As UpdateInfo = updatesOrderes(checkingVersionIndex) Dim checkingVersion As UpdateInfo = updatesOrderes(checkingVersionIndex)
@@ -47,24 +54,37 @@ Public Class ModpackInstaller
Dim actionsToAdd As New List(Of UpdateAction) Dim actionsToAdd As New List(Of UpdateAction)
For Each action In checkingVersion.Actions For Each action In checkingVersion.Actions
If Not result.UpdateActions.Any(Function(n) n.DestPath = action.DestPath) Then If Not result.Actions.Any(Function(n) n.DestPath = action.DestPath) Then
actionsToAdd.Add(action) actionsToAdd.Add(action)
End If End If
Next Next
result.UpdateActions.InsertRange(0, actionsToAdd) result.Actions.InsertRange(0, actionsToAdd)
checkingVersionIndex += 1 checkingVersionIndex += 1
checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex) checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex)
Loop Loop
Else
result.HasError = True
End If
End If
If Not result.IsInstalled Then
Dim infos As InstallInfos = Await DownloadInstallInfos()
If infos IsNot Nothing AndAlso infos.Actions.Count <> 0 Then
result.Actions.AddRange(infos.Actions)
Else
result.HasError = True
End If End If
End If End If
Return result Return result
End Function End Function
Public Async Function InstallUpdates(checkResult As UpdateCheckResult) As Task(Of Boolean?) Public Async Function Install(checkResult As UpdateCheckResult) As Task(Of Boolean?)
Dim modpackInfo As ModpackInfo Dim modpackInfo As ModpackInfo
Dim processed As Integer = 0
If ModpackInfo.HasModpackInfo(localPath) Then If ModpackInfo.HasModpackInfo(localPath) Then
modpackInfo = ModpackInfo.Load(localPath) modpackInfo = ModpackInfo.Load(localPath)
@@ -72,41 +92,41 @@ Public Class ModpackInstaller
modpackInfo = New ModpackInfo modpackInfo = New ModpackInfo
End If End If
Dim processed As Integer = 0 For Each iaction As InstallAction In checkResult.Actions
For Each action As UpdateAction In checkResult.UpdateActions Dim destFilePath As String = Path.Combine(localPath, iaction.DestPath)
Dim destFilePath As String = Path.Combine(localPath, action.DestPath)
Select Case action.Type If TypeOf iaction Is UpdateAction Then
Case UpdateActionType.Update Dim uaction As UpdateAction = iaction
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
Dim sRemote As Stream = Await httpClient.GetStreamAsync(action.DownloadUrl) Select Case uaction.Type
Dim fs As New FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite) Case UpdateActionType.Update
Await sRemote.CopyToAsync(fs) Await InstallFile(destFilePath, uaction.DownloadUrl, uaction.IsZip, uaction.ZipPath)
sRemote.Close() Case UpdateActionType.Delete
fs.Close() If uaction.IsDirectory Then
Case UpdateActionType.Delete If Directory.Exists(destFilePath) Then
If action.IsDirectory Then Directory.Delete(destFilePath, True)
If Directory.Exists(destFilePath) Then End If
Directory.Delete(destFilePath, True) Else
If File.Exists(destFilePath) Then
File.Delete(destFilePath)
End If
End If End If
Else Case UpdateActionType.Copy
If File.Exists(destFilePath) Then Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath)
File.Delete(destFilePath) If File.Exists(srcFilePath) Then
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
File.Copy(srcFilePath, destFilePath, True)
End If End If
End If Case UpdateActionType.Move
Case UpdateActionType.Copy Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath)
Dim srcFilePath As String = Path.Combine(localPath, action.SrcPath) If File.Exists(srcFilePath) Then
If File.Exists(srcFilePath) Then Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) File.Move(srcFilePath, destFilePath, True)
File.Copy(srcFilePath, destFilePath, True) End If
End If End Select
Case UpdateActionType.Move Else
Dim srcFilePath As String = Path.Combine(localPath, action.SrcPath) Await InstallFile(destFilePath, iaction.DownloadUrl, iaction.IsZip, iaction.ZipPath)
If File.Exists(srcFilePath) Then End If
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
File.Move(srcFilePath, destFilePath, True)
End If
End Select
processed += 1 processed += 1
RaiseEvent InstallProgessUpdated(checkResult, processed) RaiseEvent InstallProgessUpdated(checkResult, processed)
@@ -118,4 +138,23 @@ Public Class ModpackInstaller
Return True Return True
End Function End Function
Private Async Function InstallFile(destFilePath As String, sourceUrl As String, isZip As Boolean, zipPath As String) As Task
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
Dim fsDestinationPath As String = If(isZip, Path.Combine(Path.GetTempPath(), $"mc_update_file_{Date.Now.ToBinary}.zip"), destFilePath)
Dim sRemote As Stream = Await httpClient.GetStreamAsync(sourceUrl)
Dim fs As New FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite)
Await sRemote.CopyToAsync(fs)
sRemote.Close()
fs.Close()
If isZip Then
Dim zipDir As String = $"{Path.GetFileNameWithoutExtension(fsDestinationPath)}_extracted"
ZipFile.ExtractToDirectory(fsDestinationPath, zipDir)
'...
File.Delete(fsDestinationPath)
Directory.Delete(zipDir, True)
End If
End Function
End Class End Class

View File

@@ -5,6 +5,10 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ModpackUpdater.Model\ModpackUpdater.Model.vbproj" /> <ProjectReference Include="..\ModpackUpdater.Model\ModpackUpdater.Model.vbproj" />
</ItemGroup> </ItemGroup>

View File

@@ -4,12 +4,13 @@ Public Class UpdateCheckResult
Public Property CurrentVersion As Version Public Property CurrentVersion As Version
Public Property LatestVersion As Version Public Property LatestVersion As Version
Public ReadOnly Property UpdateActions As New List(Of UpdateAction) Public ReadOnly Property Actions As New List(Of InstallAction)
Public Property IsInstalled As Boolean
Public Property HasError As Boolean Public Property HasError As Boolean
Public ReadOnly Property HasUpdates As Boolean Public ReadOnly Property HasUpdates As Boolean
Get Get
Return CurrentVersion < LatestVersion Return Not IsInstalled OrElse CurrentVersion < LatestVersion
End Get End Get
End Property End Property

View File

@@ -1,9 +1,8 @@
Imports Newtonsoft.Json.Converters Public Class InstallAction
Public Class InstallAction
Public Property IsZip As Boolean
Public Property ZipPath As String
Public Property DestPath As String Public Property DestPath As String
Public Property DownloadUrl As String Public Property DownloadUrl As String
Public Property IsZip As Boolean
End Class End Class

View File

@@ -2,12 +2,11 @@
Imports Newtonsoft.Json.Converters Imports Newtonsoft.Json.Converters
Public Class UpdateAction Public Class UpdateAction
Inherits InstallAction
<JsonConverter(GetType(StringEnumConverter))> <JsonConverter(GetType(StringEnumConverter))>
Public Property Type As UpdateActionType Public Property Type As UpdateActionType
Public Property DestPath As String
Public Property SrcPath As String Public Property SrcPath As String
Public Property DownloadUrl As String
Public Property IsDirectory As Boolean Public Property IsDirectory As Boolean
End Class End Class

View File

@@ -97,7 +97,7 @@ Public Class Form1
SetStatus(LangRes.StatusText_CheckingForUpdates, MySymbols.icons8_update_16px) SetStatus(LangRes.StatusText_CheckingForUpdates, MySymbols.icons8_update_16px)
Try Try
lastUpdateCheckResult = Await updater.CheckForUpdates(Not AppConfig.Instance.AllowRemoveLocalFiles) lastUpdateCheckResult = Await updater.Check(Not AppConfig.Instance.AllowRemoveLocalFiles)
Catch ex As Exception Catch ex As Exception
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px) SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px)
Finally Finally
@@ -112,7 +112,7 @@ Public Class Form1
currentUpdating = True currentUpdating = True
Try Try
If Await updater.InstallUpdates(lastUpdateCheckResult) Then If Await updater.Install(lastUpdateCheckResult) Then
lastUpdateCheckResult = Nothing 'Reset last update check, a new one would be needed now. lastUpdateCheckResult = Nothing 'Reset last update check, a new one would be needed now.
SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px) SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px)
Else Else
@@ -132,7 +132,7 @@ Public Class Form1
End Function End Function
Private Sub Update_InstallProgessUpdated(result As UpdateCheckResult, processedSyncs As Integer) Private Sub Update_InstallProgessUpdated(result As UpdateCheckResult, processedSyncs As Integer)
Dim actionCount = result.UpdateActions.Count Dim actionCount = result.Actions.Count
SetStatus(Math.Round(processedSyncs / actionCount * 100, 1) & "%", MySymbols.icons8_software_installer_16px) SetStatus(Math.Round(processedSyncs / actionCount * 100, 1) & "%", MySymbols.icons8_software_installer_16px)
End Sub End Sub