[Globals] Make parallel options easier to use

This commit is contained in:
Matt Nadareski
2017-03-14 20:28:23 -07:00
parent 2a70e9c381
commit ac14a3a1f4
5 changed files with 80 additions and 69 deletions

View File

@@ -1,4 +1,6 @@
namespace SabreTools.Helper.Data using System.Threading.Tasks;
namespace SabreTools.Helper.Data
{ {
public class Globals public class Globals
{ {
@@ -25,9 +27,18 @@
} }
public static int MaxDegreeOfParallelism public static int MaxDegreeOfParallelism
{ {
get { return _maxDegreeOfParallelism; }
set { _maxDegreeOfParallelism = value; } set { _maxDegreeOfParallelism = value; }
} }
public static ParallelOptions ParallelOptions
{
get
{
return new ParallelOptions()
{
MaxDegreeOfParallelism = _maxDegreeOfParallelism
};
}
}
#endregion #endregion
} }

View File

@@ -40,14 +40,14 @@ namespace SabreTools.Helper.Dats
// First do the initial sort of all of the roms // First do the initial sort of all of the roms
List<string> keys = Keys.ToList(); List<string> keys = Keys.ToList();
Parallel.ForEach(keys, Parallel.ForEach(keys,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
key => key =>
{ {
List<DatItem> roms = this[key]; List<DatItem> roms = this[key];
// Now add each of the roms to their respective games // Now add each of the roms to their respective games
Parallel.ForEach(roms, Parallel.ForEach(roms,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
rom => rom =>
{ {
string newkey = ""; string newkey = "";
@@ -125,7 +125,7 @@ namespace SabreTools.Helper.Dats
// Now go through and sort all of the individual lists // Now go through and sort all of the individual lists
keys = sortable.Keys.ToList(); keys = sortable.Keys.ToList();
Parallel.ForEach(keys, Parallel.ForEach(keys,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
key => key =>
{ {
// Get the possibly unsorted list // Get the possibly unsorted list

View File

@@ -103,7 +103,7 @@ namespace SabreTools.Helper.Dats
// Parse all of the DATs into their own DatFiles in the array // Parse all of the DATs into their own DatFiles in the array
Parallel.For(0, Parallel.For(0,
inputs.Count, inputs.Count,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
i => i =>
{ {
string input = inputs[i]; string input = inputs[i];
@@ -122,13 +122,13 @@ namespace SabreTools.Helper.Dats
Globals.Logger.User("Populating internal DAT"); Globals.Logger.User("Populating internal DAT");
Parallel.For(0, inputs.Count, Parallel.For(0, inputs.Count,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
i => i =>
{ {
// Get the list of keys from the DAT // Get the list of keys from the DAT
List<string> keys = datHeaders[i].Keys.ToList(); List<string> keys = datHeaders[i].Keys.ToList();
Parallel.ForEach(keys, Parallel.ForEach(keys,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
key => key =>
{ {
// Add everything from the key to the internal DAT // Add everything from the key to the internal DAT
@@ -536,7 +536,7 @@ namespace SabreTools.Helper.Dats
SplitType splitType, bool trim, bool single, string root) SplitType splitType, bool trim, bool single, string root)
{ {
Parallel.ForEach(inputFileNames, Parallel.ForEach(inputFileNames,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
inputFileName => inputFileName =>
{ {
// Clean the input string // Clean the input string
@@ -561,7 +561,7 @@ namespace SabreTools.Helper.Dats
inputFileName = Path.GetFullPath(inputFileName) + Path.DirectorySeparatorChar; inputFileName = Path.GetFullPath(inputFileName) + Path.DirectorySeparatorChar;
Parallel.ForEach(Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories), Parallel.ForEach(Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories),
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
file => file =>
{ {
Globals.Logger.User("Processing \"" + Path.GetFullPath(file).Remove(0, inputFileName.Length) + "\""); Globals.Logger.User("Processing \"" + Path.GetFullPath(file).Remove(0, inputFileName.Length) + "\"");

View File

@@ -66,28 +66,28 @@ namespace SabreTools.Helper.Dats
// Process the files in the main folder // Process the files in the main folder
List<string> files = Directory.EnumerateFiles(basePath, "*", SearchOption.TopDirectoryOnly).ToList(); List<string> files = Directory.EnumerateFiles(basePath, "*", SearchOption.TopDirectoryOnly).ToList();
Parallel.ForEach(files, Parallel.ForEach(files,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
item => item =>
{ {
PopulateFromDirCheckFile(item, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate, PopulateFromDirCheckFile(item, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate,
tempDir, copyFiles, headerToCheckAgainst); tempDir, copyFiles, headerToCheckAgainst);
}); });
// Find all top-level subfolders // Find all top-level subfolders
files = Directory.EnumerateDirectories(basePath, "*", SearchOption.TopDirectoryOnly).ToList(); files = Directory.EnumerateDirectories(basePath, "*", SearchOption.TopDirectoryOnly).ToList();
Parallel.ForEach(files, Parallel.ForEach(files,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
item => item =>
{ {
List<string> subfiles = Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories).ToList(); List<string> subfiles = Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories).ToList();
Parallel.ForEach(subfiles, Parallel.ForEach(subfiles,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
subitem => subitem =>
{ {
PopulateFromDirCheckFile(subitem, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate, PopulateFromDirCheckFile(subitem, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate,
tempDir, copyFiles, headerToCheckAgainst); tempDir, copyFiles, headerToCheckAgainst);
}); });
}); });
// Now find all folders that are empty, if we are supposed to // Now find all folders that are empty, if we are supposed to
if (!Romba && addBlanks) if (!Romba && addBlanks)
@@ -98,50 +98,50 @@ namespace SabreTools.Helper.Dats
.ToList(); .ToList();
Parallel.ForEach(empties, Parallel.ForEach(empties,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
dir => dir =>
{
// Get the full path for the directory
string fulldir = Path.GetFullPath(dir);
// Set the temporary variables
string gamename = "";
string romname = "";
// If we have a SuperDAT, we want anything that's not the base path as the game, and the file as the rom
if (Type == "SuperDAT")
{ {
// Get the full path for the directory gamename = fulldir.Remove(0, basePath.Length + 1);
string fulldir = Path.GetFullPath(dir); romname = "-";
}
// Set the temporary variables // Otherwise, we want just the top level folder as the game, and the file as everything else
string gamename = ""; else
string romname = ""; {
gamename = fulldir.Remove(0, basePath.Length + 1).Split(Path.DirectorySeparatorChar)[0];
romname = Path.Combine(fulldir.Remove(0, basePath.Length + 1 + gamename.Length), "-");
}
// If we have a SuperDAT, we want anything that's not the base path as the game, and the file as the rom // Sanitize the names
if (Type == "SuperDAT") if (gamename.StartsWith(Path.DirectorySeparatorChar.ToString()))
{ {
gamename = fulldir.Remove(0, basePath.Length + 1); gamename = gamename.Substring(1);
romname = "-"; }
} if (gamename.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
gamename = gamename.Substring(0, gamename.Length - 1);
}
if (romname.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
romname = romname.Substring(1);
}
if (romname.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
romname = romname.Substring(0, romname.Length - 1);
}
// Otherwise, we want just the top level folder as the game, and the file as everything else Globals.Logger.Verbose("Adding blank empty folder: " + gamename);
else this["null"].Add(new Rom(romname, gamename, omitFromScan));
{
gamename = fulldir.Remove(0, basePath.Length + 1).Split(Path.DirectorySeparatorChar)[0];
romname = Path.Combine(fulldir.Remove(0, basePath.Length + 1 + gamename.Length), "-");
}
// Sanitize the names
if (gamename.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
gamename = gamename.Substring(1);
}
if (gamename.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
gamename = gamename.Substring(0, gamename.Length - 1);
}
if (romname.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
romname = romname.Substring(1);
}
if (romname.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
romname = romname.Substring(0, romname.Length - 1);
}
Globals.Logger.Verbose("Adding blank empty folder: " + gamename);
this["null"].Add(new Rom(romname, gamename, omitFromScan));
}); });
} }
} }
@@ -260,7 +260,7 @@ namespace SabreTools.Helper.Dats
Globals.Logger.Verbose(Path.GetFileName(item) + " treated like an archive"); Globals.Logger.Verbose(Path.GetFileName(item) + " treated like an archive");
List<string> extracted = Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).ToList(); List<string> extracted = Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).ToList();
Parallel.ForEach(extracted, Parallel.ForEach(extracted,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
entry => entry =>
{ {
PopulateFromDirProcessFile(entry, PopulateFromDirProcessFile(entry,
@@ -284,7 +284,7 @@ namespace SabreTools.Helper.Dats
.ToList(); .ToList();
Parallel.ForEach(empties, Parallel.ForEach(empties,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
dir => dir =>
{ {
// Get the full path for the directory // Get the full path for the directory

View File

@@ -39,7 +39,7 @@ namespace SabreTools.Helper.External
subdirs.Clear(); subdirs.Clear();
Parallel.ForEach(dirs, Parallel.ForEach(dirs,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
currentDir => currentDir =>
{ {
string[] subDirs = Directory.GetDirectories(currentDir); string[] subDirs = Directory.GetDirectories(currentDir);
@@ -57,7 +57,7 @@ namespace SabreTools.Helper.External
{ {
FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly); FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);
Parallel.ForEach(files, Parallel.ForEach(files,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism }, Globals.ParallelOptions,
info => info =>
{ {
action(info); action(info);