mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
DatFile should not care about specialized removal
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
namespace SabreTools.DatFiles.Test
|
||||
{
|
||||
public partial class DatFileTests
|
||||
{
|
||||
#region RemoveHeaderFields
|
||||
|
||||
// TODO: Write RemoveHeaderFields tests
|
||||
// - Empty list
|
||||
// - Full list
|
||||
|
||||
#endregion
|
||||
|
||||
#region RemoveItemFields
|
||||
|
||||
// TODO: Write RemoveItemFields tests
|
||||
// - Both lists empty
|
||||
// - Machine only
|
||||
// - Item only
|
||||
// - Nested
|
||||
|
||||
#endregion
|
||||
|
||||
#region RemoveItemFieldsDB
|
||||
|
||||
// TODO: Write RemoveItemFieldsDB tests
|
||||
// - Both lists empty
|
||||
// - Machine only
|
||||
// - Item only
|
||||
// - Nested
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,397 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
|
||||
namespace SabreTools.DatFiles
|
||||
{
|
||||
public partial class DatFile
|
||||
{
|
||||
#region Removal
|
||||
|
||||
/// <summary>
|
||||
/// Remove header fields with given values
|
||||
/// </summary>
|
||||
public void RemoveHeaderFields(List<string> fields)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (fields.Count == 0)
|
||||
return;
|
||||
|
||||
foreach (var fieldName in fields)
|
||||
{
|
||||
bool removed = Header.RemoveField(fieldName);
|
||||
_logger.Verbose($"Header field {fieldName} {(removed ? "removed" : "could not be removed")}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply removals to the item dictionary
|
||||
/// </summary>
|
||||
public void RemoveItemFields(List<string> machineFields, Dictionary<string, List<string>> itemFields)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (machineFields.Count == 0 && itemFields.Count == 0)
|
||||
return;
|
||||
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(Items.Keys, Core.Globals.ParallelOptions, key =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(Items.Keys, key =>
|
||||
#else
|
||||
foreach (var key in Items.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem>? items = Items[key];
|
||||
if (items == null)
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
return;
|
||||
#else
|
||||
continue;
|
||||
#endif
|
||||
|
||||
for (int j = 0; j < items.Count; j++)
|
||||
{
|
||||
RemoveFields(items[j], machineFields, itemFields);
|
||||
}
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply removals to the item dictionary
|
||||
/// </summary>
|
||||
public void RemoveItemFieldsDB(List<string> machineFields, Dictionary<string, List<string>> itemFields)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (machineFields.Count == 0 && itemFields.Count == 0)
|
||||
return;
|
||||
|
||||
// Handle machine removals
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(ItemsDB.GetMachines(), Core.Globals.ParallelOptions, kvp =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(ItemsDB.GetMachines(), kvp =>
|
||||
#else
|
||||
foreach (var kvp in ItemsDB.GetMachines())
|
||||
#endif
|
||||
{
|
||||
RemoveFields(kvp.Value, machineFields);
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
|
||||
// Handle item removals
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(ItemsDB.SortedKeys, Core.Globals.ParallelOptions, key =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(ItemsDB.SortedKeys, key =>
|
||||
#else
|
||||
foreach (var key in ItemsDB.SortedKeys)
|
||||
#endif
|
||||
{
|
||||
var items = ItemsDB.GetItemsForBucket(key);
|
||||
if (items == null)
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
return;
|
||||
#else
|
||||
continue;
|
||||
#endif
|
||||
|
||||
foreach (var item in items.Values)
|
||||
{
|
||||
RemoveFields(item, [], itemFields);
|
||||
}
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove machine fields with given values
|
||||
/// </summary>
|
||||
private static void RemoveFields(Machine? machine, List<string> fields)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (machine == null || fields.Count == 0)
|
||||
return;
|
||||
|
||||
foreach (var fieldName in fields)
|
||||
{
|
||||
machine.RemoveField(fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="datItem">DatItem to remove fields from</param>
|
||||
private static void RemoveFields(DatItem? datItem, List<string> machineFields, Dictionary<string, List<string>> itemFields)
|
||||
{
|
||||
if (datItem == null)
|
||||
return;
|
||||
|
||||
#region Common
|
||||
|
||||
// Handle Machine fields
|
||||
var machine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
if (machineFields.Count > 0 && machine != null)
|
||||
RemoveFields(machine, machineFields);
|
||||
|
||||
// If there are no field names, return
|
||||
if (itemFields == null || itemFields.Count == 0)
|
||||
return;
|
||||
|
||||
// If there are no field names for this type or generic, return
|
||||
string? itemType = datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue();
|
||||
if (itemType == null || (!itemFields.ContainsKey(itemType) && !itemFields.ContainsKey("item")))
|
||||
return;
|
||||
|
||||
// Get the combined list of fields to remove
|
||||
var fields = new HashSet<string>();
|
||||
if (itemFields.ContainsKey(itemType))
|
||||
fields.UnionWith(itemFields[itemType]);
|
||||
if (itemFields.ContainsKey("item"))
|
||||
fields.UnionWith(itemFields["item"]);
|
||||
|
||||
// If the field specifically contains Name, set it separately
|
||||
if (fields.Contains(Models.Metadata.Rom.NameKey))
|
||||
datItem.SetName(null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Item-Specific
|
||||
|
||||
// Handle unnested removals first
|
||||
foreach (var datItemField in fields)
|
||||
{
|
||||
datItem.RemoveField(datItemField);
|
||||
}
|
||||
|
||||
// Handle nested removals
|
||||
switch (datItem)
|
||||
{
|
||||
case Adjuster adjuster: RemoveFields(adjuster, itemFields); break;
|
||||
case Configuration configuration: RemoveFields(configuration, itemFields); break;
|
||||
case ConfSetting confSetting: RemoveFields(confSetting, itemFields); break;
|
||||
case Device device: RemoveFields(device, itemFields); break;
|
||||
case DipSwitch dipSwitch: RemoveFields(dipSwitch, itemFields); break;
|
||||
case DipValue dipValue: RemoveFields(dipValue, itemFields); break;
|
||||
case Disk disk: RemoveFields(disk, itemFields); break;
|
||||
case Input input: RemoveFields(input, itemFields); break;
|
||||
case Part part: RemoveFields(part, itemFields); break;
|
||||
case Port port: RemoveFields(port, itemFields); break;
|
||||
case Rom rom: RemoveFields(rom, itemFields); break;
|
||||
case Slot slot: RemoveFields(slot, itemFields); break;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="adjuster">Adjuster to remove fields from</param>
|
||||
private static void RemoveFields(Adjuster adjuster, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var conditions = adjuster.GetFieldValue<Condition[]?>(Models.Metadata.Adjuster.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration to remove fields from</param>
|
||||
private static void RemoveFields(Configuration configuration, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var conditions = configuration.GetFieldValue<Condition[]?>(Models.Metadata.Configuration.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition, [], fields);
|
||||
}
|
||||
|
||||
var locations = configuration.GetFieldValue<ConfLocation[]?>(Models.Metadata.Configuration.ConfLocationKey) ?? [];
|
||||
foreach (ConfLocation subLocation in locations)
|
||||
{
|
||||
RemoveFields(subLocation, [], fields);
|
||||
}
|
||||
|
||||
var settings = configuration.GetFieldValue<ConfSetting[]?>(Models.Metadata.Configuration.ConfSettingKey) ?? [];
|
||||
foreach (ConfSetting subSetting in settings)
|
||||
{
|
||||
RemoveFields(subSetting as DatItem, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="confsetting">ConfSetting to remove fields from</param>
|
||||
private static void RemoveFields(ConfSetting confsetting, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var conditions = confsetting.GetFieldValue<Condition[]?>(Models.Metadata.ConfSetting.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="device">Device to remove fields from</param>
|
||||
private static void RemoveFields(Device device, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var extensions = device.GetFieldValue<Extension[]?>(Models.Metadata.Device.ExtensionKey) ?? [];
|
||||
foreach (Extension subExtension in extensions)
|
||||
{
|
||||
RemoveFields(subExtension, [], fields);
|
||||
}
|
||||
|
||||
var instances = device.GetFieldValue<Instance[]?>(Models.Metadata.Device.InstanceKey) ?? [];
|
||||
foreach (Instance subInstance in instances)
|
||||
{
|
||||
RemoveFields(subInstance, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="dipSwitch">DipSwitch to remove fields from</param>
|
||||
private static void RemoveFields(DipSwitch dipSwitch, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var conditions = dipSwitch.GetFieldValue<Condition[]?>(Models.Metadata.DipSwitch.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition, [], fields);
|
||||
}
|
||||
|
||||
var locations = dipSwitch.GetFieldValue<DipLocation[]?>(Models.Metadata.DipSwitch.DipLocationKey) ?? [];
|
||||
foreach (DipLocation subLocation in locations)
|
||||
{
|
||||
RemoveFields(subLocation, [], fields);
|
||||
}
|
||||
|
||||
var dipValues = dipSwitch.GetFieldValue<DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey) ?? [];
|
||||
foreach (DipValue subValue in dipValues)
|
||||
{
|
||||
RemoveFields(subValue as DatItem, [], fields);
|
||||
}
|
||||
|
||||
var part = dipSwitch.GetFieldValue<Part?>(DipSwitch.PartKey);
|
||||
if (part != null)
|
||||
RemoveFields(part as DatItem, [], fields);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="dipValue">DipValue to remove fields from</param>
|
||||
private static void RemoveFields(DipValue dipValue, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var conditions = dipValue.GetFieldValue<Condition[]?>(Models.Metadata.DipValue.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="disk">Disk to remove fields from</param>
|
||||
private static void RemoveFields(Disk disk, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var diskArea = disk.GetFieldValue<DiskArea?>(Disk.DiskAreaKey);
|
||||
if (diskArea != null)
|
||||
RemoveFields(diskArea as DatItem, [], fields);
|
||||
|
||||
var part = disk.GetFieldValue<Part?>(Disk.PartKey);
|
||||
if (part != null)
|
||||
RemoveFields(part as DatItem, [], fields);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="input">Input to remove fields from</param>
|
||||
private static void RemoveFields(Input input, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var controls = input.GetFieldValue<Control[]?>(Models.Metadata.Input.ControlKey) ?? [];
|
||||
foreach (Control subControl in controls)
|
||||
{
|
||||
RemoveFields(subControl, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="part">Part to remove fields from</param>
|
||||
private static void RemoveFields(Part part, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var features = part.GetFieldValue<PartFeature[]?>(Models.Metadata.Part.FeatureKey) ?? [];
|
||||
foreach (PartFeature subPartFeature in features)
|
||||
{
|
||||
RemoveFields(subPartFeature, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="port">Port to remove fields from</param>
|
||||
private static void RemoveFields(Port port, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var analogs = port.GetFieldValue<Analog[]?>(Models.Metadata.Port.AnalogKey) ?? [];
|
||||
foreach (Analog subAnalog in analogs)
|
||||
{
|
||||
RemoveFields(subAnalog, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="rom">Rom to remove fields from</param>
|
||||
private static void RemoveFields(Rom rom, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var dataArea = rom.GetFieldValue<DataArea?>(Rom.DataAreaKey);
|
||||
if (dataArea != null)
|
||||
RemoveFields(dataArea as DatItem, [], fields);
|
||||
|
||||
var part = rom.GetFieldValue<Part?>(Rom.PartKey);
|
||||
if (part != null)
|
||||
RemoveFields(part as DatItem, [], fields);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="slot">Slot to remove fields from</param>
|
||||
private static void RemoveFields(Slot slot, Dictionary<string, List<string>> fields)
|
||||
{
|
||||
var slotOptions = slot.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey) ?? [];
|
||||
foreach (SlotOption subSlotOption in slotOptions)
|
||||
{
|
||||
RemoveFields(subSlotOption, [], fields);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using SabreTools.Core.Filter;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatFiles;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.IO.Logging;
|
||||
|
||||
namespace SabreTools.DatTools
|
||||
@@ -130,12 +133,396 @@ namespace SabreTools.DatTools
|
||||
public void ApplyRemovals(DatFile datFile)
|
||||
{
|
||||
InternalStopwatch watch = new("Applying removals to DAT");
|
||||
datFile.RemoveHeaderFields(HeaderFieldNames);
|
||||
datFile.RemoveItemFields(MachineFieldNames, ItemFieldNames);
|
||||
datFile.RemoveItemFieldsDB(MachineFieldNames, ItemFieldNames);
|
||||
|
||||
RemoveHeaderFields(datFile.Header);
|
||||
RemoveItemFields(datFile.Items);
|
||||
RemoveItemFieldsDB(datFile.ItemsDB);
|
||||
|
||||
watch.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove header fields with given values
|
||||
/// </summary>
|
||||
public void RemoveHeaderFields(DatHeader? datHeader)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (datHeader == null || HeaderFieldNames.Count == 0)
|
||||
return;
|
||||
|
||||
foreach (var fieldName in HeaderFieldNames)
|
||||
{
|
||||
bool removed = datHeader.RemoveField(fieldName);
|
||||
_logger.Verbose($"Header field {fieldName} {(removed ? "removed" : "could not be removed")}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply removals to the item dictionary
|
||||
/// </summary>
|
||||
/// TODO: Does this need to be multi-threaded?
|
||||
public void RemoveItemFields(ItemDictionary? itemDictionary)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (itemDictionary == null || (MachineFieldNames.Count == 0 && ItemFieldNames.Count == 0))
|
||||
return;
|
||||
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(itemDictionary.Keys, Core.Globals.ParallelOptions, key =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(itemDictionary.Keys, key =>
|
||||
#else
|
||||
foreach (var key in itemDictionary.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem>? items = itemDictionary[key];
|
||||
if (items == null)
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
return;
|
||||
#else
|
||||
continue;
|
||||
#endif
|
||||
|
||||
for (int j = 0; j < items.Count; j++)
|
||||
{
|
||||
RemoveFields(items[j]);
|
||||
}
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply removals to the item dictionary
|
||||
/// </summary>
|
||||
/// TODO: Does this need to be multi-threaded?
|
||||
public void RemoveItemFieldsDB(ItemDictionaryDB? itemDictionary)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (itemDictionary == null || (MachineFieldNames.Count == 0 && ItemFieldNames.Count == 0))
|
||||
return;
|
||||
|
||||
// Handle machine removals
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(itemDictionary.GetMachines(), Core.Globals.ParallelOptions, kvp =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(itemDictionary.GetMachines(), kvp =>
|
||||
#else
|
||||
foreach (var kvp in itemDictionary.GetMachines())
|
||||
#endif
|
||||
{
|
||||
RemoveFields(kvp.Value);
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
|
||||
// Handle item removals
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(ItemsDB.SortedKeys, Core.Globals.ParallelOptions, key =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(ItemsDB.SortedKeys, key =>
|
||||
#else
|
||||
foreach (var key in itemDictionary.SortedKeys)
|
||||
#endif
|
||||
{
|
||||
var items = itemDictionary.GetItemsForBucket(key);
|
||||
if (items == null)
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
return;
|
||||
#else
|
||||
continue;
|
||||
#endif
|
||||
|
||||
foreach (var item in items.Values)
|
||||
{
|
||||
RemoveFields(item);
|
||||
}
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove machine fields with given values
|
||||
/// </summary>
|
||||
private void RemoveFields(Machine? machine)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (machine == null || MachineFieldNames.Count == 0)
|
||||
return;
|
||||
|
||||
foreach (var fieldName in MachineFieldNames)
|
||||
{
|
||||
machine.RemoveField(fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="datItem">DatItem to remove fields from</param>
|
||||
private void RemoveFields(DatItem? datItem)
|
||||
{
|
||||
if (datItem == null)
|
||||
return;
|
||||
|
||||
#region Common
|
||||
|
||||
// Handle Machine fields
|
||||
var machine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
if (MachineFieldNames.Count > 0 && machine != null)
|
||||
RemoveFields(machine);
|
||||
|
||||
// If there are no field names, return
|
||||
if (ItemFieldNames == null || ItemFieldNames.Count == 0)
|
||||
return;
|
||||
|
||||
// If there are no field names for this type or generic, return
|
||||
string? itemType = datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue();
|
||||
if (itemType == null || (!ItemFieldNames.ContainsKey(itemType) && !ItemFieldNames.ContainsKey("item")))
|
||||
return;
|
||||
|
||||
// Get the combined list of fields to remove
|
||||
var fields = new HashSet<string>();
|
||||
if (ItemFieldNames.ContainsKey(itemType))
|
||||
fields.UnionWith(ItemFieldNames[itemType]);
|
||||
if (ItemFieldNames.ContainsKey("item"))
|
||||
fields.UnionWith(ItemFieldNames["item"]);
|
||||
|
||||
// If the field specifically contains Name, set it separately
|
||||
if (fields.Contains(Models.Metadata.Rom.NameKey))
|
||||
datItem.SetName(null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Item-Specific
|
||||
|
||||
// Handle unnested removals first
|
||||
foreach (var datItemField in fields)
|
||||
{
|
||||
datItem.RemoveField(datItemField);
|
||||
}
|
||||
|
||||
// Handle nested removals
|
||||
switch (datItem)
|
||||
{
|
||||
case Adjuster adjuster: RemoveNestedFields(adjuster); break;
|
||||
case Configuration configuration: RemoveNestedFields(configuration); break;
|
||||
case ConfSetting confSetting: RemoveNestedFields(confSetting); break;
|
||||
case Device device: RemoveNestedFields(device); break;
|
||||
case DipSwitch dipSwitch: RemoveNestedFields(dipSwitch); break;
|
||||
case DipValue dipValue: RemoveNestedFields(dipValue); break;
|
||||
case Disk disk: RemoveNestedFields(disk); break;
|
||||
case Input input: RemoveNestedFields(input); break;
|
||||
case Part part: RemoveNestedFields(part); break;
|
||||
case Port port: RemoveNestedFields(port); break;
|
||||
case Rom rom: RemoveNestedFields(rom); break;
|
||||
case Slot slot: RemoveNestedFields(slot); break;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="adjuster">Adjuster to remove fields from</param>
|
||||
private void RemoveNestedFields(Adjuster adjuster)
|
||||
{
|
||||
var conditions = adjuster.GetFieldValue<Condition[]?>(Models.Metadata.Adjuster.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration to remove fields from</param>
|
||||
private void RemoveNestedFields(Configuration configuration)
|
||||
{
|
||||
var conditions = configuration.GetFieldValue<Condition[]?>(Models.Metadata.Configuration.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
|
||||
var locations = configuration.GetFieldValue<ConfLocation[]?>(Models.Metadata.Configuration.ConfLocationKey) ?? [];
|
||||
foreach (ConfLocation subLocation in locations)
|
||||
{
|
||||
RemoveFields(subLocation);
|
||||
}
|
||||
|
||||
var settings = configuration.GetFieldValue<ConfSetting[]?>(Models.Metadata.Configuration.ConfSettingKey) ?? [];
|
||||
foreach (ConfSetting subSetting in settings)
|
||||
{
|
||||
RemoveFields(subSetting);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="confsetting">ConfSetting to remove fields from</param>
|
||||
private void RemoveNestedFields(ConfSetting confsetting)
|
||||
{
|
||||
var conditions = confsetting.GetFieldValue<Condition[]?>(Models.Metadata.ConfSetting.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="device">Device to remove fields from</param>
|
||||
private void RemoveNestedFields(Device device)
|
||||
{
|
||||
var extensions = device.GetFieldValue<Extension[]?>(Models.Metadata.Device.ExtensionKey) ?? [];
|
||||
foreach (Extension subExtension in extensions)
|
||||
{
|
||||
RemoveFields(subExtension);
|
||||
}
|
||||
|
||||
var instances = device.GetFieldValue<Instance[]?>(Models.Metadata.Device.InstanceKey) ?? [];
|
||||
foreach (Instance subInstance in instances)
|
||||
{
|
||||
RemoveFields(subInstance);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="dipSwitch">DipSwitch to remove fields from</param>
|
||||
private void RemoveNestedFields(DipSwitch dipSwitch)
|
||||
{
|
||||
var conditions = dipSwitch.GetFieldValue<Condition[]?>(Models.Metadata.DipSwitch.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
|
||||
var locations = dipSwitch.GetFieldValue<DipLocation[]?>(Models.Metadata.DipSwitch.DipLocationKey) ?? [];
|
||||
foreach (DipLocation subLocation in locations)
|
||||
{
|
||||
RemoveFields(subLocation);
|
||||
}
|
||||
|
||||
var dipValues = dipSwitch.GetFieldValue<DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey) ?? [];
|
||||
foreach (DipValue subValue in dipValues)
|
||||
{
|
||||
RemoveFields(subValue);
|
||||
}
|
||||
|
||||
var part = dipSwitch.GetFieldValue<Part?>(DipSwitch.PartKey);
|
||||
if (part != null)
|
||||
RemoveFields(part);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="dipValue">DipValue to remove fields from</param>
|
||||
private void RemoveNestedFields(DipValue dipValue)
|
||||
{
|
||||
var conditions = dipValue.GetFieldValue<Condition[]?>(Models.Metadata.DipValue.ConditionKey) ?? [];
|
||||
foreach (Condition subCondition in conditions)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="disk">Disk to remove fields from</param>
|
||||
private void RemoveNestedFields(Disk disk)
|
||||
{
|
||||
var diskArea = disk.GetFieldValue<DiskArea?>(Disk.DiskAreaKey);
|
||||
if (diskArea != null)
|
||||
RemoveFields(diskArea);
|
||||
|
||||
var part = disk.GetFieldValue<Part?>(Disk.PartKey);
|
||||
if (part != null)
|
||||
RemoveFields(part);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="input">Input to remove fields from</param>
|
||||
private void RemoveNestedFields(Input input)
|
||||
{
|
||||
var controls = input.GetFieldValue<Control[]?>(Models.Metadata.Input.ControlKey) ?? [];
|
||||
foreach (Control subControl in controls)
|
||||
{
|
||||
RemoveFields(subControl);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="part">Part to remove fields from</param>
|
||||
private void RemoveNestedFields(Part part)
|
||||
{
|
||||
var features = part.GetFieldValue<PartFeature[]?>(Models.Metadata.Part.FeatureKey) ?? [];
|
||||
foreach (PartFeature subPartFeature in features)
|
||||
{
|
||||
RemoveFields(subPartFeature);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="port">Port to remove fields from</param>
|
||||
private void RemoveNestedFields(Port port)
|
||||
{
|
||||
var analogs = port.GetFieldValue<Analog[]?>(Models.Metadata.Port.AnalogKey) ?? [];
|
||||
foreach (Analog subAnalog in analogs)
|
||||
{
|
||||
RemoveFields(subAnalog);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="rom">Rom to remove fields from</param>
|
||||
private void RemoveNestedFields(Rom rom)
|
||||
{
|
||||
var dataArea = rom.GetFieldValue<DataArea?>(Rom.DataAreaKey);
|
||||
if (dataArea != null)
|
||||
RemoveFields(dataArea);
|
||||
|
||||
var part = rom.GetFieldValue<Part?>(Rom.PartKey);
|
||||
if (part != null)
|
||||
RemoveFields(part);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="slot">Slot to remove fields from</param>
|
||||
private void RemoveNestedFields(Slot slot)
|
||||
{
|
||||
var slotOptions = slot.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey) ?? [];
|
||||
foreach (SlotOption subSlotOption in slotOptions)
|
||||
{
|
||||
RemoveFields(subSlotOption);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user