Parallelise some troublesome loops

This commit is contained in:
Chomp 2021-08-31 18:52:28 +01:00
parent f0d48619bf
commit d38cc9c3ea
2 changed files with 14 additions and 14 deletions

View File

@ -8,6 +8,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Generator
{
@ -28,7 +29,7 @@ namespace Generator
LoggingHelpers.LogToConsole("Started processing bot loot");
// Iterate over assault/raider etc
foreach (var botToUpdate in _botsWithGear)
Parallel.ForEach(_botsWithGear, botToUpdate =>
{
var rawBotsOfSameType = _rawParsedBots
.Where(x => x.Info.Settings.Role.Equals(botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase))
@ -36,7 +37,7 @@ namespace Generator
if (rawBotsOfSameType.Count == 0)
{
continue;
return;
}
foreach (var rawParsedBot in rawBotsOfSameType)
@ -48,7 +49,7 @@ namespace Generator
AddBackbackLoot(botToUpdate, rawBotsOfSameType);
AddSecureContainerLoot(botToUpdate, rawBotsOfSameType);
AddSpecialLoot(botToUpdate);
}
});
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot loot. Took: {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Generator
{
@ -30,22 +31,20 @@ namespace Generator
Console.WriteLine($"{botFiles.Count} bot dump files found");
var parsedBots = new List<Datum>();
foreach (var file in botFiles)
{
Parallel.ForEach(botFiles, file => {
var splitFile = file.Split("\\");
var json = File.ReadAllText(file);
try
{
json = PruneMalformedBsgJson(json, splitFile.Last());
var bots = ParseJson(json, file);
var bots = ParseJson(json);
if (bots == null || bots.Count == 0)
{
Console.WriteLine($"skipping file: {splitFile.Last()}. no bots found, ");
continue;
return;
}
Console.WriteLine($"parsing: {bots.Count} bots in file {splitFile.Last()}");
@ -59,7 +58,7 @@ namespace Generator
failedFilesCount++;
Console.WriteLine($"JSON Error message: {jex.Message} || file: {splitFile.Last()}");
}
}
});
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Cleaned and Parsed: {parsedBots.Count} bots. Failed: {failedFilesCount}. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
@ -87,10 +86,10 @@ namespace Generator
return o.ToString();
}
private static List<Datum> ParseJson(string json, string file)
private static List<Datum> ParseJson(string json)
{
//Console.WriteLine($"parsing file {file}");
var serialisedObject = JsonConvert.DeserializeObject<Models.Input.Root>(json);
var serialisedObject = JsonConvert.DeserializeObject<Root>(json);
return serialisedObject.data;
}
}