mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Add and use ItemDictionary class
This commit is contained in:
@@ -468,9 +468,9 @@ have a current entry in the DAT index.";
|
||||
string crcsha1query = "INSERT OR IGNORE INTO crcsha1 (crc, sha1) VALUES";
|
||||
string md5sha1query = "INSERT OR IGNORE INTO md5sha1 (md5, sha1) VALUES";
|
||||
|
||||
foreach (string key in df.Keys)
|
||||
foreach (string key in df.Items.Keys)
|
||||
{
|
||||
List<DatItem> datItems = df[key];
|
||||
List<DatItem> datItems = df.Items[key];
|
||||
foreach (Rom rom in datItems)
|
||||
{
|
||||
// If we care about if the file exists, check the databse first
|
||||
@@ -504,7 +504,7 @@ have a current entry in the DAT index.";
|
||||
}
|
||||
|
||||
// Add to the Dat
|
||||
need.Add(key, rom);
|
||||
need.Items.Add(key, rom);
|
||||
}
|
||||
}
|
||||
// Otherwise, just add the file to the list
|
||||
@@ -532,7 +532,7 @@ have a current entry in the DAT index.";
|
||||
}
|
||||
|
||||
// Add to the Dat
|
||||
need.Add(key, rom);
|
||||
need.Items.Add(key, rom);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1566,7 +1566,7 @@ contents of any changed dats.";
|
||||
|
||||
// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually
|
||||
datroot.PopulateFromDir(_dats, Hash.DeepHashes, false, false, SkipFileType.None, false, false, _tmpdir, false, null, true, null);
|
||||
datroot.BucketBy(BucketedBy.SHA1, DedupeType.None);
|
||||
datroot.Items.BucketBy(BucketedBy.SHA1, DedupeType.None);
|
||||
|
||||
// Create a List of dat hashes in the database (SHA-1)
|
||||
List<string> databaseDats = new List<string>();
|
||||
@@ -1585,9 +1585,9 @@ contents of any changed dats.";
|
||||
{
|
||||
sldr.Read();
|
||||
string hash = sldr.GetString(0);
|
||||
if (datroot.Contains(hash))
|
||||
if (datroot.Items.ContainsKey(hash))
|
||||
{
|
||||
datroot.Remove(hash);
|
||||
datroot.Items.Remove(hash);
|
||||
databaseDats.Add(hash);
|
||||
}
|
||||
else if (!databaseDats.Contains(hash))
|
||||
@@ -1596,7 +1596,7 @@ contents of any changed dats.";
|
||||
}
|
||||
}
|
||||
|
||||
datroot.BucketBy(BucketedBy.Game, DedupeType.None, norename: true);
|
||||
datroot.Items.BucketBy(BucketedBy.Game, DedupeType.None, norename: true);
|
||||
|
||||
watch.Stop();
|
||||
|
||||
@@ -1605,9 +1605,9 @@ contents of any changed dats.";
|
||||
|
||||
// Loop through the Dictionary and add all data
|
||||
watch.Start("Adding new DAT information");
|
||||
foreach (string key in datroot.Keys)
|
||||
foreach (string key in datroot.Items.Keys)
|
||||
{
|
||||
foreach (Rom value in datroot[key])
|
||||
foreach (Rom value in datroot.Items[key])
|
||||
{
|
||||
AddDatToDatabase(value, dbc);
|
||||
}
|
||||
@@ -1695,7 +1695,7 @@ contents of any changed dats.";
|
||||
|
||||
// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually
|
||||
depot.PopulateFromDir(depotname, Hash.DeepHashes, false, false, SkipFileType.None, false, false, _tmpdir, false, null, true, null);
|
||||
depot.BucketBy(BucketedBy.SHA1, DedupeType.None);
|
||||
depot.Items.BucketBy(BucketedBy.SHA1, DedupeType.None);
|
||||
|
||||
// Set the base queries to use
|
||||
string crcquery = "INSERT OR IGNORE INTO crc (crc) VALUES";
|
||||
@@ -1706,10 +1706,10 @@ contents of any changed dats.";
|
||||
|
||||
// Once we have both, check for any new files
|
||||
List<string> dupehashes = new List<string>();
|
||||
List<string> keys = depot.Keys;
|
||||
IEnumerable<string> keys = depot.Items.Keys;
|
||||
foreach (string key in keys)
|
||||
{
|
||||
List<DatItem> roms = depot[key];
|
||||
List<DatItem> roms = depot.Items[key];
|
||||
foreach (Rom rom in roms)
|
||||
{
|
||||
if (hashes.Contains(rom.SHA1))
|
||||
|
||||
@@ -261,9 +261,9 @@ namespace RombaSharp
|
||||
|
||||
// Loop through the parsed entries
|
||||
bool hasItems = false;
|
||||
foreach (string romkey in tempdat.Keys)
|
||||
foreach (string romkey in tempdat.Items.Keys)
|
||||
{
|
||||
foreach (DatItem datItem in tempdat[romkey])
|
||||
foreach (DatItem datItem in tempdat.Items[romkey])
|
||||
{
|
||||
Globals.Logger.Verbose($"Checking and adding file '{datItem.Name}'");
|
||||
|
||||
|
||||
@@ -135,9 +135,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -465,9 +465,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -411,8 +411,14 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
|
||||
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
|
||||
/// <param name="statDatFormat" > Set the statistics output format to use</param>
|
||||
public static void OutputStats(List<string> inputs, string reportName, string outDir, bool single,
|
||||
bool baddumpCol, bool nodumpCol, StatReportFormat statDatFormat)
|
||||
public static void OutputStats(
|
||||
List<string> inputs,
|
||||
string reportName,
|
||||
string outDir,
|
||||
bool single,
|
||||
bool baddumpCol,
|
||||
bool nodumpCol,
|
||||
StatReportFormat statDatFormat)
|
||||
{
|
||||
// If there's no output format, set the default
|
||||
if (statDatFormat == StatReportFormat.None)
|
||||
@@ -480,23 +486,23 @@ namespace SabreTools.Library.DatFiles
|
||||
Globals.Logger.Verbose($"Beginning stat collection for '{file}'", false);
|
||||
List<string> games = new List<string>();
|
||||
DatFile datdata = DatFile.CreateAndParse(file);
|
||||
datdata.BucketBy(BucketedBy.Game, DedupeType.None, norename: true);
|
||||
datdata.Items.BucketBy(BucketedBy.Game, DedupeType.None, norename: true);
|
||||
|
||||
// Output single DAT stats (if asked)
|
||||
Globals.Logger.User($"Adding stats for file '{file}'\n", false);
|
||||
if (single)
|
||||
{
|
||||
reports.ForEach(report => report.ReplaceStatistics(datdata.DatHeader.FileName, datdata.Keys.Count, datdata.DatStats));
|
||||
reports.ForEach(report => report.ReplaceStatistics(datdata.DatHeader.FileName, datdata.Items.Keys.Count, datdata.Items.Statistics));
|
||||
reports.ForEach(report => report.Write());
|
||||
}
|
||||
|
||||
// Add single DAT stats to dir
|
||||
dirStats.AddStats(datdata.DatStats);
|
||||
dirStats.GameCount += datdata.Keys.Count();
|
||||
dirStats.AddStats(datdata.Items.Statistics);
|
||||
dirStats.GameCount += datdata.Items.Keys.Count();
|
||||
|
||||
// Add single DAT stats to totals
|
||||
totalStats.AddStats(datdata.DatStats);
|
||||
totalStats.GameCount += datdata.Keys.Count();
|
||||
totalStats.AddStats(datdata.Items.Statistics);
|
||||
totalStats.GameCount += datdata.Items.Keys.Count();
|
||||
|
||||
// Make sure to assign the new directory
|
||||
lastdir = thisdir;
|
||||
|
||||
@@ -291,9 +291,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -111,9 +111,9 @@ namespace SabreTools.Library.DatFiles
|
||||
};
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -781,25 +781,24 @@ namespace SabreTools.Library.DatFiles
|
||||
// We remove any blanks, if we aren't supposed to have any
|
||||
if (!datFile.DatHeader.KeepEmptyGames)
|
||||
{
|
||||
foreach (string key in datFile.Keys)
|
||||
foreach (string key in datFile.Items.Keys)
|
||||
{
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
if (items == null)
|
||||
continue;
|
||||
|
||||
List<DatItem> newitems = items.Where(i => i.ItemType != ItemType.Blank).ToList();
|
||||
|
||||
datFile.Remove(key);
|
||||
datFile.AddRange(key, newitems);
|
||||
datFile.Items.Remove(key);
|
||||
datFile.Items.AddRange(key, newitems);
|
||||
}
|
||||
}
|
||||
|
||||
// Loop over every key in the dictionary
|
||||
List<string> keys = datFile.Keys;
|
||||
foreach (string key in keys)
|
||||
foreach (string key in datFile.Items.Keys)
|
||||
{
|
||||
// For every item in the current key
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
List<DatItem> newitems = new List<DatItem>();
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
@@ -843,8 +842,8 @@ namespace SabreTools.Library.DatFiles
|
||||
}
|
||||
}
|
||||
|
||||
datFile.Remove(key);
|
||||
datFile.AddRange(key, newitems);
|
||||
datFile.Items.Remove(key);
|
||||
datFile.Items.AddRange(key, newitems);
|
||||
}
|
||||
|
||||
// If we are removing scene dates, do that now
|
||||
@@ -890,25 +889,24 @@ namespace SabreTools.Library.DatFiles
|
||||
// We remove any blanks, if we aren't supposed to have any
|
||||
if (!outDat.DatHeader.KeepEmptyGames)
|
||||
{
|
||||
foreach (string key in outDat.Keys)
|
||||
foreach (string key in outDat.Items.Keys)
|
||||
{
|
||||
List<DatItem> items = outDat[key];
|
||||
List<DatItem> items = outDat.Items[key];
|
||||
if (items == null)
|
||||
continue;
|
||||
|
||||
List<DatItem> newitems = items.Where(i => i.ItemType != ItemType.Blank).ToList();
|
||||
|
||||
outDat.Remove(key);
|
||||
outDat.AddRange(key, newitems);
|
||||
outDat.Items.Remove(key);
|
||||
outDat.Items.AddRange(key, newitems);
|
||||
}
|
||||
}
|
||||
|
||||
// Loop over every key in the dictionary
|
||||
List<string> keys = datFile.Keys;
|
||||
foreach (string key in keys)
|
||||
foreach (string key in datFile.Items.Keys)
|
||||
{
|
||||
// For every item in the current key
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
List<DatItem> newitems = new List<DatItem>();
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
@@ -955,7 +953,7 @@ namespace SabreTools.Library.DatFiles
|
||||
}
|
||||
}
|
||||
|
||||
outDat.AddRange(key, newitems);
|
||||
outDat.Items.AddRange(key, newitems);
|
||||
}
|
||||
|
||||
// If we are removing scene dates, do that now
|
||||
@@ -1415,7 +1413,7 @@ namespace SabreTools.Library.DatFiles
|
||||
Globals.Logger.User("Creating device non-merged sets from the DAT");
|
||||
|
||||
// For sake of ease, the first thing we want to do is bucket by game
|
||||
datFile.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
datFile.Items.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
|
||||
// Now we want to loop through all of the games and set the correct information
|
||||
while (AddRomsFromDevices(datFile, false, false)) ;
|
||||
@@ -1435,7 +1433,7 @@ namespace SabreTools.Library.DatFiles
|
||||
Globals.Logger.User("Creating fully non-merged sets from the DAT");
|
||||
|
||||
// For sake of ease, the first thing we want to do is bucket by game
|
||||
datFile.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
datFile.Items.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
|
||||
// Now we want to loop through all of the games and set the correct information
|
||||
while (AddRomsFromDevices(datFile, true, true)) ;
|
||||
@@ -1459,7 +1457,7 @@ namespace SabreTools.Library.DatFiles
|
||||
Globals.Logger.User("Creating merged sets from the DAT");
|
||||
|
||||
// For sake of ease, the first thing we want to do is bucket by game
|
||||
datFile.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
datFile.Items.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
|
||||
// Now we want to loop through all of the games and set the correct information
|
||||
AddRomsFromChildren(datFile);
|
||||
@@ -1482,7 +1480,7 @@ namespace SabreTools.Library.DatFiles
|
||||
Globals.Logger.User("Creating non-merged sets from the DAT");
|
||||
|
||||
// For sake of ease, the first thing we want to do is bucket by game
|
||||
datFile.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
datFile.Items.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
|
||||
// Now we want to loop through all of the games and set the correct information
|
||||
AddRomsFromParent(datFile);
|
||||
@@ -1505,7 +1503,7 @@ namespace SabreTools.Library.DatFiles
|
||||
Globals.Logger.User("Creating split sets from the DAT");
|
||||
|
||||
// For sake of ease, the first thing we want to do is bucket by game
|
||||
datFile.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
datFile.Items.BucketBy(BucketedBy.Game, mergeroms, norename: true);
|
||||
|
||||
// Now we want to loop through all of the games and set the correct information
|
||||
RemoveRomsFromChild(datFile);
|
||||
@@ -1524,37 +1522,35 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="datFile">DatFile to filter</param>
|
||||
private void AddRomsFromBios(DatFile datFile)
|
||||
{
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
// If the game has no items in it, we want to continue
|
||||
if (datFile[game].Count == 0)
|
||||
if (datFile.Items[game].Count == 0)
|
||||
continue;
|
||||
|
||||
// Determine if the game has a parent or not
|
||||
string parent = null;
|
||||
if (!string.IsNullOrWhiteSpace(datFile[game][0].RomOf))
|
||||
parent = datFile[game][0].RomOf;
|
||||
if (!string.IsNullOrWhiteSpace(datFile.Items[game][0].RomOf))
|
||||
parent = datFile.Items[game][0].RomOf;
|
||||
|
||||
// If the parent doesnt exist, we want to continue
|
||||
if (string.IsNullOrWhiteSpace(parent))
|
||||
continue;
|
||||
|
||||
// If the parent doesn't have any items, we want to continue
|
||||
if (datFile[parent].Count == 0)
|
||||
if (datFile.Items[parent].Count == 0)
|
||||
continue;
|
||||
|
||||
// If the parent exists and has items, we copy the items from the parent to the current game
|
||||
DatItem copyFrom = datFile[game][0];
|
||||
List<DatItem> parentItems = datFile[parent];
|
||||
DatItem copyFrom = datFile.Items[game][0];
|
||||
List<DatItem> parentItems = datFile.Items[parent];
|
||||
foreach (DatItem item in parentItems)
|
||||
{
|
||||
DatItem datItem = (DatItem)item.Clone();
|
||||
datItem.CopyMachineInformation(copyFrom);
|
||||
if (datFile[game].Where(i => i.Name == datItem.Name).Count() == 0 && !datFile[game].Contains(datItem))
|
||||
datFile.Add(game, datItem);
|
||||
if (datFile.Items[game].Where(i => i.Name == datItem.Name).Count() == 0 && !datFile.Items[game].Contains(datItem))
|
||||
datFile.Items.Add(game, datItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1568,49 +1564,47 @@ namespace SabreTools.Library.DatFiles
|
||||
private bool AddRomsFromDevices(DatFile datFile, bool dev = false, bool slotoptions = false)
|
||||
{
|
||||
bool foundnew = false;
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
// If the game doesn't have items, we continue
|
||||
if (datFile[game] == null || datFile[game].Count == 0)
|
||||
if (datFile.Items[game] == null || datFile.Items[game].Count == 0)
|
||||
continue;
|
||||
|
||||
// If the game (is/is not) a bios, we want to continue
|
||||
if (dev ^ (datFile[game][0].MachineType.HasFlag(MachineType.Device)))
|
||||
if (dev ^ (datFile.Items[game][0].MachineType.HasFlag(MachineType.Device)))
|
||||
continue;
|
||||
|
||||
// If the game has no devices, we continue
|
||||
if (datFile[game][0].Devices == null
|
||||
|| datFile[game][0].Devices.Count == 0
|
||||
|| (slotoptions && datFile[game][0].SlotOptions == null)
|
||||
|| (slotoptions && datFile[game][0].SlotOptions.Count == 0))
|
||||
if (datFile.Items[game][0].Devices == null
|
||||
|| datFile.Items[game][0].Devices.Count == 0
|
||||
|| (slotoptions && datFile.Items[game][0].SlotOptions == null)
|
||||
|| (slotoptions && datFile.Items[game][0].SlotOptions.Count == 0))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Determine if the game has any devices or not
|
||||
List<string> devices = datFile[game][0].Devices;
|
||||
List<string> devices = datFile.Items[game][0].Devices;
|
||||
List<string> newdevs = new List<string>();
|
||||
foreach (string device in devices)
|
||||
{
|
||||
// If the device doesn't exist then we continue
|
||||
if (datFile[device].Count == 0)
|
||||
if (datFile.Items[device].Count == 0)
|
||||
continue;
|
||||
|
||||
// Otherwise, copy the items from the device to the current game
|
||||
DatItem copyFrom = datFile[game][0];
|
||||
List<DatItem> devItems = datFile[device];
|
||||
DatItem copyFrom = datFile.Items[game][0];
|
||||
List<DatItem> devItems = datFile.Items[device];
|
||||
foreach (DatItem item in devItems)
|
||||
{
|
||||
DatItem datItem = (DatItem)item.Clone();
|
||||
newdevs.AddRange(datItem.Devices ?? new List<string>());
|
||||
datItem.CopyMachineInformation(copyFrom);
|
||||
if (datFile[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
|
||||
if (datFile.Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
|
||||
{
|
||||
foundnew = true;
|
||||
datFile.Add(game, datItem);
|
||||
datFile.Items.Add(game, datItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1618,34 +1612,34 @@ namespace SabreTools.Library.DatFiles
|
||||
// Now that every device is accounted for, add the new list of devices, if they don't already exist
|
||||
foreach (string device in newdevs)
|
||||
{
|
||||
if (!datFile[game][0].Devices.Contains(device))
|
||||
datFile[game][0].Devices.Add(device);
|
||||
if (!datFile.Items[game][0].Devices.Contains(device))
|
||||
datFile.Items[game][0].Devices.Add(device);
|
||||
}
|
||||
|
||||
// If we're checking slotoptions too
|
||||
if (slotoptions)
|
||||
{
|
||||
// Determine if the game has any slotoptions or not
|
||||
List<string> slotopts = datFile[game][0].SlotOptions;
|
||||
List<string> slotopts = datFile.Items[game][0].SlotOptions;
|
||||
List<string> newslotopts = new List<string>();
|
||||
foreach (string slotopt in slotopts)
|
||||
{
|
||||
// If the slotoption doesn't exist then we continue
|
||||
if (datFile[slotopt].Count == 0)
|
||||
if (datFile.Items[slotopt].Count == 0)
|
||||
continue;
|
||||
|
||||
// Otherwise, copy the items from the slotoption to the current game
|
||||
DatItem copyFrom = datFile[game][0];
|
||||
List<DatItem> slotItems = datFile[slotopt];
|
||||
DatItem copyFrom = datFile.Items[game][0];
|
||||
List<DatItem> slotItems = datFile.Items[slotopt];
|
||||
foreach (DatItem item in slotItems)
|
||||
{
|
||||
DatItem datItem = (DatItem)item.Clone();
|
||||
newslotopts.AddRange(datItem.SlotOptions ?? new List<string>());
|
||||
datItem.CopyMachineInformation(copyFrom);
|
||||
if (datFile[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
|
||||
if (datFile.Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
|
||||
{
|
||||
foundnew = true;
|
||||
datFile.Add(game, datItem);
|
||||
datFile.Items.Add(game, datItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1653,8 +1647,8 @@ namespace SabreTools.Library.DatFiles
|
||||
// Now that every slotoption is accounted for, add the new list of slotoptions, if they don't already exist
|
||||
foreach (string slotopt in newslotopts)
|
||||
{
|
||||
if (!datFile[game][0].SlotOptions.Contains(slotopt))
|
||||
datFile[game][0].SlotOptions.Add(slotopt);
|
||||
if (!datFile.Items[game][0].SlotOptions.Contains(slotopt))
|
||||
datFile.Items[game][0].SlotOptions.Add(slotopt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1668,45 +1662,43 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="datFile">DatFile to filter</param>
|
||||
private void AddRomsFromParent(DatFile datFile)
|
||||
{
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
// If the game has no items in it, we want to continue
|
||||
if (datFile[game].Count == 0)
|
||||
if (datFile.Items[game].Count == 0)
|
||||
continue;
|
||||
|
||||
// Determine if the game has a parent or not
|
||||
string parent = null;
|
||||
if (!string.IsNullOrWhiteSpace(datFile[game][0].CloneOf))
|
||||
parent = datFile[game][0].CloneOf;
|
||||
if (!string.IsNullOrWhiteSpace(datFile.Items[game][0].CloneOf))
|
||||
parent = datFile.Items[game][0].CloneOf;
|
||||
|
||||
// If the parent doesnt exist, we want to continue
|
||||
if (string.IsNullOrWhiteSpace(parent))
|
||||
continue;
|
||||
|
||||
// If the parent doesn't have any items, we want to continue
|
||||
if (datFile[parent].Count == 0)
|
||||
if (datFile.Items[parent].Count == 0)
|
||||
continue;
|
||||
|
||||
// If the parent exists and has items, we copy the items from the parent to the current game
|
||||
DatItem copyFrom = datFile[game][0];
|
||||
List<DatItem> parentItems = datFile[parent];
|
||||
DatItem copyFrom = datFile.Items[game][0];
|
||||
List<DatItem> parentItems = datFile.Items[parent];
|
||||
foreach (DatItem item in parentItems)
|
||||
{
|
||||
DatItem datItem = (DatItem)item.Clone();
|
||||
datItem.CopyMachineInformation(copyFrom);
|
||||
if (datFile[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0
|
||||
&& !datFile[game].Contains(datItem))
|
||||
if (datFile.Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0
|
||||
&& !datFile.Items[game].Contains(datItem))
|
||||
{
|
||||
datFile.Add(game, datItem);
|
||||
datFile.Items.Add(game, datItem);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we want to get the parent romof tag and put it in each of the items
|
||||
List<DatItem> items = datFile[game];
|
||||
string romof = datFile[parent][0].RomOf;
|
||||
List<DatItem> items = datFile.Items[game];
|
||||
string romof = datFile.Items[parent][0].RomOf;
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
item.RomOf = romof;
|
||||
@@ -1720,27 +1712,25 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="datFile">DatFile to filter</param>
|
||||
private void AddRomsFromChildren(DatFile datFile)
|
||||
{
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
// If the game has no items in it, we want to continue
|
||||
if (datFile[game].Count == 0)
|
||||
if (datFile.Items[game].Count == 0)
|
||||
continue;
|
||||
|
||||
// Determine if the game has a parent or not
|
||||
string parent = null;
|
||||
if (!string.IsNullOrWhiteSpace(datFile[game][0].CloneOf))
|
||||
parent = datFile[game][0].CloneOf;
|
||||
if (!string.IsNullOrWhiteSpace(datFile.Items[game][0].CloneOf))
|
||||
parent = datFile.Items[game][0].CloneOf;
|
||||
|
||||
// If there is no parent, then we continue
|
||||
if (string.IsNullOrWhiteSpace(parent))
|
||||
continue;
|
||||
|
||||
// Otherwise, move the items from the current game to a subfolder of the parent game
|
||||
DatItem copyFrom = datFile[parent].Count == 0 ? new Rom { MachineName = parent, MachineDescription = parent } : datFile[parent][0];
|
||||
List<DatItem> items = datFile[game];
|
||||
DatItem copyFrom = datFile.Items[parent].Count == 0 ? new Rom { MachineName = parent, MachineDescription = parent } : datFile.Items[parent][0];
|
||||
List<DatItem> items = datFile.Items[game];
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
// Special disk handling
|
||||
@@ -1749,23 +1739,23 @@ namespace SabreTools.Library.DatFiles
|
||||
Disk disk = item as Disk;
|
||||
|
||||
// If the merge tag exists and the parent already contains it, skip
|
||||
if (disk.MergeTag != null && datFile[parent].Select(i => i.Name).Contains(disk.MergeTag))
|
||||
if (disk.MergeTag != null && datFile.Items[parent].Select(i => i.Name).Contains(disk.MergeTag))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the merge tag exists but the parent doesn't contain it, add to parent
|
||||
else if (disk.MergeTag != null && !datFile[parent].Select(i => i.Name).Contains(disk.MergeTag))
|
||||
else if (disk.MergeTag != null && !datFile.Items[parent].Select(i => i.Name).Contains(disk.MergeTag))
|
||||
{
|
||||
item.CopyMachineInformation(copyFrom);
|
||||
datFile.Add(parent, item);
|
||||
datFile.Items.Add(parent, item);
|
||||
}
|
||||
|
||||
// If there is no merge tag, add to parent
|
||||
else if (disk.MergeTag == null)
|
||||
{
|
||||
item.CopyMachineInformation(copyFrom);
|
||||
datFile.Add(parent, item);
|
||||
datFile.Items.Add(parent, item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1775,39 +1765,39 @@ namespace SabreTools.Library.DatFiles
|
||||
Rom rom = item as Rom;
|
||||
|
||||
// If the merge tag exists and the parent already contains it, skip
|
||||
if (rom.MergeTag != null && datFile[parent].Select(i => i.Name).Contains(rom.MergeTag))
|
||||
if (rom.MergeTag != null && datFile.Items[parent].Select(i => i.Name).Contains(rom.MergeTag))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the merge tag exists but the parent doesn't contain it, add to subfolder of parent
|
||||
else if (rom.MergeTag != null && !datFile[parent].Select(i => i.Name).Contains(rom.MergeTag))
|
||||
else if (rom.MergeTag != null && !datFile.Items[parent].Select(i => i.Name).Contains(rom.MergeTag))
|
||||
{
|
||||
item.Name = $"{item.MachineName}\\{item.Name}";
|
||||
item.CopyMachineInformation(copyFrom);
|
||||
datFile.Add(parent, item);
|
||||
datFile.Items.Add(parent, item);
|
||||
}
|
||||
|
||||
// If the parent doesn't already contain this item, add to subfolder of parent
|
||||
else if (!datFile[parent].Contains(item))
|
||||
else if (!datFile.Items[parent].Contains(item))
|
||||
{
|
||||
item.Name = $"{item.MachineName}\\{item.Name}";
|
||||
item.CopyMachineInformation(copyFrom);
|
||||
datFile.Add(parent, item);
|
||||
datFile.Items.Add(parent, item);
|
||||
}
|
||||
}
|
||||
|
||||
// All other that would be missing to subfolder of parent
|
||||
else if (!datFile[parent].Contains(item))
|
||||
else if (!datFile.Items[parent].Contains(item))
|
||||
{
|
||||
item.Name = $"{item.MachineName}\\{item.Name}";
|
||||
item.CopyMachineInformation(copyFrom);
|
||||
datFile.Add(parent, item);
|
||||
datFile.Items.Add(parent, item);
|
||||
}
|
||||
}
|
||||
|
||||
// Then, remove the old game so it's not picked up by the writer
|
||||
datFile.Remove(game);
|
||||
datFile.Items.Remove(game);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1817,16 +1807,14 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="datFile">DatFile to filter</param>
|
||||
private void RemoveBiosAndDeviceSets(DatFile datFile)
|
||||
{
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
if (datFile[game].Count > 0
|
||||
&& (datFile[game][0].MachineType.HasFlag(MachineType.Bios)
|
||||
|| datFile[game][0].MachineType.HasFlag(MachineType.Device)))
|
||||
if (datFile.Items[game].Count > 0
|
||||
&& (datFile.Items[game][0].MachineType.HasFlag(MachineType.Bios)
|
||||
|| datFile.Items[game][0].MachineType.HasFlag(MachineType.Device)))
|
||||
{
|
||||
datFile.Remove(game);
|
||||
datFile.Items.Remove(game);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1839,40 +1827,38 @@ namespace SabreTools.Library.DatFiles
|
||||
private void RemoveBiosRomsFromChild(DatFile datFile, bool bios = false)
|
||||
{
|
||||
// Loop through the romof tags
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
// If the game has no items in it, we want to continue
|
||||
if (datFile[game].Count == 0)
|
||||
if (datFile.Items[game].Count == 0)
|
||||
continue;
|
||||
|
||||
// If the game (is/is not) a bios, we want to continue
|
||||
if (bios ^ datFile[game][0].MachineType.HasFlag(MachineType.Bios))
|
||||
if (bios ^ datFile.Items[game][0].MachineType.HasFlag(MachineType.Bios))
|
||||
continue;
|
||||
|
||||
// Determine if the game has a parent or not
|
||||
string parent = null;
|
||||
if (!string.IsNullOrWhiteSpace(datFile[game][0].RomOf))
|
||||
parent = datFile[game][0].RomOf;
|
||||
if (!string.IsNullOrWhiteSpace(datFile.Items[game][0].RomOf))
|
||||
parent = datFile.Items[game][0].RomOf;
|
||||
|
||||
// If the parent doesnt exist, we want to continue
|
||||
if (string.IsNullOrWhiteSpace(parent))
|
||||
continue;
|
||||
|
||||
// If the parent doesn't have any items, we want to continue
|
||||
if (datFile[parent].Count == 0)
|
||||
if (datFile.Items[parent].Count == 0)
|
||||
continue;
|
||||
|
||||
// If the parent exists and has items, we remove the items that are in the parent from the current game
|
||||
List<DatItem> parentItems = datFile[parent];
|
||||
List<DatItem> parentItems = datFile.Items[parent];
|
||||
foreach (DatItem item in parentItems)
|
||||
{
|
||||
DatItem datItem = (DatItem)item.Clone();
|
||||
while (datFile[game].Contains(datItem))
|
||||
while (datFile.Items[game].Contains(datItem))
|
||||
{
|
||||
datFile.Remove(game, datItem);
|
||||
datFile.Items.Remove(game, datItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1884,42 +1870,40 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="datFile">DatFile to filter</param>
|
||||
private void RemoveRomsFromChild(DatFile datFile)
|
||||
{
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
// If the game has no items in it, we want to continue
|
||||
if (datFile[game].Count == 0)
|
||||
if (datFile.Items[game].Count == 0)
|
||||
continue;
|
||||
|
||||
// Determine if the game has a parent or not
|
||||
string parent = null;
|
||||
if (!string.IsNullOrWhiteSpace(datFile[game][0].CloneOf))
|
||||
parent = datFile[game][0].CloneOf;
|
||||
if (!string.IsNullOrWhiteSpace(datFile.Items[game][0].CloneOf))
|
||||
parent = datFile.Items[game][0].CloneOf;
|
||||
|
||||
// If the parent doesnt exist, we want to continue
|
||||
if (string.IsNullOrWhiteSpace(parent))
|
||||
continue;
|
||||
|
||||
// If the parent doesn't have any items, we want to continue
|
||||
if (datFile[parent].Count == 0)
|
||||
if (datFile.Items[parent].Count == 0)
|
||||
continue;
|
||||
|
||||
// If the parent exists and has items, we remove the parent items from the current game
|
||||
List<DatItem> parentItems = datFile[parent];
|
||||
List<DatItem> parentItems = datFile.Items[parent];
|
||||
foreach (DatItem item in parentItems)
|
||||
{
|
||||
DatItem datItem = (DatItem)item.Clone();
|
||||
while (datFile[game].Contains(datItem))
|
||||
while (datFile.Items[game].Contains(datItem))
|
||||
{
|
||||
datFile.Remove(game, datItem);
|
||||
datFile.Items.Remove(game, datItem);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we want to get the parent romof tag and put it in each of the remaining items
|
||||
List<DatItem> items = datFile[game];
|
||||
string romof = datFile[parent][0].RomOf;
|
||||
List<DatItem> items = datFile.Items[game];
|
||||
string romof = datFile.Items[parent][0].RomOf;
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
item.RomOf = romof;
|
||||
@@ -1933,12 +1917,10 @@ namespace SabreTools.Library.DatFiles
|
||||
/// <param name="datFile">DatFile to filter</param>
|
||||
private void RemoveTagsFromChild(DatFile datFile)
|
||||
{
|
||||
List<string> games = datFile.Keys;
|
||||
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
|
||||
|
||||
List<string> games = datFile.Items.Keys.OrderBy(g => g).ToList();
|
||||
foreach (string game in games)
|
||||
{
|
||||
List<DatItem> items = datFile[game];
|
||||
List<DatItem> items = datFile.Items[game];
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
item.CloneOf = null;
|
||||
@@ -1962,10 +1944,9 @@ namespace SabreTools.Library.DatFiles
|
||||
{
|
||||
// First we want to get a mapping for all games to description
|
||||
ConcurrentDictionary<string, string> mapping = new ConcurrentDictionary<string, string>();
|
||||
List<string> keys = datFile.Keys;
|
||||
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
||||
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
// If the key mapping doesn't exist, add it
|
||||
@@ -1974,10 +1955,9 @@ namespace SabreTools.Library.DatFiles
|
||||
});
|
||||
|
||||
// Now we loop through every item and update accordingly
|
||||
keys = datFile.Keys;
|
||||
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
||||
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
List<DatItem> newItems = new List<DatItem>();
|
||||
foreach (DatItem item in items)
|
||||
{
|
||||
@@ -2002,8 +1982,8 @@ namespace SabreTools.Library.DatFiles
|
||||
}
|
||||
|
||||
// Replace the old list of roms with the new one
|
||||
datFile.Remove(key);
|
||||
datFile.AddRange(key, newItems);
|
||||
datFile.Items.Remove(key);
|
||||
datFile.Items.AddRange(key, newItems);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -2020,9 +2000,9 @@ namespace SabreTools.Library.DatFiles
|
||||
private void OneRomPerGame(DatFile datFile)
|
||||
{
|
||||
// For each rom, we want to update the game to be "<game name>/<rom name>"
|
||||
Parallel.ForEach(datFile.Keys, Globals.ParallelOptions, key =>
|
||||
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
for (int i = 0; i < items.Count; i++)
|
||||
{
|
||||
string[] splitname = items[i].Name.Split('.');
|
||||
@@ -2044,10 +2024,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string pattern = @"([0-9]{2}\.[0-9]{2}\.[0-9]{2}-)(.*?-.*?)";
|
||||
|
||||
// Now process all of the roms
|
||||
List<string> keys = datFile.Keys;
|
||||
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
||||
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
List<DatItem> items = datFile[key];
|
||||
List<DatItem> items = datFile.Items[key];
|
||||
for (int j = 0; j < items.Count; j++)
|
||||
{
|
||||
DatItem item = items[j];
|
||||
@@ -2060,8 +2039,8 @@ namespace SabreTools.Library.DatFiles
|
||||
items[j] = item;
|
||||
}
|
||||
|
||||
datFile.Remove(key);
|
||||
datFile.AddRange(key, items);
|
||||
datFile.Items.Remove(key);
|
||||
datFile.Items.AddRange(key, items);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -126,9 +126,9 @@ namespace SabreTools.Library.DatFiles
|
||||
};
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
576
SabreTools.Library/DatFiles/ItemDictionary.cs
Normal file
576
SabreTools.Library/DatFiles/ItemDictionary.cs
Normal file
@@ -0,0 +1,576 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using SabreTools.Library.Data;
|
||||
using SabreTools.Library.DatItems;
|
||||
using NaturalSort;
|
||||
|
||||
namespace SabreTools.Library.DatFiles
|
||||
{
|
||||
/// <summary>
|
||||
/// Item dictionary with statistics, bucketing, and sorting
|
||||
/// </summary>
|
||||
public class ItemDictionary : IDictionary<string, List<DatItem>>
|
||||
{
|
||||
#region Private instance variables
|
||||
|
||||
/// <summary>
|
||||
/// Determine the bucketing key for all items
|
||||
/// </summary>
|
||||
private BucketedBy bucketedBy;
|
||||
|
||||
/// <summary>
|
||||
/// Determine merging type for all items
|
||||
/// </summary>
|
||||
private DedupeType mergedBy;
|
||||
|
||||
/// <summary>
|
||||
/// Internal dictionary for the class
|
||||
/// </summary>
|
||||
private Dictionary<string, List<DatItem>> items;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Publically available fields
|
||||
|
||||
/// <summary>
|
||||
/// DatStats object for reporting
|
||||
/// </summary>
|
||||
public DatStats Statistics { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the keys from the file dictionary
|
||||
/// </summary>
|
||||
/// <returns>List of the keys</returns>
|
||||
public ICollection<string> Keys
|
||||
{
|
||||
get { return items.Keys; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the keys in sorted order from the file dictionary
|
||||
/// </summary>
|
||||
/// <returns>List of the keys in sorted order</returns>
|
||||
public List<string> SortedKeys
|
||||
{
|
||||
get
|
||||
{
|
||||
var keys = items.Keys.ToList();
|
||||
keys.Sort(new NaturalComparer());
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Passthrough to access the file dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to reference</param>
|
||||
public List<DatItem> this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
// Explicit lock for some weird corner cases
|
||||
lock (key)
|
||||
{
|
||||
// Ensure the key exists
|
||||
EnsureKey(key);
|
||||
|
||||
// Now return the value
|
||||
return items[key];
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
AddRange(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a value to the file dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to add to</param>
|
||||
/// <param name="value">Value to add to the dictionary</param>
|
||||
public void Add(string key, DatItem value)
|
||||
{
|
||||
// Explicit lock for some weird corner cases
|
||||
lock (key)
|
||||
{
|
||||
// Ensure the key exists
|
||||
EnsureKey(key);
|
||||
|
||||
// If item is null, don't add it
|
||||
if (value == null)
|
||||
return;
|
||||
|
||||
// Now add the value
|
||||
items[key].Add(value);
|
||||
|
||||
// Now update the statistics
|
||||
Statistics.AddItem(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a range of values to the file dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to add to</param>
|
||||
/// <param name="value">Value to add to the dictionary</param>
|
||||
public void Add(string key, List<DatItem> value)
|
||||
{
|
||||
AddRange(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a range of values to the file dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to add to</param>
|
||||
/// <param name="value">Value to add to the dictionary</param>
|
||||
public void AddRange(string key, List<DatItem> value)
|
||||
{
|
||||
// Explicit lock for some weird corner cases
|
||||
lock (key)
|
||||
{
|
||||
// Ensure the key exists
|
||||
EnsureKey(key);
|
||||
|
||||
// Now add the value
|
||||
items[key].AddRange(value);
|
||||
|
||||
// Now update the statistics
|
||||
foreach (DatItem item in value)
|
||||
{
|
||||
Statistics.AddItem(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get if the file dictionary contains the key
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to check</param>
|
||||
/// <returns>True if the key exists, false otherwise</returns>
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
// If the key is null, we return false since keys can't be null
|
||||
if (key == null)
|
||||
return false;
|
||||
|
||||
// Explicit lock for some weird corner cases
|
||||
lock (key)
|
||||
{
|
||||
return items.ContainsKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get if the file dictionary contains the key and value
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to check</param>
|
||||
/// <param name="value">Value in the dictionary to check</param>
|
||||
/// <returns>True if the key exists, false otherwise</returns>
|
||||
public bool Contains(string key, DatItem value)
|
||||
{
|
||||
// If the key is null, we return false since keys can't be null
|
||||
if (key == null)
|
||||
return false;
|
||||
|
||||
// Explicit lock for some weird corner cases
|
||||
lock (key)
|
||||
{
|
||||
if (items.ContainsKey(key))
|
||||
return items[key].Contains(value);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a key from the file dictionary if it exists
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to remove</param>
|
||||
public bool Remove(string key)
|
||||
{
|
||||
// If the key doesn't exist, return
|
||||
if (!ContainsKey(key))
|
||||
return false;
|
||||
|
||||
// Remove the statistics first
|
||||
foreach (DatItem item in items[key])
|
||||
{
|
||||
Statistics.RemoveItem(item);
|
||||
}
|
||||
|
||||
// Remove the key from the dictionary
|
||||
return items.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove the first instance of a value from the file dictionary if it exists
|
||||
/// </summary>
|
||||
/// <param name="key">Key in the dictionary to remove from</param>
|
||||
/// <param name="value">Value to remove from the dictionary</param>
|
||||
public bool Remove(string key, DatItem value)
|
||||
{
|
||||
// If the key and value doesn't exist, return
|
||||
if (!Contains(key, value))
|
||||
return false;
|
||||
|
||||
// Remove the statistics first
|
||||
Statistics.RemoveItem(value);
|
||||
|
||||
return items[key].Remove(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override the internal BucketedBy value
|
||||
/// </summary>
|
||||
/// <param name="newBucket"></param>
|
||||
public void SetBucketedBy(BucketedBy newBucket)
|
||||
{
|
||||
bucketedBy = newBucket;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure the key exists in the items dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key to ensure</param>
|
||||
private void EnsureKey(string key)
|
||||
{
|
||||
// If the key is missing from the dictionary, add it
|
||||
if (!items.ContainsKey(key))
|
||||
items.Add(key, new List<DatItem>());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Generic constructor
|
||||
/// </summary>
|
||||
public ItemDictionary()
|
||||
{
|
||||
bucketedBy = BucketedBy.Default;
|
||||
mergedBy = DedupeType.None;
|
||||
items = new Dictionary<string, List<DatItem>>();
|
||||
|
||||
Statistics = new DatStats();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for statistics only
|
||||
/// </summary>
|
||||
/// <param name="stats">Existing statistics to pre-populate</param>
|
||||
public ItemDictionary(DatStats stats)
|
||||
{
|
||||
bucketedBy = BucketedBy.Default;
|
||||
mergedBy = DedupeType.None;
|
||||
items = new Dictionary<string, List<DatItem>>();
|
||||
|
||||
Statistics = stats;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Custom Functionality
|
||||
|
||||
/// <summary>
|
||||
/// Take the arbitrarily bucketed Files Dictionary and convert to one bucketed by a user-defined method
|
||||
/// </summary>
|
||||
/// <param name="bucketBy">BucketedBy enum representing how to bucket the individual items</param>
|
||||
/// <param name="dedupeType">Dedupe type that should be used</param>
|
||||
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
|
||||
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
||||
public void BucketBy(BucketedBy bucketBy, DedupeType dedupeType, bool lower = true, bool norename = true)
|
||||
{
|
||||
// If we have a situation where there's no dictionary or no keys at all, we skip
|
||||
if (items == null || items.Count == 0)
|
||||
return;
|
||||
|
||||
// If the sorted type isn't the same, we want to sort the dictionary accordingly
|
||||
if (this.bucketedBy != bucketBy)
|
||||
{
|
||||
Globals.Logger.User($"Organizing roms by {bucketBy}");
|
||||
|
||||
// Set the sorted type
|
||||
this.bucketedBy = bucketBy;
|
||||
|
||||
// Reset the merged type since this might change the merge
|
||||
this.mergedBy = DedupeType.None;
|
||||
|
||||
// First do the initial sort of all of the roms inplace
|
||||
List<string> oldkeys = Keys.ToList();
|
||||
for (int k = 0; k < oldkeys.Count; k++)
|
||||
{
|
||||
string key = oldkeys[k];
|
||||
|
||||
// Get the unsorted current list
|
||||
List<DatItem> items = this[key];
|
||||
|
||||
// Now add each of the roms to their respective keys
|
||||
for (int i = 0; i < items.Count; i++)
|
||||
{
|
||||
DatItem item = items[i];
|
||||
if (item == null)
|
||||
continue;
|
||||
|
||||
// We want to get the key most appropriate for the given sorting type
|
||||
string newkey = item.GetKey(bucketBy, lower, norename);
|
||||
|
||||
// If the key is different, move the item to the new key
|
||||
if (newkey != key)
|
||||
{
|
||||
Add(newkey, item);
|
||||
Remove(key, item);
|
||||
i--; // This make sure that the pointer stays on the correct since one was removed
|
||||
}
|
||||
}
|
||||
|
||||
// If the key is now empty, remove it
|
||||
if (this[key].Count == 0)
|
||||
Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
// If the merge type isn't the same, we want to merge the dictionary accordingly
|
||||
if (this.mergedBy != dedupeType)
|
||||
{
|
||||
Globals.Logger.User($"Deduping roms by {dedupeType}");
|
||||
|
||||
// Set the sorted type
|
||||
this.mergedBy = dedupeType;
|
||||
|
||||
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
// Get the possibly unsorted list
|
||||
List<DatItem> sortedlist = this[key];
|
||||
|
||||
// Sort the list of items to be consistent
|
||||
DatItem.Sort(ref sortedlist, false);
|
||||
|
||||
// If we're merging the roms, do so
|
||||
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == BucketedBy.Game))
|
||||
sortedlist = DatItem.Merge(sortedlist);
|
||||
|
||||
// Add the list back to the dictionary
|
||||
Remove(key);
|
||||
AddRange(key, sortedlist);
|
||||
});
|
||||
}
|
||||
// If the merge type is the same, we want to sort the dictionary to be consistent
|
||||
else
|
||||
{
|
||||
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
// Get the possibly unsorted list
|
||||
List<DatItem> sortedlist = this[key];
|
||||
|
||||
// Sort the list of items to be consistent
|
||||
DatItem.Sort(ref sortedlist, false);
|
||||
});
|
||||
}
|
||||
|
||||
// Now clean up all empty keys
|
||||
ClearEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all items marked for removal
|
||||
/// </summary>
|
||||
public void ClearMarked()
|
||||
{
|
||||
foreach (string key in items.Keys)
|
||||
{
|
||||
List<DatItem> oldItemList = items[key];
|
||||
List<DatItem> newItemList = oldItemList.Where(i => !i.Remove).ToList();
|
||||
|
||||
Remove(key);
|
||||
AddRange(key, newItemList);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List all duplicates found in a DAT based on a DatItem
|
||||
/// </summary>
|
||||
/// <param name="datItem">Item to try to match</param>
|
||||
/// <param name="remove">True to mark matched roms for removal from the input, false otherwise (default)</param>
|
||||
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
||||
/// <returns>List of matched DatItem objects</returns>
|
||||
public List<DatItem> GetDuplicates(DatItem datItem, bool remove = false, bool sorted = false)
|
||||
{
|
||||
List<DatItem> output = new List<DatItem>();
|
||||
|
||||
// Check for an empty rom list first
|
||||
if (Statistics.Count == 0)
|
||||
return output;
|
||||
|
||||
// We want to get the proper key for the DatItem
|
||||
string key = SortAndGetKey(datItem, sorted);
|
||||
|
||||
// If the key doesn't exist, return the empty list
|
||||
if (!ContainsKey(key))
|
||||
return output;
|
||||
|
||||
// Try to find duplicates
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> left = new List<DatItem>();
|
||||
for (int i = 0; i < roms.Count; i++)
|
||||
{
|
||||
DatItem other = roms[i];
|
||||
|
||||
if (datItem.Equals(other))
|
||||
{
|
||||
other.Remove = true;
|
||||
output.Add(other);
|
||||
}
|
||||
else
|
||||
{
|
||||
left.Add(other);
|
||||
}
|
||||
}
|
||||
|
||||
// If we're in removal mode, add back all roms with the proper flags
|
||||
if (remove)
|
||||
{
|
||||
Remove(key);
|
||||
AddRange(key, output);
|
||||
AddRange(key, left);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a DAT contains the given DatItem
|
||||
/// </summary>
|
||||
/// <param name="datItem">Item to try to match</param>
|
||||
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
||||
/// <returns>True if it contains the rom, false otherwise</returns>
|
||||
public bool HasDuplicates(DatItem datItem, bool sorted = false)
|
||||
{
|
||||
// Check for an empty rom list first
|
||||
if (Statistics.Count == 0)
|
||||
return false;
|
||||
|
||||
// We want to get the proper key for the DatItem
|
||||
string key = SortAndGetKey(datItem, sorted);
|
||||
|
||||
// If the key doesn't exist, return the empty list
|
||||
if (!ContainsKey(key))
|
||||
return false;
|
||||
|
||||
// Try to find duplicates
|
||||
List<DatItem> roms = this[key];
|
||||
return roms.Any(r => datItem.Equals(r));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recalculate the statistics for the Dat
|
||||
/// </summary>
|
||||
public void RecalculateStats()
|
||||
{
|
||||
// Wipe out any stats already there
|
||||
Statistics.Reset();
|
||||
|
||||
// If we have a blank Dat in any way, return
|
||||
if (this == null || Statistics.Count == 0)
|
||||
return;
|
||||
|
||||
// Loop through and add
|
||||
Parallel.ForEach(items.Keys, Globals.ParallelOptions, key =>
|
||||
{
|
||||
List<DatItem> datItems = items[key];
|
||||
foreach (DatItem item in datItems)
|
||||
{
|
||||
Statistics.AddItem(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove any keys that have null or empty values
|
||||
/// </summary>
|
||||
private void ClearEmpty()
|
||||
{
|
||||
foreach (string key in items.Keys)
|
||||
{
|
||||
if (items[key] == null || items[key].Count == 0)
|
||||
items.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sort the input DAT and get the key to be used by the item
|
||||
/// </summary>
|
||||
/// <param name="datItem">Item to try to match</param>
|
||||
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
||||
/// <returns>Key to try to use</returns>
|
||||
private string SortAndGetKey(DatItem datItem, bool sorted = false)
|
||||
{
|
||||
// If we're not already sorted, take care of it
|
||||
if (!sorted)
|
||||
BucketBy(Statistics.GetBestAvailable(), DedupeType.None);
|
||||
|
||||
// Now that we have the sorted type, we get the proper key
|
||||
return datItem.GetKey(bucketedBy);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDictionary Implementations
|
||||
|
||||
public ICollection<List<DatItem>> Values => ((IDictionary<string, List<DatItem>>)items).Values;
|
||||
|
||||
public int Count => ((ICollection<KeyValuePair<string, List<DatItem>>>)items).Count;
|
||||
|
||||
public bool IsReadOnly => ((ICollection<KeyValuePair<string, List<DatItem>>>)items).IsReadOnly;
|
||||
|
||||
public bool TryGetValue(string key, out List<DatItem> value)
|
||||
{
|
||||
return ((IDictionary<string, List<DatItem>>)items).TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<string, List<DatItem>> item)
|
||||
{
|
||||
((ICollection<KeyValuePair<string, List<DatItem>>>)items).Add(item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
((ICollection<KeyValuePair<string, List<DatItem>>>)items).Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<string, List<DatItem>> item)
|
||||
{
|
||||
return ((ICollection<KeyValuePair<string, List<DatItem>>>)items).Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<string, List<DatItem>>[] array, int arrayIndex)
|
||||
{
|
||||
((ICollection<KeyValuePair<string, List<DatItem>>>)items).CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<string, List<DatItem>> item)
|
||||
{
|
||||
return ((ICollection<KeyValuePair<string, List<DatItem>>>)items).Remove(item);
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, List<DatItem>>> GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<KeyValuePair<string, List<DatItem>>>)items).GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable)items).GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -777,9 +777,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -254,9 +254,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -575,9 +575,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -672,9 +672,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -68,9 +68,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -758,9 +758,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
@@ -837,7 +837,7 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
xtw.WriteStartElement("configuration");
|
||||
xtw.WriteElementString("datName", DatHeader.Name);
|
||||
xtw.WriteElementString("datVersion", DatStats.Count.ToString());
|
||||
xtw.WriteElementString("datVersion", Items.Statistics.Count.ToString());
|
||||
xtw.WriteElementString("system", "none");
|
||||
xtw.WriteElementString("screenshotsWidth", "240");
|
||||
xtw.WriteElementString("screenshotsHeight", "160");
|
||||
|
||||
@@ -502,9 +502,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -393,9 +393,9 @@ namespace SabreTools.Library.DatFiles
|
||||
List<string> splitpath = new List<string>();
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -564,9 +564,9 @@ namespace SabreTools.Library.DatFiles
|
||||
List<string> splitpath = new List<string>();
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -883,9 +883,9 @@ namespace SabreTools.Library.DatFiles
|
||||
WriteHeader(svw);
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
@@ -375,14 +375,14 @@ namespace SabreTools.Library.DatFiles
|
||||
// If the rom is continue or ignore, add the size to the previous rom
|
||||
if (reader.GetAttribute("loadflag") == "continue" || reader.GetAttribute("loadflag") == "ignore")
|
||||
{
|
||||
int index = this[key].Count - 1;
|
||||
DatItem lastrom = this[key][index];
|
||||
int index = Items[key].Count - 1;
|
||||
DatItem lastrom = Items[key][index];
|
||||
if (lastrom.ItemType == ItemType.Rom)
|
||||
{
|
||||
((Rom)lastrom).Size += Sanitizer.CleanSize(reader.GetAttribute("size"));
|
||||
}
|
||||
this[key].RemoveAt(index);
|
||||
this[key].Add(lastrom);
|
||||
Items[key].RemoveAt(index);
|
||||
Items[key].Add(lastrom);
|
||||
reader.Read();
|
||||
continue;
|
||||
}
|
||||
@@ -555,9 +555,9 @@ namespace SabreTools.Library.DatFiles
|
||||
string lastgame = null;
|
||||
|
||||
// Use a sorted list of games to output
|
||||
foreach (string key in SortedKeys)
|
||||
foreach (string key in Items.SortedKeys)
|
||||
{
|
||||
List<DatItem> roms = this[key];
|
||||
List<DatItem> roms = Items[key];
|
||||
|
||||
// Resolve the names in the block
|
||||
roms = DatItem.ResolveNames(roms);
|
||||
|
||||
Reference in New Issue
Block a user