install: more directory power

This commit is contained in:
2024-06-17 18:46:49 +02:00
parent f1b11ea3c6
commit af58f0d8ea
2 changed files with 64 additions and 7 deletions

View File

@@ -0,0 +1,35 @@
Imports System.IO
Public Module Extensions
Public Sub CopyDirectory(sourceDir As String, destinationDir As String, recursive As Boolean)
' Get information about the source directory
Dim dir As New DirectoryInfo(sourceDir)
' Check if the source directory exists
If Not dir.Exists Then
Throw New DirectoryNotFoundException($"Source directory not found: {dir.FullName}")
End If
' Cache directories before we start copying
Dim dirs As DirectoryInfo() = dir.GetDirectories()
' Create the destination directory
Directory.CreateDirectory(destinationDir)
' Get the files in the source directory and copy to the destination directory
For Each file As FileInfo In dir.GetFiles()
Dim targetFilePath As String = Path.Combine(destinationDir, file.Name)
file.CopyTo(targetFilePath)
Next
' If recursive and copying subdirectories, recursively call this method
If recursive Then
For Each subDir As DirectoryInfo In dirs
Dim newDestinationDir As String = Path.Combine(destinationDir, subDir.Name)
CopyDirectory(subDir.FullName, newDestinationDir, True)
Next
End If
End Sub
End Module

View File

@@ -118,15 +118,27 @@ Public Class ModpackInstaller
End If End If
Case UpdateActionType.Copy Case UpdateActionType.Copy
Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath) Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath)
If File.Exists(srcFilePath) Then If uaction.IsDirectory Then
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) If Directory.Exists(srcFilePath) Then
File.Copy(srcFilePath, destFilePath, True) CopyDirectory(srcFilePath, destFilePath, True)
End If
Else
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.Move
Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath) Dim srcFilePath As String = Path.Combine(localPath, uaction.SrcPath)
If File.Exists(srcFilePath) Then If uaction.IsDirectory Then
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) If Directory.Exists(srcFilePath) Then
File.Move(srcFilePath, destFilePath, True) Directory.Move(srcFilePath, destFilePath)
End If
Else
If File.Exists(srcFilePath) Then
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
File.Move(srcFilePath, destFilePath, True)
End If
End If End If
End Select End Select
Else Else
@@ -137,9 +149,11 @@ Public Class ModpackInstaller
RaiseEvent InstallProgessUpdated(checkResult, processed) RaiseEvent InstallProgessUpdated(checkResult, processed)
Next Next
'Save new modpack info
modpackInfo.Version = checkResult.LatestVersion modpackInfo.Version = checkResult.LatestVersion
modpackInfo.Save(localPath) modpackInfo.Save(localPath)
'Delete cached zip files
For Each task In localZipCache For Each task In localZipCache
Directory.Delete(task.ExtractedZipPath, True) Directory.Delete(task.ExtractedZipPath, True)
Next Next
@@ -150,6 +164,7 @@ Public Class ModpackInstaller
Private Async Function InstallFile(destFilePath As String, sourceUrl As String, isZip As Boolean, zipPath As String, localZipCache As List(Of LocalZipCacheEntry)) As Task Private Async Function InstallFile(destFilePath As String, sourceUrl As String, isZip As Boolean, zipPath As String, localZipCache As List(Of LocalZipCacheEntry)) As Task
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)) Directory.CreateDirectory(Path.GetDirectoryName(destFilePath))
'Download
Dim fsDestinationPath As String = If(isZip, Path.Combine(Path.GetTempPath(), $"mc_update_file_{Date.Now.ToBinary}.zip"), 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 sRemote As Stream = Await httpClient.GetStreamAsync(sourceUrl)
Dim fs As New FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite) Dim fs As New FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite)
@@ -157,10 +172,17 @@ Public Class ModpackInstaller
sRemote.Close() sRemote.Close()
fs.Close() fs.Close()
'Handle zip file
If isZip Then If isZip Then
'Extract
Dim zipDir As String = $"{Path.GetFileNameWithoutExtension(fsDestinationPath)}_extracted" Dim zipDir As String = $"{Path.GetFileNameWithoutExtension(fsDestinationPath)}_extracted"
ZipFile.ExtractToDirectory(fsDestinationPath, zipDir) ZipFile.ExtractToDirectory(fsDestinationPath, zipDir)
'...
'Copy content
Dim zipSrc As String = Path.Combine(zipDir, zipPath)
CopyDirectory(zipSrc, destFilePath, True)
'Delete/cache temporary files
File.Delete(fsDestinationPath) File.Delete(fsDestinationPath)
localZipCache?.Add(New LocalZipCacheEntry With { localZipCache?.Add(New LocalZipCacheEntry With {
.DownloadUrl = sourceUrl, .DownloadUrl = sourceUrl,