Reduce unnecessary round-trip conversions

This commit is contained in:
Matt Nadareski
2024-12-06 23:16:09 -05:00
parent d78ff5eb67
commit c8c10659b1
34 changed files with 597 additions and 765 deletions

View File

@@ -22,7 +22,7 @@ namespace SabreTools.Core.Filter
public FilterRunner(string[] filterStrings)
{
var filters = new List<FilterObject>();
List<FilterObject> filters = [];
foreach (string filterString in filterStrings)
{
try

View File

@@ -25,7 +25,7 @@ namespace SabreTools.Core.Tools
return false;
// Otherwise, they need to match exactly
return Enumerable.SequenceEqual(firstHash, secondHash);
return firstHash.SequenceEqual(secondHash);
}
/// <summary>

View File

@@ -537,7 +537,7 @@ namespace SabreTools.DatFiles
var confLocations = ReadItemArray<Models.Metadata.ConfLocation>(item, Models.Metadata.Configuration.ConfLocationKey);
if (confLocations != null)
{
var subLocations = new List<DatItems.Formats.ConfLocation>();
List<DatItems.Formats.ConfLocation> subLocations = [];
foreach (var location in confLocations)
{
var subItem = new DatItems.Formats.ConfLocation(location);
@@ -555,7 +555,7 @@ namespace SabreTools.DatFiles
var confSettings = ReadItemArray<Models.Metadata.ConfSetting>(item, Models.Metadata.Configuration.ConfSettingKey);
if (confSettings != null)
{
var subValues = new List<DatItems.Formats.ConfSetting>();
List<DatItems.Formats.ConfSetting> subValues = [];
foreach (var setting in confSettings)
{
var subItem = new DatItems.Formats.ConfSetting(setting);
@@ -626,7 +626,7 @@ namespace SabreTools.DatFiles
var extensions = ReadItemArray<Models.Metadata.Extension>(item, Models.Metadata.Device.ExtensionKey);
if (extensions != null)
{
var subExtensions = new List<DatItems.Formats.Extension>();
List<DatItems.Formats.Extension> subExtensions = [];
foreach (var extension in extensions)
{
var subItem = new DatItems.Formats.Extension(extension);
@@ -709,7 +709,7 @@ namespace SabreTools.DatFiles
var dipLocations = ReadItemArray<Models.Metadata.DipLocation>(item, Models.Metadata.DipSwitch.DipLocationKey);
if (dipLocations != null)
{
var subLocations = new List<DatItems.Formats.DipLocation>();
List<DatItems.Formats.DipLocation> subLocations = [];
foreach (var location in dipLocations)
{
var subItem = new DatItems.Formats.DipLocation(location);
@@ -727,7 +727,7 @@ namespace SabreTools.DatFiles
var dipValues = ReadItemArray<Models.Metadata.DipValue>(item, Models.Metadata.DipSwitch.DipValueKey);
if (dipValues != null)
{
var subValues = new List<DatItems.Formats.DipValue>();
List<DatItems.Formats.DipValue> subValues = [];
foreach (var value in dipValues)
{
var subItem = new DatItems.Formats.DipValue(value);
@@ -1093,7 +1093,7 @@ namespace SabreTools.DatFiles
var controls = ReadItemArray<Models.Metadata.Control>(item, Models.Metadata.Input.ControlKey);
if (controls != null)
{
var subControls = new List<DatItems.Formats.Control>();
List<DatItems.Formats.Control> subControls = [];
foreach (var control in controls)
{
var subItem = new DatItems.Formats.Control(control);
@@ -1319,7 +1319,7 @@ namespace SabreTools.DatFiles
var dipLocations = ReadItemArray<Models.Metadata.DipLocation>(dipSwitch, Models.Metadata.DipSwitch.DipLocationKey);
if (dipLocations != null)
{
var subLocations = new List<DatItems.Formats.DipLocation>();
List<DatItems.Formats.DipLocation> subLocations = [];
foreach (var location in dipLocations)
{
var subItem = new DatItems.Formats.DipLocation(location);
@@ -1337,7 +1337,7 @@ namespace SabreTools.DatFiles
var dipValues = ReadItemArray<Models.Metadata.DipValue>(dipSwitch, Models.Metadata.DipSwitch.DipValueKey);
if (dipValues != null)
{
var subValues = new List<DatItems.Formats.DipValue>();
List<DatItems.Formats.DipValue> subValues = [];
foreach (var value in dipValues)
{
var subItem = new DatItems.Formats.DipValue(value);
@@ -1420,7 +1420,7 @@ namespace SabreTools.DatFiles
var analogs = ReadItemArray<Models.Metadata.Analog>(item, Models.Metadata.Port.AnalogKey);
if (analogs != null)
{
var subAnalogs = new List<DatItems.Formats.Analog>();
List<DatItems.Formats.Analog> subAnalogs = [];
foreach (var analog in analogs)
{
var subItem = new DatItems.Formats.Analog(analog);
@@ -1636,7 +1636,7 @@ namespace SabreTools.DatFiles
var slotOptions = ReadItemArray<Models.Metadata.SlotOption>(item, Models.Metadata.Slot.SlotOptionKey);
if (slotOptions != null)
{
var subOptions = new List<DatItems.Formats.SlotOption>();
List<DatItems.Formats.SlotOption> subOptions = [];
foreach (var slotOption in slotOptions)
{
var subItem = new DatItems.Formats.SlotOption(slotOption);

View File

@@ -82,9 +82,9 @@ namespace SabreTools.DatFiles
continue;
#endif
for (int j = 0; j < items.Length; j++)
foreach (var item in items.Values)
{
RemoveFields(items[j].Item2, machineFieldNames, itemFieldNames);
RemoveFields(item, machineFieldNames, itemFieldNames);
}
#if NET40_OR_GREATER || NETCOREAPP
});

View File

@@ -100,7 +100,7 @@ namespace SabreTools.DatFiles
private Models.Metadata.Machine[]? ConvertMachines(bool ignoreblanks = false)
{
// Create a machine list to hold all outputs
var machines = new List<Models.Metadata.Machine>();
List<Models.Metadata.Machine> machines = [];
// Loop through the sorted items and create games for them
foreach (string key in Items.SortedKeys)
@@ -374,7 +374,7 @@ namespace SabreTools.DatFiles
{
// Get existing data areas as a list
var dataAreasArr = partItems[partName].Read<Models.Metadata.DataArea[]>(Models.Metadata.Part.DataAreaKey) ?? [];
var dataAreas = new List<Models.Metadata.DataArea>(dataAreasArr);
List<Models.Metadata.DataArea> dataAreas = [.. dataAreasArr];
// Find the existing disk area to append to, otherwise create a new disk area
int dataAreaIndex = dataAreas.FindIndex(da => da.ReadString(Models.Metadata.DataArea.NameKey) == dataAreaName);
@@ -397,7 +397,7 @@ namespace SabreTools.DatFiles
// Get existing roms as a list
var romsArr = aggregateDataArea.Read<Models.Metadata.Rom[]>(Models.Metadata.DataArea.RomKey) ?? [];
var roms = new List<Models.Metadata.Rom>(romsArr);
List<Models.Metadata.Rom> roms = [.. romsArr];
// Add the rom to the data area
roms.Add(romItem);
@@ -431,7 +431,7 @@ namespace SabreTools.DatFiles
{
// Get existing disk areas as a list
var diskAreasArr = partItems[partName].Read<Models.Metadata.DiskArea[]>(Models.Metadata.Part.DiskAreaKey) ?? [];
var diskAreas = new List<Models.Metadata.DiskArea>(diskAreasArr);
List<Models.Metadata.DiskArea> diskAreas = [.. diskAreasArr];
// Find the existing disk area to append to, otherwise create a new disk area
int diskAreaIndex = diskAreas.FindIndex(da => da.ReadString(Models.Metadata.DiskArea.NameKey) == diskAreaName);
@@ -451,7 +451,7 @@ namespace SabreTools.DatFiles
// Get existing disks as a list
var disksArr = aggregateDiskArea.Read<Models.Metadata.Disk[]>(Models.Metadata.DiskArea.DiskKey) ?? [];
var disks = new List<Models.Metadata.Disk>(disksArr);
List<Models.Metadata.Disk> disks = [.. disksArr];
// Add the disk to the data area
disks.Add(diskItem);
@@ -475,7 +475,7 @@ namespace SabreTools.DatFiles
{
// Get existing dipswitches as a list
var dipSwitchesArr = partItems[partName].Read<Models.Metadata.DipSwitch[]>(Models.Metadata.Part.DipSwitchKey) ?? [];
var dipSwitches = new List<Models.Metadata.DipSwitch>(dipSwitchesArr);
List<Models.Metadata.DipSwitch> dipSwitches = [.. dipSwitchesArr];
// Clear any empty fields
ClearEmptyKeys(dipSwitchItem);
@@ -492,7 +492,7 @@ namespace SabreTools.DatFiles
{
// Get existing features as a list
var featuresArr = partItems[partName].Read<Models.Metadata.Feature[]>(Models.Metadata.Part.FeatureKey) ?? [];
var features = new List<Models.Metadata.Feature>(featuresArr);
List<Models.Metadata.Feature> features = [.. featuresArr];
// Clear any empty fields
ClearEmptyKeys(featureItem);
@@ -523,17 +523,17 @@ namespace SabreTools.DatFiles
private Models.Metadata.Machine[]? ConvertMachinesDB(bool ignoreblanks = false)
{
// Create a machine list to hold all outputs
var machines = new List<Models.Metadata.Machine>();
List<Models.Metadata.Machine> machines = [];
// Loop through the sorted items and create games for them
foreach (string key in ItemsDB.SortedKeys)
{
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || items.Length == 0)
if (items == null || items.Count == 0)
continue;
// Create a machine to hold everything
var machine = ItemsDB.GetMachineForItem(items[0].Item1).Item2!.GetInternalClone();
var machine = ItemsDB.GetMachineForItem(items.First().Key).Value!.GetInternalClone();
// Handle Trurip object, if it exists
if (machine.ContainsKey(Models.Metadata.Machine.TruripKey))
@@ -557,19 +557,16 @@ namespace SabreTools.DatFiles
Dictionary<Models.Metadata.Part, (Models.Metadata.DiskArea, Models.Metadata.Disk)> diskAreaMappings = [];
// Loop through and convert the items to respective lists
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
// Get the item
var item = items[index];
// Check for a "null" item
item = ProcessNullifiedItem(item);
var item = ProcessNullifiedItem(kvp);
// Skip if we're ignoring the item
if (ShouldIgnore(item, ignoreblanks))
if (ShouldIgnore(item.Value, ignoreblanks))
continue;
switch (item.Item2)
switch (item.Value)
{
case DatItems.Formats.Adjuster adjuster:
var adjusterItem = ProcessItem(adjuster);
@@ -797,7 +794,7 @@ namespace SabreTools.DatFiles
{
// Get existing data areas as a list
var dataAreasArr = partItems[partName].Read<Models.Metadata.DataArea[]>(Models.Metadata.Part.DataAreaKey) ?? [];
var dataAreas = new List<Models.Metadata.DataArea>(dataAreasArr);
List<Models.Metadata.DataArea> dataAreas = [.. dataAreasArr];
// Find the existing disk area to append to, otherwise create a new disk area
int dataAreaIndex = dataAreas.FindIndex(da => da.ReadString(Models.Metadata.DataArea.NameKey) == dataAreaName);
@@ -820,7 +817,7 @@ namespace SabreTools.DatFiles
// Get existing roms as a list
var romsArr = aggregateDataArea.Read<Models.Metadata.Rom[]>(Models.Metadata.DataArea.RomKey) ?? [];
var roms = new List<Models.Metadata.Rom>(romsArr);
List<Models.Metadata.Rom> roms = [.. romsArr];
// Add the rom to the data area
roms.Add(romItem);
@@ -854,7 +851,7 @@ namespace SabreTools.DatFiles
{
// Get existing disk areas as a list
var diskAreasArr = partItems[partName].Read<Models.Metadata.DiskArea[]>(Models.Metadata.Part.DiskAreaKey) ?? [];
var diskAreas = new List<Models.Metadata.DiskArea>(diskAreasArr);
List<Models.Metadata.DiskArea> diskAreas = [.. diskAreasArr];
// Find the existing disk area to append to, otherwise create a new disk area
int diskAreaIndex = diskAreas.FindIndex(da => da.ReadString(Models.Metadata.DiskArea.NameKey) == diskAreaName);
@@ -874,7 +871,7 @@ namespace SabreTools.DatFiles
// Get existing disks as a list
var disksArr = aggregateDiskArea.Read<Models.Metadata.Disk[]>(Models.Metadata.DiskArea.DiskKey) ?? [];
var disks = new List<Models.Metadata.Disk>(disksArr);
List<Models.Metadata.Disk> disks = [.. disksArr];
// Add the disk to the data area
disks.Add(diskItem);
@@ -898,7 +895,7 @@ namespace SabreTools.DatFiles
{
// Get existing dipswitches as a list
var dipSwitchesArr = partItems[partName].Read<Models.Metadata.DipSwitch[]>(Models.Metadata.Part.DipSwitchKey) ?? [];
var dipSwitches = new List<Models.Metadata.DipSwitch>(dipSwitchesArr);
List<Models.Metadata.DipSwitch> dipSwitches = [.. dipSwitchesArr];
// Clear any empty fields
ClearEmptyKeys(dipSwitchItem);
@@ -915,7 +912,7 @@ namespace SabreTools.DatFiles
{
// Get existing features as a list
var featuresArr = partItems[partName].Read<Models.Metadata.Feature[]>(Models.Metadata.Part.FeatureKey) ?? [];
var features = new List<Models.Metadata.Feature>(featuresArr);
List<Models.Metadata.Feature> features = [.. featuresArr];
// Clear any empty fields
ClearEmptyKeys(featureItem);
@@ -970,7 +967,7 @@ namespace SabreTools.DatFiles
var confLocations = item.GetFieldValue<DatItems.Formats.ConfLocation[]?>(Models.Metadata.Configuration.ConfLocationKey);
if (confLocations != null)
{
var confLocationItems = new List<Models.Metadata.ConfLocation>();
List<Models.Metadata.ConfLocation> confLocationItems = [];
foreach (var confLocation in confLocations)
{
var confLocationItem = confLocation.GetInternalClone();
@@ -983,7 +980,7 @@ namespace SabreTools.DatFiles
var confSettings = item.GetFieldValue<DatItems.Formats.ConfSetting[]?>(Models.Metadata.Configuration.ConfSettingKey);
if (confSettings != null)
{
var confSettingItems = new List<Models.Metadata.ConfSetting>();
List<Models.Metadata.ConfSetting> confSettingItems = [];
foreach (var confSetting in confSettings)
{
var confSettingItem = confSetting.GetInternalClone();
@@ -1011,7 +1008,7 @@ namespace SabreTools.DatFiles
var extensions = item.GetFieldValue<DatItems.Formats.Extension[]?>(Models.Metadata.Device.ExtensionKey);
if (extensions != null)
{
var extensionItems = new List<Models.Metadata.Extension>();
List<Models.Metadata.Extension> extensionItems = [];
foreach (var extension in extensions)
{
var extensionItem = extension.GetInternalClone();
@@ -1040,7 +1037,7 @@ namespace SabreTools.DatFiles
var dipLocations = item.GetFieldValue<DatItems.Formats.DipLocation[]?>(Models.Metadata.DipSwitch.DipLocationKey);
if (dipLocations != null)
{
var dipLocationItems = new List<Models.Metadata.DipLocation>();
List<Models.Metadata.DipLocation> dipLocationItems = [];
foreach (var dipLocation in dipLocations)
{
var extensionItem = dipLocation.GetInternalClone();
@@ -1053,7 +1050,7 @@ namespace SabreTools.DatFiles
var dipValues = item.GetFieldValue<DatItems.Formats.DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey);
if (dipValues != null)
{
var dipValueItems = new List<Models.Metadata.DipValue>();
List<Models.Metadata.DipValue> dipValueItems = [];
foreach (var dipValue in dipValues)
{
var extensionItem = dipValue.GetInternalClone();
@@ -1104,7 +1101,7 @@ namespace SabreTools.DatFiles
var controls = item.GetFieldValue<DatItems.Formats.Control[]?>(Models.Metadata.Input.ControlKey);
if (controls != null)
{
var controlItems = new List<Models.Metadata.Control>();
List<Models.Metadata.Control> controlItems = [];
foreach (var control in controls)
{
var controlItem = control.GetInternalClone();
@@ -1128,7 +1125,7 @@ namespace SabreTools.DatFiles
var analogs = item.GetFieldValue<DatItems.Formats.Analog[]?>(Models.Metadata.Port.AnalogKey);
if (analogs != null)
{
var analogItems = new List<Models.Metadata.Analog>();
List<Models.Metadata.Analog> analogItems = [];
foreach (var analog in analogs)
{
var extensionItem = analog.GetInternalClone();
@@ -1252,7 +1249,7 @@ namespace SabreTools.DatFiles
var slotOptions = item.GetFieldValue<DatItems.Formats.SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey);
if (slotOptions != null)
{
var slotOptionItems = new List<Models.Metadata.SlotOption>();
List<Models.Metadata.SlotOption> slotOptionItems = [];
foreach (var slotOption in slotOptions)
{
var extensionItem = slotOption.GetInternalClone();

View File

@@ -342,21 +342,21 @@ namespace SabreTools.DatFiles
/// <param name="item">DatItem to create a prefix/postfix for</param>
/// <param name="prefix">True for prefix, false for postfix</param>
/// <returns>Sanitized string representing the postfix or prefix</returns>
protected string CreatePrefixPostfixDB((long, DatItem) item, bool prefix)
protected string CreatePrefixPostfixDB(KeyValuePair<long, DatItem> item, bool prefix)
{
// Get machine for the item
var machine = ItemsDB.GetMachineForItem(item.Item1);
if (machine.Item2 == null)
var machine = ItemsDB.GetMachineForItem(item.Key);
if (machine.Value == null)
return string.Empty;
// Initialize strings
string? type = item.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string? type = item.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string fix,
game = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty,
manufacturer = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.ManufacturerKey) ?? string.Empty,
publisher = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.PublisherKey) ?? string.Empty,
category = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.CategoryKey) ?? string.Empty,
name = item.Item2.GetName() ?? type.AsEnumValue<ItemType>().AsStringValue() ?? string.Empty,
game = machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty,
manufacturer = machine.Value.GetStringFieldValue(Models.Metadata.Machine.ManufacturerKey) ?? string.Empty,
publisher = machine.Value.GetStringFieldValue(Models.Metadata.Machine.PublisherKey) ?? string.Empty,
category = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CategoryKey) ?? string.Empty,
name = item.Value.GetName() ?? type.AsEnumValue<ItemType>().AsStringValue() ?? string.Empty,
crc = string.Empty,
md5 = string.Empty,
sha1 = string.Empty,
@@ -384,19 +384,19 @@ namespace SabreTools.DatFiles
}
// Ensure we have the proper values for replacement
if (item.Item2 is Disk disk)
if (item.Value is Disk disk)
{
md5 = disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key) ?? string.Empty;
sha1 = disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key) ?? string.Empty;
}
else if (item.Item2 is Media media)
else if (item.Value is Media media)
{
md5 = media.GetStringFieldValue(Models.Metadata.Media.MD5Key) ?? string.Empty;
sha1 = media.GetStringFieldValue(Models.Metadata.Media.SHA1Key) ?? string.Empty;
sha256 = media.GetStringFieldValue(Models.Metadata.Media.SHA256Key) ?? string.Empty;
spamsum = media.GetStringFieldValue(Models.Metadata.Media.SpamSumKey) ?? string.Empty;
}
else if (item.Item2 is Rom rom)
else if (item.Value is Rom rom)
{
crc = rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey) ?? string.Empty;
md5 = rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key) ?? string.Empty;
@@ -539,7 +539,7 @@ namespace SabreTools.DatFiles
/// <param name="item">DatItem to update</param>
/// <param name="forceRemoveQuotes">True if the Quotes flag should be ignored, false otherwise</param>
/// <param name="forceRomName">True if the UseRomName should be always on (default), false otherwise</param>
protected void ProcessItemNameDB((long, DatItem) item, bool forceRemoveQuotes, bool forceRomName = true)
protected void ProcessItemNameDB(KeyValuePair<long, DatItem> item, bool forceRemoveQuotes, bool forceRomName = true)
{
// Backup relevant values and set new ones accordingly
bool? quotesBackup = Header.GetBoolFieldValue(DatHeader.QuotesKey);
@@ -550,12 +550,12 @@ namespace SabreTools.DatFiles
Header.SetFieldValue<bool>(DatHeader.UseRomNameKey, true);
// Get machine for the item
var machine = ItemsDB.GetMachineForItem(item.Item1);
var machine = ItemsDB.GetMachineForItem(item.Key);
// Get the name to update
string? name = (Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
? item.Item2.GetName()
: machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)) ?? string.Empty;
? item.Value.GetName()
: machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey)) ?? string.Empty;
// Create the proper Prefix and Postfix
string pre = CreatePrefixPostfixDB(item, true);
@@ -565,34 +565,34 @@ namespace SabreTools.DatFiles
var outputDepot = Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey);
if (outputDepot?.IsActive == true)
{
if (item.Item2 is Disk disk)
if (item.Value is Disk disk)
{
// We can only write out if there's a SHA-1
string? sha1 = disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key);
if (!string.IsNullOrEmpty(sha1))
{
name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/');
item.Item2.SetName($"{pre}{name}{post}");
item.Value.SetName($"{pre}{name}{post}");
}
}
else if (item.Item2 is Media media)
else if (item.Value is Media media)
{
// We can only write out if there's a SHA-1
string? sha1 = media.GetStringFieldValue(Models.Metadata.Media.SHA1Key);
if (!string.IsNullOrEmpty(sha1))
{
name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/');
item.Item2.SetName($"{pre}{name}{post}");
item.Value.SetName($"{pre}{name}{post}");
}
}
else if (item.Item2 is Rom rom)
else if (item.Value is Rom rom)
{
// We can only write out if there's a SHA-1
string? sha1 = rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key);
if (!string.IsNullOrEmpty(sha1))
{
name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/');
item.Item2.SetName($"{pre}{name}{post}");
item.Value.SetName($"{pre}{name}{post}");
}
}
@@ -619,14 +619,14 @@ namespace SabreTools.DatFiles
name += addExtension;
if (Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true && Header.GetBoolFieldValue(DatHeader.GameNameKey) == true)
name = Path.Combine(machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty, name);
name = Path.Combine(machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty, name);
// Now assign back the formatted name
name = $"{pre}{name}{post}";
if (Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
item.Item2.SetName(name);
else if (machine.Item2 != null)
machine.Item2.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, name);
item.Value.SetName(name);
else if (machine.Value != null)
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, name);
// Restore all relevant values
if (forceRemoveQuotes)
@@ -684,15 +684,15 @@ namespace SabreTools.DatFiles
/// </summary>
/// <param name="item">DatItem to check for "null" status</param>
/// <returns>Cleaned DatItem</returns>
protected (long, DatItem) ProcessNullifiedItem((long, DatItem) item)
protected KeyValuePair<long, DatItem> ProcessNullifiedItem(KeyValuePair<long, DatItem> item)
{
// If we don't have a Rom, we can ignore it
if (item.Item2 is not Rom rom)
if (item.Value is not Rom rom)
return item;
// Get machine for the item
var machine = ItemsDB.GetMachineForItem(item.Item1);
var machineObj = machine.Item2;
var machine = ItemsDB.GetMachineForItem(item.Key);
var machineObj = machine.Value;
if (machineObj == null)
return item;
@@ -720,7 +720,7 @@ namespace SabreTools.DatFiles
rom.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey) == "null" ? ZeroHash.SpamSumStr : null);
}
return (item.Item1, rom);
return new KeyValuePair<long, DatItem>(item.Key, rom);
}
/// <summary>
@@ -766,15 +766,15 @@ namespace SabreTools.DatFiles
/// <param name="datItems">DatItems to check</param>
/// <returns>True if the machine contains at least one writable item, false otherwise</returns>
/// <remarks>Empty machines are kept with this</remarks>
protected bool ContainsWritable((long, DatItem)[]? datItems)
protected bool ContainsWritable(Dictionary<long, DatItem>? datItems)
{
// Empty machines are considered writable
if (datItems == null || datItems.Length == 0)
if (datItems == null || datItems.Count == 0)
return true;
foreach ((long, DatItem) datItem in datItems)
foreach (var datItem in datItems)
{
ItemType itemType = datItem.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>();
ItemType itemType = datItem.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>();
if (Array.Exists(GetSupportedTypes(), t => t == itemType))
return true;
}
@@ -852,76 +852,6 @@ namespace SabreTools.DatFiles
return false;
}
/// <summary>
/// Get if an item should be ignored on write
/// </summary>
/// <param name="datItem">DatItem to check</param>
/// <param name="ignoreBlanks">True if blank roms should be skipped on output, false otherwise</param>
/// <returns>True if the item should be skipped on write, false otherwise</returns>
protected bool ShouldIgnore((long, DatItem?) datItem, bool ignoreBlanks)
{
// If this is invoked with a null DatItem, we ignore
if (datItem.Item1 < 0 || datItem.Item2 == null)
{
logger?.Verbose($"Item was skipped because it was null");
return true;
}
// If the item is supposed to be removed, we ignore
if (datItem.Item2.GetBoolFieldValue(DatItem.RemoveKey) == true)
{
string itemString = JsonConvert.SerializeObject(datItem, Formatting.None);
logger?.Verbose($"Item '{itemString}' was skipped because it was marked for removal");
return true;
}
// If we have the Blank dat item, we ignore
if (datItem.Item2 is Blank)
{
string itemString = JsonConvert.SerializeObject(datItem, Formatting.None);
logger?.Verbose($"Item '{itemString}' was skipped because it was of type 'Blank'");
return true;
}
// If we're ignoring blanks and we have a Rom
if (ignoreBlanks && datItem.Item2 is Rom rom)
{
// If we have a 0-size or blank rom, then we ignore
long? size = rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey);
if (size == 0 || size == null)
{
string itemString = JsonConvert.SerializeObject(datItem, Formatting.None);
logger?.Verbose($"Item '{itemString}' was skipped because it had an invalid size");
return true;
}
}
// If we have an item type not in the list of supported values
string datFormat = Header?.GetFieldValue<DatFormat>(DatHeader.DatFormatKey).ToString() ?? "Unknown Format";
ItemType itemType = datItem.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>();
if (!Array.Exists(GetSupportedTypes(), t => t == itemType))
{
string itemString = JsonConvert.SerializeObject(datItem, Formatting.None);
logger?.Verbose($"Item '{itemString}' was skipped because it was not supported in {datFormat}");
return true;
}
// If we have an item with missing required fields
List<string>? missingFields = GetMissingRequiredFields(datItem.Item2);
if (missingFields != null && missingFields.Count != 0)
{
string itemString = JsonConvert.SerializeObject(datItem, Formatting.None);
#if NET20 || NET35
logger?.Verbose($"Item '{itemString}' was skipped because it was missing required fields for {datFormat}: {string.Join(", ", [.. missingFields])}");
#else
logger?.Verbose($"Item '{itemString}' was skipped because it was missing required fields for {datFormat}: {string.Join(", ", missingFields)}");
#endif
return true;
}
return false;
}
#endregion
}
}

View File

@@ -28,7 +28,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -73,7 +73,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{
case Release release:

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -55,7 +55,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -30,7 +30,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -244,7 +244,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{
case BiosSet biosset:

View File

@@ -248,7 +248,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{
case Release release:

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.DatItems;
@@ -118,28 +119,30 @@ namespace SabreTools.DatFiles.Formats
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || !ContainsWritable(items))
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
if (itemsDict == null || !ContainsWritable(itemsDict))
continue;
// Resolve the names in the block
items = [.. DatItem.ResolveNamesDB([.. items])];
var items = DatItem.ResolveNamesDB([.. itemsDict]);
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
// Check for a "null" item
var datItem = items[index];
datItem = ProcessNullifiedItem(datItem);
var datItem = ProcessNullifiedItem(kvp);
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(datItem.Key);
// Write out the item if we're using machine names or we're not ignoring
if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true || !ShouldIgnore(datItem, ignoreblanks))
if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
|| !ShouldIgnore(datItem.Value, ignoreblanks))
{
WriteDatItemDB(sw, datItem, lastgame);
}
// Set the new data to compare against
lastgame = machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
}
}
@@ -182,19 +185,25 @@ namespace SabreTools.DatFiles.Formats
/// <param name="sw">StreamWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <param name="lastgame">The name of the last game to be output</param>
private void WriteDatItemDB(StreamWriter sw, (long, DatItem) datItem, string? lastgame)
private void WriteDatItemDB(StreamWriter sw, KeyValuePair<long, DatItem> datItem, string? lastgame)
{
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(datItem.Key);
// Process the item name
ProcessItemNameDB(datItem, false, forceRomName: false);
// Romba mode automatically uses item name
if (Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey)?.IsActive == true || Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
sw.Write($"{datItem.Item2.GetName() ?? string.Empty}\n");
else if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true && machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey) != lastgame)
sw.Write($"{machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty}\n");
if (Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey)?.IsActive == true
|| Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
{
sw.Write($"{datItem.Value.GetName() ?? string.Empty}\n");
}
else if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
&& machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) != lastgame)
{
sw.Write($"{machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty}\n");
}
sw.Flush();
}

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{

View File

@@ -60,7 +60,7 @@ The softwaredb.xml file contains information about rom mapper types
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -406,11 +406,11 @@ namespace SabreTools.DatFiles.Formats
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && !string.Equals(lastgame, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
SabreJSON.WriteEndGame(jtw);
WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
SabreJSON.WriteStartGame(jtw, datItem);
WriteStartGame(jtw, datItem);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
@@ -425,7 +425,7 @@ namespace SabreTools.DatFiles.Formats
}
// Write the file footer out
SabreJSON.WriteFooter(jtw);
WriteFooter(jtw);
logger.User($"'{outfile}' written!{Environment.NewLine}");
jtw.Close();
@@ -473,42 +473,40 @@ namespace SabreTools.DatFiles.Formats
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || !ContainsWritable(items))
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
if (itemsDict == null || !ContainsWritable(itemsDict))
continue;
// Resolve the names in the block
items = [.. DatItem.ResolveNamesDB([.. items])];
var items = DatItem.ResolveNamesDB([.. itemsDict]);
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
var datItem = items[index];
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(kvp.Key);
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && !string.Equals(lastgame, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
SabreJSON.WriteEndGame(jtw);
if (lastgame != null && !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGameDB(jtw, datItem);
if (lastgame == null || !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGame(jtw, kvp.Value);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
var datItem = ProcessNullifiedItem(kvp);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItemDB(jtw, datItem);
if (!ShouldIgnore(datItem.Value, ignoreblanks))
WriteDatItem(jtw, datItem.Value);
// Set the new data to compare against
lastgame = machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
}
}
// Write the file footer out
SabreJSON.WriteFooter(jtw);
WriteFooter(jtw);
logger.User($"'{outfile}' written!{Environment.NewLine}");
jtw.Close();
@@ -567,34 +565,6 @@ namespace SabreTools.DatFiles.Formats
jtw.Flush();
}
/// <summary>
/// Write out Game start using the supplied JsonTextWriter
/// </summary>
/// <param name="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteStartGameDB(JsonTextWriter jtw, (long, DatItem) datItem)
{
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
// No game should start with a path separator
if (!string.IsNullOrEmpty(machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)))
machine.Item2!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!.TrimStart(Path.DirectorySeparatorChar));
// Build the state
jtw.WriteStartObject();
// Write the Machine
jtw.WritePropertyName("machine");
JsonSerializer js = new() { Formatting = Formatting.Indented };
js.Serialize(jtw, machine.Item2!);
jtw.WritePropertyName("items");
jtw.WriteStartArray();
jtw.Flush();
}
/// <summary>
/// Write out Game end using the supplied JsonTextWriter
/// </summary>
@@ -634,30 +604,6 @@ namespace SabreTools.DatFiles.Formats
jtw.Flush();
}
/// <summary>
/// Write out DatItem using the supplied JsonTextWriter
/// </summary>
/// <param name="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItemDB(JsonTextWriter jtw, (long, DatItem) datItem)
{
// Pre-process the item name
ProcessItemNameDB(datItem, true);
// Build the state
jtw.WriteStartObject();
// Write the DatItem
jtw.WritePropertyName("datitem");
JsonSerializer js = new() { ContractResolver = new BaseFirstContractResolver(), Formatting = Formatting.Indented };
js.Serialize(jtw, datItem.Item2);
// End item
jtw.WriteEndObject();
jtw.Flush();
}
/// <summary>
/// Write out DAT footer using the supplied JsonTextWriter
/// </summary>

View File

@@ -303,37 +303,35 @@ namespace SabreTools.DatFiles.Formats
foreach (string key in ItemsDB.SortedKeys)
{
// If this machine doesn't contain any writable items, skip
var items = ItemsDB.GetItemsForBucket(key, filter: true);
if (items == null || !ContainsWritable(items))
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
if (itemsDict == null || !ContainsWritable(itemsDict))
continue;
// Resolve the names in the block
items = [.. DatItem.ResolveNamesDB([.. items])];
var items = DatItem.ResolveNamesDB([.. itemsDict]);
for (int index = 0; index < items.Length; index++)
foreach (var kvp in items)
{
var datItem = items[index];
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
var machine = ItemsDB.GetMachineForItem(kvp.Key);
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && !string.Equals(lastgame, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
if (lastgame != null && !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || !string.Equals(lastgame, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGameDB(xtw, datItem);
if (lastgame == null || !string.Equals(lastgame, machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey), StringComparison.OrdinalIgnoreCase))
WriteStartGame(xtw, kvp.Value);
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
var datItem = ProcessNullifiedItem(kvp);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItemDB(xtw, datItem);
if (!ShouldIgnore(datItem.Value, ignoreblanks))
WriteDatItem(xtw, datItem.Value);
// Set the new data to compare against
lastgame = machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
}
}
@@ -397,31 +395,6 @@ namespace SabreTools.DatFiles.Formats
xtw.Flush();
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteStartGameDB(XmlTextWriter xtw, (long, DatItem) datItem)
{
// Get the machine for the item
var machine = ItemsDB.GetMachineForItem(datItem.Item1);
// No game should start with a path separator
machine.Item2!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machine.Item2!.GetStringFieldValue(Models.Metadata.Machine.NameKey)?.TrimStart(Path.DirectorySeparatorChar) ?? string.Empty);
// Write the machine
xtw.WriteStartElement("directory");
XmlSerializer xs = new(typeof(Machine));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, machine.Item2, ns);
xtw.WriteStartElement("files");
xtw.Flush();
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
@@ -456,25 +429,6 @@ namespace SabreTools.DatFiles.Formats
xtw.Flush();
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItemDB(XmlTextWriter xtw, (long, DatItem) datItem)
{
// Pre-process the item name
ProcessItemNameDB(datItem, true);
// Write the DatItem
XmlSerializer xs = new(typeof(DatItem));
XmlSerializerNamespaces ns = new();
ns.Add("", "");
xs.Serialize(xtw, datItem.Item2, ns);
xtw.Flush();
}
/// <summary>
/// Write out DAT footer using the supplied StreamWriter
/// </summary>

View File

@@ -54,7 +54,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
// Check item name
if (string.IsNullOrEmpty(datItem.GetName()))

View File

@@ -96,7 +96,7 @@ namespace SabreTools.DatFiles.Formats
/// <inheritdoc/>
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
{
var missingFields = new List<string>();
List<string> missingFields = [];
switch (datItem)
{

View File

@@ -846,7 +846,7 @@ namespace SabreTools.DatFiles
#endif
// Filter all items in the current key
var newItems = new List<DatItem>();
List<DatItem> newItems = [];
foreach (var item in items)
{
if (item.PassesFilter(filterRunner))
@@ -1242,15 +1242,16 @@ namespace SabreTools.DatFiles
foreach (string machine in machines)
{
// If the machine doesn't have items, we continue
if (this[machine] == null || this[machine]!.Count == 0)
var datItems = this[machine];
if (datItems == null || datItems.Count == 0)
continue;
// If the machine (is/is not) a device, we want to continue
if (dev ^ (this[machine]![0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
if (dev ^ (datItems[0].GetFieldValue<Machine>(DatItem.MachineKey)!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
continue;
// Get all device reference names from the current machine
List<string?> deviceReferences = this[machine]!
List<string?> deviceReferences = datItems
.FindAll(i => i is DeviceRef)
.ConvertAll(i => i as DeviceRef)
.ConvertAll(dr => dr!.GetName())
@@ -1258,7 +1259,7 @@ namespace SabreTools.DatFiles
.ToList();
// Get all slot option names from the current machine
List<string?> slotOptions = this[machine]!
List<string?> slotOptions = datItems
.FindAll(i => i is Slot)
.ConvertAll(i => i as Slot)
.FindAll(s => s!.SlotOptionsSpecified)
@@ -1274,13 +1275,13 @@ namespace SabreTools.DatFiles
var newDeviceReferences = new HashSet<string>();
foreach (string? deviceReference in deviceReferences)
{
// If the machine doesn't exist then we continue
if (deviceReference == null || this[deviceReference] == null || this[deviceReference]!.Count == 0)
// If the device reference is missing
if (string.IsNullOrEmpty(deviceReference))
continue;
// Add to the list of new device reference names
var devItems = this[deviceReference];
if (devItems == null)
var devItems = this[deviceReference!];
if (devItems == null || devItems.Count == 0)
continue;
newDeviceReferences.UnionWith(devItems
@@ -1288,11 +1289,11 @@ namespace SabreTools.DatFiles
.ConvertAll(i => (i as DeviceRef)!.GetName()!));
// Set new machine information and add to the current machine
DatItem copyFrom = this[machine]![0];
DatItem copyFrom = datItems[0];
foreach (DatItem item in devItems)
{
// If the parent machine doesn't already contain this item, add it
if (!this[machine]!.Exists(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) && i.GetName() == item.GetName()))
if (!datItems.Exists(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) && i.GetName() == item.GetName()))
{
// Set that we found new items
foundnew = true;
@@ -1312,7 +1313,7 @@ namespace SabreTools.DatFiles
{
var deviceRef = new DeviceRef();
deviceRef.SetName(deviceReference);
this[machine]!.Add(deviceRef);
datItems.Add(deviceRef);
}
}
}
@@ -1324,13 +1325,14 @@ namespace SabreTools.DatFiles
var newSlotOptions = new HashSet<string>();
foreach (string? slotOption in slotOptions)
{
// If the slot option is missing
if (string.IsNullOrEmpty(slotOption))
// If the machine doesn't exist then we continue
if (slotOption == null || this[slotOption] == null || this[slotOption]!.Count == 0)
continue;
// Add to the list of new slot option names
var slotItems = this[slotOption];
if (slotItems == null)
var slotItems = this[slotOption!];
if (slotItems == null || slotItems.Count == 0)
continue;
newSlotOptions.UnionWith(slotItems
@@ -1340,11 +1342,11 @@ namespace SabreTools.DatFiles
.Select(o => o.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey)!));
// Set new machine information and add to the current machine
DatItem copyFrom = this[machine]![0];
DatItem copyFrom = datItems[0];
foreach (DatItem item in slotItems)
{
// If the parent machine doesn't already contain this item, add it
if (!this[machine]!.Exists(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) && i.GetName() == item.GetName()))
if (!datItems.Exists(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) && i.GetName() == item.GetName()))
{
// Set that we found new items
foundnew = true;
@@ -1368,7 +1370,7 @@ namespace SabreTools.DatFiles
var slotItem = new Slot();
slotItem.SetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey, [slotOptionItem]);
this[machine]!.Add(slotItem);
datItems.Add(slotItem);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;
using Newtonsoft.Json;
@@ -729,45 +730,43 @@ namespace SabreTools.DatItems
/// </summary>
/// <param name="infiles">List of File objects representing the roms to be merged</param>
/// <returns>A List of DatItem objects representing the renamed roms</returns>
public static List<(long, DatItem)> ResolveNamesDB(List<(long, DatItem)> infiles)
public static List<KeyValuePair<long, DatItem>> ResolveNamesDB(List<KeyValuePair<long, DatItem>> infiles)
{
// Create the output list
List<(long, DatItem)> output = [];
// Create the output dict
List<KeyValuePair<long, DatItem>> output = [];
// First we want to make sure the list is in alphabetical order
Sort(ref infiles, true);
// Now we want to loop through and check names
(long, DatItem?) lastItem = (-1, null);
DatItem? lastItem = null;
string? lastrenamed = null;
int lastid = 0;
for (int i = 0; i < infiles.Count; i++)
foreach (var datItem in infiles)
{
var datItem = infiles[i];
// If we have the first item, we automatically add it
if (lastItem.Item2 == null)
if (lastItem == null)
{
output.Add(datItem);
lastItem = datItem;
lastItem = datItem.Value;
continue;
}
// Get the last item name, if applicable
string lastItemName = lastItem.Item2.GetName()
?? lastItem.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
string lastItemName = lastItem.GetName()
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty;
// Get the current item name, if applicable
string datItemName = datItem.Item2.GetName()
?? datItem.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
string datItemName = datItem.Value.GetName()
?? datItem.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty;
// If the current item exactly matches the last item, then we don't add it
#if NETFRAMEWORK
if ((datItem.Item2.GetDuplicateStatus(lastItem.Item2) & DupeType.All) != 0)
if ((datItem.Value.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
#else
if (datItem.Item2.GetDuplicateStatus(lastItem.Item2).HasFlag(DupeType.All))
if (datItem.Value.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
#endif
{
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
@@ -779,9 +778,9 @@ namespace SabreTools.DatItems
{
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
if (datItem.Item2 is Disk || datItem.Item2 is Formats.File || datItem.Item2 is Media || datItem.Item2 is Rom)
if (datItem.Value is Disk || datItem.Value is Formats.File || datItem.Value is Media || datItem.Value is Rom)
{
datItemName += GetDuplicateSuffix(datItem.Item2);
datItemName += GetDuplicateSuffix(datItem.Value);
lastrenamed ??= datItemName;
}
@@ -800,8 +799,7 @@ namespace SabreTools.DatItems
}
// Set the item name back to the datItem
datItem.Item2.SetName(datItemName);
datItem.Value.SetName(datItemName);
output.Add(datItem);
}
@@ -809,7 +807,7 @@ namespace SabreTools.DatItems
else
{
output.Add(datItem);
lastItem = datItem;
lastItem = datItem.Value;
lastrenamed = null;
lastid = 0;
}
@@ -895,41 +893,41 @@ namespace SabreTools.DatItems
/// <param name="roms">List of File objects representing the roms to be sorted</param>
/// <param name="norename">True if files are not renamed, false otherwise</param>
/// <returns>True if it sorted correctly, false otherwise</returns>
public static bool Sort(ref List<(long, DatItem)> roms, bool norename)
public static bool Sort(ref List<KeyValuePair<long, DatItem>> roms, bool norename)
{
roms.Sort(delegate ((long, DatItem) x, (long, DatItem) y)
roms.Sort(delegate (KeyValuePair<long, DatItem> x, KeyValuePair<long, DatItem> y)
{
try
{
var nc = new NaturalComparer();
// If machine names don't match
string? xMachineName = x.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? yMachineName = y.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? xMachineName = x.Value.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? yMachineName = y.Value.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
if (xMachineName != yMachineName)
return nc.Compare(xMachineName, yMachineName);
// If types don't match
string? xType = x.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string? yType = y.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string? xType = x.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string? yType = y.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
if (xType != yType)
return xType.AsEnumValue<ItemType>() - yType.AsEnumValue<ItemType>();
// If directory names don't match
string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Item2.GetName() ?? string.Empty));
string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Item2.GetName() ?? string.Empty));
string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName() ?? string.Empty));
string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName() ?? string.Empty));
if (xDirectoryName != yDirectoryName)
return nc.Compare(xDirectoryName, yDirectoryName);
// If item names don't match
string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Item2.GetName() ?? string.Empty));
string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Item2.GetName() ?? string.Empty));
string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName() ?? string.Empty));
string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName() ?? string.Empty));
if (xName != yName)
return nc.Compare(xName, yName);
// Otherwise, compare on machine or source, depending on the flag
int? xSourceIndex = x.Item2.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
int? ySourceIndex = y.Item2.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
int? xSourceIndex = x.Value.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
int? ySourceIndex = y.Value.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0);
}
catch

View File

@@ -211,10 +211,10 @@ namespace SabreTools.DatTools
if (items == null)
continue;
foreach ((long, DatItem) item in items)
foreach (var item in items)
{
// If we have a null item, we can't clean it it
if (item.Item2 == null)
if (item.Value == null)
continue;
// Run cleaning per item
@@ -284,17 +284,17 @@ namespace SabreTools.DatTools
/// </summary>
/// <param name="db">ItemDictionaryDB to get machine information from</param>
/// <param name="datItem">DatItem to clean</param>
internal void CleanDatItemDB(ItemDictionaryDB db, (long, DatItem) datItem)
internal void CleanDatItemDB(ItemDictionaryDB db, KeyValuePair<long, DatItem> datItem)
{
// Get the machine associated with the item, if possible
var machine = db.GetMachineForItem(datItem.Item1);
if (machine.Item2 == null)
var machine = db.GetMachineForItem(datItem.Key);
if (machine.Value == null)
return;
// Get the fields for processing
string? machineName = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? machineDesc = machine.Item2.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey);
string? datItemName = datItem.Item2.GetName();
string? machineName = machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? machineDesc = machine.Value.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey);
string? datItemName = datItem.Value.GetName();
// If we're stripping unicode characters, strip machine name and description
if (RemoveUnicode)
@@ -331,9 +331,9 @@ namespace SabreTools.DatTools
}
// Set the fields back, if necessary
machine.Item2.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machineName);
machine.Item2.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, machineDesc);
datItem.Item2.SetName(datItemName);
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machineName);
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, machineDesc);
datItem.Value.SetName(datItemName);
}
#endregion

View File

@@ -121,18 +121,18 @@ namespace SabreTools.DatTools
continue;
#endif
foreach ((long, DatItem) item in items)
foreach (var item in items)
{
var source = datFile.ItemsDB.GetSourceForItem(item.Item1);
if (source.Item2 == null)
var source = datFile.ItemsDB.GetSourceForItem(item.Key);
if (source.Value == null)
continue;
var machine = datFile.ItemsDB.GetMachineForItem(item.Item1);
if (machine.Item2 == null)
var machine = datFile.ItemsDB.GetMachineForItem(item.Key);
if (machine.Value == null)
continue;
string filename = inputs[source.Item2.Index].CurrentPath;
string rootpath = inputs[source.Item2.Index].ParentPath ?? string.Empty;
string filename = inputs[source.Value.Index].CurrentPath;
string rootpath = inputs[source.Value.Index].ParentPath ?? string.Empty;
if (rootpath.Length > 0
#if NETFRAMEWORK
@@ -148,9 +148,9 @@ namespace SabreTools.DatTools
filename = filename.Remove(0, rootpath.Length);
machine.Item2.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ machine.Item2.GetStringFieldValue(Models.Metadata.Machine.NameKey));
+ machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
#if NET40_OR_GREATER || NETCOREAPP
});
@@ -317,15 +317,15 @@ namespace SabreTools.DatTools
continue;
#endif
foreach ((long, DatItem) datItem in datItems)
foreach (var datItem in datItems)
{
var dupes = datFile.ItemsDB.GetDuplicates(datItem, sorted: true);
if (datItem.Item2.Clone() is not DatItem newDatItem)
if (datItem.Value.Clone() is not DatItem newDatItem)
continue;
// Replace fields from the first duplicate, if we have one
if (dupes.Count > 0)
Replacer.ReplaceFields(datItem.Item2, dupes[0].Item2, itemFieldNames);
Replacer.ReplaceFields(datItem.Value, dupes.First().Value, itemFieldNames);
}
#if NET40_OR_GREATER || NETCOREAPP
});
@@ -358,12 +358,12 @@ namespace SabreTools.DatTools
continue;
#endif
foreach ((long, DatItem) datItem in datItems)
foreach (var datItem in datItems)
{
var datMachine = datFile.ItemsDB.GetMachineForItem(datFile.ItemsDB.GetItemsForBucket(key)![0].Item1);
var intMachine = intDat.ItemsDB.GetMachineForItem(datItem.Item1);
if (datMachine.Item2 != null && intMachine.Item2 != null)
Replacer.ReplaceFields(intMachine.Item2, datMachine.Item2, machineFieldNames, onlySame);
var datMachine = datFile.ItemsDB.GetMachineForItem(datFile.ItemsDB.GetItemsForBucket(key)!.First().Key);
var intMachine = intDat.ItemsDB.GetMachineForItem(datItem.Key);
if (datMachine.Value != null && intMachine.Value != null)
Replacer.ReplaceFields(intMachine.Value, datMachine.Value, machineFieldNames, onlySame);
}
#if NET40_OR_GREATER || NETCOREAPP
});
@@ -648,11 +648,11 @@ namespace SabreTools.DatTools
watch.Start("Populating duplicate DAT");
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -701,7 +701,7 @@ namespace SabreTools.DatTools
var currentSource = sources[sourceIndex];
string? currentMachineName = machines[machineIndex].GetStringFieldValue(Models.Metadata.Machine.NameKey);
var currentMachine = datFile.ItemsDB.GetMachine(currentMachineName);
if (currentMachine.Item2 == null)
if (currentMachine.Value == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
@@ -711,15 +711,15 @@ namespace SabreTools.DatTools
// Get the source-specific machine
string? renamedMachineName = $"{currentMachineName} ({Path.GetFileNameWithoutExtension(inputs[currentSource!.Index].CurrentPath)})";
var renamedMachine = datFile.ItemsDB.GetMachine(renamedMachineName);
if (renamedMachine.Item2 == null)
if (renamedMachine.Value == null)
{
var newMachine = currentMachine.Item2.Clone() as Machine;
var newMachine = currentMachine.Value.Clone() as Machine;
newMachine!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, renamedMachineName);
long newMachineIndex = dupeData.ItemsDB.AddMachine(newMachine!);
renamedMachine = (newMachineIndex, newMachine);
renamedMachine = new KeyValuePair<long, Machine?>(newMachineIndex, newMachine);
}
dupeData.ItemsDB.AddItem(item.Value, renamedMachine.Item1, sourceRemapping[sourceIndex], statsOnly: false);
dupeData.ItemsDB.AddItem(item.Value, renamedMachine.Key, sourceRemapping[sourceIndex], statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP
});
#else
@@ -888,11 +888,11 @@ namespace SabreTools.DatTools
watch.Start("Populating all individual DATs");
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -1081,11 +1081,11 @@ namespace SabreTools.DatTools
watch.Start("Populating no duplicate DAT");
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -1134,7 +1134,7 @@ namespace SabreTools.DatTools
var currentSource = sources[sourceIndex];
string? currentMachineName = machines[machineIndex].GetStringFieldValue(Models.Metadata.Machine.NameKey);
var currentMachine = datFile.ItemsDB.GetMachine(currentMachineName);
if (currentMachine.Item2 == null)
if (currentMachine.Value == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
@@ -1144,15 +1144,15 @@ namespace SabreTools.DatTools
// Get the source-specific machine
string? renamedMachineName = $"{currentMachineName} ({Path.GetFileNameWithoutExtension(inputs[currentSource!.Index].CurrentPath)})";
var renamedMachine = datFile.ItemsDB.GetMachine(renamedMachineName);
if (renamedMachine.Item2 == null)
if (renamedMachine.Value == null)
{
var newMachine = currentMachine.Item2.Clone() as Machine;
var newMachine = currentMachine.Value.Clone() as Machine;
newMachine!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, renamedMachineName);
long newMachineIndex = outerDiffData.ItemsDB.AddMachine(newMachine);
renamedMachine = (newMachineIndex, newMachine);
renamedMachine = new KeyValuePair<long, Machine?>(newMachineIndex, newMachine);
}
outerDiffData.ItemsDB.AddItem(item.Value, renamedMachine.Item1, sourceRemapping[sourceIndex], statsOnly: false);
outerDiffData.ItemsDB.AddItem(item.Value, renamedMachine.Key, sourceRemapping[sourceIndex], statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP
});
#else
@@ -1254,11 +1254,11 @@ namespace SabreTools.DatTools
private static void AddFromExistingDB(DatFile addTo, DatFile addFrom, bool delete = false)
{
// Get all current items, machines, and mappings
var datItems = addFrom.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = addFrom.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = addFrom.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = addFrom.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = addFrom.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = addFrom.ItemsDB.GetItems();
var machines = addFrom.ItemsDB.GetMachines();
var sources = addFrom.ItemsDB.GetSources();
var itemMachineMappings = addFrom.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = addFrom.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -1359,11 +1359,11 @@ namespace SabreTools.DatTools
private static void FillWithSourceIndexDB(DatFile datFile, DatFile indexDat, int index)
{
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();

View File

@@ -182,11 +182,11 @@ namespace SabreTools.DatTools
// Loop through and set the fields accordingly
foreach (var datItem in datItems)
{
var machine = datFile.ItemsDB.GetMachineForItem(datItem.Item1);
if (machine.Item2 != null)
setter.SetFields(machine.Item2);
var machine = datFile.ItemsDB.GetMachineForItem(datItem.Key);
if (machine.Value != null)
setter.SetFields(machine.Value);
setter.SetFields(datItem.Item2);
setter.SetFields(datItem.Value);
}
}
}

View File

@@ -603,7 +603,7 @@ namespace SabreTools.DatTools
/// <param name="inverse">True if the DAT should be used as a filter instead of a template, false otherwise</param>
/// <param name="dupes">Output list of duplicate items to rebuild to</param>
/// <returns>True if the item should be rebuilt, false otherwise</returns>
private static bool ShouldRebuildDB(DatFile datFile, (long, DatItem) datItem, Stream? stream, bool inverse, out List<(long, DatItem)> dupes)
private static bool ShouldRebuildDB(DatFile datFile, KeyValuePair<long, DatItem> datItem, Stream? stream, bool inverse, out Dictionary<long, DatItem> dupes)
{
// Find if the file has duplicates in the DAT
dupes = datFile.ItemsDB.GetDuplicates(datItem);
@@ -645,7 +645,7 @@ namespace SabreTools.DatTools
}
long index = datFile.ItemsDB.AddItem(item, machineIndex, -1, false);
dupes.Add((index, item));
dupes[index] = item;
return true;
}

View File

@@ -142,11 +142,11 @@ namespace SabreTools.DatTools
extBDat.Header.SetFieldValue<string?>(Models.Metadata.Header.DescriptionKey, extBDat.Header.GetStringFieldValue(Models.Metadata.Header.DescriptionKey) + $" ({newExtBString})");
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -350,11 +350,11 @@ namespace SabreTools.DatTools
}
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -680,11 +680,11 @@ namespace SabreTools.DatTools
greaterThan.Header.SetFieldValue<string?>(Models.Metadata.Header.DescriptionKey, greaterThan.Header.GetStringFieldValue(Models.Metadata.Header.DescriptionKey) + $" (equal-greater than {radix})");
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();
@@ -936,11 +936,11 @@ namespace SabreTools.DatTools
private static void FillWithItemTypeDB(DatFile datFile, DatFile indexDat, ItemType itemType)
{
// Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2);
var machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var sources = datFile.ItemsDB.GetSources().ToDictionary(m => m.Item1, m => m.Item2);
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings().ToDictionary(m => m.Item1, m => m.Item2);
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings().ToDictionary(m => m.Item1, m => m.Item2);
var datItems = datFile.ItemsDB.GetItems();
var machines = datFile.ItemsDB.GetMachines();
var sources = datFile.ItemsDB.GetSources();
var itemMachineMappings = datFile.ItemsDB.GetItemMachineMappings();
var itemSourceMappings = datFile.ItemsDB.GetItemSourceMappings();
// Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>();

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Core.Tools;
using SabreTools.DatFiles;
using SabreTools.DatItems;
@@ -180,8 +181,8 @@ namespace SabreTools.DatTools
continue;
// Now we want to remove all duplicates from the DAT
datFile.ItemsDB.GetDuplicates((-1, new Rom(fileinfo)))
.AddRange(datFile.ItemsDB.GetDuplicates((-1, new Disk(fileinfo))));
datFile.ItemsDB.GetDuplicates(new KeyValuePair<long, DatItem>(-1, new Rom(fileinfo)))
.Concat(datFile.ItemsDB.GetDuplicates(new KeyValuePair<long, DatItem>(-1, new Disk(fileinfo))));
}
watch.Stop();
@@ -270,14 +271,14 @@ namespace SabreTools.DatTools
if (items == null)
continue;
for (int i = 0; i < items.Length; i++)
foreach (var item in items)
{
// Get the source associated with the item
var source = datFile.ItemsDB.GetSourceForItem(items[i].Item1);
var source = datFile.ItemsDB.GetSourceForItem(item.Key);
// Unmatched items will have a source ID of int.MaxValue, remove all others
if (source.Item2?.Index != int.MaxValue)
items[i].Item2.SetFieldValue<bool?>(DatItem.RemoveKey, true);
if (source.Value?.Index != int.MaxValue)
item.Value.SetFieldValue<bool?>(DatItem.RemoveKey, true);
}
}

View File

@@ -144,11 +144,11 @@ namespace SabreTools.DatTools
datFile.ItemsDB.DatStatistics.DisplayName = datFile.Header.GetStringFieldValue(DatHeader.FileNameKey);
datFile.ItemsDB.DatStatistics.MachineCount = datFile.Items.Keys.Count;
var statsList = new List<DatStatistics>
{
List<DatStatistics> statsList =
[
datFile.Items.DatStatistics,
//datFile.ItemsDB.DatStatistics,
};
];
var consoleOutput = BaseReport.Create(StatReportFormat.None, statsList);
consoleOutput!.WriteToFile(null, true, true);
}

