using System.Collections.Generic;
using SabreTools.Core.Filter;
using SabreTools.DatFiles;
using SabreTools.Logging;
namespace SabreTools.Filtering
{
///
/// Represents the removal operations that need to be performed on a set of items, usually a DAT
///
public class Remover
{
#region Fields
///
/// List of header fields to exclude from writing
///
public List HeaderFieldNames { get; } = [];
///
/// List of machine fields to exclude from writing
///
public List MachineFieldNames { get; } = [];
///
/// List of fields to exclude from writing
///
public Dictionary> ItemFieldNames { get; } = [];
#endregion
#region Logging
///
/// Logging object
///
protected Logger logger;
#endregion
#region Constructors
///
/// Constructor
///
public Remover()
{
logger = new Logger(this);
}
#endregion
#region Population
///
/// Populate the exclusion objects using a field name
///
/// Field name
public void PopulateExclusions(string field)
=> PopulateExclusionsFromList([field]);
///
/// Populate the exclusion objects using a set of field names
///
/// List of field names
public void PopulateExclusionsFromList(List? fields)
{
// If the list is null or empty, just return
if (fields == null || fields.Count == 0)
return;
var watch = new InternalStopwatch("Populating removals from list");
foreach (string field in fields)
{
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.");
}
watch.Stop();
}
///
/// Set remover from a value
///
/// Key for the remover to be set
private bool SetRemover(string field)
{
// If the key is null or empty, return false
if (string.IsNullOrEmpty(field))
return false;
// Get the parser pair out of it, if possible
(string? type, string? key) = FilterParser.ParseFilterId(field);
if (type == null || key == null)
return false;
switch (type)
{
case Models.Metadata.MetadataFile.HeaderKey:
HeaderFieldNames.Add(key);
return true;
case Models.Metadata.MetadataFile.MachineKey:
MachineFieldNames.Add(key);
return true;
default:
if (!ItemFieldNames.ContainsKey(type))
ItemFieldNames[type] = [];
ItemFieldNames[type].Add(key);
return true;
}
}
#endregion
#region Running
///
/// Remove fields from a DatFile
///
/// Current DatFile object to run operations on
public void ApplyRemovals(DatFile datFile)
{
InternalStopwatch watch = new("Applying removals to DAT");
datFile.ApplyRemovals(HeaderFieldNames, MachineFieldNames, ItemFieldNames);
watch.Stop();
}
#endregion
}
}