Skip to content

Commit

Permalink
Merge pull request #468 from kittycat2002/master
Browse files Browse the repository at this point in the history
Made hash reports multi-threaded
  • Loading branch information
bcssov authored Nov 1, 2023
2 parents 9edbd38 + f8b5289 commit 13c150b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
62 changes: 46 additions & 16 deletions src/IronyModManager.Services/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,38 +599,68 @@ protected virtual IGame InitModel(IGameType gameType, IGameSettings gameSettings
protected virtual async Task<IEnumerable<IHashReport>> ParseReportAsync(IGame game, string basePath, IEnumerable<string> files)
{
var reports = new List<IHashReport>();
var reports2 = new List<IHashFileReport[]>();

var total = files.Where(p => game.GameFolders.Any(x => p.StartsWith(x))).Count();
var progress = 0;
double lastPercentage = 0;
int i = 0;
var tasks = new List<Task<Tuple<int, int, IHashFileReport>>>();

foreach (var item in game.GameFolders)
{
var report = GetModelInstance<IHashReport>();
report.Name = item;
report.ReportType = HashReportType.Game;
var hashReports = new List<IHashFileReport>();
int j = 0;

foreach (var file in files.Where(p => p.StartsWith(item)))
{
var info = reader.GetFileInfo(basePath, file);
if (info != null)
string folderPath = basePath;
string filePath = file;
int folderIndex = i;
int fileIndex = j;
tasks.Add(Task.Run(() =>
{
var fileReport = GetModelInstance<IHashFileReport>();
fileReport.File = file;
fileReport.Hash = info.ContentSHA;
hashReports.Add(fileReport);
}
progress++;
var percentage = GetProgressPercentage(total, progress);
if (percentage != lastPercentage)
{
await messageBus.PublishAsync(new ModReportExportEvent(1, percentage));
}
lastPercentage = percentage;
var info = reader.GetFileInfo(folderPath, filePath);
if (info != null)
{
var fileReport = GetModelInstance<IHashFileReport>();
fileReport.File = filePath;
fileReport.Hash = info.ContentSHA;
return new Tuple<int, int, IHashFileReport>(folderIndex, fileIndex, fileReport);
}
return null;
}));
j++;
}
report.Reports = hashReports;
reports.Add(report);
reports2.Add(new IHashFileReport[j]);
i++;
}

while (tasks.Count > 0)
{
i = Task.WaitAny(tasks.ToArray());
Task<Tuple<int, int, IHashFileReport>> task = tasks[i];
tasks.RemoveAt(i);
if (task.Result != null)
{
reports2[task.Result.Item1][task.Result.Item2] = task.Result.Item3;
}
progress++;
var percentage = GetProgressPercentage(total, progress);
if (percentage != lastPercentage)
{
await messageBus.PublishAsync(new ModReportExportEvent(1, percentage));
}
lastPercentage = percentage;
}
for (i=0; i < reports.Count; i++)
{
reports[i].Reports = reports2[i].ToList();
}

return reports;
}

Expand Down
64 changes: 48 additions & 16 deletions src/IronyModManager.Services/ModCollectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,37 +712,69 @@ protected virtual void MapImportResult(IModCollection modCollection, ICollection
protected virtual async Task<IEnumerable<IHashReport>> ParseReportAsync(IEnumerable<IMod> mods)
{
var game = GameService.GetSelected();

var reports = new List<IHashReport>();
var reports2 = new List<IHashFileReport[]>();

var total = mods.SelectMany(p => p.Files).Count(p => game.GameFolders.Any(a => p.StartsWith(a)));
var progress = 0;
double lastPercentage = 0;
int i = 0;
var tasks = new List<Task<Tuple<int,int,IHashFileReport>>>();

foreach (var mod in mods)
{
var report = GetModelInstance<IHashReport>();
report.Name = mod.Name;
report.ReportType = HashReportType.Collection;
var hashReports = new List<IHashFileReport>();
int j = 0;

foreach (var item in mod.Files.Where(p => game.GameFolders.Any(a => p.StartsWith(a))))
{
var info = Reader.GetFileInfo(mod.FullPath, item);
if (info != null)
{
var fileReport = GetModelInstance<IHashFileReport>();
fileReport.File = item;
fileReport.Hash = info.ContentSHA;
hashReports.Add(fileReport);
}
progress++;
var percentage = GetProgressPercentage(total, progress);
if (percentage != lastPercentage)
string modPath = mod.FullPath;
string filePath = item;
int modIndex = i;
int fileIndex = j;
tasks.Add(Task.Run(() =>
{
await messageBus.PublishAsync(new ModReportExportEvent(1, percentage));
}
lastPercentage = percentage;
var info = Reader.GetFileInfo(modPath, filePath);
if (info != null)
{
var fileReport = GetModelInstance<IHashFileReport>();
fileReport.File = filePath;
fileReport.Hash = info.ContentSHA;
return new Tuple<int, int, IHashFileReport>(modIndex, fileIndex, fileReport);
}
return null;
}));
j++;
}
report.Reports = hashReports;
reports.Add(report);
reports2.Add(new IHashFileReport[j]);
i++;
}
while (tasks.Count > 0)
{
i = Task.WaitAny(tasks.ToArray());
Tuple<int, int, IHashFileReport> result = tasks[i].Result;
tasks.RemoveAt(i);
if (result != null)
{
reports2[result.Item1][result.Item2] = result.Item3;
}
progress++;
var percentage = GetProgressPercentage(total, progress);
if (percentage != lastPercentage)
{
await messageBus.PublishAsync(new ModReportExportEvent(1, percentage));
}
lastPercentage = percentage;
}
for (i = 0; i < reports.Count; i++)
{
reports[i].Reports = reports2[i].ToList();
}

return reports;
}

Expand Down

0 comments on commit 13c150b

Please sign in to comment.