View File

@@ -459,7 +459,7 @@ namespace SabreTools.FileTypes.Archives
// Map all inputs to index
Dictionary<string, int> inputIndexMap = [];
var oldZipFileContents = new List<string>();
List<string> oldZipFileContents = [];
for (int i = 0; i < oldZipFile.LocalFilesCount(); i++)
{
oldZipFileContents.Add(oldZipFile.GetLocalFile(i).Filename!);
@@ -675,7 +675,7 @@ namespace SabreTools.FileTypes.Archives
Dictionary<string, int> inputIndexMap = new();
for (int i = 0; i < inputFiles.Count; i++)
{
var oldZipFileContents = new List<string>();
List<string> oldZipFileContents = [];
for (int j = 0; j < oldZipFile.LocalFilesCount(); j++)
{
oldZipFileContents.Add(oldZipFile.GetLocalFile(j).Filename!);

View File

@@ -314,7 +314,7 @@ namespace SabreTools.FileTypes.Archives
if (Filename == null)
return null;
var found = new List<BaseFile>();
List<BaseFile> found = [];
string? gamename = Path.GetFileNameWithoutExtension(Filename);
try
@@ -492,7 +492,7 @@ namespace SabreTools.FileTypes.Archives
if (zf == null)
throw new Exception($"Could not open {Filename} as a zip file");
var zipEntries = new List<(string, bool)>();
List<(string, bool)> zipEntries = [];
for (int i = 0; i < zf.Entries.Count; i++)
{
// Get the local file
@@ -638,7 +638,7 @@ namespace SabreTools.FileTypes.Archives
// Map all inputs to index
Dictionary<string, int> inputIndexMap = new();
var oldZipFileContents = new List<string>();
List<string> oldZipFileContents = [];
for (int i = 0; i < oldZipFile.LocalFilesCount(); i++)
{
oldZipFileContents.Add(oldZipFile.GetLocalFile(i).Filename!);
@@ -850,7 +850,7 @@ namespace SabreTools.FileTypes.Archives
Dictionary<string, int> inputIndexMap = new();
for (int i = 0; i < inputFiles.Count; i++)
{
var oldZipFileContents = new List<string>();
List<string> oldZipFileContents = [];
for (int j = 0; j < oldZipFile.LocalFilesCount(); j++)
{
oldZipFileContents.Add(oldZipFile.GetLocalFile(j).Filename!);