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.Compression
Imports System.Net
Imports System.Net.Http
@@ -28,17 +29,23 @@ Public Class ModpackInstaller
Return UpdateInfos.Parse(content)
End Function
Public Async Function CheckForUpdates(ignoreRevmoedFiles As Boolean) As Task(Of UpdateCheckResult)
Dim infos As UpdateInfos = Await DownloadUpdateInfos()
Private Async Function DownloadInstallInfos() As Task(Of InstallInfos)
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
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 infos As UpdateInfos = Await DownloadUpdateInfos()
Dim modpackInfo As ModpackInfo = ModpackInfo.Load(localPath)
If ModpackInfo.HasModpackInfo(localPath) Then
Dim modpackInfo As ModpackInfo = ModpackInfo.Load(localPath)
If infos IsNot Nothing AndAlso infos.Updates.Count <> 0 Then
Dim updatesOrderes = infos.Updates.OrderByDescending(Function(n) n.Version)
result.LatestVersion = updatesOrderes.First.Version
result.CurrentVersion = modpackInfo.Version
result.IsInstalled = True
Dim checkingVersionIndex As Integer = 0
Dim checkingVersion As UpdateInfo = updatesOrderes(checkingVersionIndex)
@@ -47,24 +54,37 @@ Public Class ModpackInstaller
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
If Not result.Actions.Any(Function(n) n.DestPath = action.DestPath) Then
actionsToAdd.Add(action)
End If
Next
result.UpdateActions.InsertRange(0, actionsToAdd)
result.Actions.InsertRange(0, actionsToAdd)
checkingVersionIndex += 1
checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex)
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
Return result
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 processed As Integer = 0
If ModpackInfo.HasModpackInfo(localPath) Then
modpackInfo = ModpackInfo.Load(localPath)
@@ -72,41 +92,41 @@ Public Class ModpackInstaller
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)
For Each iaction As InstallAction In checkResult.Actions
Dim destFilePath As String = Path.Combine(localPath, iaction.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)
If TypeOf iaction Is UpdateAction Then
Dim uaction As UpdateAction = iaction
Select Case uaction.Type
Case UpdateActionType.Update
Await InstallFile(destFilePath, uaction.DownloadUrl, uaction.IsZip, uaction.ZipPath)
Case UpdateActionType.Delete
If uaction.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
Else
If File.Exists(destFilePath) Then
File.Delete(destFilePath)
Case UpdateActionType.Copy
Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath)
If File.Exists(srcFilePath) Then
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
File.Copy(srcFilePath, destFilePath, True)
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
Case UpdateActionType.Move
Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath)
If File.Exists(srcFilePath) Then
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
File.Move(srcFilePath, destFilePath, True)
End If
End Select
Else
Await InstallFile(destFilePath, iaction.DownloadUrl, iaction.IsZip, iaction.ZipPath)
End If
processed += 1
RaiseEvent InstallProgessUpdated(checkResult, processed)
@@ -118,4 +138,23 @@ Public Class ModpackInstaller
Return True
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

View File

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

View File

@@ -4,12 +4,13 @@ Public Class UpdateCheckResult
Public Property CurrentVersion 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 ReadOnly Property HasUpdates As Boolean
Get
Return CurrentVersion < LatestVersion
Return Not IsInstalled OrElse CurrentVersion < LatestVersion
End Get
End Property