2021-02-01 12:11:32 -08:00
|
|
|
using System.Collections.Generic;
|
2021-02-03 09:07:29 -08:00
|
|
|
using System.Linq;
|
2021-02-01 12:11:32 -08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using SabreTools.Core;
|
|
|
|
|
using SabreTools.DatFiles;
|
|
|
|
|
using SabreTools.DatItems;
|
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>
|
|
|
|
|
/// DatItemRemover to remove fields from DatHeaders
|
|
|
|
|
/// </summary>
|
2024-02-28 19:19:50 -05:00
|
|
|
public DatHeaderRemover? DatHeaderRemover { get; set; }
|
2021-02-01 12:11:32 -08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// DatItemRemover to remove fields from DatItems
|
|
|
|
|
/// </summary>
|
2024-02-28 19:19:50 -05:00
|
|
|
public DatItemRemover? DatItemRemover { get; set; }
|
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
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
public void PopulateExclusionsFromList(List<string> fields)
|
|
|
|
|
{
|
|
|
|
|
// Instantiate the removers, if necessary
|
|
|
|
|
DatHeaderRemover ??= new DatHeaderRemover();
|
|
|
|
|
DatItemRemover ??= new DatItemRemover();
|
|
|
|
|
|
|
|
|
|
// If the list is null or empty, just return
|
|
|
|
|
if (fields == null || fields.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
InternalStopwatch watch = new("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
|
|
|
{
|
2021-02-01 12:11:32 -08:00
|
|
|
// If we don't even have a possible field name
|
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// DatHeader fields
|
|
|
|
|
if (DatHeaderRemover.SetRemover(field))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Machine and DatItem fields
|
|
|
|
|
if (DatItemRemover.SetRemover(field))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// If we didn't match anything, log an error
|
|
|
|
|
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-02 14:09:49 -08:00
|
|
|
|
|
|
|
|
watch.Stop();
|
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)
|
|
|
|
|
{
|
|
|
|
|
// If the removers don't exist, we can't use it
|
|
|
|
|
if (DatHeaderRemover == null && DatItemRemover == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
InternalStopwatch watch = new("Applying removals to DAT");
|
2021-02-01 12:11:32 -08:00
|
|
|
|
|
|
|
|
// Remove DatHeader fields
|
2021-02-03 09:07:29 -08:00
|
|
|
if (DatHeaderRemover != null && DatHeaderRemover.DatHeaderFields.Any())
|
2021-02-01 12:11:32 -08:00
|
|
|
DatHeaderRemover.RemoveFields(datFile.Header);
|
|
|
|
|
|
|
|
|
|
// Remove DatItem and Machine fields
|
2021-02-03 09:07:29 -08:00
|
|
|
if (DatItemRemover != null && (DatItemRemover.MachineFields.Any() || DatItemRemover.DatItemFields.Any()))
|
2021-02-01 12:11:32 -08:00
|
|
|
{
|
2024-02-28 21:59:13 -05:00
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2021-02-01 12:11:32 -08:00
|
|
|
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
|
2024-02-28 21:59:13 -05:00
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
Parallel.ForEach(datFile.Items.Keys, key =>
|
|
|
|
|
#else
|
|
|
|
|
foreach (var key in datFile.Items.Keys)
|
|
|
|
|
#endif
|
2021-02-01 12:11:32 -08:00
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
ConcurrentList<DatItem>? items = datFile.Items[key];
|
|
|
|
|
if (items == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-01 12:11:32 -08:00
|
|
|
for (int j = 0; j < items.Count; j++)
|
|
|
|
|
{
|
|
|
|
|
DatItemRemover.RemoveFields(items[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
datFile.Items.Remove(key);
|
|
|
|
|
datFile.Items.AddRange(key, items);
|
2024-02-28 21:59:13 -05:00
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2021-02-01 12:11:32 -08:00
|
|
|
});
|
2024-02-28 21:59:13 -05:00
|
|
|
#else
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-02-01 12:11:32 -08:00
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|