Fix filtering, merging, blanks, and bucketing

This commit is contained in:
Matt Nadareski
2020-08-31 15:33:05 -07:00
parent a70dc3bad6
commit 58039f5093
9 changed files with 137 additions and 114 deletions

View File

@@ -1383,87 +1383,112 @@ namespace SabreTools.Library.DatFiles
if (Items[game] == null || Items[game].Count == 0) if (Items[game] == null || Items[game].Count == 0)
continue; continue;
// If the game (is/is not) a bios, we want to continue // If the game (is/is not) a device, we want to continue
if (dev ^ (Items[game][0].Machine.MachineType.HasFlag(MachineType.Device))) if (dev ^ (Items[game][0].Machine.MachineType.HasFlag(MachineType.Device)))
continue; continue;
// If the game has no devices, we continue // If the game has no devices, mark it
bool devices = true;
if (Items[game][0].Machine.DeviceReferences == null if (Items[game][0].Machine.DeviceReferences == null
|| Items[game][0].Machine.DeviceReferences.Count == 0 || Items[game][0].Machine.DeviceReferences.Count == 0)
|| (slotoptions && Items[game][0].Machine.Slots == null)
|| (slotoptions && Items[game][0].Machine.Slots.Count == 0))
{ {
continue; devices = false;
} }
// Determine if the game has any devices or not // If we're checking devices
List<ListXmlDeviceReference> deviceReferences = Items[game][0].Machine.DeviceReferences; if (devices)
List<ListXmlDeviceReference> newdevs = new List<ListXmlDeviceReference>();
foreach (ListXmlDeviceReference deviceReference in deviceReferences)
{ {
// If the device doesn't exist then we continue // Determine if the game has any devices or not
if (Items[deviceReference.Name].Count == 0) List<string> deviceReferences = Items[game][0].Machine.DeviceReferences.Select(d => d.Name).ToList();
continue; List<string> newdevs = new List<string>();
foreach (string deviceReference in deviceReferences)
// Otherwise, copy the items from the device to the current game
DatItem copyFrom = Items[game][0];
List<DatItem> devItems = Items[deviceReference.Name];
foreach (DatItem item in devItems)
{ {
DatItem datItem = (DatItem)item.Clone(); // If the device doesn't exist then we continue
newdevs.AddRange(datItem.Machine.DeviceReferences ?? new List<ListXmlDeviceReference>()); if (Items[deviceReference] == null || Items[deviceReference].Count == 0)
datItem.CopyMachineInformation(copyFrom); continue;
if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
// Otherwise, copy the items from the device to the current game
DatItem copyFrom = Items[game][0];
List<DatItem> devItems = Items[deviceReference];
foreach (DatItem item in devItems)
{ {
foundnew = true; DatItem datItem = (DatItem)item.Clone();
Items.Add(game, datItem); newdevs.AddRange((datItem.Machine.DeviceReferences ?? new List<ListXmlDeviceReference>()).Select(d => d.Name).ToList());
} datItem.CopyMachineInformation(copyFrom);
} if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
}
// Now that every device is accounted for, add the new list of devices, if they don't already exist
foreach (ListXmlDeviceReference device in newdevs)
{
if (!Items[game][0].Machine.DeviceReferences.Select(d => d.Name).Contains(device.Name))
Items[game][0].Machine.DeviceReferences.Add(new ListXmlDeviceReference() { Name = device.Name });
}
// If we're checking slotoptions too
if (slotoptions)
{
// Determine if the game has any slots or not
List<ListXmlSlot> slots = Items[game][0].Machine.Slots;
List<ListXmlSlot> newSlots = new List<ListXmlSlot>();
foreach (ListXmlSlot slot in slots)
{
foreach (ListXmlSlotOption slotOption in slot.SlotOptions)
{
// If the slotoption doesn't exist then we continue
if (Items[slotOption.Name].Count == 0)
continue;
// Otherwise, copy the items from the slotoption to the current game
DatItem copyFrom = Items[game][0];
List<DatItem> slotItems = Items[slotOption.Name];
foreach (DatItem item in slotItems)
{ {
DatItem datItem = (DatItem)item.Clone(); foundnew = true;
newSlots.AddRange(datItem.Machine.Slots ?? new List<ListXmlSlot>()); Items.Add(game, datItem);
datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
{
foundnew = true;
Items.Add(game, datItem);
}
} }
} }
} }
// Now that every slotoption is accounted for, add the new list of slots, if they don't already exist // Now that every device is accounted for, add the new list of devices, if they don't already exist
foreach (ListXmlSlot slot in newSlots) foreach (string device in newdevs)
{ {
Items[game][0].Machine.Slots.Add(slot); if (!deviceReferences.Contains(device))
Items[game][0].Machine.DeviceReferences.Add(new ListXmlDeviceReference() { Name = device });
}
}
// If we're checking slotoptions
if (slotoptions)
{
// If the game has no slot options, we continue
if (Items[game][0].Machine.Slots == null
|| Items[game][0].Machine.Slots
.SelectMany(s => s.SlotOptions ?? new List<ListXmlSlotOption>()).Count() == 0)
{
continue;
}
// Determine if the game has any slot options or not
List<string> slotOptions = Items[game][0].Machine.Slots
.Where(s => s.SlotOptions != null && s.SlotOptions.Count != 0)
.SelectMany(s => s.SlotOptions)
.Select(o => o.DeviceName)
.Distinct()
.ToList();
List<string> newSlotOptions = new List<string>();
foreach (string slotOption in slotOptions)
{
// If the slot option doesn't exist then we continue
if (Items[slotOption] == null || Items[slotOption].Count == 0)
continue;
// Otherwise, copy the items from the slot option to the current game
DatItem copyFrom = Items[game][0];
List<DatItem> slotOptionItems = Items[slotOption];
foreach (DatItem item in slotOptionItems)
{
DatItem datItem = (DatItem)item.Clone();
List<string> machineSlotOptions =
(datItem.Machine.Slots ?? new List<ListXmlSlot>())
.Where(s => s.SlotOptions != null && s.SlotOptions.Count != 0)
.SelectMany(s => s.SlotOptions)
.Select(o => o.DeviceName)
.Distinct()
.ToList();
newSlotOptions.AddRange(machineSlotOptions);
datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
{
foundnew = true;
Items.Add(game, datItem);
}
}
}
// Ensure we don't have unnecessary duplicates
newSlotOptions = newSlotOptions.Distinct().ToList();
// Now that every slot option is accounted for, add the new list of options, if they don't already exist
foreach (string slotOption in newSlotOptions)
{
if (!slotOptions.Contains(slotOption))
Items[game][0].Machine.Slots.Add(new ListXmlSlot() { SlotOptions = new List<ListXmlSlotOption> { new ListXmlSlotOption { DeviceName = slotOption } } });
} }
} }
} }

