[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
{
@@ -25,9 +27,18 @@
}
public static int MaxDegreeOfParallelism
{
get { return _maxDegreeOfParallelism; }
set { _maxDegreeOfParallelism = value; }
}
public static ParallelOptions ParallelOptions
{
get
{
return new ParallelOptions()
{
MaxDegreeOfParallelism = _maxDegreeOfParallelism
};
}
}
#endregion
}

View File

@@ -40,14 +40,14 @@ namespace SabreTools.Helper.Dats
// First do the initial sort of all of the roms
List<string> keys = Keys.ToList();
Parallel.ForEach(keys,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
key =>
{
List<DatItem> roms = this[key];
// Now add each of the roms to their respective games
Parallel.ForEach(roms,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
rom =>
{
string newkey = "";
@@ -125,7 +125,7 @@ namespace SabreTools.Helper.Dats
// Now go through and sort all of the individual lists
keys = sortable.Keys.ToList();
Parallel.ForEach(keys,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
key =>
{
// 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
Parallel.For(0,
inputs.Count,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
i =>
{
string input = inputs[i];
@@ -122,13 +122,13 @@ namespace SabreTools.Helper.Dats
Globals.Logger.User("Populating internal DAT");
Parallel.For(0, inputs.Count,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
i =>
{
// Get the list of keys from the DAT
List<string> keys = datHeaders[i].Keys.ToList();
Parallel.ForEach(keys,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
key =>
{
// 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)
{
Parallel.ForEach(inputFileNames,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
inputFileName =>
{
// Clean the input string
@@ -561,7 +561,7 @@ namespace SabreTools.Helper.Dats
inputFileName = Path.GetFullPath(inputFileName) + Path.DirectorySeparatorChar;
Parallel.ForEach(Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories),
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
file =>
{
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
List<string> files = Directory.EnumerateFiles(basePath, "*", SearchOption.TopDirectoryOnly).ToList();
Parallel.ForEach(files,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
item =>
{
PopulateFromDirCheckFile(item, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate,
tempDir, copyFiles, headerToCheckAgainst);
});
{
PopulateFromDirCheckFile(item, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate,
tempDir, copyFiles, headerToCheckAgainst);
});
// Find all top-level subfolders
files = Directory.EnumerateDirectories(basePath, "*", SearchOption.TopDirectoryOnly).ToList();
Parallel.ForEach(files,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
item =>
{
List<string> subfiles = Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories).ToList();
Parallel.ForEach(subfiles,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
subitem =>
{
PopulateFromDirCheckFile(subitem, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate,
tempDir, copyFiles, headerToCheckAgainst);
});
});
{
List<string> subfiles = Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories).ToList();
Parallel.ForEach(subfiles,
Globals.ParallelOptions,
subitem =>
{
PopulateFromDirCheckFile(subitem, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, addBlanks, addDate,
tempDir, copyFiles, headerToCheckAgainst);
});
});
// Now find all folders that are empty, if we are supposed to
if (!Romba && addBlanks)
@@ -98,50 +98,50 @@ namespace SabreTools.Helper.Dats
.ToList();
Parallel.ForEach(empties,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
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
string fulldir = Path.GetFullPath(dir);
gamename = fulldir.Remove(0, basePath.Length + 1);
romname = "-";
}
// Set the temporary variables
string gamename = "";
string romname = "";
// Otherwise, we want just the top level folder as the game, and the file as everything else
else
{
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
if (Type == "SuperDAT")
{
gamename = fulldir.Remove(0, basePath.Length + 1);
romname = "-";
}
// 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);
}
// Otherwise, we want just the top level folder as the game, and the file as everything else
else
{
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));
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");
List<string> extracted = Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).ToList();
Parallel.ForEach(extracted,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
entry =>
{
PopulateFromDirProcessFile(entry,
@@ -284,7 +284,7 @@ namespace SabreTools.Helper.Dats
.ToList();
Parallel.ForEach(empties,
new ParallelOptions { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
Globals.ParallelOptions,
dir =>
{
// Get the full path for the directory

View File

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