mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Consolidate replacement code
This commit is contained in:
@@ -176,6 +176,7 @@ namespace SabreTools.DatFiles
|
|||||||
/// <param name="datItem">DatItem to set fields on</param>
|
/// <param name="datItem">DatItem to set fields on</param>
|
||||||
public void SetFields(DatItem datItem)
|
public void SetFields(DatItem datItem)
|
||||||
{
|
{
|
||||||
|
// If we have an invalid input, return
|
||||||
if (datItem == null)
|
if (datItem == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
@@ -394,7 +394,17 @@ namespace SabreTools.DatItems
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fieldName">Field to remove</param>
|
/// <param name="fieldName">Field to remove</param>
|
||||||
/// <returns>True if the removal was successful, false otherwise</returns>
|
/// <returns>True if the removal was successful, false otherwise</returns>
|
||||||
public bool RemoveField(string? fieldName) => FieldManipulator.RemoveField(_internal, fieldName);
|
public bool RemoveField(string? fieldName)
|
||||||
|
=> FieldManipulator.RemoveField(_internal, fieldName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Replace a field from another DatItem
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="other">DatItem to replace field from</param>
|
||||||
|
/// <param name="fieldName">Field to replace</param>
|
||||||
|
/// <returns>True if the replacement was successful, false otherwise</returns>
|
||||||
|
public bool ReplaceField(DatItem? other, string? fieldName)
|
||||||
|
=> FieldManipulator.ReplaceField(other?._internal, _internal, fieldName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set a field in the DatItem from a mapping string
|
/// Set a field in the DatItem from a mapping string
|
||||||
@@ -403,7 +413,8 @@ namespace SabreTools.DatItems
|
|||||||
/// <param name="value">String representing the value to set</param>
|
/// <param name="value">String representing the value to set</param>
|
||||||
/// <returns>True if the removal was successful, false otherwise</returns>
|
/// <returns>True if the removal was successful, false otherwise</returns>
|
||||||
/// <remarks>This only performs minimal validation before setting</remarks>
|
/// <remarks>This only performs minimal validation before setting</remarks>
|
||||||
public bool SetField(string? fieldName, string value) => FieldManipulator.SetField(_internal, fieldName, value);
|
public bool SetField(string? fieldName, string value)
|
||||||
|
=> FieldManipulator.SetField(_internal, fieldName, value);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -557,6 +557,15 @@ namespace SabreTools.DatItems
|
|||||||
public bool RemoveField(string? fieldName)
|
public bool RemoveField(string? fieldName)
|
||||||
=> FieldManipulator.RemoveField(_machine, fieldName);
|
=> FieldManipulator.RemoveField(_machine, fieldName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Replace a field from another Machine
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="other">Machine to replace field from</param>
|
||||||
|
/// <param name="fieldName">Field to replace</param>
|
||||||
|
/// <returns>True if the replacement was successful, false otherwise</returns>
|
||||||
|
public bool ReplaceField(Machine? other, string? fieldName)
|
||||||
|
=> FieldManipulator.ReplaceField(other?._machine, _machine, fieldName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set a field in the Machine from a mapping string
|
/// Set a field in the Machine from a mapping string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -95,20 +95,20 @@ namespace SabreTools.DatTools
|
|||||||
/// </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="machineFields">List of MachineFields representing what should be updated</param>
|
/// <param name="machineFieldNames">List of machine field names representing what should be updated</param>
|
||||||
/// <param name="datItemFields">List of DatItemFields 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>
|
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
|
||||||
public static void BaseReplace(
|
public static void BaseReplace(
|
||||||
DatFile datFile,
|
DatFile datFile,
|
||||||
DatFile intDat,
|
DatFile intDat,
|
||||||
List<MachineField> machineFields,
|
List<string> machineFieldNames,
|
||||||
List<DatItemField> datItemFields,
|
Dictionary<string, List<string>> itemFieldNames,
|
||||||
bool onlySame)
|
bool onlySame)
|
||||||
{
|
{
|
||||||
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.FileName}' from the base DAT");
|
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.FileName}' 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 (datItemFields.Any())
|
if (itemFieldNames.Any())
|
||||||
{
|
{
|
||||||
// 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.Items.BucketBy(ItemKey.CRC, DedupeType.Full);
|
datFile.Items.BucketBy(ItemKey.CRC, DedupeType.Full);
|
||||||
@@ -140,7 +140,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.First(), datItemFields);
|
Replacer.ReplaceFields(newDatItem, dupes.First(), itemFieldNames);
|
||||||
|
|
||||||
newDatItems.Add(newDatItem);
|
newDatItems.Add(newDatItem);
|
||||||
}
|
}
|
||||||
@@ -156,7 +156,7 @@ namespace SabreTools.DatTools
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we are matching based on Machine fields of any sort
|
// If we are matching based on Machine fields of any sort
|
||||||
if (machineFields.Any())
|
if (machineFieldNames.Any())
|
||||||
{
|
{
|
||||||
// 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.Items.BucketBy(ItemKey.Machine, DedupeType.Full);
|
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.Full);
|
||||||
@@ -189,7 +189,7 @@ namespace SabreTools.DatTools
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (datFile.Items.ContainsKey(key) && list.Count > 0)
|
if (datFile.Items.ContainsKey(key) && list.Count > 0)
|
||||||
Replacer.ReplaceFields(newDatItem.Machine, list[0].Machine, machineFields, onlySame);
|
Replacer.ReplaceFields(newDatItem.Machine, list[0].Machine, machineFieldNames, onlySame);
|
||||||
|
|
||||||
newDatItems.Add(newDatItem);
|
newDatItems.Add(newDatItem);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using SabreTools.Core;
|
|
||||||
using SabreTools.Models.Metadata;
|
using SabreTools.Models.Metadata;
|
||||||
|
|
||||||
namespace SabreTools.Filter
|
namespace SabreTools.Filter
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -16,7 +16,10 @@ namespace SabreTools.Test.Filtering
|
|||||||
var datItem = CreateDatItem();
|
var datItem = CreateDatItem();
|
||||||
var repDatItem = CreateDatItem();
|
var repDatItem = CreateDatItem();
|
||||||
repDatItem.SetName("bar");
|
repDatItem.SetName("bar");
|
||||||
var fields = new List<DatItemField> { DatItemField.Name };
|
var fields = new Dictionary<string, List<string>>
|
||||||
|
{
|
||||||
|
["item"] = [Models.Metadata.Rom.NameKey]
|
||||||
|
};
|
||||||
Replacer.ReplaceFields(datItem, repDatItem, fields);
|
Replacer.ReplaceFields(datItem, repDatItem, fields);
|
||||||
Assert.Equal("bar", datItem.GetName());
|
Assert.Equal("bar", datItem.GetName());
|
||||||
}
|
}
|
||||||
@@ -27,7 +30,7 @@ namespace SabreTools.Test.Filtering
|
|||||||
var datItem = CreateDatItem();
|
var datItem = CreateDatItem();
|
||||||
var repDatItem = CreateDatItem();
|
var repDatItem = CreateDatItem();
|
||||||
repDatItem.Machine.Name = "foo";
|
repDatItem.Machine.Name = "foo";
|
||||||
var fields = new List<MachineField> { MachineField.Name };
|
List<string> fields = [Models.Metadata.Machine.NameKey];
|
||||||
Replacer.ReplaceFields(datItem.Machine, repDatItem.Machine, fields, false);
|
Replacer.ReplaceFields(datItem.Machine, repDatItem.Machine, fields, false);
|
||||||
Assert.Equal("foo", datItem.Machine.Name);
|
Assert.Equal("foo", datItem.Machine.Name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2046,28 +2046,37 @@ Some special strings that can be used:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get update DatItem fields from feature list
|
/// Get update Machine fields from feature list
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static List<DatItemField> GetUpdateDatItemFields(Dictionary<string, Feature> features)
|
protected static List<string> GetUpdateMachineFields(Dictionary<string, Feature> features)
|
||||||
{
|
{
|
||||||
List<DatItemField> updateFields = new();
|
List<string> updateFields = [];
|
||||||
foreach (string fieldName in GetList(features, UpdateFieldListValue))
|
foreach (string fieldName in GetList(features, UpdateFieldListValue))
|
||||||
{
|
{
|
||||||
updateFields.Add(fieldName.AsDatItemField());
|
(string? itemType, string? key) = SabreTools.Filter.FilterParser.ParseFilterId(fieldName);
|
||||||
|
if (itemType == Models.Metadata.MetadataFile.MachineKey)
|
||||||
|
updateFields.Add(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
return updateFields;
|
return updateFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get update Machine fields from feature list
|
/// Get update DatItem fields from feature list
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static List<MachineField> GetUpdateMachineFields(Dictionary<string, Feature> features)
|
protected static Dictionary<string, List<string>> GetUpdateDatItemFields(Dictionary<string, Feature> features)
|
||||||
{
|
{
|
||||||
List<MachineField> updateFields = new();
|
Dictionary<string, List<string>> updateFields = [];
|
||||||
foreach (string fieldName in GetList(features, UpdateFieldListValue))
|
foreach (string fieldName in GetList(features, UpdateFieldListValue))
|
||||||
{
|
{
|
||||||
updateFields.Add(fieldName.AsMachineField());
|
(string? itemType, string? key) = SabreTools.Filter.FilterParser.ParseFilterId(fieldName);
|
||||||
|
if (itemType != Models.Metadata.MetadataFile.HeaderKey && itemType != Models.Metadata.MetadataFile.MachineKey)
|
||||||
|
{
|
||||||
|
if (!updateFields.ContainsKey(itemType))
|
||||||
|
updateFields[itemType] = [];
|
||||||
|
|
||||||
|
updateFields[itemType].Add(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return updateFields;
|
return updateFields;
|
||||||
|
|||||||
@@ -90,8 +90,8 @@ namespace SabreTools.Features
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Get feature flags
|
// Get feature flags
|
||||||
var updateDatItemFields = GetUpdateDatItemFields(features);
|
var updateMachineFieldNames = GetUpdateMachineFields(features);
|
||||||
var updateMachineFields = GetUpdateMachineFields(features);
|
var updateItemFieldNames = GetUpdateDatItemFields(features);
|
||||||
var updateMode = GetUpdateMode(features);
|
var updateMode = GetUpdateMode(features);
|
||||||
|
|
||||||
// Normalize the extensions
|
// Normalize the extensions
|
||||||
@@ -138,8 +138,11 @@ namespace SabreTools.Features
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If no update fields are set, default to Names
|
// If no update fields are set, default to Names
|
||||||
if (updateDatItemFields == null || updateDatItemFields.Count == 0)
|
if (updateItemFieldNames == null || updateItemFieldNames.Count == 0)
|
||||||
updateDatItemFields = new List<DatItemField>() { DatItemField.Name };
|
{
|
||||||
|
updateItemFieldNames = [];
|
||||||
|
updateItemFieldNames["item"] = [Models.Metadata.Rom.NameKey];
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure we only have files in the inputs
|
// Ensure we only have files in the inputs
|
||||||
List<ParentablePath> inputPaths = PathTool.GetFilesOnly(Inputs, appendparent: true);
|
List<ParentablePath> inputPaths = PathTool.GetFilesOnly(Inputs, appendparent: true);
|
||||||
@@ -391,8 +394,8 @@ namespace SabreTools.Features
|
|||||||
DatFileTool.BaseReplace(
|
DatFileTool.BaseReplace(
|
||||||
userInputDat,
|
userInputDat,
|
||||||
repDat,
|
repDat,
|
||||||
updateMachineFields,
|
updateMachineFieldNames,
|
||||||
updateDatItemFields,
|
updateItemFieldNames,
|
||||||
GetBoolean(features, OnlySameValue));
|
GetBoolean(features, OnlySameValue));
|
||||||
|
|
||||||
// Finally output the replaced DatFile
|
// Finally output the replaced DatFile
|
||||||
|
|||||||
Reference in New Issue
Block a user