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,20 +410,48 @@ namespace ModAssistant.Pages
return;
}
using (Stream stream = await DownloadMod(Utils.Constants.BeatModsURL + downloadLink))
using (ZipArchive archive = new ZipArchive(stream))
while (true)
{
foreach (ZipArchiveEntry file in archive.Entries)
List<ZipArchiveEntry> files = new List<ZipArchiveEntry>(filesCount);
using (Stream stream = await DownloadMod(Utils.Constants.BeatModsURL + downloadLink))
using (ZipArchive archive = new ZipArchive(stream))
{
string fileDirectory = Path.GetDirectoryName(Path.Combine(directory, file.FullName));
if (!Directory.Exists(fileDirectory))
foreach (ZipArchiveEntry file in archive.Entries)
{
Directory.CreateDirectory(fileDirectory);
string fileDirectory = Path.GetDirectoryName(Path.Combine(directory, file.FullName));
if (!Directory.Exists(fileDirectory))
{
Directory.CreateDirectory(fileDirectory);
}
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 (!string.IsNullOrEmpty(file.Name))
if (files.Count == filesCount)
{
await ExtractFile(file, Path.Combine(directory, file.FullName), 3.0, mod.name, 10);
foreach (ZipArchiveEntry file in files)
{
await ExtractFile(file, Path.Combine(directory, file.FullName), 3.0, mod.name, 10);
}
break;
}
}
}
@ -642,7 +673,7 @@ namespace ModAssistant.Pages
{
get
{
if (PromotionTexts == null || string.IsNullOrEmpty(PromotionTexts[0])) return "-15,0,0,0";
if (PromotionTexts == null || string.IsNullOrEmpty(PromotionTexts[0])) return "-15,0,0,0";
return "0,0,5,0";
}
}