Force source index for IDDB AddItem

This commit is contained in:
Matt Nadareski
2024-03-20 01:29:59 -04:00
parent b48fbcc04d
commit abcfb44455
3 changed files with 199 additions and 74 deletions

View File

@@ -179,7 +179,7 @@ namespace SabreTools.DatFiles
/// <param name="machineIndex">Index of the machine related to the item</param> /// <param name="machineIndex">Index of the machine related to the item</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise (default)</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise (default)</param>
/// <returns>The index for the added item, -1 on error</returns> /// <returns>The index for the added item, -1 on error</returns>
public long AddItem(DatItem item, long machineIndex, long sourceIndex = -1, bool statsOnly = false) public long AddItem(DatItem item, long machineIndex, long sourceIndex, bool statsOnly = false)
{ {
// If we have a Disk, Media, or Rom, clean the hash data // If we have a Disk, Media, or Rom, clean the hash data
if (item is Disk disk) if (item is Disk disk)
@@ -1551,6 +1551,9 @@ namespace SabreTools.DatFiles
if (items == null || items.Length == 0) if (items == null || items.Length == 0)
continue; continue;
// Get the source for the first item
var source = GetSourceForItem(items[0].Item1);
// Get the machine for the first item // Get the machine for the first item
var machine = GetMachineForItem(items[0].Item1); var machine = GetMachineForItem(items[0].Item1);
if (machine.Item2 == null) if (machine.Item2 == null)
@@ -1579,7 +1582,7 @@ namespace SabreTools.DatFiles
{ {
DatItem datItem = (item.Item2.Clone() as DatItem)!; DatItem datItem = (item.Item2.Clone() as DatItem)!;
if (!items.Where(i => i.Item2.GetName() == datItem.GetName()).Any() && !items.Any(i => i.Item2 == datItem)) if (!items.Where(i => i.Item2.GetName() == datItem.GetName()).Any() && !items.Any(i => i.Item2 == datItem))
AddItem(datItem, machine.Item1); AddItem(datItem, machine.Item1, source.Item1);
} }
} }
} }
@@ -1602,6 +1605,9 @@ namespace SabreTools.DatFiles
if (items == null || items.Length == 0) if (items == null || items.Length == 0)
continue; continue;
// Get the source for the first item
var source = GetSourceForItem(items[0].Item1);
// Get the machine for the first item // Get the machine for the first item
var machine = GetMachineForItem(items[0].Item1); var machine = GetMachineForItem(items[0].Item1);
if (machine.Item2 == null) if (machine.Item2 == null)
@@ -1667,7 +1673,7 @@ namespace SabreTools.DatFiles
// Clone the item and then add it // Clone the item and then add it
DatItem datItem = (item.Item2.Clone() as DatItem)!; DatItem datItem = (item.Item2.Clone() as DatItem)!;
AddItem(datItem, machine.Item1); AddItem(datItem, machine.Item1, source.Item1);
} }
} }
} }
@@ -1679,7 +1685,7 @@ namespace SabreTools.DatFiles
{ {
var deviceRef = new DeviceRef(); var deviceRef = new DeviceRef();
deviceRef.SetName(deviceReference); deviceRef.SetName(deviceReference);
AddItem(deviceRef, machine.Item1); AddItem(deviceRef, machine.Item1, source.Item1);
} }
} }
} }
@@ -1724,7 +1730,7 @@ namespace SabreTools.DatFiles
// Clone the item and then add it // Clone the item and then add it
DatItem datItem = (item.Item2.Clone() as DatItem)!; DatItem datItem = (item.Item2.Clone() as DatItem)!;
AddItem(datItem, machine.Item1); AddItem(datItem, machine.Item1, source.Item1);
} }
} }
} }
@@ -1740,7 +1746,7 @@ namespace SabreTools.DatFiles
var slotItem = new Slot(); var slotItem = new Slot();
slotItem.SetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey, [slotOptionItem]); slotItem.SetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey, [slotOptionItem]);
AddItem(slotItem, machine.Item1); AddItem(slotItem, machine.Item1, source.Item1);
} }
} }
} }
@@ -1762,6 +1768,9 @@ namespace SabreTools.DatFiles
if (items == null || items.Length == 0) if (items == null || items.Length == 0)
continue; continue;
// Get the source for the first item
var source = GetSourceForItem(items[0].Item1);
// Get the machine for the first item in the list // Get the machine for the first item in the list
var machine = GetMachineForItem(items[0].Item1); var machine = GetMachineForItem(items[0].Item1);
if (machine.Item2 == null) if (machine.Item2 == null)
@@ -1792,7 +1801,7 @@ namespace SabreTools.DatFiles
if (!items.Where(i => i.Item2.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant()).Any() if (!items.Where(i => i.Item2.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant()).Any()
&& !items.Any(i => i.Item2 == datItem)) && !items.Any(i => i.Item2 == datItem))
{ {
AddItem(datItem, machine.Item1); AddItem(datItem, machine.Item1, source.Item1);
} }
} }

