From 2884dcab5ba1ac8174456fc6bab68014bf987549 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 16 Apr 2026 16:11:25 -0400 Subject: [PATCH] Remove need for GetMachineForItem --- .../DatFileTests.Filtering.cs | 4 +-- .../DatFileTests.Splitting.cs | 9 +++--- .../ItemDatabaseTests.cs | 27 +--------------- .../DatFile.Filtering.cs | 2 +- .../DatFile.Splitting.cs | 30 +++++++++--------- .../DatFile.ToMetadata.cs | 2 +- SabreTools.Metadata.DatFiles/DatFile.cs | 10 ++---- .../Formats/Missfile.cs | 6 ++-- .../Formats/SabreJSON.cs | 4 +-- .../Formats/SabreXML.cs | 4 +-- SabreTools.Metadata.DatFiles/ItemDatabase.cs | 31 +++++-------------- 11 files changed, 42 insertions(+), 87 deletions(-) diff --git a/SabreTools.Metadata.DatFiles.Test/DatFileTests.Filtering.cs b/SabreTools.Metadata.DatFiles.Test/DatFileTests.Filtering.cs index 4584b59f..36a568e6 100644 --- a/SabreTools.Metadata.DatFiles.Test/DatFileTests.Filtering.cs +++ b/SabreTools.Metadata.DatFiles.Test/DatFileTests.Filtering.cs @@ -205,12 +205,12 @@ namespace SabreTools.Metadata.DatFiles.Test Assert.Equal(2, actualDatItems.Count); var actualRom = Assert.Single(actualDatItems, i => i.Value is Rom); - var actualRomMachine = datFile.GetMachineForItemDB(actualRom.Key); + var actualRomMachine = datFile.GetMachineDB(actualRom.Value.MachineIndex); Assert.NotNull(actualRomMachine.Value); Assert.Equal("machine/rom", actualRomMachine.Value.Name); var actualDisk = Assert.Single(actualDatItems, i => i.Value is Disk); - var actualDiskMachine = datFile.GetMachineForItemDB(actualDisk.Key); + var actualDiskMachine = datFile.GetMachineDB(actualDisk.Value.MachineIndex); Assert.NotNull(actualDiskMachine.Value); Assert.Equal("machine/disk", actualDiskMachine.Value.Name); } diff --git a/SabreTools.Metadata.DatFiles.Test/DatFileTests.Splitting.cs b/SabreTools.Metadata.DatFiles.Test/DatFileTests.Splitting.cs index 10f3293e..29fb3b47 100644 --- a/SabreTools.Metadata.DatFiles.Test/DatFileTests.Splitting.cs +++ b/SabreTools.Metadata.DatFiles.Test/DatFileTests.Splitting.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using SabreTools.Metadata.DatFiles.Formats; using SabreTools.Metadata.DatItems; using SabreTools.Metadata.DatItems.Formats; @@ -845,8 +846,8 @@ namespace SabreTools.Metadata.DatFiles.Test datFile.RemoveItemsFromCloneOfChild(); Assert.Single(datFile.GetItemsForBucketDB("parent")); - long actual = Assert.Single(datFile.GetItemsForBucketDB("child")).Key; - Machine? actualMachine = datFile.GetMachineForItemDB(actual).Value; + KeyValuePair actual = Assert.Single(datFile.GetItemsForBucketDB("child")); + Machine? actualMachine = datFile.GetMachineDB(actual.Value.MachineIndex).Value; Assert.NotNull(actualMachine); Assert.Equal("child", actualMachine.Name); Assert.Equal("romof", actualMachine.RomOf); @@ -975,8 +976,8 @@ namespace SabreTools.Metadata.DatFiles.Test datFile.RemoveItemsFromRomOfChild(); Assert.Single(datFile.GetItemsForBucketDB("parent")); - long actual = Assert.Single(datFile.GetItemsForBucketDB("child")).Key; - Machine? actualMachine = datFile.GetMachineForItemDB(actual).Value; + KeyValuePair actual = Assert.Single(datFile.GetItemsForBucketDB("child")); + Machine? actualMachine = datFile.GetMachineDB(actual.Value.MachineIndex).Value; Assert.NotNull(actualMachine); Assert.Equal("child", actualMachine.Name); } diff --git a/SabreTools.Metadata.DatFiles.Test/ItemDatabaseTests.cs b/SabreTools.Metadata.DatFiles.Test/ItemDatabaseTests.cs index 2999fc56..ddc19bf7 100644 --- a/SabreTools.Metadata.DatFiles.Test/ItemDatabaseTests.cs +++ b/SabreTools.Metadata.DatFiles.Test/ItemDatabaseTests.cs @@ -524,31 +524,6 @@ namespace SabreTools.Metadata.DatFiles.Test #endregion - #region GetMachineForItem - - [Fact] - public void GetMachineForItemTest() - { - Source source = new Source(0, source: null); - Machine machine = new Machine(); - DatItem item = new Rom(); - - var dict = new ItemDatabase(); - - long sourceIndex = dict.AddSource(source); - item.SourceIndex = sourceIndex; - long machineIndex = dict.AddMachine(machine); - item.MachineIndex = machineIndex; - - long itemIndex = dict.AddItem(item, statsOnly: false); - - var actual = dict.GetMachineForItem(itemIndex); - Assert.Equal(0, actual.Key); - Assert.NotNull(actual.Value); - } - - #endregion - #region GetSource [Fact] @@ -560,7 +535,7 @@ namespace SabreTools.Metadata.DatFiles.Test Assert.Equal(0, sourceIndex); var actual = dict.GetSource(sourceIndex); - Assert.NotNull(actual); + Assert.NotNull(actual.Value); } #endregion diff --git a/SabreTools.Metadata.DatFiles/DatFile.Filtering.cs b/SabreTools.Metadata.DatFiles/DatFile.Filtering.cs index 95ae6cf5..2012634c 100644 --- a/SabreTools.Metadata.DatFiles/DatFile.Filtering.cs +++ b/SabreTools.Metadata.DatFiles/DatFile.Filtering.cs @@ -589,7 +589,7 @@ namespace SabreTools.Metadata.DatFiles return; // Get the current machine - var machine = GetMachineForItemDB(datItem.Key); + var machine = GetMachineDB(datItem.Value.MachineIndex); if (machine.Value is null) return; diff --git a/SabreTools.Metadata.DatFiles/DatFile.Splitting.cs b/SabreTools.Metadata.DatFiles/DatFile.Splitting.cs index 6d4bc291..383658ee 100644 --- a/SabreTools.Metadata.DatFiles/DatFile.Splitting.cs +++ b/SabreTools.Metadata.DatFiles/DatFile.Splitting.cs @@ -403,7 +403,7 @@ namespace SabreTools.Metadata.DatFiles continue; // Get the machine for the first item - var machine = GetMachineForItemDB(items.First().Key); + var machine = GetMachineDB(items.First().Value.MachineIndex); if (machine.Value is null) continue; @@ -428,7 +428,7 @@ namespace SabreTools.Metadata.DatFiles if (parentItems.Count == 0) continue; - string? machineName = GetMachineForItemDB(item.Key).Value?.Name; + string? machineName = GetMachineDB(item.Value.MachineIndex).Value?.Name; // Special disk handling if (item.Value is Disk disk) @@ -591,7 +591,7 @@ namespace SabreTools.Metadata.DatFiles var source = ItemsDB.GetSource(items.First().Value.SourceIndex); // Get the machine for the first item in the list - var machine = GetMachineForItemDB(items.First().Key); + var machine = GetMachineDB(items.First().Value.MachineIndex); if (machine.Value is null) continue; @@ -620,16 +620,16 @@ namespace SabreTools.Metadata.DatFiles } // Get the parent machine - var parentMachine = GetMachineForItemDB(GetItemsForBucketDB(cloneOf).First().Key); + var parentMachine = GetMachineDB(GetItemsForBucketDB(cloneOf).First().Value.MachineIndex); if (parentMachine.Value is null) continue; // Now we want to get the parent romof tag and put it in each of the items items = GetItemsForBucketDB(bucket); string? romof = parentMachine.Value.RomOf; - foreach (var key in items.Keys) + foreach (var kvp in items) { - var itemMachine = GetMachineForItemDB(key); + var itemMachine = GetMachineDB(kvp.Value.MachineIndex); if (itemMachine.Value is null) continue; @@ -815,7 +815,7 @@ namespace SabreTools.Metadata.DatFiles var source = ItemsDB.GetSource(firstItem.Value.SourceIndex); // Get the machine for the first item - var machine = GetMachineForItemDB(firstItem.Key); + var machine = GetMachineDB(firstItem.Value.MachineIndex); if (machine.Value is null) continue; @@ -863,7 +863,7 @@ namespace SabreTools.Metadata.DatFiles .Select(i => (i as DeviceRef)!.Name!)); // Set new machine information and add to the current machine - var copyFrom = GetMachineForItemDB(items.First().Key); + var copyFrom = GetMachineDB(items.First().Value.MachineIndex); if (copyFrom.Value is null) continue; @@ -927,7 +927,7 @@ namespace SabreTools.Metadata.DatFiles .Select(o => o.DevName!)); // Set new machine information and add to the current machine - var copyFrom = GetMachineForItemDB(GetItemsForBucketDB(bucket).First().Key); + var copyFrom = GetMachineDB(GetItemsForBucketDB(bucket).First().Value.MachineIndex); if (copyFrom.Value is null) continue; @@ -1039,7 +1039,7 @@ namespace SabreTools.Metadata.DatFiles var source = ItemsDB.GetSource(items.First().Value.SourceIndex); // Get the machine for the first item - var machine = GetMachineForItemDB(items.First().Key); + var machine = GetMachineDB(items.First().Value.MachineIndex); if (machine.Value is null) continue; @@ -1137,7 +1137,7 @@ namespace SabreTools.Metadata.DatFiles #endif // Get the machine - var machine = GetMachineForItemDB(items.First().Key); + var machine = GetMachineDB(items.First().Value.MachineIndex); if (machine.Value is null) #if NET40_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER return; @@ -1235,7 +1235,7 @@ namespace SabreTools.Metadata.DatFiles continue; // Get the machine for the first item - var machine = GetMachineForItemDB(items.First().Key); + var machine = GetMachineDB(items.First().Value.MachineIndex); if (machine.Value is null) continue; @@ -1257,14 +1257,14 @@ namespace SabreTools.Metadata.DatFiles // Now we want to get the parent romof tag and put it in each of the remaining items items = GetItemsForBucketDB(bucket); - machine = GetMachineForItemDB(GetItemsForBucketDB(cloneOf).First().Key); + machine = GetMachineDB(GetItemsForBucketDB(cloneOf).First().Value.MachineIndex); if (machine.Value is null) continue; string? romof = machine.Value.RomOf; foreach (var item in items) { - machine = GetMachineForItemDB(item.Key); + machine = GetMachineDB(item.Value.MachineIndex); if (machine.Value is null) continue; @@ -1338,7 +1338,7 @@ namespace SabreTools.Metadata.DatFiles continue; // Get the machine for the item - var machine = GetMachineForItemDB(items.First().Key); + var machine = GetMachineDB(items.First().Value.MachineIndex); if (machine.Value is null) continue; diff --git a/SabreTools.Metadata.DatFiles/DatFile.ToMetadata.cs b/SabreTools.Metadata.DatFiles/DatFile.ToMetadata.cs index 015baa45..12abd742 100644 --- a/SabreTools.Metadata.DatFiles/DatFile.ToMetadata.cs +++ b/SabreTools.Metadata.DatFiles/DatFile.ToMetadata.cs @@ -424,7 +424,7 @@ namespace SabreTools.Metadata.DatFiles continue; // Create a machine to hold everything - var machine = GetMachineForItemDB(items.First().Key).Value!.GetInternalClone(); + var machine = GetMachineDB(items.First().Value.MachineIndex).Value!.GetInternalClone(); // Create mapping dictionaries for the Parts, DataAreas, and DiskAreas associated with this machine Dictionary partMappings = []; diff --git a/SabreTools.Metadata.DatFiles/DatFile.cs b/SabreTools.Metadata.DatFiles/DatFile.cs index 0b749ceb..364fbbb9 100644 --- a/SabreTools.Metadata.DatFiles/DatFile.cs +++ b/SabreTools.Metadata.DatFiles/DatFile.cs @@ -277,12 +277,6 @@ namespace SabreTools.Metadata.DatFiles public IDictionary GetMachinesDB() => ItemsDB.GetMachines(); - /// - /// Get the index and machine associated with an item index - /// - public KeyValuePair GetMachineForItemDB(long itemIndex) - => ItemsDB.GetMachineForItem(itemIndex); - /// /// Get the index and source associated with a source index /// @@ -1215,8 +1209,8 @@ namespace SabreTools.Metadata.DatFiles } // If machine names don't match - string? xMachineName = ItemsDB.GetMachineForItem(x.Key).Value?.Name; - string? yMachineName = ItemsDB.GetMachineForItem(y.Key).Value?.Name; + string? xMachineName = ItemsDB.GetMachine(x.Value.MachineIndex).Value?.Name; + string? yMachineName = ItemsDB.GetMachine(y.Value.MachineIndex).Value?.Name; if (xMachineName != yMachineName) return nc.Compare(xMachineName, yMachineName); diff --git a/SabreTools.Metadata.DatFiles/Formats/Missfile.cs b/SabreTools.Metadata.DatFiles/Formats/Missfile.cs index d05b50a0..95e799e4 100644 --- a/SabreTools.Metadata.DatFiles/Formats/Missfile.cs +++ b/SabreTools.Metadata.DatFiles/Formats/Missfile.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -142,7 +142,7 @@ namespace SabreTools.Metadata.DatFiles.Formats var datItem = new KeyValuePair(kvp.Key, ProcessNullifiedItem(kvp.Value)); // Get the machine for the item - var machine = GetMachineForItemDB(datItem.Key); + var machine = GetMachineDB(datItem.Value.MachineIndex); // Write out the item if we're using machine names or we're not ignoring if (!Modifiers.UseRomName || !ShouldIgnore(datItem.Value, ignoreblanks)) @@ -186,7 +186,7 @@ namespace SabreTools.Metadata.DatFiles.Formats /// The name of the last game to be output private void WriteDatItemDB(StreamWriter sw, KeyValuePair datItem, string? lastgame) { - var machine = GetMachineForItemDB(datItem.Key).Value; + var machine = GetMachineDB(datItem.Value.MachineIndex).Value; WriteDatItemImpl(sw, datItem.Value, machine!, lastgame); } diff --git a/SabreTools.Metadata.DatFiles/Formats/SabreJSON.cs b/SabreTools.Metadata.DatFiles/Formats/SabreJSON.cs index 58fa7251..08548456 100644 --- a/SabreTools.Metadata.DatFiles/Formats/SabreJSON.cs +++ b/SabreTools.Metadata.DatFiles/Formats/SabreJSON.cs @@ -524,7 +524,7 @@ namespace SabreTools.Metadata.DatFiles.Formats foreach (var kvp in items) { // Get the machine for the item - var machine = GetMachineForItemDB(kvp.Key); + var machine = GetMachineDB(kvp.Value.MachineIndex); // If we have a different game and we're not at the start of the list, output the end of last item if (lastgame is not null && !string.Equals(lastgame, machine.Value!.Name, StringComparison.OrdinalIgnoreCase)) @@ -656,7 +656,7 @@ namespace SabreTools.Metadata.DatFiles.Formats private void WriteDatItemDB(JsonTextWriter jtw, KeyValuePair datItem) { // Get the machine for the item - var machine = GetMachineForItemDB(datItem.Key); + var machine = GetMachineDB(datItem.Value.MachineIndex); // Pre-process the item name ProcessItemName(datItem.Value, machine.Value, forceRemoveQuotes: true, forceRomName: false); diff --git a/SabreTools.Metadata.DatFiles/Formats/SabreXML.cs b/SabreTools.Metadata.DatFiles/Formats/SabreXML.cs index fb7f8c2f..4f65296a 100644 --- a/SabreTools.Metadata.DatFiles/Formats/SabreXML.cs +++ b/SabreTools.Metadata.DatFiles/Formats/SabreXML.cs @@ -364,7 +364,7 @@ namespace SabreTools.Metadata.DatFiles.Formats foreach (var kvp in items) { // Get the machine for the item - var machine = GetMachineForItemDB(kvp.Key); + var machine = GetMachineDB(kvp.Value.MachineIndex); // If we have a different game and we're not at the start of the list, output the end of last item if (lastgame is not null && !string.Equals(lastgame, machine.Value!.Name, StringComparison.OrdinalIgnoreCase)) @@ -491,7 +491,7 @@ namespace SabreTools.Metadata.DatFiles.Formats private void WriteDatItemDB(XmlTextWriter xtw, KeyValuePair datItem) { // Get the machine for the item - var machine = GetMachineForItemDB(datItem.Key); + var machine = GetMachineDB(datItem.Value.MachineIndex); // Pre-process the item name ProcessItemName(datItem.Value, machine.Value, forceRemoveQuotes: true, forceRomName: false); diff --git a/SabreTools.Metadata.DatFiles/ItemDatabase.cs b/SabreTools.Metadata.DatFiles/ItemDatabase.cs index cf7f0aa7..bace7932 100644 --- a/SabreTools.Metadata.DatFiles/ItemDatabase.cs +++ b/SabreTools.Metadata.DatFiles/ItemDatabase.cs @@ -382,21 +382,6 @@ namespace SabreTools.Metadata.DatFiles return new KeyValuePair(machine.Key, machine.Value); } - /// - /// Get the index and machine associated with an item index - /// - public KeyValuePair GetMachineForItem(long itemIndex) - { - if (!_items.TryGetValue(itemIndex, out var item)) - return new KeyValuePair(-1, null); - - long machineIndex = item.MachineIndex; - if (!_machines.TryGetValue(machineIndex, out var machine)) - return new KeyValuePair(-1, null); - - return new KeyValuePair(machineIndex, machine); - } - /// /// Get all machines and their indicies /// @@ -607,9 +592,9 @@ namespace SabreTools.Metadata.DatFiles return output; // Get the machines for comparison - var selfMachine = GetMachineForItem(selfItem.Value.Key).Value; + var selfMachine = GetMachine(selfItem.Value.Value.MachineIndex).Value; string? selfMachineName = selfMachine?.Name; - var lastMachine = GetMachineForItem(lastItem.Value.Key).Value; + var lastMachine = GetMachine(lastItem.Value.Value.MachineIndex).Value; string? lastMachineName = lastMachine?.Name; // If the duplicate is external already @@ -655,7 +640,7 @@ namespace SabreTools.Metadata.DatFiles // We want to get the proper key for the DatItem, ignoring the index _ = SortAndGetKey(datItem, sorted); - var machine = GetMachineForItem(datItem.Key); + var machine = GetMachine(datItem.Value.MachineIndex); var source = GetSource(datItem.Value.SourceIndex); string key = datItem.Value.GetKey(_bucketedBy, machine.Value, source.Value); @@ -698,7 +683,7 @@ namespace SabreTools.Metadata.DatFiles // We want to get the proper key for the DatItem, ignoring the index _ = SortAndGetKey(datItem, sorted); - var machine = GetMachineForItem(datItem.Key); + var machine = GetMachine(datItem.Value.MachineIndex); var source = GetSource(datItem.Value.SourceIndex); string key = datItem.Value.GetKey(_bucketedBy, machine.Value, source.Value); @@ -792,8 +777,8 @@ namespace SabreTools.Metadata.DatFiles var itemSource = GetSource(datItem.SourceIndex); // Get the machines associated with the items - var savedMachine = GetMachineForItem(savedIndex); - var itemMachine = GetMachineForItem(itemIndex); + var savedMachine = GetMachine(savedItem.MachineIndex); + var itemMachine = GetMachine(datItem.MachineIndex); // If the current source has a lower ID than the saved, use the saved source if (itemSource.Value?.Index < savedSource.Value?.Index) @@ -1038,8 +1023,8 @@ namespace SabreTools.Metadata.DatFiles } // Get the machines - Machine? xMachine = GetMachineForItem(x.Key).Value; - Machine? yMachine = GetMachineForItem(y.Key).Value; + Machine? xMachine = GetMachine(x.Value.MachineIndex).Value; + Machine? yMachine = GetMachine(y.Value.MachineIndex).Value; // If machine names don't match string? xMachineName = xMachine?.Name;