From 377d361399b9890996ba0603968e5347008add98 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 11 Jan 2017 09:57:39 -0800 Subject: [PATCH] [DatFile] Create and use helper functions for split types --- .../Dats/Partials/DatFile.Bucketing.cs | 569 +++++++----------- 1 file changed, 208 insertions(+), 361 deletions(-) diff --git a/SabreTools.Helper/Dats/Partials/DatFile.Bucketing.cs b/SabreTools.Helper/Dats/Partials/DatFile.Bucketing.cs index fba1ec75..ab2e3148 100644 --- a/SabreTools.Helper/Dats/Partials/DatFile.Bucketing.cs +++ b/SabreTools.Helper/Dats/Partials/DatFile.Bucketing.cs @@ -384,213 +384,17 @@ namespace SabreTools.Helper.Dats BucketByGame(mergeroms, true, logger, output); _sortedBy = SortedBy.Default; + // Now we want to loop through all of the games and add the information from devices + AddRomsFromDevices(logger); + // Now we want to loop through all of the games and set the correct information - List games = Keys.ToList(); - foreach (string game in games) - { - // Determine if the game has any devices or not - if (this[game][0].Machine.Devices.Count > 0) - { - List devices = this[game][0].Machine.Devices; - foreach (string device in devices) - { - // If the device doesn't exist then we continue - if (this[device].Count == 0) - { - continue; - } + AddRomsFromParent(logger); - // Otherwise, copy the items from the device to the current game - Machine musheen = this[game][0].Machine; - List devItems = this[device]; - foreach (DatItem item in devItems) - { - // Figure out the type of the item and add it accordingly - switch (item.Type) - { - case ItemType.Archive: - Archive archive = ((Archive)item).Clone() as Archive; - archive.Machine = musheen; - if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) - { - this[game].Add(archive); - } + // Now that we have looped through the cloneof tags, we loop through the romof tags + RemoveBiosRomsFromChild(logger); - break; - case ItemType.BiosSet: - BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; - biosSet.Machine = musheen; - if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) - { - this[game].Add(biosSet); - } - - break; - case ItemType.Disk: - Disk disk = ((Disk)item).Clone() as Disk; - disk.Machine = musheen; - if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) - { - this[game].Add(disk); - } - - break; - case ItemType.Release: - Release release = ((Release)item).Clone() as Release; - release.Machine = musheen; - if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) - { - this[game].Add(release); - } - - break; - case ItemType.Rom: - Rom rom = ((Rom)item).Clone() as Rom; - rom.Machine = musheen; - if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) - { - this[game].Add(rom); - } - - break; - case ItemType.Sample: - Sample sample = ((Sample)item).Clone() as Sample; - sample.Machine = musheen; - if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) - { - this[game].Add(sample); - } - - break; - } - } - - // Then, remove the romof and cloneof tags so it's not picked up by the manager - devItems = this[game]; - foreach (DatItem item in devItems) - { - item.Machine.CloneOf = null; - item.Machine.RomOf = null; - } - } - } - - // Determine if the game has a parent or not - string parent = null; - if (!String.IsNullOrEmpty(this[game][0].Machine.CloneOf)) - { - parent = this[game][0].Machine.CloneOf; - } - else if (!String.IsNullOrEmpty(this[game][0].Machine.RomOf)) - { - parent = this[game][0].Machine.RomOf; - } - - // If there is no parent, then we continue - if (String.IsNullOrEmpty(parent)) - { - continue; - } - - // If the parent doesn't exist, then we continue and remove - if (this[parent].Count == 0) - { - List curitems = this[game]; - foreach (DatItem item in curitems) - { - item.Machine.CloneOf = null; - item.Machine.RomOf = null; - } - - continue; - } - - // Otherwise, copy the items from the parent to the current game - Machine currentMachine = this[game][0].Machine; - List items = this[parent]; - foreach (DatItem item in items) - { - // Figure out the type of the item and add it accordingly - switch (item.Type) - { - case ItemType.Archive: - Archive archive = ((Archive)item).Clone() as Archive; - archive.Machine = currentMachine; - if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) - { - this[game].Add(archive); - } - - break; - case ItemType.BiosSet: - BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; - biosSet.Machine = currentMachine; - if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) - { - this[game].Add(biosSet); - } - - break; - case ItemType.Disk: - Disk disk = ((Disk)item).Clone() as Disk; - disk.Machine = currentMachine; - if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) - { - this[game].Add(disk); - } - - break; - case ItemType.Release: - Release release = ((Release)item).Clone() as Release; - release.Machine = currentMachine; - if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) - { - this[game].Add(release); - } - - break; - case ItemType.Rom: - Rom rom = ((Rom)item).Clone() as Rom; - rom.Machine = currentMachine; - if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) - { - this[game].Add(rom); - } - - break; - case ItemType.Sample: - Sample sample = ((Sample)item).Clone() as Sample; - sample.Machine = currentMachine; - if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) - { - this[game].Add(sample); - } - - break; - } - } - - // Then, remove the romof and cloneof tags so it's not picked up by the manager - items = this[game]; - foreach (DatItem item in items) - { - item.Machine.CloneOf = null; - item.Machine.RomOf = null; - } - } - - // Finally, we want to remove all games that have the BIOS or Device tags - games = Keys.ToList(); - foreach (string game in games) - { - if (this[game].Count > 0) - { - if (this[game][0].Machine.MachineType == MachineType.Bios || this[game][0].Machine.MachineType == MachineType.Device) - { - Remove(game); - } - } - } + // Finally, remove the romof and cloneof tags so it's not picked up by the manager + RemoveTagsFromChild(logger); } /// @@ -608,50 +412,13 @@ namespace SabreTools.Helper.Dats _sortedBy = SortedBy.Default; // Now we want to loop through all of the games and set the correct information - List games = Keys.ToList(); - foreach (string game in games) - { - // Determine if the game has a parent or not - string parent = null; - if (!String.IsNullOrEmpty(this[game][0].Machine.CloneOf)) - { - parent = this[game][0].Machine.CloneOf; - } + AddRomsToParent(logger); - // If there is no parent, then we continue - if (String.IsNullOrEmpty(parent)) - { - continue; - } - - // Otherwise, move the items from the current game to a subfolder of the parent game - Machine parentMachine = this[parent].Count == 0 ? new Machine { Name = parent, Description = parent } : this[parent][0].Machine; - List items = this[game]; - foreach (DatItem item in items) - { - if (!this[parent].Contains(item)) - { - item.Name = item.Machine.Name + "\\" + item.Name; - item.Machine = parentMachine; - this[parent].Add(item); - } - } - - // Then, remove the old game so it's not picked up by the writer - Remove(game); - } + // Now that we have looped through the cloneof tags, we loop through the romof tags + RemoveBiosRomsFromChild(logger); // Finally, remove the romof and cloneof tags so it's not picked up by the manager - games = Keys.ToList(); - foreach (string game in games) - { - List items = this[game]; - foreach (DatItem item in items) - { - item.Machine.CloneOf = null; - item.Machine.RomOf = null; - } - } + RemoveTagsFromChild(logger); } /// @@ -669,6 +436,142 @@ namespace SabreTools.Helper.Dats _sortedBy = SortedBy.Default; // Now we want to loop through all of the games and set the correct information + AddRomsFromParent(logger); + + // Now that we have looped through the cloneof tags, we loop through the romof tags + RemoveBiosRomsFromChild(logger); + + // Finally, remove the romof and cloneof tags so it's not picked up by the manager + RemoveTagsFromChild(logger); + } + + /// + /// Use cloneof and romof tags to create split sets and remove the tags + /// + /// True if roms should be deduped, false otherwise + /// Logger object for file and console output + /// True if the number of hashes counted is to be output (default), false otherwise + public void CreateSplitSets(bool mergeroms, Logger logger, bool output = true) + { + logger.User("Creating split sets from the DAT"); + + // For sake of ease, the first thing we want to do is sort by game + BucketByGame(mergeroms, true, logger, output); + _sortedBy = SortedBy.Default; + + // Now we want to loop through all of the games and set the correct information + RemoveRomsFromChild(logger); + + // Now that we have looped through the cloneof tags, we loop through the romof tags + RemoveBiosRomsFromChild(logger); + + // Finally, remove the romof and cloneof tags so it's not picked up by the manager + RemoveTagsFromChild(logger); + } + + #endregion + + #region Helper Methods + + /// + /// Use device_ref tags to add roms to the children + /// + /// Logger object for file and console output + private void AddRomsFromDevices(Logger logger) + { + List games = Keys.ToList(); + foreach (string game in games) + { + // If the game has no devices, we continue + if (this[game][0].Machine.Devices.Count == 0) + { + continue; + } + + // Determine if the game has any devices or not + List devices = this[game][0].Machine.Devices; + foreach (string device in devices) + { + // If the device doesn't exist then we continue + if (this[device].Count == 0) + { + continue; + } + + // Otherwise, copy the items from the device to the current game + Machine musheen = this[game][0].Machine; + List devItems = this[device]; + foreach (DatItem item in devItems) + { + // Figure out the type of the item and add it accordingly + switch (item.Type) + { + case ItemType.Archive: + Archive archive = ((Archive)item).Clone() as Archive; + archive.Machine = musheen; + if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) + { + this[game].Add(archive); + } + + break; + case ItemType.BiosSet: + BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; + biosSet.Machine = musheen; + if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) + { + this[game].Add(biosSet); + } + + break; + case ItemType.Disk: + Disk disk = ((Disk)item).Clone() as Disk; + disk.Machine = musheen; + if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) + { + this[game].Add(disk); + } + + break; + case ItemType.Release: + Release release = ((Release)item).Clone() as Release; + release.Machine = musheen; + if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) + { + this[game].Add(release); + } + + break; + case ItemType.Rom: + Rom rom = ((Rom)item).Clone() as Rom; + rom.Machine = musheen; + if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) + { + this[game].Add(rom); + } + + break; + case ItemType.Sample: + Sample sample = ((Sample)item).Clone() as Sample; + sample.Machine = musheen; + if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) + { + this[game].Add(sample); + } + + break; + } + } + } + } + } + + /// + /// Use cloneof tags to add roms to the children, setting the new romof tag in the process + /// + /// Logger object for file and console output + private void AddRomsFromParent(Logger logger) + { List games = Keys.ToList(); foreach (string game in games) { @@ -770,9 +673,57 @@ namespace SabreTools.Helper.Dats item.Machine.RomOf = romof; } } + } - // Now that we have looped through the cloneof tags, we loop through the romof tags - games = Keys.ToList(); + /// + /// Use cloneof tags to add roms to the parents, removing the child sets in the process + /// + /// + private void AddRomsToParent(Logger logger) + { + List games = Keys.ToList(); + foreach (string game in games) + { + // Determine if the game has a parent or not + string parent = null; + if (!String.IsNullOrEmpty(this[game][0].Machine.CloneOf)) + { + parent = this[game][0].Machine.CloneOf; + } + + // If there is no parent, then we continue + if (String.IsNullOrEmpty(parent)) + { + continue; + } + + // Otherwise, move the items from the current game to a subfolder of the parent game + Machine parentMachine = this[parent].Count == 0 ? new Machine { Name = parent, Description = parent } : this[parent][0].Machine; + List items = this[game]; + foreach (DatItem item in items) + { + if (!this[parent].Contains(item)) + { + item.Name = item.Machine.Name + "\\" + item.Name; + item.Machine = parentMachine; + + this[parent].Add(item); + } + } + + // Then, remove the old game so it's not picked up by the writer + Remove(game); + } + } + + /// + /// Use romof tags to remove roms from the children + /// + /// Logger object for file and console output + private void RemoveBiosRomsFromChild(Logger logger) + { + // Loop through the romof tags + List games = Keys.ToList(); foreach (string game in games) { // If the game has no items in it, we want to continue @@ -859,35 +810,14 @@ namespace SabreTools.Helper.Dats } } } - - // Finally, remove the romof and cloneof tags so it's not picked up by the manager - games = Keys.ToList(); - foreach (string game in games) - { - List items = this[game]; - foreach (DatItem item in items) - { - item.Machine.CloneOf = null; - item.Machine.RomOf = null; - } - } } /// - /// Use cloneof and romof tags to create split sets and remove the tags + /// Use cloneof tags to remove roms from the children /// - /// True if roms should be deduped, false otherwise /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - public void CreateSplitSets(bool mergeroms, Logger logger, bool output = true) + private void RemoveRomsFromChild(Logger logger) { - logger.User("Creating split sets from the DAT"); - - // For sake of ease, the first thing we want to do is sort by game - BucketByGame(mergeroms, true, logger, output); - _sortedBy = SortedBy.Default; - - // Now we want to loop through all of the games and set the correct information List games = Keys.ToList(); foreach (string game in games) { @@ -983,98 +913,15 @@ namespace SabreTools.Helper.Dats item.Machine.RomOf = romof; } } + } - // Now that we have looped through the cloneof tags, we loop through the romof tags - games = Keys.ToList(); - foreach (string game in games) - { - // If the game has no items in it, we want to continue - if (this[game].Count == 0) - { - continue; - } - - // Determine if the game has a parent or not - string parent = null; - if (!String.IsNullOrEmpty(this[game][0].Machine.RomOf)) - { - parent = this[game][0].Machine.RomOf; - } - - // If the parent doesnt exist, we want to continue - if (String.IsNullOrEmpty(parent)) - { - continue; - } - - // If the parent doesn't have any items, we want to continue - if (this[parent].Count == 0) - { - continue; - } - - // If the parent exists and has items, we remove the items that are in the parent from the current game - Machine currentMachine = this[game][0].Machine; - List parentItems = this[parent]; - foreach (DatItem item in parentItems) - { - // Figure out the type of the item and add it accordingly - switch (item.Type) - { - case ItemType.Archive: - Archive archive = ((Archive)item).Clone() as Archive; - if (this[game].Contains(archive)) - { - this[game].Remove(archive); - } - - break; - case ItemType.BiosSet: - BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; - if (this[game].Contains(biosSet)) - { - this[game].Remove(biosSet); - } - - break; - case ItemType.Disk: - Disk disk = ((Disk)item).Clone() as Disk; - if (this[game].Contains(disk)) - { - this[game].Remove(disk); - } - - break; - case ItemType.Release: - Release release = ((Release)item).Clone() as Release; - if (this[game].Contains(release)) - { - this[game].Remove(release); - } - - break; - case ItemType.Rom: - Rom rom = ((Rom)item).Clone() as Rom; - if (this[game].Contains(rom)) - { - this[game].Remove(rom); - } - - break; - case ItemType.Sample: - Sample sample = ((Sample)item).Clone() as Sample; - if (this[game].Contains(sample)) - { - this[game].Remove(sample); - } - - break; - } - } - } - - // Finally, remove the romof and cloneof tags so it's not picked up by the manager - games = Keys.ToList(); + /// + /// Remove all romof and cloneof tags from all games + /// + /// + private void RemoveTagsFromChild(Logger logger) + { + List games = Keys.ToList(); foreach (string game in games) { List items = this[game];