Ensure key ordering for merge/split

This commit is contained in:
Matt Nadareski
2020-07-23 16:21:19 -07:00
parent 151b0b4a79
commit 6a0efdc3a2

View File

@@ -5,7 +5,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using NaturalSort;
using SabreTools.Library.Data; using SabreTools.Library.Data;
using SabreTools.Library.DatItems; using SabreTools.Library.DatItems;
using SabreTools.Library.Tools; using SabreTools.Library.Tools;
@@ -1525,6 +1525,8 @@ namespace SabreTools.Library.DatFiles
private void AddRomsFromBios(DatFile datFile) private void AddRomsFromBios(DatFile datFile)
{ {
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
// If the game has no items in it, we want to continue // If the game has no items in it, we want to continue
@@ -1567,6 +1569,8 @@ namespace SabreTools.Library.DatFiles
{ {
bool foundnew = false; bool foundnew = false;
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
// If the game doesn't have items, we continue // If the game doesn't have items, we continue
@@ -1665,6 +1669,8 @@ namespace SabreTools.Library.DatFiles
private void AddRomsFromParent(DatFile datFile) private void AddRomsFromParent(DatFile datFile)
{ {
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
// If the game has no items in it, we want to continue // If the game has no items in it, we want to continue
@@ -1715,6 +1721,8 @@ namespace SabreTools.Library.DatFiles
private void AddRomsFromChildren(DatFile datFile) private void AddRomsFromChildren(DatFile datFile)
{ {
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
// If the game has no items in it, we want to continue // If the game has no items in it, we want to continue
@@ -1735,34 +1743,84 @@ namespace SabreTools.Library.DatFiles
List<DatItem> items = datFile[game]; List<DatItem> items = datFile[game];
foreach (DatItem item in items) foreach (DatItem item in items)
{ {
// If the disk doesn't have a valid merge tag OR the merged file doesn't exist in the parent, then add it // Special disk handling
if (item.ItemType == ItemType.Disk && (((Disk)item).MergeTag == null || !datFile[parent].Select(i => i.Name).Contains(((Disk)item).MergeTag))) if (item.ItemType == ItemType.Disk)
{ {
// Rename the child so it's in a subfolder Disk disk = item as Disk;
item.Name = $"{item.MachineName}\\{item.Name}";
// Update the machine to be the new parent // If the merge tag exists and the parent already contains it, skip
item.CopyMachineInformation(copyFrom); if (disk.MergeTag != null && datFile[parent].Select(i => i.Name).Contains(disk.MergeTag))
{
continue;
}
// Add the rom to the parent set // If the merge tag exists but the parent doesn't contain it, add
datFile.Add(parent, item); else if (disk.MergeTag != null && !datFile[parent].Select(i => i.Name).Contains(disk.MergeTag))
{
// Rename the child so it's in a subfolder
item.Name = $"{item.MachineName}\\{item.Name}";
// Update the machine to be the new parent
item.CopyMachineInformation(copyFrom);
// Add the rom to the parent set
datFile.Add(parent, item);
}
// If the parent doesn't already contain this item, add it
else if (!datFile[parent].Contains(item))
{
// Rename the child so it's in a subfolder
item.Name = $"{item.MachineName}\\{item.Name}";
// Update the machine to be the new parent
item.CopyMachineInformation(copyFrom);
// Add the rom to the parent set
datFile.Add(parent, item);
}
} }
// If the rom doesn't have a valid merge tag OR the merged file doesn't exist in the parent, then add it // Special rom handling
else if (item.ItemType == ItemType.Rom && (((Rom)item).MergeTag == null || !datFile[parent].Select(i => i.Name).Contains(((Rom)item).MergeTag))) else if (item.ItemType == ItemType.Rom)
{ {
// Rename the child so it's in a subfolder Rom rom = item as Rom;
item.Name = $"{item.MachineName}\\{item.Name}";
// Update the machine to be the new parent // If the merge tag exists and the parent already contains it, skip
item.CopyMachineInformation(copyFrom); if (rom.MergeTag != null && datFile[parent].Select(i => i.Name).Contains(rom.MergeTag))
{
continue;
}
// Add the rom to the parent set // If the merge tag exists but the parent doesn't contain it, add
datFile.Add(parent, item); else if (rom.MergeTag != null && !datFile[parent].Select(i => i.Name).Contains(rom.MergeTag))
{
// Rename the child so it's in a subfolder
item.Name = $"{item.MachineName}\\{item.Name}";
// Update the machine to be the new parent
item.CopyMachineInformation(copyFrom);
// Add the rom to the parent set
datFile.Add(parent, item);
}
// If the parent doesn't already contain this item, add it
else if (!datFile[parent].Contains(item))
{
// Rename the child so it's in a subfolder
item.Name = $"{item.MachineName}\\{item.Name}";
// Update the machine to be the new parent
item.CopyMachineInformation(copyFrom);
// Add the rom to the parent set
datFile.Add(parent, item);
}
} }
// Otherwise, if the parent doesn't already contain the non-disk (or a merge-equivalent), add it // All other that would be missing
else if (item.ItemType != ItemType.Disk && !datFile[parent].Contains(item)) else if (!datFile[parent].Contains(item))
{ {
// Rename the child so it's in a subfolder // Rename the child so it's in a subfolder
item.Name = $"{item.MachineName}\\{item.Name}"; item.Name = $"{item.MachineName}\\{item.Name}";
@@ -1787,6 +1845,8 @@ namespace SabreTools.Library.DatFiles
private void RemoveBiosAndDeviceSets(DatFile datFile) private void RemoveBiosAndDeviceSets(DatFile datFile)
{ {
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
if (datFile[game].Count > 0 if (datFile[game].Count > 0
@@ -1807,6 +1867,8 @@ namespace SabreTools.Library.DatFiles
{ {
// Loop through the romof tags // Loop through the romof tags
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
// If the game has no items in it, we want to continue // If the game has no items in it, we want to continue
@@ -1850,6 +1912,8 @@ namespace SabreTools.Library.DatFiles
private void RemoveRomsFromChild(DatFile datFile) private void RemoveRomsFromChild(DatFile datFile)
{ {
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
// If the game has no items in it, we want to continue // If the game has no items in it, we want to continue
@@ -1897,6 +1961,8 @@ namespace SabreTools.Library.DatFiles
private void RemoveTagsFromChild(DatFile datFile) private void RemoveTagsFromChild(DatFile datFile)
{ {
List<string> games = datFile.Keys; List<string> games = datFile.Keys;
games = games.OrderBy(g => g, NaturalComparer.Default).ToList();
foreach (string game in games) foreach (string game in games)
{ {
List<DatItem> items = datFile[game]; List<DatItem> items = datFile[game];