Files
SabreTools/SabreTools.DatTools/Remover.cs

139 lines
4.0 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
2024-10-30 11:26:56 -04:00
namespace SabreTools.DatTools
2021-01-29 22:54:16 -08:00
{
/// <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-10-24 04:01:45 -04:00
public readonly List<string> HeaderFieldNames = [];
/// <summary>
2024-03-05 16:37:52 -05:00
/// List of machine fields to exclude from writing
/// </summary>
2024-10-24 04:01:45 -04:00
public readonly List<string> MachineFieldNames = [];
2024-03-05 16:37:52 -05:00
/// <summary>
/// List of fields to exclude from writing
/// </summary>
2024-10-24 04:01:45 -04:00
public readonly Dictionary<string, List<string>> ItemFieldNames = [];
#endregion
2021-01-29 22:54:16 -08:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
2025-01-08 16:59:44 -05:00
protected Logger _logger;
2021-01-29 22:54:16 -08:00
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public Remover()
{
2025-01-08 16:59:44 -05:00
_logger = new Logger(this);
2021-01-29 22:54:16 -08:00
}
#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)
2025-01-08 16:59:44 -05:00
_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
}
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-10-24 04:01:45 -04:00
try
2024-03-05 16:37:52 -05:00
{
2024-10-24 04:01:45 -04:00
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
/// <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
}
}