using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using SabreTools.Core; using SabreTools.DatFiles; using SabreTools.DatItems; 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 /// /// DatItemRemover to remove fields from DatHeaders /// public DatHeaderRemover? DatHeaderRemover { get; set; } /// /// DatItemRemover to remove fields from DatItems /// public DatItemRemover? DatItemRemover { get; set; } #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 set of field names /// /// List of field names public void PopulateExclusionsFromList(List 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; InternalStopwatch watch = new("Populating removals from list"); foreach (string field in fields) { // 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."); } watch.Stop(); } #endregion #region Running /// /// Remove fields from a DatFile /// /// Current DatFile object to run operations on public void ApplyRemovals(DatFile datFile) { // If the removers don't exist, we can't use it if (DatHeaderRemover == null && DatItemRemover == null) return; InternalStopwatch watch = new("Applying removals to DAT"); // Remove DatHeader fields if (DatHeaderRemover != null && DatHeaderRemover.DatHeaderFields.Any()) DatHeaderRemover.RemoveFields(datFile.Header); // Remove DatItem and Machine fields if (DatItemRemover != null && (DatItemRemover.MachineFields.Any() || DatItemRemover.DatItemFields.Any())) { Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key => { ConcurrentList? items = datFile.Items[key]; if (items == null) return; for (int j = 0; j < items.Count; j++) { DatItemRemover.RemoveFields(items[j]); } datFile.Items.Remove(key); datFile.Items.AddRange(key, items); }); } watch.Stop(); } #endregion } }