View File

@@ -3,6 +3,8 @@ using System.IO;
using System.Linq; using System.Linq;
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.VisualBasic;
#endif #endif
using SabreTools.Core; using SabreTools.Core;
using SabreTools.DatFiles; using SabreTools.DatFiles;
@@ -641,10 +643,20 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = dupeData.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -667,8 +679,9 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
#if NETFRAMEWORK #if NETFRAMEWORK
if ((item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.External) != 0) if ((item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.External) != 0)
@@ -683,7 +696,7 @@ namespace SabreTools.DatTools
continue; continue;
#endif #endif
dupeData.ItemsDB.AddItem(newrom, machineRemapping[machineIndex], statsOnly: false); dupeData.ItemsDB.AddItem(newrom, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
@@ -855,10 +868,25 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = outDats[0].ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
for (int i = 1; i < outDats.Count; i++)
{
_ = outDats[i].ItemsDB.AddSource(source.Value);
}
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -881,8 +909,9 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
if (item.Value.GetFieldValue<Source?>(DatItem.SourceKey) == null) if (item.Value.GetFieldValue<Source?>(DatItem.SourceKey) == null)
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
@@ -896,7 +925,7 @@ namespace SabreTools.DatTools
#else #else
if (item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.Internal) || item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) == 0x00) if (item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.Internal) || item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) == 0x00)
#endif #endif
outDats[item.Value.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); outDats[item.Value.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -1030,10 +1059,20 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = outerDiffData.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -1056,8 +1095,9 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
#if NETFRAMEWORK #if NETFRAMEWORK
if ((item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.Internal) != 0 || item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) == 0x00) if ((item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.Internal) != 0 || item.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) == 0x00)
@@ -1073,7 +1113,7 @@ namespace SabreTools.DatTools
#endif #endif
newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) + $" ({Path.GetFileNameWithoutExtension(inputs[newrom.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].CurrentPath)})"); newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) + $" ({Path.GetFileNameWithoutExtension(inputs[newrom.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].CurrentPath)})");
outerDiffData.ItemsDB.AddItem(newrom, machineRemapping[machineIndex], statsOnly: false); outerDiffData.ItemsDB.AddItem(newrom, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
@@ -1178,10 +1218,20 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = addFrom.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = addFrom.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = addFrom.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = addTo.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -1199,9 +1249,11 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
addTo.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); long sourceIndex = itemSourceMappings[item.Key];
addTo.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
// Now remove the key from the source DAT // Now remove the key from the source DAT
if (delete) if (delete)
@@ -1270,10 +1322,20 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = indexDat.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -1291,11 +1353,12 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
if (item.Value.GetFieldValue<Source?>(DatItem.SourceKey) != null && item.Value.GetFieldValue<Source?>(DatItem.SourceKey)!.Index == index) if (item.Value.GetFieldValue<Source?>(DatItem.SourceKey) != null && item.Value.GetFieldValue<Source?>(DatItem.SourceKey)!.Index == index)
indexDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); indexDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else

View File

