Files
SabreTools/SabreTools.Filtering/Remover.cs

135 lines
3.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using SabreTools.Core.Filter;
using SabreTools.DatFiles;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging;
2021-01-29 22:54:16 -08:00
namespace SabreTools.Filtering
{
/// <summary>
/// Represents the removal operations that need to be performed on a set of items, usually a DAT
/// </summary>
public class Remover
2021-01-29 22:54:16 -08:00
{
#region Fields
/// <summary>
2024-03-05 16:37:52 -05:00
/// List of header fields to exclude from writing
/// </summary>
2024-03-05 16:37:52 -05:00
public List<string> HeaderFieldNames { get; } = [];
/// <summary>
2024-03-05 16:37:52 -05:00
/// List of machine fields to exclude from writing
/// </summary>
2024-03-05 16:37:52 -05:00
public List<string> MachineFieldNames { get; } = [];
/// <summary>
/// List of fields to exclude from writing
/// </summary>
public Dictionary<string, List<string>> ItemFieldNames { get; } = [];
#endregion
2021-01-29 22:54:16 -08:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
protected Logger logger;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public Remover()
{
logger = new Logger(this);
}
#endregion
#region Population
2021-01-29 22:54:16 -08:00
2024-03-05 16:37:52 -05:00
/// <summary>
/// Populate the exclusion objects using a field name
/// </summary>
2024-03-05 17:17:40 -05:00
/// <param name="field">Field name</param>
2024-03-05 16:37:52 -05:00
public void PopulateExclusions(string field)
=> PopulateExclusionsFromList([field]);
2021-01-29 22:54:16 -08:00
/// <summary>
/// Populate the exclusion objects using a set of field names
2021-01-29 22:54:16 -08:00
/// </summary>
/// <param name="fields">List of field names</param>
2024-03-05 13:32:49 -05:00
public void PopulateExclusionsFromList(List<string>? fields)
{
// If the list is null or empty, just return
if (fields == null || fields.Count == 0)
return;
2024-03-05 16:37:52 -05:00
var watch = new InternalStopwatch("Populating removals from list");
2021-02-03 09:07:29 -08:00
foreach (string field in fields)
2024-02-28 21:59:13 -05:00
{
2024-03-05 16:37:52 -05:00
bool removerSet = SetRemover(field);
if (!removerSet)
logger.Warning($"The value {field} did not match any known field names. Please check the wiki for more details on supported field names.");
}
2024-03-05 16:37:52 -05:00
watch.Stop();
}
2024-03-05 16:37:52 -05:00
/// <summary>
/// Set remover from a value
/// </summary>
/// <param name="field">Key for the remover to be set</param>
private bool SetRemover(string field)
{
// If the key is null or empty, return false
if (string.IsNullOrEmpty(field))
return false;
2024-03-05 16:37:52 -05:00
// Get the parser pair out of it, if possible
2024-03-05 17:17:40 -05:00
(string? type, string? key) = FilterParser.ParseFilterId(field);
if (type == null || key == null)
return false;
switch (type)
2024-03-05 16:37:52 -05:00
{
2024-03-05 17:17:40 -05:00
case Models.Metadata.MetadataFile.HeaderKey:
HeaderFieldNames.Add(key);
return true;
2024-03-05 16:37:52 -05:00
2024-03-05 17:17:40 -05:00
case Models.Metadata.MetadataFile.MachineKey:
MachineFieldNames.Add(key);
return true;
2024-03-05 16:37:52 -05:00
2024-03-05 17:17:40 -05:00
default:
if (!ItemFieldNames.ContainsKey(type))
ItemFieldNames[type] = [];
2024-03-05 16:37:52 -05:00
2024-03-05 17:17:40 -05:00
ItemFieldNames[type].Add(key);
return true;
}
}
#endregion
#region Running
/// <summary>
/// Remove fields from a DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
public void ApplyRemovals(DatFile datFile)
{
InternalStopwatch watch = new("Applying removals to DAT");
datFile.ApplyRemovals(HeaderFieldNames, MachineFieldNames, ItemFieldNames);
2021-02-02 14:09:49 -08:00
watch.Stop();
}
2021-01-29 22:54:16 -08:00
#endregion
}
}