mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Make BaseReplace call both implementations
This commit is contained in:
@@ -17,6 +17,8 @@ namespace SabreTools.DatTools
|
|||||||
/// TODO: Add tests for BaseReplace methods
|
/// TODO: Add tests for BaseReplace methods
|
||||||
public static class Replacer
|
public static class Replacer
|
||||||
{
|
{
|
||||||
|
#region BaseReplace
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replace item values from the base set represented by the current DAT
|
/// Replace item values from the base set represented by the current DAT
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -35,8 +37,31 @@ namespace SabreTools.DatTools
|
|||||||
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.GetStringFieldValue(DatHeader.FileNameKey)}' from the base DAT");
|
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.GetStringFieldValue(DatHeader.FileNameKey)}' from the base DAT");
|
||||||
|
|
||||||
// If we are matching based on DatItem fields of any sort
|
// If we are matching based on DatItem fields of any sort
|
||||||
if (itemFieldNames.Count > 0)
|
BaseReplaceItemsImpl(datFile, intDat, itemFieldNames);
|
||||||
|
BaseReplaceItemsDBImpl(datFile, intDat, itemFieldNames);
|
||||||
|
|
||||||
|
// If we are matching based on Machine fields of any sort
|
||||||
|
BaseReplaceMachinesImpl(datFile, intDat, machineFieldNames, onlySame);
|
||||||
|
BaseReplaceMachinesDBImpl(datFile, intDat, machineFieldNames, onlySame);
|
||||||
|
|
||||||
|
watch.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Replace item values from the base set represented by the current DAT
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datFile">Current DatFile object to use for updating</param>
|
||||||
|
/// <param name="intDat">DatFile to replace the values in</param>
|
||||||
|
/// <param name="itemFieldNames">List of item field names representing what should be updated</param>
|
||||||
|
private static void BaseReplaceItemsImpl(
|
||||||
|
DatFile datFile,
|
||||||
|
DatFile intDat,
|
||||||
|
Dictionary<string, List<string>> itemFieldNames)
|
||||||
{
|
{
|
||||||
|
// Check for field names
|
||||||
|
if (itemFieldNames.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
// For comparison's sake, we want to use CRC as the base bucketing
|
// For comparison's sake, we want to use CRC as the base bucketing
|
||||||
datFile.BucketBy(ItemKey.CRC);
|
datFile.BucketBy(ItemKey.CRC);
|
||||||
datFile.Deduplicate();
|
datFile.Deduplicate();
|
||||||
@@ -68,7 +93,7 @@ namespace SabreTools.DatTools
|
|||||||
|
|
||||||
// Replace fields from the first duplicate, if we have one
|
// Replace fields from the first duplicate, if we have one
|
||||||
if (dupes.Count > 0)
|
if (dupes.Count > 0)
|
||||||
Replacer.ReplaceFields(newDatItem, dupes[0], itemFieldNames);
|
ReplaceFields(newDatItem, dupes[0], itemFieldNames);
|
||||||
|
|
||||||
newDatItems.Add(newDatItem);
|
newDatItems.Add(newDatItem);
|
||||||
}
|
}
|
||||||
@@ -83,77 +108,21 @@ namespace SabreTools.DatTools
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are matching based on Machine fields of any sort
|
|
||||||
if (machineFieldNames.Count > 0)
|
|
||||||
{
|
|
||||||
// For comparison's sake, we want to use Machine Name as the base bucketing
|
|
||||||
datFile.BucketBy(ItemKey.Machine);
|
|
||||||
datFile.Deduplicate();
|
|
||||||
intDat.BucketBy(ItemKey.Machine);
|
|
||||||
|
|
||||||
// Then we do a namewise comparison against the base DAT
|
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
|
||||||
Parallel.ForEach(intDat.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
|
|
||||||
#elif NET40_OR_GREATER
|
|
||||||
Parallel.ForEach(intDat.Items.SortedKeys, key =>
|
|
||||||
#else
|
|
||||||
foreach (var key in intDat.Items.SortedKeys)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
List<DatItem>? datItems = intDat.GetItemsForBucket(key);
|
|
||||||
if (datItems == null)
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
continue;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
List<DatItem> newDatItems = [];
|
|
||||||
foreach (DatItem datItem in datItems)
|
|
||||||
{
|
|
||||||
if (datItem.Clone() is not DatItem newDatItem)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var list = datFile.GetItemsForBucket(key);
|
|
||||||
if (list.Count > 0)
|
|
||||||
Replacer.ReplaceFields(newDatItem.GetFieldValue<Machine>(DatItem.MachineKey)!, list[index: 0].GetFieldValue<Machine>(DatItem.MachineKey)!, machineFieldNames, onlySame);
|
|
||||||
|
|
||||||
newDatItems.Add(newDatItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now add the new list to the key
|
|
||||||
intDat.RemoveBucket(key);
|
|
||||||
newDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
});
|
|
||||||
#else
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
watch.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replace item values from the base set represented by the current DAT
|
/// Replace item values from the base set represented by the current DAT
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="datFile">Current DatFile object to use for updating</param>
|
/// <param name="datFile">Current DatFile object to use for updating</param>
|
||||||
/// <param name="intDat">DatFile to replace the values in</param>
|
/// <param name="intDat">DatFile to replace the values in</param>
|
||||||
/// <param name="machineFieldNames">List of machine field names representing what should be updated</param>
|
|
||||||
/// <param name="itemFieldNames">List of item field names representing what should be updated</param>
|
/// <param name="itemFieldNames">List of item field names representing what should be updated</param>
|
||||||
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
|
private static void BaseReplaceItemsDBImpl(
|
||||||
public static void BaseReplaceDB(
|
|
||||||
DatFile datFile,
|
DatFile datFile,
|
||||||
DatFile intDat,
|
DatFile intDat,
|
||||||
List<string> machineFieldNames,
|
Dictionary<string, List<string>> itemFieldNames)
|
||||||
Dictionary<string, List<string>> itemFieldNames,
|
|
||||||
bool onlySame)
|
|
||||||
{
|
{
|
||||||
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.GetStringFieldValue(DatHeader.FileNameKey)}' from the base DAT");
|
// Check for field names
|
||||||
|
if (itemFieldNames.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
// If we are matching based on DatItem fields of any sort
|
|
||||||
if (itemFieldNames.Count > 0)
|
|
||||||
{
|
|
||||||
// For comparison's sake, we want to use CRC as the base bucketing
|
// For comparison's sake, we want to use CRC as the base bucketing
|
||||||
datFile.BucketBy(ItemKey.CRC);
|
datFile.BucketBy(ItemKey.CRC);
|
||||||
datFile.Deduplicate();
|
datFile.Deduplicate();
|
||||||
@@ -184,7 +153,7 @@ namespace SabreTools.DatTools
|
|||||||
|
|
||||||
// Replace fields from the first duplicate, if we have one
|
// Replace fields from the first duplicate, if we have one
|
||||||
if (dupes.Count > 0)
|
if (dupes.Count > 0)
|
||||||
Replacer.ReplaceFields(datItem.Value, dupes.First().Value, itemFieldNames);
|
ReplaceFields(datItem.Value, dupes.First().Value, itemFieldNames);
|
||||||
}
|
}
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
});
|
});
|
||||||
@@ -193,9 +162,85 @@ namespace SabreTools.DatTools
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are matching based on Machine fields of any sort
|
/// <summary>
|
||||||
if (machineFieldNames.Count > 0)
|
/// Replace machine values from the base set represented by the current DAT
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datFile">Current DatFile object to use for updating</param>
|
||||||
|
/// <param name="intDat">DatFile to replace the values in</param>
|
||||||
|
/// <param name="machineFieldNames">List of machine field names representing what should be updated</param>
|
||||||
|
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
|
||||||
|
private static void BaseReplaceMachinesImpl(
|
||||||
|
DatFile datFile,
|
||||||
|
DatFile intDat,
|
||||||
|
List<string> machineFieldNames,
|
||||||
|
bool onlySame)
|
||||||
{
|
{
|
||||||
|
// Check for field names
|
||||||
|
if (machineFieldNames.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// For comparison's sake, we want to use Machine Name as the base bucketing
|
||||||
|
datFile.BucketBy(ItemKey.Machine);
|
||||||
|
datFile.Deduplicate();
|
||||||
|
intDat.BucketBy(ItemKey.Machine);
|
||||||
|
|
||||||
|
// Then we do a namewise comparison against the base DAT
|
||||||
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
|
Parallel.ForEach(intDat.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
|
||||||
|
#elif NET40_OR_GREATER
|
||||||
|
Parallel.ForEach(intDat.Items.SortedKeys, key =>
|
||||||
|
#else
|
||||||
|
foreach (var key in intDat.Items.SortedKeys)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
List<DatItem>? datItems = intDat.GetItemsForBucket(key);
|
||||||
|
if (datItems == null)
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
List<DatItem> newDatItems = [];
|
||||||
|
foreach (DatItem datItem in datItems)
|
||||||
|
{
|
||||||
|
if (datItem.Clone() is not DatItem newDatItem)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var list = datFile.GetItemsForBucket(key);
|
||||||
|
if (list.Count > 0)
|
||||||
|
ReplaceFields(newDatItem.GetFieldValue<Machine>(DatItem.MachineKey)!, list[index: 0].GetFieldValue<Machine>(DatItem.MachineKey)!, machineFieldNames, onlySame);
|
||||||
|
|
||||||
|
newDatItems.Add(newDatItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now add the new list to the key
|
||||||
|
intDat.RemoveBucket(key);
|
||||||
|
newDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Replace machine values from the base set represented by the current DAT
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datFile">Current DatFile object to use for updating</param>
|
||||||
|
/// <param name="intDat">DatFile to replace the values in</param>
|
||||||
|
/// <param name="machineFieldNames">List of machine field names representing what should be updated</param>
|
||||||
|
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
|
||||||
|
private static void BaseReplaceMachinesDBImpl(
|
||||||
|
DatFile datFile,
|
||||||
|
DatFile intDat,
|
||||||
|
List<string> machineFieldNames,
|
||||||
|
bool onlySame)
|
||||||
|
{
|
||||||
|
// Check for field names
|
||||||
|
if (machineFieldNames.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
// For comparison's sake, we want to use Machine Name as the base bucketing
|
// For comparison's sake, we want to use Machine Name as the base bucketing
|
||||||
datFile.BucketBy(ItemKey.Machine);
|
datFile.BucketBy(ItemKey.Machine);
|
||||||
datFile.Deduplicate();
|
datFile.Deduplicate();
|
||||||
@@ -223,7 +268,7 @@ namespace SabreTools.DatTools
|
|||||||
var datMachine = datFile.GetMachineForItemDB(datFile.GetItemsForBucketDB(key)!.First().Key);
|
var datMachine = datFile.GetMachineForItemDB(datFile.GetItemsForBucketDB(key)!.First().Key);
|
||||||
var intMachine = intDat.GetMachineForItemDB(datItem.Key);
|
var intMachine = intDat.GetMachineForItemDB(datItem.Key);
|
||||||
if (datMachine.Value != null && intMachine.Value != null)
|
if (datMachine.Value != null && intMachine.Value != null)
|
||||||
Replacer.ReplaceFields(intMachine.Value, datMachine.Value, machineFieldNames, onlySame);
|
ReplaceFields(intMachine.Value, datMachine.Value, machineFieldNames, onlySame);
|
||||||
}
|
}
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
});
|
});
|
||||||
@@ -232,8 +277,9 @@ namespace SabreTools.DatTools
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
watch.Stop();
|
#endregion
|
||||||
}
|
|
||||||
|
#region ReplaceFields
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replace fields with given values
|
/// Replace fields with given values
|
||||||
@@ -469,5 +515,7 @@ namespace SabreTools.DatTools
|
|||||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SpamSumKey, newItem.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey));
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SpamSumKey, newItem.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,6 @@ namespace SabreTools.Features
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
DatFile dupeData = Diffing.Duplicates(userInputDat, inputPaths);
|
DatFile dupeData = Diffing.Duplicates(userInputDat, inputPaths);
|
||||||
//DatFile dupeData = Diffing.DuplicatesDB(userInputDat, inputPaths);
|
|
||||||
|
|
||||||
InternalStopwatch watch = new("Outputting duplicate DAT");
|
InternalStopwatch watch = new("Outputting duplicate DAT");
|
||||||
Writer.Write(dupeData, OutputDir, overwrite: false);
|
Writer.Write(dupeData, OutputDir, overwrite: false);
|
||||||
@@ -178,7 +177,6 @@ namespace SabreTools.Features
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
DatFile outerDiffData = Diffing.NoDuplicates(userInputDat, inputPaths);
|
DatFile outerDiffData = Diffing.NoDuplicates(userInputDat, inputPaths);
|
||||||
//DatFile outerDiffData = Diffing.NoDuplicatesDB(userInputDat, inputPaths);
|
|
||||||
|
|
||||||
InternalStopwatch watch = new("Outputting no duplicate DAT");
|
InternalStopwatch watch = new("Outputting no duplicate DAT");
|
||||||
Writer.Write(outerDiffData, OutputDir, overwrite: false);
|
Writer.Write(outerDiffData, OutputDir, overwrite: false);
|
||||||
@@ -194,7 +192,6 @@ namespace SabreTools.Features
|
|||||||
{
|
{
|
||||||
// Get all of the output DatFiles
|
// Get all of the output DatFiles
|
||||||
List<DatFile> datFiles = Diffing.Individuals(userInputDat, inputPaths);
|
List<DatFile> datFiles = Diffing.Individuals(userInputDat, inputPaths);
|
||||||
//List<DatFile> datFiles = Diffing.IndividualsDB(userInputDat, inputPaths);
|
|
||||||
|
|
||||||
// Loop through and output the new DatFiles
|
// Loop through and output the new DatFiles
|
||||||
InternalStopwatch watch = new("Outputting all individual DATs");
|
InternalStopwatch watch = new("Outputting all individual DATs");
|
||||||
@@ -345,7 +342,6 @@ namespace SabreTools.Features
|
|||||||
|
|
||||||
// Now replace the fields from the base DatFile
|
// Now replace the fields from the base DatFile
|
||||||
Replacer.BaseReplace(
|
Replacer.BaseReplace(
|
||||||
//Replacer.BaseReplaceDB(
|
|
||||||
userInputDat,
|
userInputDat,
|
||||||
repDat,
|
repDat,
|
||||||
updateMachineFieldNames,
|
updateMachineFieldNames,
|
||||||
|
|||||||
Reference in New Issue
Block a user