@@ -145,10 +145,21 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = extADat.ItemsDB.AddSource(source.Value);
_ = extBDat.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -167,21 +178,22 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
if (newExtA.Contains((item.Value.GetName() ?? string.Empty).GetNormalizedExtension())) if (newExtA.Contains((item.Value.GetName() ?? string.Empty).GetNormalizedExtension()))
{ {
extADat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); extADat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
} }
else if (newExtB.Contains((item.Value.GetName() ?? string.Empty).GetNormalizedExtension())) else if (newExtB.Contains((item.Value.GetName() ?? string.Empty).GetNormalizedExtension()))
{ {
extBDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); extBDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
} }
else else
{ {
extADat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); extADat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
extBDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); extBDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
@@ -383,10 +395,27 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = fieldDats[Models.Metadata.Rom.StatusKey].ItemsDB.AddSource(source.Value);
_ = fieldDats[Models.Metadata.Rom.SHA512Key].ItemsDB.AddSource(source.Value);
_ = fieldDats[Models.Metadata.Rom.SHA384Key].ItemsDB.AddSource(source.Value);
_ = fieldDats[Models.Metadata.Rom.SHA256Key].ItemsDB.AddSource(source.Value);
_ = fieldDats[Models.Metadata.Rom.SHA1Key].ItemsDB.AddSource(source.Value);
_ = fieldDats[Models.Metadata.Rom.MD5Key].ItemsDB.AddSource(source.Value);
_ = fieldDats[Models.Metadata.Rom.CRCKey].ItemsDB.AddSource(source.Value);
_ = fieldDats["null"].ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -411,53 +440,54 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
// Only process Disk, Media, and Rom // Only process Disk, Media, and Rom
switch (item.Value) switch (item.Value)
{ {
case Disk disk: case Disk disk:
if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump) if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
fieldDats[Models.Metadata.Disk.StatusKey].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Disk.StatusKey].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key))) else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key)))
fieldDats[Models.Metadata.Disk.SHA1Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Disk.SHA1Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))) else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key)))
fieldDats[Models.Metadata.Disk.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Disk.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))) else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key)))
fieldDats[Models.Metadata.Disk.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Disk.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else else
fieldDats["null"].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats["null"].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
break; break;
case Media media: case Media media:
if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key))) if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key)))
fieldDats[Models.Metadata.Media.SHA256Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Media.SHA256Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))) else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key)))
fieldDats[Models.Metadata.Media.SHA1Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Media.SHA1Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key))) else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key)))
fieldDats[Models.Metadata.Media.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Media.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else else
fieldDats["null"].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats["null"].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
break; break;
case Rom rom: case Rom rom:
if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump) if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
fieldDats[Models.Metadata.Rom.StatusKey].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.StatusKey].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA512Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA512Key)))
fieldDats[Models.Metadata.Rom.SHA512Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.SHA512Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA384Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA384Key)))
fieldDats[Models.Metadata.Rom.SHA384Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.SHA384Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA256Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA256Key)))
fieldDats[Models.Metadata.Rom.SHA256Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.SHA256Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key)))
fieldDats[Models.Metadata.Rom.SHA1Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.SHA1Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key)))
fieldDats[Models.Metadata.Rom.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.MD5Key].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey)))
fieldDats[Models.Metadata.Rom.CRCKey].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats[Models.Metadata.Rom.CRCKey].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
else else
fieldDats["null"].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); fieldDats["null"].ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
break; break;
default: default:
@@ -695,10 +725,21 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = lessThan.ItemsDB.AddSource(source.Value);
_ = greaterThan.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -717,24 +758,25 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
// If the file is not a Rom, it automatically goes in the "lesser" dat // If the file is not a Rom, it automatically goes in the "lesser" dat
if (item.Value is not Rom rom) if (item.Value is not Rom rom)
lessThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); lessThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
// If the file is a Rom and has no size, put it in the "lesser" dat // If the file is a Rom and has no size, put it in the "lesser" dat
else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) == null) else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) == null)
lessThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); lessThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
// If the file is a Rom and less than the radix, put it in the "lesser" dat // If the file is a Rom and less than the radix, put it in the "lesser" dat
else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) < radix) else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) < radix)
lessThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); lessThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
// If the file is a Rom and greater than or equal to the radix, put it in the "greater" dat // If the file is a Rom and greater than or equal to the radix, put it in the "greater" dat
else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) >= radix) else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) >= radix)
greaterThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); greaterThan.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -939,10 +981,20 @@ namespace SabreTools.DatTools
// Get all current items, machines, and mappings // Get all current items, machines, and mappings
var datItems = datFile.ItemsDB.GetItems().ToDictionary(m => m.Item1, m => m.Item2); 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 machines = datFile.ItemsDB.GetMachines().ToDictionary(m => m.Item1, m => m.Item2);
var mappings = datFile.ItemsDB.GetItemMachineMappings().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);
// Create a mapping from old machine index to new machine index // Create mappings from old index to new index
var machineRemapping = new Dictionary<long, long>(); var machineRemapping = new Dictionary<long, long>();
var sourceRemapping = new Dictionary<long, long>();
// Loop through and add all sources
foreach (var source in sources)
{
long newSourceIndex = indexDat.ItemsDB.AddSource(source.Value);
sourceRemapping[source.Key] = newSourceIndex;
}
// Loop through and add all machines // Loop through and add all machines
foreach (var machine in machines) foreach (var machine in machines)
@@ -960,11 +1012,12 @@ namespace SabreTools.DatTools
foreach (var item in datItems) foreach (var item in datItems)
#endif #endif
{ {
// Get the machine index for this item // Get the machine and source index for this item
long machineIndex = mappings[item.Key]; long machineIndex = itemMachineMappings[item.Key];
long sourceIndex = itemSourceMappings[item.Key];
if (item.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>() == itemType) if (item.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>() == itemType)
indexDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], statsOnly: false); indexDat.ItemsDB.AddItem(item.Value, machineRemapping[machineIndex], sourceRemapping[sourceIndex], statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else