diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index 9ed4e3ea..6080cebd 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -1170,6 +1170,175 @@ namespace SabreTools.DatFiles #endregion + #region Splitting + + /// + /// Remove all BIOS and device sets + /// + public void RemoveBiosAndDeviceSets() + { + List games = [.. SortedKeys]; + foreach (string game in games) + { + var items = GetDatItemsForBucket(game); + if (items == null || items.Length == 0) + continue; + + var machine = GetMachineForItem(items[0].Item1); + if (machine.Item2 == null) + continue; + + if ((machine.Item2.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true) + || (machine.Item2.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true)) + { + foreach (var item in items) + { + RemoveItem(item.Item1); + } + } + } + } + + /// + /// Use romof tags to remove bios roms from children + /// + /// True if only child Bios sets are touched, false for non-bios sets + public void RemoveBiosRomsFromChild(bool bios) + { + // Loop through the romof tags + List games = [.. SortedKeys]; + foreach (string game in games) + { + // If the game has no items in it, we want to continue + var items = GetDatItemsForBucket(game); + if (items == null || items.Length == 0) + continue; + + // Get the machine for the item + var machine = GetMachineForItem(items[0].Item1); + if (machine.Item2 == null) + continue; + + // If the game (is/is not) a bios, we want to continue + if (bios ^ (machine.Item2.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)) + continue; + + // Determine if the game has a parent or not + string? parent = null; + if (!string.IsNullOrEmpty(machine.Item2.GetStringFieldValue(Models.Metadata.Machine.RomOfKey))) + parent = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); + + // 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 + var parentItems = GetDatItemsForBucket(parent!); + if (parentItems == null || parentItems.Length == 0) + continue; + + // If the parent exists and has items, we remove the items that are in the parent from the current game + foreach ((long, DatItem) item in parentItems) + { + var matchedIndices = items.Where(i => i.Item2 == item.Item2).Select(i => i.Item1); + foreach (long index in matchedIndices) + { + RemoveItem(index); + } + } + } + } + + /// + /// Use cloneof tags to remove roms from the children + /// + public void RemoveRomsFromChild() + { + List games = [.. SortedKeys]; + foreach (string game in games) + { + var items = GetDatItemsForBucket(game); + if (items == null) + continue; + + // If the game has no items in it, we want to continue + if (items.Length == 0) + continue; + + // Get the machine for the first item + var machine = GetMachineForItem(items[0].Item1); + if (machine.Item2 == null) + continue; + + // Determine if the game has a parent or not + string? parent = null; + if (!string.IsNullOrEmpty(machine.Item2.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey))) + parent = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); + + // 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 + var parentItems = GetDatItemsForBucket(parent!); + if (parentItems == null || parentItems.Length == 0) + continue; + + // If the parent exists and has items, we remove the parent items from the current game + foreach ((long, DatItem) item in parentItems) + { + var matchedIndices = items.Where(i => i.Item2 == item.Item2).Select(i => i.Item1); + foreach (long index in matchedIndices) + { + RemoveItem(index); + } + } + + // Now we want to get the parent romof tag and put it in each of the remaining items + items = GetDatItemsForBucket(game); + machine = GetMachineForItem(GetDatItemsForBucket(parent!)![0].Item1); + if (machine.Item2 == null) + continue; + + string? romof = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); + foreach ((long, DatItem) item in items!) + { + machine = GetMachineForItem(item.Item1); + if (machine.Item2 == null) + continue; + + machine.Item2.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); + } + } + } + + /// + /// Remove all romof and cloneof tags from all games + /// + public void RemoveTagsFromChild() + { + List games = [.. SortedKeys]; + foreach (string game in games) + { + var items = GetDatItemsForBucket(game); + if (items == null) + continue; + + foreach ((long, DatItem) item in items) + { + var machine = GetMachineForItem(item.Item1); + if (machine.Item2 == null) + continue; + + machine.Item2.SetFieldValue(Models.Metadata.Machine.CloneOfKey, null); + machine.Item2.SetFieldValue(Models.Metadata.Machine.RomOfKey, null); + machine.Item2.SetFieldValue(Models.Metadata.Machine.SampleOfKey, null); + } + } + } + + #endregion + #region Statistics /// diff --git a/SabreTools.Filtering/Splitter.cs b/SabreTools.Filtering/Splitter.cs index 7b6b17b0..4d37299c 100644 --- a/SabreTools.Filtering/Splitter.cs +++ b/SabreTools.Filtering/Splitter.cs @@ -113,7 +113,7 @@ namespace SabreTools.Filtering // Then, remove the romof and cloneof tags so it's not picked up by the manager RemoveTagsFromChild(datFile); - RemoveTagsFromChildDB(datFile); + datFile.ItemsDB.RemoveTagsFromChild(); } /// @@ -132,13 +132,13 @@ namespace SabreTools.Filtering // Now that we have looped through the cloneof tags, we loop through the romof tags RemoveBiosRomsFromChild(datFile, false); - RemoveBiosRomsFromChildDB(datFile, false); + datFile.ItemsDB.RemoveBiosRomsFromChild(false); RemoveBiosRomsFromChild(datFile, true); - RemoveBiosRomsFromChildDB(datFile, true); + datFile.ItemsDB.RemoveBiosRomsFromChild(true); // Finally, remove the romof and cloneof tags so it's not picked up by the manager RemoveTagsFromChild(datFile); - RemoveTagsFromChildDB(datFile); + datFile.ItemsDB.RemoveTagsFromChild(); } /// @@ -162,7 +162,7 @@ namespace SabreTools.Filtering // Then, remove the romof and cloneof tags so it's not picked up by the manager RemoveTagsFromChild(datFile); - RemoveTagsFromChildDB(datFile); + datFile.ItemsDB.RemoveTagsFromChild(); } /// @@ -181,13 +181,13 @@ namespace SabreTools.Filtering // Now that we have looped through the cloneof tags, we loop through the romof tags RemoveBiosRomsFromChild(datFile, false); - RemoveBiosRomsFromChildDB(datFile, false); + datFile.ItemsDB.RemoveBiosRomsFromChild(false); RemoveBiosRomsFromChild(datFile, true); - RemoveBiosRomsFromChildDB(datFile, true); + datFile.ItemsDB.RemoveBiosRomsFromChild(true); // Finally, remove the romof and cloneof tags so it's not picked up by the manager RemoveTagsFromChild(datFile); - RemoveTagsFromChildDB(datFile); + datFile.ItemsDB.RemoveTagsFromChild(); } /// @@ -206,13 +206,13 @@ namespace SabreTools.Filtering // Now that we have looped through the cloneof tags, we loop through the romof tags RemoveBiosRomsFromChild(datFile, false); - RemoveBiosRomsFromChildDB(datFile, false); + datFile.ItemsDB.RemoveBiosRomsFromChild(false); RemoveBiosRomsFromChild(datFile, true); - RemoveBiosRomsFromChildDB(datFile, true); + datFile.ItemsDB.RemoveBiosRomsFromChild(true); // Finally, remove the romof and cloneof tags so it's not picked up by the manager RemoveTagsFromChild(datFile); - RemoveTagsFromChildDB(datFile); + datFile.ItemsDB.RemoveTagsFromChild(); } /// @@ -228,17 +228,17 @@ namespace SabreTools.Filtering // Now we want to loop through all of the games and set the correct information RemoveRomsFromChild(datFile); - RemoveRomsFromChildDB(datFile); + datFile.ItemsDB.RemoveRomsFromChild(); // Now that we have looped through the cloneof tags, we loop through the romof tags RemoveBiosRomsFromChild(datFile, false); - RemoveBiosRomsFromChildDB(datFile, false); + datFile.ItemsDB.RemoveBiosRomsFromChild(false); RemoveBiosRomsFromChild(datFile, true); - RemoveBiosRomsFromChildDB(datFile, true); + datFile.ItemsDB.RemoveBiosRomsFromChild(true); // Finally, remove the romof and cloneof tags so it's not picked up by the manager RemoveTagsFromChild(datFile); - RemoveTagsFromChildDB(datFile); + datFile.ItemsDB.RemoveTagsFromChild(); } /// @@ -637,31 +637,6 @@ namespace SabreTools.Filtering } } - /// - /// Remove all BIOS and device sets - /// - /// Current DatFile object to run operations on - internal static void RemoveBiosAndDeviceSetsDB(DatFile datFile) - { - List games = [.. datFile.ItemsDB.SortedKeys]; - foreach (string game in games) - { - var items = datFile.ItemsDB.GetDatItemsForBucket(game); - if (items == null) - continue; - - if (items.Length > 0 - && ((items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true) - || (items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))) - { - foreach (var item in items) - { - datFile.ItemsDB.RemoveItem(item.Item1); - } - } - } - } - /// /// Use romof tags to remove bios roms from children /// @@ -711,52 +686,6 @@ namespace SabreTools.Filtering } } - /// - /// Use romof tags to remove bios roms from children - /// - /// Current DatFile object to run operations on - /// True if only child Bios sets are touched, false for non-bios sets (default) - internal static void RemoveBiosRomsFromChildDB(DatFile datFile, bool bios = false) - { - // Loop through the romof tags - List games = [.. datFile.ItemsDB.SortedKeys]; - foreach (string game in games) - { - // If the game has no items in it, we want to continue - var items = datFile.ItemsDB.GetDatItemsForBucket(game); - if (items == null || items.Length == 0) - continue; - - // If the game (is/is not) a bios, we want to continue - if (bios ^ (items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)) - continue; - - // Determine if the game has a parent or not - string? parent = null; - if (!string.IsNullOrEmpty(items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey))) - parent = items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - - // 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 - var parentItems = datFile.ItemsDB.GetDatItemsForBucket(parent!); - if (parentItems == null || parentItems.Length == 0) - continue; - - // If the parent exists and has items, we remove the items that are in the parent from the current game - foreach ((long, DatItem) item in parentItems) - { - var matchedIndices = items.Where(i => i.Item2 == item.Item2).Select(i => i.Item1); - foreach (long index in matchedIndices) - { - datFile.ItemsDB.RemoveItem(index); - } - } - } - } - /// /// Use cloneof tags to remove roms from the children /// @@ -808,57 +737,6 @@ namespace SabreTools.Filtering } } - /// - /// Use cloneof tags to remove roms from the children - /// - /// Current DatFile object to run operations on - internal static void RemoveRomsFromChildDB(DatFile datFile) - { - List games = [.. datFile.ItemsDB.SortedKeys]; - foreach (string game in games) - { - var items = datFile.ItemsDB.GetDatItemsForBucket(game); - if (items == null) - continue; - - // If the game has no items in it, we want to continue - if (items.Length == 0) - continue; - - // Determine if the game has a parent or not - string? parent = null; - if (!string.IsNullOrEmpty(items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey))) - parent = items[0].Item2.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey); - - // 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 - var parentItems = datFile.ItemsDB.GetDatItemsForBucket(parent!); - if (parentItems == null || parentItems.Length == 0) - continue; - - // If the parent exists and has items, we remove the parent items from the current game - foreach ((long, DatItem) item in parentItems) - { - var matchedIndices = items.Where(i => i.Item2 == item.Item2).Select(i => i.Item1); - foreach (long index in matchedIndices) - { - datFile.ItemsDB.RemoveItem(index); - } - } - - // Now we want to get the parent romof tag and put it in each of the remaining items - items = datFile.ItemsDB.GetDatItemsForBucket(game); - string? romof = datFile.ItemsDB.GetDatItemsForBucket(parent)![0].Item2.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey); - foreach ((long, DatItem) item in items!) - { - item.Item2.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.RomOfKey, romof); - } - } - } - /// /// Remove all romof and cloneof tags from all games /// @@ -881,28 +759,6 @@ namespace SabreTools.Filtering } } - /// - /// Remove all romof and cloneof tags from all games - /// - /// Current DatFile object to run operations on - internal static void RemoveTagsFromChildDB(DatFile datFile) - { - List games = [.. datFile.ItemsDB.SortedKeys]; - foreach (string game in games) - { - var items = datFile.ItemsDB.GetDatItemsForBucket(game); - if (items == null) - continue; - - foreach ((long, DatItem) item in items) - { - item.Item2.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.CloneOfKey, null); - item.Item2.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.RomOfKey, null); - item.Item2.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.SampleOfKey, null); - } - } - } - #endregion } }