2021-02-01 12:11:32 -08:00
|
|
|
using System.Collections.Generic;
|
2024-03-12 22:52:36 -04:00
|
|
|
using SabreTools.Core.Filter;
|
2021-02-01 12:11:32 -08:00
|
|
|
using SabreTools.DatFiles;
|
2021-01-29 22:54:16 -08:00
|
|
|
using SabreTools.Logging;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Filtering
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the removal operations that need to be performed on a set of items, usually a DAT
|
|
|
|
|
/// </summary>
|
2021-02-01 12:11:32 -08:00
|
|
|
public class Remover
|
2021-01-29 22:54:16 -08:00
|
|
|
{
|
2021-02-01 12:11:32 -08:00
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-05 16:37:52 -05:00
|
|
|
/// List of header fields to exclude from writing
|
2021-02-01 12:11:32 -08:00
|
|
|
/// </summary>
|
2024-03-05 16:37:52 -05:00
|
|
|
public List<string> HeaderFieldNames { get; } = [];
|
2021-02-01 12:11:32 -08:00
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-05 16:37:52 -05:00
|
|
|
/// List of machine fields to exclude from writing
|
2021-02-01 12:11:32 -08:00
|
|
|
/// </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; } = [];
|
2021-02-01 12:11:32 -08:00
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
2021-02-01 12:11:32 -08:00
|
|
|
#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>
|
2021-02-01 12:11:32 -08:00
|
|
|
/// Populate the exclusion objects using a set of field names
|
2021-01-29 22:54:16 -08:00
|
|
|
/// </summary>
|
2021-02-01 12:11:32 -08:00
|
|
|
/// <param name="fields">List of field names</param>
|
2024-03-05 13:32:49 -05:00
|
|
|
public void PopulateExclusionsFromList(List<string>? fields)
|
2021-02-01 12:11:32 -08:00
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
2021-02-01 12:11:32 -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.");
|
|
|
|
|
}
|
2021-02-01 12:11:32 -08:00
|
|
|
|
2024-03-05 16:37:52 -05:00
|
|
|
watch.Stop();
|
|
|
|
|
}
|
2021-02-01 12:11:32 -08:00
|
|
|
|
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;
|
2021-02-01 12:11:32 -08:00
|
|
|
|
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;
|
2021-02-01 12:11:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
InternalStopwatch watch = new("Applying removals to DAT");
|
2024-03-19 19:39:54 -04:00
|
|
|
datFile.ApplyRemovals(HeaderFieldNames, MachineFieldNames, ItemFieldNames);
|
2021-02-02 14:09:49 -08:00
|
|
|
watch.Stop();
|
2021-02-01 12:11:32 -08:00
|
|
|
}
|
2021-01-29 22:54:16 -08:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|