View File

@@ -750,9 +750,6 @@ namespace SabreTools.Library.DatFiles
DatItem.Sort(ref sortedlist, false); DatItem.Sort(ref sortedlist, false);
}); });
} }
// Now clean up all empty keys
ClearEmpty();
} }
/// <summary> /// <summary>

View File

@@ -116,7 +116,7 @@ namespace SabreTools.Library.DatFiles
reader.MoveToContent(); reader.MoveToContent();
// Create a new machine // Create a new machine
MachineType machineType = MachineType.NULL; MachineType machineType = 0x0;
if (reader.GetAttribute("isbios").AsYesNo() == true) if (reader.GetAttribute("isbios").AsYesNo() == true)
machineType |= MachineType.Bios; machineType |= MachineType.Bios;
@@ -133,7 +133,7 @@ namespace SabreTools.Library.DatFiles
CloneOf = reader.GetAttribute("cloneof"), CloneOf = reader.GetAttribute("cloneof"),
RomOf = reader.GetAttribute("romof"), RomOf = reader.GetAttribute("romof"),
SampleOf = reader.GetAttribute("sampleof"), SampleOf = reader.GetAttribute("sampleof"),
MachineType = (machineType == MachineType.NULL ? MachineType.NULL : machineType), MachineType = (machineType == 0x0 ? MachineType.NULL : machineType),
SourceFile = reader.GetAttribute("sourcefile"), SourceFile = reader.GetAttribute("sourcefile"),
Runnable = reader.GetAttribute("runnable").AsRunnable(), Runnable = reader.GetAttribute("runnable").AsRunnable(),

