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

@@ -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);
}