Remove need for GetMachineForItem

This commit is contained in:
Matt Nadareski
2026-04-16 16:11:25 -04:00
parent ff69b83e05
commit 2884dcab5b
11 changed files with 42 additions and 87 deletions

View File

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

View File

@@ -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<long, DatItem> 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<long, DatItem> actual = Assert.Single(datFile.GetItemsForBucketDB("child"));
Machine? actualMachine = datFile.GetMachineDB(actual.Value.MachineIndex).Value;
Assert.NotNull(actualMachine);
Assert.Equal("child", actualMachine.Name);
}

View File

@@ -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

View File

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

View File

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

View File

@@ -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<Data.Models.Metadata.Part, Data.Models.Metadata.DatItem> partMappings = [];

View File

@@ -277,12 +277,6 @@ namespace SabreTools.Metadata.DatFiles
public IDictionary<long, Machine> GetMachinesDB()
=> ItemsDB.GetMachines();
/// <summary>
/// Get the index and machine associated with an item index
/// </summary>
public KeyValuePair<long, Machine?> GetMachineForItemDB(long itemIndex)
=> ItemsDB.GetMachineForItem(itemIndex);
/// <summary>
/// Get the index and source associated with a source index
/// </summary>
@@ -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);

View File

@@ -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<long, DatItem>(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
/// <param name="lastgame">The name of the last game to be output</param>
private void WriteDatItemDB(StreamWriter sw, KeyValuePair<long, DatItem> datItem, string? lastgame)
{
var machine = GetMachineForItemDB(datItem.Key).Value;
var machine = GetMachineDB(datItem.Value.MachineIndex).Value;
WriteDatItemImpl(sw, datItem.Value, machine!, lastgame);
}

View File

@@ -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<long, DatItem> 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);

View File

@@ -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<long, DatItem> 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);

View File

@@ -382,21 +382,6 @@ namespace SabreTools.Metadata.DatFiles
return new KeyValuePair<long, Machine?>(machine.Key, machine.Value);
}
/// <summary>
/// Get the index and machine associated with an item index
/// </summary>
public KeyValuePair<long, Machine?> GetMachineForItem(long itemIndex)
{
if (!_items.TryGetValue(itemIndex, out var item))
return new KeyValuePair<long, Machine?>(-1, null);
long machineIndex = item.MachineIndex;
if (!_machines.TryGetValue(machineIndex, out var machine))
return new KeyValuePair<long, Machine?>(-1, null);
return new KeyValuePair<long, Machine?>(machineIndex, machine);
}
/// <summary>
/// Get all machines and their indicies
/// </summary>
@@ -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;