View File

@@ -289,7 +289,7 @@ namespace SabreTools.Library.DatFiles
bool containsItems = false; bool containsItems = false;
// Create a new machine // Create a new machine
MachineType machineType = MachineType.NULL; MachineType machineType = 0x0;
if (reader.GetAttribute("isbios").AsYesNo() == true) if (reader.GetAttribute("isbios").AsYesNo() == true)
machineType |= MachineType.Bios; machineType |= MachineType.Bios;
@@ -313,7 +313,7 @@ namespace SabreTools.Library.DatFiles
RomOf = reader.GetAttribute("romof"), RomOf = reader.GetAttribute("romof"),
SampleOf = reader.GetAttribute("sampleof"), SampleOf = reader.GetAttribute("sampleof"),
MachineType = (machineType == MachineType.NULL ? MachineType.NULL : machineType), MachineType = (machineType == 0x0 ? MachineType.NULL : machineType),
}; };
if (Header.Type == "SuperDAT" && !keep) if (Header.Type == "SuperDAT" && !keep)

View File

@@ -115,7 +115,7 @@ namespace SabreTools.Library.DatFiles
bool containsItems = false; bool containsItems = false;
// Create a new machine // Create a new machine
MachineType machineType = MachineType.NULL; MachineType machineType = 0x0;
if (reader.GetAttribute("isbios").AsYesNo() == true) if (reader.GetAttribute("isbios").AsYesNo() == true)
machineType |= MachineType.Bios; machineType |= MachineType.Bios;
@@ -132,7 +132,7 @@ namespace SabreTools.Library.DatFiles
Supported = reader.GetAttribute("supported").AsSupported(), Supported = reader.GetAttribute("supported").AsSupported(),
CloneOf = reader.GetAttribute("cloneof"), CloneOf = reader.GetAttribute("cloneof"),
MachineType = (machineType != MachineType.NULL ? machineType : MachineType.NULL), MachineType = (machineType == 0x0 ? MachineType.NULL : machineType),
}; };
while (!reader.EOF) while (!reader.EOF)

View File

@@ -1226,24 +1226,10 @@ namespace SabreTools.Library.DatItems
{ {
if (x.Machine.Name == y.Machine.Name) if (x.Machine.Name == y.Machine.Name)
{ {
// Special case for comparing a Disk, Media, or Rom to another item type if (Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(y.Name)))
if ((x.ItemType == ItemType.Disk || x.ItemType == ItemType.Media || x.ItemType == ItemType.Rom) return nc.Compare(Path.GetFileName(Sanitizer.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Sanitizer.RemovePathUnsafeCharacters(y.Name)));
^ (y.ItemType == ItemType.Disk || y.ItemType == ItemType.Media || x.ItemType == ItemType.Rom))
{
if (x.ItemType == ItemType.Disk || x.ItemType == ItemType.Media || x.ItemType == ItemType.Rom)
return -1;
else
return 1;
}
// Otherwise, we compare names naturally return nc.Compare(Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(y.Name)));
else
{
if (Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(y.Name)))
return nc.Compare(Path.GetFileName(Sanitizer.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Sanitizer.RemovePathUnsafeCharacters(y.Name)));
return nc.Compare(Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Sanitizer.RemovePathUnsafeCharacters(y.Name)));
}
} }
return nc.Compare(x.Machine.Name, y.Machine.Name); return nc.Compare(x.Machine.Name, y.Machine.Name);

View File

