Add integrity check before file extraction (#339)

* Add integrity check before file extraction

* Use a do/while loop instead

* Files list should be in loop

* Fix ZipArchive being disposed too early
This commit is contained in:
Meivyn 2021-05-16 18:13:40 -04:00 committed by GitHub
parent 8b59a3fab6
commit 1a1b9c1aa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 9 deletions

View file

@ -119,6 +119,15 @@ namespace ModAssistant
}
}
public static string CalculateMD5FromStream(Stream stream)
{
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
public static string GetInstallDir()
{
string InstallDir = Properties.Settings.Default.InstallFolder;

View file

@ -385,10 +385,13 @@ namespace ModAssistant.Pages
private async Task InstallMod(Mod mod, string directory)
{
int filesCount = 0;
string downloadLink = null;
foreach (Mod.DownloadLink link in mod.downloads)
{
filesCount = link.hashMd5.Length;
if (link.type == "universal")
{
downloadLink = link.url;
@ -407,6 +410,10 @@ namespace ModAssistant.Pages
return;
}
while (true)
{
List<ZipArchiveEntry> files = new List<ZipArchiveEntry>(filesCount);
using (Stream stream = await DownloadMod(Utils.Constants.BeatModsURL + downloadLink))
using (ZipArchive archive = new ZipArchive(stream))
{
@ -419,9 +426,33 @@ namespace ModAssistant.Pages
}
if (!string.IsNullOrEmpty(file.Name))
{
foreach (Mod.DownloadLink download in mod.downloads)
{
foreach (Mod.FileHashes fileHash in download.hashMd5)
{
using (Stream fileStream = file.Open())
{
if (fileHash.hash == Utils.CalculateMD5FromStream(fileStream))
{
files.Add(file);
break;
}
}
}
}
}
}
if (files.Count == filesCount)
{
foreach (ZipArchiveEntry file in files)
{
await ExtractFile(file, Path.Combine(directory, file.FullName), 3.0, mod.name, 10);
}
break;
}
}
}