Skip to content

Commit

Permalink
Add integrity check before file extraction (bsmg#339)
Browse files Browse the repository at this point in the history
* Add integrity check before file extraction

* Use a do/while loop instead

* Files list should be in loop

* Fix ZipArchive being disposed too early
  • Loading branch information
Meivyn authored May 16, 2021
1 parent 8b59a3f commit 1a1b9c1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
9 changes: 9 additions & 0 deletions ModAssistant/Classes/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ public static string CalculateMD5(string filename)
}
}

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;
Expand Down
49 changes: 40 additions & 9 deletions ModAssistant/Pages/Mods.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,13 @@ await Task.Run(() =>

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;
Expand All @@ -407,20 +410,48 @@ private async Task InstallMod(Mod mod, string directory)
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;
}
}
}
Expand Down Expand Up @@ -642,7 +673,7 @@ public string PromotionMargin
{
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";
}
}
Expand Down

0 comments on commit 1a1b9c1

Please sign in to comment.