@@ -439,7 +439,7 @@ namespace SabreTools.Library.DatItems
/// <summary> /// <summary>
/// This is a fake flag that is used for filter only /// This is a fake flag that is used for filter only
/// </summary> /// </summary>
NULL = 0x00, NULL = 0,
None = 1 << 0, None = 1 << 0,
Good = 1 << 1, Good = 1 << 1,
@@ -471,10 +471,14 @@ namespace SabreTools.Library.DatItems
[Flags] [Flags]
public enum OpenMSXSubType public enum OpenMSXSubType
{ {
/// <summary>
/// This is a fake flag that is used for filter only
/// </summary>
NULL = 0, NULL = 0,
Rom = 1,
MegaRom = 2, Rom = 1 << 0,
SCCPlusCart = 3, MegaRom = 1 << 1,
SCCPlusCart = 1 << 2,
} }
/// <summary> /// <summary>
@@ -483,10 +487,14 @@ namespace SabreTools.Library.DatItems
[Flags] [Flags]
public enum MachineType public enum MachineType
{ {
NULL = 0x00, /// <summary>
Bios = 1 << 0, /// This is a fake flag that is used for filter only
Device = 1 << 1, /// </summary>
Mechanical = 1 << 2, NULL = 1 << 0,
Bios = 1 << 1,
Device = 1 << 2,
Mechanical = 1 << 3,
} }
/// <summary> /// <summary>
@@ -495,10 +503,14 @@ namespace SabreTools.Library.DatItems
[Flags] [Flags]
public enum Runnable public enum Runnable
{ {
NULL, /// <summary>
No, /// This is a fake flag that is used for filter only
Partial, /// </summary>
Yes, NULL = 0,
No = 1 << 0,
Partial = 1 << 1,
Yes = 1 << 2,
} }
/// <summary> /// <summary>
@@ -507,9 +519,13 @@ namespace SabreTools.Library.DatItems
[Flags] [Flags]
public enum Supported public enum Supported
{ {
NULL, /// <summary>
No, /// This is a fake flag that is used for filter only
Partial, /// </summary>
Yes, NULL = 0,
No = 1 << 0,
Partial = 1 << 1,
Yes = 1 << 2,
} }
} }

View File

@@ -85,7 +85,7 @@ namespace SabreTools.Library.DatItems
/// </summary> /// </summary>
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)] [JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonConverter(typeof(StringEnumConverter))] [JsonConverter(typeof(StringEnumConverter))]
public MachineType MachineType { get; set; } = MachineType.NULL; public MachineType MachineType { get; set; } = 0x0;
#endregion #endregion
@@ -740,9 +740,9 @@ namespace SabreTools.Library.DatItems
return false; return false;
// Machine_Type // Machine_Type
if (filter.Machine_Type.MatchesPositive(MachineType.NULL, MachineType) == false) if (filter.Machine_Type.MatchesPositive(0x0, MachineType) == false)
return false; return false;
if (filter.Machine_Type.MatchesNegative(MachineType.NULL, MachineType) == true) if (filter.Machine_Type.MatchesNegative(0x0, MachineType) == true)
return false; return false;
#endregion #endregion
@@ -1501,7 +1501,7 @@ namespace SabreTools.Library.DatItems
SampleOf = null; SampleOf = null;
if (fields.Contains(Field.Machine_Type)) if (fields.Contains(Field.Machine_Type))
MachineType = MachineType.NULL; MachineType = 0x0;
#endregion #endregion

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.Library.Data; using SabreTools.Library.Data;
using SabreTools.Library.DatFiles;
using SabreTools.Library.DatItems; using SabreTools.Library.DatItems;
using SabreTools.Library.Tools; using SabreTools.Library.Tools;
@@ -31,7 +30,7 @@ namespace SabreTools.Library.Filtering
public FilterItem<string> Machine_RomOf { get; private set; } = new FilterItem<string>(); public FilterItem<string> Machine_RomOf { get; private set; } = new FilterItem<string>();
public FilterItem<string> Machine_CloneOf { get; private set; } = new FilterItem<string>(); public FilterItem<string> Machine_CloneOf { get; private set; } = new FilterItem<string>();
public FilterItem<string> Machine_SampleOf { get; private set; } = new FilterItem<string>(); public FilterItem<string> Machine_SampleOf { get; private set; } = new FilterItem<string>();
public FilterItem<MachineType> Machine_Type { get; private set; } = new FilterItem<MachineType>() { Positive = MachineType.NULL, Negative = MachineType.NULL }; public FilterItem<MachineType> Machine_Type { get; private set; } = new FilterItem<MachineType>() { Positive = 0x0, Negative = 0x0 };
#endregion #endregion