mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move Remover methods because of external access differences
This commit is contained in:
@@ -1,14 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Filter;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatFiles;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.Logging;
|
||||
|
||||
namespace SabreTools.Filtering
|
||||
@@ -134,364 +126,10 @@ namespace SabreTools.Filtering
|
||||
public void ApplyRemovals(DatFile datFile)
|
||||
{
|
||||
InternalStopwatch watch = new("Applying removals to DAT");
|
||||
|
||||
// Remove DatHeader fields
|
||||
if (HeaderFieldNames.Any())
|
||||
RemoveFields(datFile.Header);
|
||||
|
||||
// Remove DatItem and Machine fields
|
||||
if (MachineFieldNames.Any() || ItemFieldNames.Any())
|
||||
{
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
|
||||
#elif NET40_OR_GREATER
|
||||
Parallel.ForEach(datFile.Items.Keys, key =>
|
||||
#else
|
||||
foreach (var key in datFile.Items.Keys)
|
||||
#endif
|
||||
{
|
||||
ConcurrentList<DatItem>? items = datFile.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]);
|
||||
}
|
||||
|
||||
datFile.Items.Remove(key);
|
||||
datFile.Items.AddRange(key, items);
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
datFile.ApplyRemovals(HeaderFieldNames, MachineFieldNames, ItemFieldNames);
|
||||
watch.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="datItem">DatHeader to remove fields from</param>
|
||||
public void RemoveFields(DatHeader datHeader)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (datHeader == null || !HeaderFieldNames.Any())
|
||||
return;
|
||||
|
||||
foreach (var fieldName in HeaderFieldNames)
|
||||
{
|
||||
datHeader.RemoveField(fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="machine">Machine to remove fields from</param>
|
||||
public void RemoveFields(Machine? machine)
|
||||
{
|
||||
// If we have an invalid input, return
|
||||
if (machine == null || !MachineFieldNames.Any())
|
||||
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>
|
||||
public void RemoveFields(DatItem? datItem)
|
||||
{
|
||||
if (datItem == null)
|
||||
return;
|
||||
|
||||
#region Common
|
||||
|
||||
// Handle Machine fields
|
||||
if (MachineFieldNames.Any() && datItem.GetFieldValue<Machine>(DatItem.MachineKey) != null)
|
||||
RemoveFields(datItem.GetFieldValue<Machine>(DatItem.MachineKey));
|
||||
|
||||
// If there are no field names, return
|
||||
if (ItemFieldNames == null || !ItemFieldNames.Any())
|
||||
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 fieldNames = new List<string>();
|
||||
if (ItemFieldNames.ContainsKey(itemType))
|
||||
fieldNames.AddRange(ItemFieldNames[itemType]);
|
||||
if (ItemFieldNames.ContainsKey("item"))
|
||||
fieldNames.AddRange(ItemFieldNames["item"]);
|
||||
fieldNames = fieldNames.Distinct().ToList();
|
||||
|
||||
// If the field specifically contains Name, set it separately
|
||||
if (fieldNames.Contains(Models.Metadata.Rom.NameKey))
|
||||
datItem.SetName(null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Item-Specific
|
||||
|
||||
// Handle unnested removals first
|
||||
foreach (var datItemField in fieldNames)
|
||||
{
|
||||
datItem.RemoveField(datItemField);
|
||||
}
|
||||
|
||||
// Handle nested removals
|
||||
switch (datItem)
|
||||
{
|
||||
case Adjuster adjuster: RemoveFields(adjuster); break;
|
||||
case Configuration configuration: RemoveFields(configuration); break;
|
||||
case ConfSetting confSetting: RemoveFields(confSetting); break;
|
||||
case Device device: RemoveFields(device); break;
|
||||
case DipSwitch dipSwitch: RemoveFields(dipSwitch); break;
|
||||
case DipValue dipValue: RemoveFields(dipValue); break;
|
||||
case Disk disk: RemoveFields(disk); break;
|
||||
case Input input: RemoveFields(input); break;
|
||||
case Part part: RemoveFields(part); break;
|
||||
case Port port: RemoveFields(port); break;
|
||||
case Rom rom: RemoveFields(rom); break;
|
||||
case Slot slot: RemoveFields(slot); break;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="adjuster">Adjuster to remove fields from</param>
|
||||
private void RemoveFields(Adjuster adjuster)
|
||||
{
|
||||
if (!adjuster.ConditionsSpecified)
|
||||
return;
|
||||
|
||||
foreach (Condition subCondition in adjuster.GetFieldValue<Condition[]?>(Models.Metadata.Adjuster.ConditionKey)!)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration to remove fields from</param>
|
||||
private void RemoveFields(Configuration configuration)
|
||||
{
|
||||
if (configuration.ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition subCondition in configuration.GetFieldValue<Condition[]?>(Models.Metadata.Configuration.ConditionKey)!)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
|
||||
if (configuration.LocationsSpecified)
|
||||
{
|
||||
foreach (ConfLocation subLocation in configuration.GetFieldValue<ConfLocation[]?>(Models.Metadata.Configuration.ConfLocationKey)!)
|
||||
{
|
||||
RemoveFields(subLocation);
|
||||
}
|
||||
}
|
||||
|
||||
if (configuration.SettingsSpecified)
|
||||
{
|
||||
foreach (ConfSetting subSetting in configuration.GetFieldValue<ConfSetting[]?>(Models.Metadata.Configuration.ConfSettingKey)!)
|
||||
{
|
||||
RemoveFields(subSetting as DatItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="confsetting">ConfSetting to remove fields from</param>
|
||||
private void RemoveFields(ConfSetting confsetting)
|
||||
{
|
||||
if (confsetting.ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition subCondition in confsetting.GetFieldValue<Condition[]?>(Models.Metadata.ConfSetting.ConditionKey)!)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="device">Device to remove fields from</param>
|
||||
private void RemoveFields(Device device)
|
||||
{
|
||||
if (device.ExtensionsSpecified)
|
||||
{
|
||||
foreach (Extension subExtension in device.GetFieldValue<Extension[]?>(Models.Metadata.Device.ExtensionKey)!)
|
||||
{
|
||||
RemoveFields(subExtension);
|
||||
}
|
||||
}
|
||||
|
||||
if (device.InstancesSpecified)
|
||||
{
|
||||
foreach (Instance subInstance in device.GetFieldValue<Instance[]?>(Models.Metadata.Device.InstanceKey)!)
|
||||
{
|
||||
RemoveFields(subInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="dipSwitch">DipSwitch to remove fields from</param>
|
||||
private void RemoveFields(DipSwitch dipSwitch)
|
||||
{
|
||||
if (dipSwitch.ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition subCondition in dipSwitch.GetFieldValue<Condition[]?>(Models.Metadata.DipSwitch.ConditionKey)!)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
|
||||
if (dipSwitch.LocationsSpecified)
|
||||
{
|
||||
foreach (DipLocation subLocation in dipSwitch.GetFieldValue<DipLocation[]?>(Models.Metadata.DipSwitch.DipLocationKey)!)
|
||||
{
|
||||
RemoveFields(subLocation);
|
||||
}
|
||||
}
|
||||
|
||||
if (dipSwitch.ValuesSpecified)
|
||||
{
|
||||
foreach (DipValue subValue in dipSwitch.GetFieldValue<DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey)!)
|
||||
{
|
||||
RemoveFields(subValue as DatItem);
|
||||
}
|
||||
}
|
||||
|
||||
if (dipSwitch.PartSpecified)
|
||||
RemoveFields(dipSwitch.GetFieldValue<Part?>(DipSwitch.PartKey)! as DatItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="dipValue">DipValue to remove fields from</param>
|
||||
private void RemoveFields(DipValue dipValue)
|
||||
{
|
||||
if (dipValue.ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition subCondition in dipValue.GetFieldValue<Condition[]?>(Models.Metadata.DipValue.ConditionKey)!)
|
||||
{
|
||||
RemoveFields(subCondition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="disk">Disk to remove fields from</param>
|
||||
private void RemoveFields(Disk disk)
|
||||
{
|
||||
if (disk.DiskAreaSpecified)
|
||||
RemoveFields(disk.GetFieldValue<DiskArea?>(Disk.DiskAreaKey)! as DatItem);
|
||||
|
||||
if (disk.PartSpecified)
|
||||
RemoveFields(disk.GetFieldValue<Part?>(Disk.PartKey)! as DatItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="input">Input to remove fields from</param>
|
||||
private void RemoveFields(Input input)
|
||||
{
|
||||
if (input.ControlsSpecified)
|
||||
{
|
||||
foreach (Control subControl in input.GetFieldValue<Control[]?>(Models.Metadata.Input.ControlKey)!)
|
||||
{
|
||||
RemoveFields(subControl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="part">Part to remove fields from</param>
|
||||
private void RemoveFields(Part part)
|
||||
{
|
||||
if (part.FeaturesSpecified)
|
||||
{
|
||||
foreach (PartFeature subPartFeature in part.GetFieldValue<PartFeature[]?>(Models.Metadata.Part.FeatureKey)!)
|
||||
{
|
||||
RemoveFields(subPartFeature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="port">Port to remove fields from</param>
|
||||
private void RemoveFields(Port port)
|
||||
{
|
||||
if (port.AnalogsSpecified)
|
||||
{
|
||||
foreach (Analog subAnalog in port.GetFieldValue<Analog[]?>(Models.Metadata.Port.AnalogKey)!)
|
||||
{
|
||||
RemoveFields(subAnalog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="rom">Rom to remove fields from</param>
|
||||
private void RemoveFields(Rom rom)
|
||||
{
|
||||
if (rom.DataAreaSpecified)
|
||||
RemoveFields(rom.GetFieldValue<DataArea?>(Rom.DataAreaKey)!);
|
||||
|
||||
if (rom.PartSpecified)
|
||||
RemoveFields(rom.GetFieldValue<Part?>(Rom.PartKey)! as DatItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields with given values
|
||||
/// </summary>
|
||||
/// <param name="slot">Slot to remove fields from</param>
|
||||
private void RemoveFields(Slot slot)
|
||||
{
|
||||
if (slot.SlotOptionsSpecified)
|
||||
{
|
||||
foreach (SlotOption subSlotOption in slot.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
|
||||
{
|
||||
RemoveFields(subSlotOption);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user