using System.Collections.Generic;
using SabreTools.Core.Filter;
using SabreTools.DatFiles;
using SabreTools.IO.Logging;
namespace SabreTools.DatTools
{
///
/// 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 readonly List HeaderFieldNames = [];
///
/// List of machine fields to exclude from writing
///
public readonly List MachineFieldNames = [];
///
/// List of fields to exclude from writing
///
public readonly Dictionary> ItemFieldNames = [];
#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
try
{
var key = new FilterKey(field);
switch (key.ItemName)
{
case Models.Metadata.MetadataFile.HeaderKey:
HeaderFieldNames.Add(key.FieldName);
return true;
case Models.Metadata.MetadataFile.MachineKey:
MachineFieldNames.Add(key.FieldName);
return true;
default:
if (!ItemFieldNames.ContainsKey(key.ItemName))
ItemFieldNames[key.ItemName] = [];
ItemFieldNames[key.ItemName].Add(key.FieldName);
return true;
}
}
catch
{
return false;
}
}
#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
}
}