Items are responsible for their own filters

This commit is contained in:
Matt Nadareski
2020-08-17 23:09:35 -07:00
parent 42b3bd906a
commit 5c11066a07
6 changed files with 529 additions and 491 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.Library.Filtering;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace SabreTools.Library.DatItems namespace SabreTools.Library.DatItems
@@ -135,6 +136,32 @@ namespace SabreTools.Library.DatItems
#endregion #endregion
#region Filtering
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <returns>True if the item passed the filter, false otherwise</returns>
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on description
if (filter.Description.MatchesNeutral(null, Description) == false)
return false;
// Filter on default
if (filter.Default.MatchesNeutral(null, Default) == false)
return false;
return true;
}
#endregion
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <summary>

View File

@@ -7,6 +7,7 @@ using System.Net;
using SabreTools.Library.Data; using SabreTools.Library.Data;
using SabreTools.Library.DatFiles; using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes; using SabreTools.Library.FileTypes;
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools; using SabreTools.Library.Tools;
using NaturalSort; using NaturalSort;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -887,6 +888,201 @@ namespace SabreTools.Library.DatItems
#endregion #endregion
#region Filtering
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <returns>True if the item passed the filter, false otherwise</returns>
public virtual bool PassesFilter(Filter filter)
{
#region Machine Filters
// Filter on machine name
bool? machineNameFound = filter.MachineName.MatchesPositiveSet(MachineName);
if (filter.IncludeOfInGame)
{
machineNameFound |= (filter.MachineName.MatchesPositiveSet(CloneOf) == true);
machineNameFound |= (filter.MachineName.MatchesPositiveSet(RomOf) == true);
}
if (machineNameFound == false)
return false;
machineNameFound = filter.MachineName.MatchesNegativeSet(MachineName);
if (filter.IncludeOfInGame)
{
machineNameFound |= (filter.MachineName.MatchesNegativeSet(CloneOf) == true);
machineNameFound |= (filter.MachineName.MatchesNegativeSet(RomOf) == true);
}
if (machineNameFound == false)
return false;
// Filter on comment
if (filter.Comment.MatchesPositiveSet(Comment) == false)
return false;
if (filter.Comment.MatchesNegativeSet(Comment) == true)
return false;
// Filter on machine description
if (filter.MachineDescription.MatchesPositiveSet(MachineDescription) == false)
return false;
if (filter.MachineDescription.MatchesNegativeSet(MachineDescription) == true)
return false;
// Filter on year
if (filter.Year.MatchesPositiveSet(Year) == false)
return false;
if (filter.Year.MatchesNegativeSet(Year) == true)
return false;
// Filter on manufacturer
if (filter.Manufacturer.MatchesPositiveSet(Manufacturer) == false)
return false;
if (filter.Manufacturer.MatchesNegativeSet(Manufacturer) == true)
return false;
// Filter on publisher
if (filter.Publisher.MatchesPositiveSet(Publisher) == false)
return false;
if (filter.Publisher.MatchesNegativeSet(Publisher) == true)
return false;
// Filter on category
if (filter.Category.MatchesPositiveSet(Category) == false)
return false;
if (filter.Category.MatchesNegativeSet(Category) == true)
return false;
// Filter on romof
if (filter.RomOf.MatchesPositiveSet(RomOf) == false)
return false;
if (filter.RomOf.MatchesNegativeSet(RomOf) == true)
return false;
// Filter on cloneof
if (filter.CloneOf.MatchesPositiveSet(CloneOf) == false)
return false;
if (filter.CloneOf.MatchesNegativeSet(CloneOf) == true)
return false;
// Filter on sampleof
if (filter.SampleOf.MatchesPositiveSet(SampleOf) == false)
return false;
if (filter.SampleOf.MatchesNegativeSet(SampleOf) == true)
return false;
// Filter on supported
if (filter.Supported.MatchesNeutral(null, Supported) == false)
return false;
// Filter on source file
if (filter.SourceFile.MatchesPositiveSet(SourceFile) == false)
return false;
if (filter.SourceFile.MatchesNegativeSet(SourceFile) == true)
return false;
// Filter on runnable
if (filter.Runnable.MatchesNeutral(null, Runnable) == false)
return false;
// Filter on board
if (filter.Board.MatchesPositiveSet(Board) == false)
return false;
if (filter.Board.MatchesNegativeSet(Board) == true)
return false;
// Filter on rebuildto
if (filter.RebuildTo.MatchesPositiveSet(RebuildTo) == false)
return false;
if (filter.RebuildTo.MatchesNegativeSet(RebuildTo) == true)
return false;
// Filter on devices
if (Devices != null && Devices.Any())
{
bool anyPositiveDevice = false;
bool anyNegativeDevice = false;
foreach (string device in Devices)
{
anyPositiveDevice |= filter.Devices.MatchesPositiveSet(device) != false;
anyNegativeDevice |= filter.Devices.MatchesNegativeSet(device) == false;
}
if (!anyPositiveDevice || anyNegativeDevice)
return false;
}
// Filter on slot options
if (SlotOptions != null && SlotOptions.Any())
{
bool anyPositiveSlotOption = false;
bool anyNegativeSlotOption = false;
foreach (string slotOption in SlotOptions)
{
anyPositiveSlotOption |= filter.SlotOptions.MatchesPositiveSet(slotOption) != false;
anyNegativeSlotOption |= filter.SlotOptions.MatchesNegativeSet(slotOption) == false;
}
if (!anyPositiveSlotOption || anyNegativeSlotOption)
return false;
}
// Filter on machine type
if (filter.MachineTypes.MatchesPositive(MachineType.NULL, MachineType) == false)
return false;
if (filter.MachineTypes.MatchesNegative(MachineType.NULL, MachineType) == true)
return false;
#endregion
#region DatItem Filters
// Filter on item type
if (filter.ItemTypes.MatchesPositiveSet(ItemType.ToString()) == false)
return false;
if (filter.ItemTypes.MatchesNegativeSet(ItemType.ToString()) == true)
return false;
// Filter on item name
if (filter.ItemName.MatchesPositiveSet(Name) == false)
return false;
if (filter.ItemName.MatchesNegativeSet(Name) == true)
return false;
// Filter on part name
if (filter.PartName.MatchesPositiveSet(PartName) == false)
return false;
if (filter.PartName.MatchesNegativeSet(PartName) == true)
return false;
// Filter on part interface
if (filter.PartInterface.MatchesPositiveSet(PartInterface) == false)
return false;
if (filter.PartInterface.MatchesNegativeSet(PartInterface) == true)
return false;
// Filter on area name
if (filter.AreaName.MatchesPositiveSet(AreaName) == false)
return false;
if (filter.AreaName.MatchesNegativeSet(AreaName) == true)
return false;
// Filter on area size
if (filter.AreaSize.MatchesNeutral(null, AreaSize) == false)
return false;
else if (filter.AreaSize.MatchesPositive(null, AreaSize) == false)
return false;
else if (filter.AreaSize.MatchesNegative(null, AreaSize) == false)
return false;
#endregion
return true;
}
#endregion
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <summary>

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.Library.DatFiles; using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes; using SabreTools.Library.FileTypes;
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools; using SabreTools.Library.Tools;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -481,6 +482,94 @@ namespace SabreTools.Library.DatItems
#endregion #endregion
#region Filtering
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <returns>True if the item passed the filter, false otherwise</returns>
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on MD5
if (filter.MD5.MatchesPositiveSet(MD5) == false)
return false;
if (filter.MD5.MatchesNegativeSet(MD5) == true)
return false;
#if NET_FRAMEWORK
// Filter on RIPEMD160
if (filter.RIPEMD160.MatchesPositiveSet(RIPEMD160) == false)
return false;
if (filter.RIPEMD160.MatchesNegativeSet(RIPEMD160) == true)
return false;
#endif
// Filter on SHA-1
if (filter.SHA1.MatchesPositiveSet(SHA1) == false)
return false;
if (filter.SHA1.MatchesNegativeSet(SHA1) == true)
return false;
// Filter on SHA-256
if (filter.SHA256.MatchesPositiveSet(SHA256) == false)
return false;
if (filter.SHA256.MatchesNegativeSet(SHA256) == true)
return false;
// Filter on SHA-384
if (filter.SHA384.MatchesPositiveSet(SHA384) == false)
return false;
if (filter.SHA384.MatchesNegativeSet(SHA384) == true)
return false;
// Filter on SHA-512
if (filter.SHA512.MatchesPositiveSet(SHA512) == false)
return false;
if (filter.SHA512.MatchesNegativeSet(SHA512) == true)
return false;
// Filter on merge tag
if (filter.MergeTag.MatchesPositiveSet(MergeTag) == false)
return false;
if (filter.MergeTag.MatchesNegativeSet(MergeTag) == true)
return false;
// Filter on region
if (filter.Region.MatchesPositiveSet(Region) == false)
return false;
if (filter.Region.MatchesNegativeSet(Region) == true)
return false;
// Filter on index
if (filter.Index.MatchesPositiveSet(Index) == false)
return false;
if (filter.Index.MatchesNegativeSet(Index) == true)
return false;
// Filter on writable
if (filter.Writable.MatchesNeutral(null, Writable) == false)
return false;
// Filter on status
if (filter.Status.MatchesPositive(ItemStatus.NULL, ItemStatus) == false)
return false;
if (filter.Status.MatchesNegative(ItemStatus.NULL, ItemStatus) == true)
return false;
// Filter on optional
if (filter.Optional.MatchesNeutral(null, Optional) == false)
return false;
return true;
}
#endregion
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <summary>

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.Library.Filtering;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace SabreTools.Library.DatItems namespace SabreTools.Library.DatItems
@@ -163,6 +164,46 @@ namespace SabreTools.Library.DatItems
#endregion #endregion
#region Filtering
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <returns>True if the item passed the filter, false otherwise</returns>
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on region
if (filter.Region.MatchesPositiveSet(Region) == false)
return false;
if (filter.Region.MatchesNegativeSet(Region) == true)
return false;
// Filter on language
if (filter.Language.MatchesPositiveSet(Language) == false)
return false;
if (filter.Language.MatchesNegativeSet(Language) == true)
return false;
// Filter on date
if (filter.Date.MatchesPositiveSet(Date) == false)
return false;
if (filter.Date.MatchesNegativeSet(Date) == true)
return false;
// Filter on default
if (filter.Default.MatchesNeutral(null, Default) == false)
return false;
return true;
}
#endregion
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <summary>

View File

@@ -2,6 +2,7 @@
using SabreTools.Library.Data; using SabreTools.Library.Data;
using SabreTools.Library.DatFiles; using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes; using SabreTools.Library.FileTypes;
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools; using SabreTools.Library.Tools;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -506,6 +507,120 @@ namespace SabreTools.Library.DatItems
#endregion #endregion
#region Filtering
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <returns>True if the item passed the filter, false otherwise</returns>
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on bios
if (filter.Bios.MatchesPositiveSet(Bios) == false)
return false;
if (filter.Bios.MatchesNegativeSet(Bios) == true)
return false;
// Filter on rom size
if (filter.Size.MatchesNeutral(-1, Size) == false)
return false;
else if (filter.Size.MatchesPositive(-1, Size) == false)
return false;
else if (filter.Size.MatchesNegative(-1, Size) == false)
return false;
// Filter on CRC
if (filter.CRC.MatchesPositiveSet(CRC) == false)
return false;
if (filter.CRC.MatchesNegativeSet(CRC) == true)
return false;
// Filter on MD5
if (filter.MD5.MatchesPositiveSet(MD5) == false)
return false;
if (filter.MD5.MatchesNegativeSet(MD5) == true)
return false;
#if NET_FRAMEWORK
// Filter on RIPEMD160
if (filter.RIPEMD160.MatchesPositiveSet(RIPEMD160) == false)
return false;
if (filter.RIPEMD160.MatchesNegativeSet(RIPEMD160) == true)
return false;
#endif
// Filter on SHA-1
if (filter.SHA1.MatchesPositiveSet(SHA1) == false)
return false;
if (filter.SHA1.MatchesNegativeSet(SHA1) == true)
return false;
// Filter on SHA-256
if (filter.SHA256.MatchesPositiveSet(SHA256) == false)
return false;
if (filter.SHA256.MatchesNegativeSet(SHA256) == true)
return false;
// Filter on SHA-384
if (filter.SHA384.MatchesPositiveSet(SHA384) == false)
return false;
if (filter.SHA384.MatchesNegativeSet(SHA384) == true)
return false;
// Filter on SHA-512
if (filter.SHA512.MatchesPositiveSet(SHA512) == false)
return false;
if (filter.SHA512.MatchesNegativeSet(SHA512) == true)
return false;
// Filter on merge tag
if (filter.MergeTag.MatchesPositiveSet(MergeTag) == false)
return false;
if (filter.MergeTag.MatchesNegativeSet(MergeTag) == true)
return false;
// Filter on region
if (filter.Region.MatchesPositiveSet(Region) == false)
return false;
if (filter.Region.MatchesNegativeSet(Region) == true)
return false;
// Filter on offset
if (filter.Offset.MatchesPositiveSet(Offset) == false)
return false;
if (filter.Offset.MatchesNegativeSet(Offset) == true)
return false;
// Filter on date
if (filter.Date.MatchesPositiveSet(Date) == false)
return false;
if (filter.Date.MatchesNegativeSet(Date) == true)
return false;
// Filter on status
if (filter.Status.MatchesPositive(ItemStatus.NULL, ItemStatus) == false)
return false;
if (filter.Status.MatchesNegative(ItemStatus.NULL, ItemStatus) == true)
return false;
// Filter on optional
if (filter.Optional.MatchesNeutral(null, Optional) == false)
return false;
// Filter on inverted
if (filter.Inverted.MatchesNeutral(null, Inverted) == false)
return false;
return true;
}
#endregion
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <summary>

View File

@@ -19,101 +19,101 @@ namespace SabreTools.Library.Filtering
/// TODO: Can clever use of Filtering allow for easier external splitting methods? /// TODO: Can clever use of Filtering allow for easier external splitting methods?
public class Filter public class Filter
{ {
#region Private instance variables #region Pubically facing variables
#region Machine Filters #region Machine Filters
/// <summary> /// <summary>
/// Include or exclude machine names /// Include or exclude machine names
/// </summary> /// </summary>
private FilterItem<string> MachineName = new FilterItem<string>(); public FilterItem<string> MachineName { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine comments /// Include or exclude machine comments
/// </summary> /// </summary>
private FilterItem<string> Comment = new FilterItem<string>(); public FilterItem<string> Comment { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine descriptions /// Include or exclude machine descriptions
/// </summary> /// </summary>
private FilterItem<string> MachineDescription = new FilterItem<string>(); public FilterItem<string> MachineDescription { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine years /// Include or exclude machine years
/// </summary> /// </summary>
private FilterItem<string> Year = new FilterItem<string>(); public FilterItem<string> Year { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine manufacturers /// Include or exclude machine manufacturers
/// </summary> /// </summary>
private FilterItem<string> Manufacturer = new FilterItem<string>(); public FilterItem<string> Manufacturer { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine publishers /// Include or exclude machine publishers
/// </summary> /// </summary>
private FilterItem<string> Publisher = new FilterItem<string>(); public FilterItem<string> Publisher { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine categories /// Include or exclude machine categories
/// </summary> /// </summary>
private FilterItem<string> Category = new FilterItem<string>(); public FilterItem<string> Category { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine romof /// Include or exclude machine romof
/// </summary> /// </summary>
private FilterItem<string> RomOf = new FilterItem<string>(); public FilterItem<string> RomOf { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine cloneof /// Include or exclude machine cloneof
/// </summary> /// </summary>
private FilterItem<string> CloneOf = new FilterItem<string>(); public FilterItem<string> CloneOf { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine sampleof /// Include or exclude machine sampleof
/// </summary> /// </summary>
private FilterItem<string> SampleOf = new FilterItem<string>(); public FilterItem<string> SampleOf { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude items with the "Supported" tag /// Include or exclude items with the "Supported" tag
/// </summary> /// </summary>
private FilterItem<bool?> Supported = new FilterItem<bool?>() { Neutral = null }; public FilterItem<bool?> Supported { get; private set; } = new FilterItem<bool?>() { Neutral = null };
/// <summary> /// <summary>
/// Include or exclude machine source file /// Include or exclude machine source file
/// </summary> /// </summary>
private FilterItem<string> SourceFile = new FilterItem<string>(); public FilterItem<string> SourceFile { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude items with the "Runnable" tag /// Include or exclude items with the "Runnable" tag
/// </summary> /// </summary>
private FilterItem<bool?> Runnable = new FilterItem<bool?>() { Neutral = null }; public FilterItem<bool?> Runnable { get; private set; } = new FilterItem<bool?>() { Neutral = null };
/// <summary> /// <summary>
/// Include or exclude machine board /// Include or exclude machine board
/// </summary> /// </summary>
private FilterItem<string> Board = new FilterItem<string>(); public FilterItem<string> Board { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine rebuildto /// Include or exclude machine rebuildto
/// </summary> /// </summary>
private FilterItem<string> RebuildTo = new FilterItem<string>(); public FilterItem<string> RebuildTo { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine devices /// Include or exclude machine devices
/// </summary> /// </summary>
private FilterItem<string> Devices = new FilterItem<string>(); public FilterItem<string> Devices { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude machine slotoptions /// Include or exclude machine slotoptions
/// </summary> /// </summary>
private FilterItem<string> SlotOptions = new FilterItem<string>(); public FilterItem<string> SlotOptions { get; private set; } = new FilterItem<string>();
// TODO: Machine.Infos - List<KeyValuePair<string, string>> // TODO: Machine.Infos - List<KeyValuePair<string, string>>
/// <summary> /// <summary>
/// Include or exclude machine types /// Include or exclude machine types
/// </summary> /// </summary>
private FilterItem<MachineType> MachineTypes = new FilterItem<MachineType>() { Positive = MachineType.NULL, Negative = MachineType.NULL }; public FilterItem<MachineType> MachineTypes { get; private set; } = new FilterItem<MachineType>() { Positive = MachineType.NULL, Negative = MachineType.NULL };
#endregion #endregion
@@ -122,150 +122,146 @@ namespace SabreTools.Library.Filtering
/// <summary> /// <summary>
/// Include or exclude item types /// Include or exclude item types
/// </summary> /// </summary>
private FilterItem<string> ItemTypes = new FilterItem<string>(); public FilterItem<string> ItemTypes { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude item names /// Include or exclude item names
/// </summary> /// </summary>
private FilterItem<string> ItemName = new FilterItem<string>(); public FilterItem<string> ItemName { get; private set; } = new FilterItem<string>();
// TODO: DatItem.Features - List<KeyValuePair<string, string>> // TODO: DatItem.Features - List<KeyValuePair<string, string>>
/// <summary> /// <summary>
/// Include or exclude part names /// Include or exclude part names
/// </summary> /// </summary>
private FilterItem<string> PartName = new FilterItem<string>(); public FilterItem<string> PartName { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude part interfaces /// Include or exclude part interfaces
/// </summary> /// </summary>
private FilterItem<string> PartInterface = new FilterItem<string>(); public FilterItem<string> PartInterface { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude area names /// Include or exclude area names
/// </summary> /// </summary>
private FilterItem<string> AreaName = new FilterItem<string>(); public FilterItem<string> AreaName { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude area sizes /// Include or exclude area sizes
/// </summary> /// </summary>
/// <remarks>Positive means "Greater than or equal", Negative means "Less than or equal", Neutral means "Equal"</remarks> /// <remarks>Positive means "Greater than or equal", Negative means "Less than or equal", Neutral means "Equal"</remarks>
private FilterItem<long?> AreaSize = new FilterItem<long?>() { Positive = null, Negative = null, Neutral = null }; public FilterItem<long?> AreaSize { get; private set; } = new FilterItem<long?>() { Positive = null, Negative = null, Neutral = null };
/// <summary> /// <summary>
/// Include or exclude items with the "Default" tag /// Include or exclude items with the "Default" tag
/// </summary> /// </summary>
private FilterItem<bool?> Default = new FilterItem<bool?>() { Neutral = null }; public FilterItem<bool?> Default { get; private set; } = new FilterItem<bool?>() { Neutral = null };
/// <summary> /// <summary>
/// Include or exclude descriptions /// Include or exclude descriptions
/// </summary> /// </summary>
private FilterItem<string> Description = new FilterItem<string>(); public FilterItem<string> Description { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude item sizes /// Include or exclude item sizes
/// </summary> /// </summary>
/// <remarks>Positive means "Greater than or equal", Negative means "Less than or equal", Neutral means "Equal"</remarks> /// <remarks>Positive means "Greater than or equal", Negative means "Less than or equal", Neutral means "Equal"</remarks>
private FilterItem<long> Size = new FilterItem<long>() { Positive = -1, Negative = -1, Neutral = -1 }; public FilterItem<long> Size { get; private set; } = new FilterItem<long>() { Positive = -1, Negative = -1, Neutral = -1 };
/// <summary> /// <summary>
/// Include or exclude CRC32 hashes /// Include or exclude CRC32 hashes
/// </summary> /// </summary>
private FilterItem<string> CRC = new FilterItem<string>(); public FilterItem<string> CRC { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude MD5 hashes /// Include or exclude MD5 hashes
/// </summary> /// </summary>
private FilterItem<string> MD5 = new FilterItem<string>(); public FilterItem<string> MD5 { get; private set; } = new FilterItem<string>();
#if NET_FRAMEWORK #if NET_FRAMEWORK
/// <summary> /// <summary>
/// Include or exclude RIPEMD160 hashes /// Include or exclude RIPEMD160 hashes
/// </summary> /// </summary>
private FilterItem<string> RIPEMD160 = new FilterItem<string>(); public FilterItem<string> RIPEMD160 { get; private set; } = new FilterItem<string>();
#endif #endif
/// <summary> /// <summary>
/// Include or exclude SHA-1 hashes /// Include or exclude SHA-1 hashes
/// </summary> /// </summary>
private FilterItem<string> SHA1 = new FilterItem<string>(); public FilterItem<string> SHA1 { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude SHA-256 hashes /// Include or exclude SHA-256 hashes
/// </summary> /// </summary>
private FilterItem<string> SHA256 = new FilterItem<string>(); public FilterItem<string> SHA256 { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude SHA-384 hashes /// Include or exclude SHA-384 hashes
/// </summary> /// </summary>
private FilterItem<string> SHA384 = new FilterItem<string>(); public FilterItem<string> SHA384 { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude SHA-512 hashes /// Include or exclude SHA-512 hashes
/// </summary> /// </summary>
private FilterItem<string> SHA512 = new FilterItem<string>(); public FilterItem<string> SHA512 { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude merge tags /// Include or exclude merge tags
/// </summary> /// </summary>
private FilterItem<string> MergeTag = new FilterItem<string>(); public FilterItem<string> MergeTag { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude regions /// Include or exclude regions
/// </summary> /// </summary>
private FilterItem<string> Region = new FilterItem<string>(); public FilterItem<string> Region { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude indexes /// Include or exclude indexes
/// </summary> /// </summary>
private FilterItem<string> Index = new FilterItem<string>(); public FilterItem<string> Index { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude items with the "Writable" tag /// Include or exclude items with the "Writable" tag
/// </summary> /// </summary>
private FilterItem<bool?> Writable = new FilterItem<bool?>() { Neutral = null }; public FilterItem<bool?> Writable { get; private set; } = new FilterItem<bool?>() { Neutral = null };
/// <summary> /// <summary>
/// Include or exclude items with the "Writable" tag /// Include or exclude items with the "Writable" tag
/// </summary> /// </summary>
private FilterItem<bool?> Optional = new FilterItem<bool?>() { Neutral = null }; public FilterItem<bool?> Optional { get; private set; } = new FilterItem<bool?>() { Neutral = null };
/// <summary> /// <summary>
/// Include or exclude item statuses /// Include or exclude item statuses
/// </summary> /// </summary>
private FilterItem<ItemStatus> Status = new FilterItem<ItemStatus>() { Positive = ItemStatus.NULL, Negative = ItemStatus.NULL }; public FilterItem<ItemStatus> Status { get; private set; } = new FilterItem<ItemStatus>() { Positive = ItemStatus.NULL, Negative = ItemStatus.NULL };
/// <summary> /// <summary>
/// Include or exclude languages /// Include or exclude languages
/// </summary> /// </summary>
private FilterItem<string> Language = new FilterItem<string>(); public FilterItem<string> Language { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude dates /// Include or exclude dates
/// </summary> /// </summary>
private FilterItem<string> Date = new FilterItem<string>(); public FilterItem<string> Date { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude bioses /// Include or exclude bioses
/// </summary> /// </summary>
private FilterItem<string> Bios = new FilterItem<string>(); public FilterItem<string> Bios { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude offsets /// Include or exclude offsets
/// </summary> /// </summary>
private FilterItem<string> Offset = new FilterItem<string>(); public FilterItem<string> Offset { get; private set; } = new FilterItem<string>();
/// <summary> /// <summary>
/// Include or exclude offsets /// Include or exclude offsets
/// </summary> /// </summary>
private FilterItem<bool?> Inverted = new FilterItem<bool?>(); public FilterItem<bool?> Inverted { get; private set; } = new FilterItem<bool?>();
#endregion #endregion
#endregion // Private instance variables
#region Pubically facing variables
#region Manipulation Flags #region Manipulation Flags
/// <summary> /// <summary>
@@ -806,20 +802,21 @@ namespace SabreTools.Library.Filtering
/// <param name="datFile">DatFile to filter</param> /// <param name="datFile">DatFile to filter</param>
/// <param name="useTags">True if DatFile tags override splitting, false otherwise</param> /// <param name="useTags">True if DatFile tags override splitting, false otherwise</param>
/// <returns>True if the DatFile was filtered, false on error</returns> /// <returns>True if the DatFile was filtered, false on error</returns>
/// TODO: Should this be inverted to be in DatFile?
public bool FilterDatFile(DatFile datFile, bool useTags) public bool FilterDatFile(DatFile datFile, bool useTags)
{ {
try try
{ {
// Process description to machine name // Process description to machine name
if (this.DescriptionAsName) if (DescriptionAsName)
MachineDescriptionToName(datFile); MachineDescriptionToName(datFile);
// If we are using tags from the DAT, set the proper input for split type unless overridden // If we are using tags from the DAT, set the proper input for split type unless overridden
if (useTags && this.InternalSplit == SplitType.None) if (useTags && InternalSplit == SplitType.None)
this.InternalSplit = datFile.Header.ForceMerging.AsSplitType(); InternalSplit = datFile.Header.ForceMerging.AsSplitType();
// Run internal splitting // Run internal splitting
ProcessSplitType(datFile, this.InternalSplit); ProcessSplitType(datFile, InternalSplit);
// Loop over every key in the dictionary // Loop over every key in the dictionary
List<string> keys = datFile.Items.Keys.ToList(); List<string> keys = datFile.Items.Keys.ToList();
@@ -830,11 +827,15 @@ namespace SabreTools.Library.Filtering
List<DatItem> newitems = new List<DatItem>(); List<DatItem> newitems = new List<DatItem>();
foreach (DatItem item in items) foreach (DatItem item in items)
{ {
// If we have a null item, we can't pass it
if (item == null)
continue;
// If the rom passes the filter, include it // If the rom passes the filter, include it
if (ItemPasses(item)) if (item.PassesFilter(this))
{ {
// If we're stripping unicode characters, do so from all relevant things // If we're stripping unicode characters, do so from all relevant things
if (this.RemoveUnicode) if (RemoveUnicode)
{ {
item.Name = Sanitizer.RemoveUnicodeCharacters(item.Name); item.Name = Sanitizer.RemoveUnicodeCharacters(item.Name);
item.MachineName = Sanitizer.RemoveUnicodeCharacters(item.MachineName); item.MachineName = Sanitizer.RemoveUnicodeCharacters(item.MachineName);
@@ -842,21 +843,21 @@ namespace SabreTools.Library.Filtering
} }
// If we're in cleaning mode, do so from all relevant things // If we're in cleaning mode, do so from all relevant things
if (this.Clean) if (Clean)
{ {
item.MachineName = Sanitizer.CleanGameName(item.MachineName); item.MachineName = Sanitizer.CleanGameName(item.MachineName);
item.MachineDescription = Sanitizer.CleanGameName(item.MachineDescription); item.MachineDescription = Sanitizer.CleanGameName(item.MachineDescription);
} }
// If we are in single game mode, rename all games // If we are in single game mode, rename all games
if (this.Single) if (Single)
item.MachineName = "!"; item.MachineName = "!";
// If we are in NTFS trim mode, trim the game name // If we are in NTFS trim mode, trim the game name
if (this.Trim) if (Trim)
{ {
// Windows max name length is 260 // Windows max name length is 260
int usableLength = 260 - item.MachineName.Length - this.Root.Length; int usableLength = 260 - item.MachineName.Length - Root.Length;
if (item.Name.Length > usableLength) if (item.Name.Length > usableLength)
{ {
string ext = Path.GetExtension(item.Name); string ext = Path.GetExtension(item.Name);
@@ -919,437 +920,6 @@ namespace SabreTools.Library.Filtering
return true; return true;
} }
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="item">DatItem to check</param>
/// <returns>True if the file passed the filter, false otherwise</returns>
public bool ItemPasses(DatItem item)
{
// If the item is null, we automatically fail it
if (item == null)
return false;
#region Machine Filters
// Filter on machine name
bool? machineNameFound = this.MachineName.MatchesPositiveSet(item.MachineName);
if (this.IncludeOfInGame)
{
machineNameFound |= (this.MachineName.MatchesPositiveSet(item.CloneOf) == true);
machineNameFound |= (this.MachineName.MatchesPositiveSet(item.RomOf) == true);
}
if (machineNameFound == false)
return false;
machineNameFound = this.MachineName.MatchesNegativeSet(item.MachineName);
if (this.IncludeOfInGame)
{
machineNameFound |= (this.MachineName.MatchesNegativeSet(item.CloneOf) == true);
machineNameFound |= (this.MachineName.MatchesNegativeSet(item.RomOf) == true);
}
if (machineNameFound == false)
return false;
// Filter on comment
if (this.Comment.MatchesPositiveSet(item.Comment) == false)
return false;
if (this.Comment.MatchesNegativeSet(item.Comment) == true)
return false;
// Filter on machine description
if (this.MachineDescription.MatchesPositiveSet(item.MachineDescription) == false)
return false;
if (this.MachineDescription.MatchesNegativeSet(item.MachineDescription) == true)
return false;
// Filter on year
if (this.Year.MatchesPositiveSet(item.Year) == false)
return false;
if (this.Year.MatchesNegativeSet(item.Year) == true)
return false;
// Filter on manufacturer
if (this.Manufacturer.MatchesPositiveSet(item.Manufacturer) == false)
return false;
if (this.Manufacturer.MatchesNegativeSet(item.Manufacturer) == true)
return false;
// Filter on publisher
if (this.Publisher.MatchesPositiveSet(item.Publisher) == false)
return false;
if (this.Publisher.MatchesNegativeSet(item.Publisher) == true)
return false;
// Filter on category
if (this.Category.MatchesPositiveSet(item.Category) == false)
return false;
if (this.Category.MatchesNegativeSet(item.Category) == true)
return false;
// Filter on romof
if (this.RomOf.MatchesPositiveSet(item.RomOf) == false)
return false;
if (this.RomOf.MatchesNegativeSet(item.RomOf) == true)
return false;
// Filter on cloneof
if (this.CloneOf.MatchesPositiveSet(item.CloneOf) == false)
return false;
if (this.CloneOf.MatchesNegativeSet(item.CloneOf) == true)
return false;
// Filter on sampleof
if (this.SampleOf.MatchesPositiveSet(item.SampleOf) == false)
return false;
if (this.SampleOf.MatchesNegativeSet(item.SampleOf) == true)
return false;
// Filter on supported
if (this.Supported.MatchesNeutral(null, item.Supported) == false)
return false;
// Filter on source file
if (this.SourceFile.MatchesPositiveSet(item.SourceFile) == false)
return false;
if (this.SourceFile.MatchesNegativeSet(item.SourceFile) == true)
return false;
// Filter on runnable
if (this.Runnable.MatchesNeutral(null, item.Runnable) == false)
return false;
// Filter on board
if (this.Board.MatchesPositiveSet(item.Board) == false)
return false;
if (this.Board.MatchesNegativeSet(item.Board) == true)
return false;
// Filter on rebuildto
if (this.RebuildTo.MatchesPositiveSet(item.RebuildTo) == false)
return false;
if (this.RebuildTo.MatchesNegativeSet(item.RebuildTo) == true)
return false;
// Filter on devices
if (item.Devices != null && item.Devices.Any())
{
bool anyPositiveDevice = false;
bool anyNegativeDevice = false;
foreach (string device in item.Devices)
{
anyPositiveDevice |= this.Devices.MatchesPositiveSet(device) != false;
anyNegativeDevice |= this.Devices.MatchesNegativeSet(device) == false;
}
if (!anyPositiveDevice || anyNegativeDevice)
return false;
}
// Filter on slot options
if (item.SlotOptions != null && item.SlotOptions.Any())
{
bool anyPositiveSlotOption = false;
bool anyNegativeSlotOption = false;
foreach (string slotOption in item.SlotOptions)
{
anyPositiveSlotOption |= this.SlotOptions.MatchesPositiveSet(slotOption) != false;
anyNegativeSlotOption |= this.SlotOptions.MatchesNegativeSet(slotOption) == false;
}
if (!anyPositiveSlotOption || anyNegativeSlotOption)
return false;
}
// Filter on machine type
if (this.MachineTypes.MatchesPositive(MachineType.NULL, item.MachineType) == false)
return false;
if (this.MachineTypes.MatchesNegative(MachineType.NULL, item.MachineType) == true)
return false;
#endregion
#region DatItem Filters
// Filter on item type
// TODO: Remove default filtering at some point
if (this.ItemTypes.PositiveSet.Count == 0 && this.ItemTypes.NegativeSet.Count == 0
&& item.ItemType != ItemType.Rom && item.ItemType != ItemType.Disk && item.ItemType != ItemType.Blank)
return false;
if (this.ItemTypes.MatchesPositiveSet(item.ItemType.ToString()) == false)
return false;
if (this.ItemTypes.MatchesNegativeSet(item.ItemType.ToString()) == true)
return false;
// Filter on item name
if (this.ItemName.MatchesPositiveSet(item.Name) == false)
return false;
if (this.ItemName.MatchesNegativeSet(item.Name) == true)
return false;
// Filter on part name
if (this.PartName.MatchesPositiveSet(item.PartName) == false)
return false;
if (this.PartName.MatchesNegativeSet(item.PartName) == true)
return false;
// Filter on part interface
if (this.PartInterface.MatchesPositiveSet(item.PartInterface) == false)
return false;
if (this.PartInterface.MatchesNegativeSet(item.PartInterface) == true)
return false;
// Filter on area name
if (this.AreaName.MatchesPositiveSet(item.AreaName) == false)
return false;
if (this.AreaName.MatchesNegativeSet(item.AreaName) == true)
return false;
// Filter on area size
if (this.AreaSize.MatchesNeutral(null, item.AreaSize) == false)
return false;
else if (this.AreaSize.MatchesPositive(null, item.AreaSize) == false)
return false;
else if (this.AreaSize.MatchesNegative(null, item.AreaSize) == false)
return false;
// Take care of item-specific differences
switch (item.ItemType)
{
case ItemType.Archive:
// Archive has no special fields
break;
case ItemType.BiosSet:
BiosSet biosSet = (BiosSet)item;
// Filter on description
if (this.Description.MatchesNeutral(null, biosSet.Description) == false)
return false;
// Filter on default
if (this.Default.MatchesNeutral(null, biosSet.Default) == false)
return false;
break;
case ItemType.Blank:
// Blank has no special fields
break;
case ItemType.Disk:
Disk disk = (Disk)item;
// Filter on MD5
if (this.MD5.MatchesPositiveSet(disk.MD5) == false)
return false;
if (this.MD5.MatchesNegativeSet(disk.MD5) == true)
return false;
#if NET_FRAMEWORK
// Filter on RIPEMD160
if (this.RIPEMD160.MatchesPositiveSet(disk.RIPEMD160) == false)
return false;
if (this.RIPEMD160.MatchesNegativeSet(disk.RIPEMD160) == true)
return false;
#endif
// Filter on SHA-1
if (this.SHA1.MatchesPositiveSet(disk.SHA1) == false)
return false;
if (this.SHA1.MatchesNegativeSet(disk.SHA1) == true)
return false;
// Filter on SHA-256
if (this.SHA256.MatchesPositiveSet(disk.SHA256) == false)
return false;
if (this.SHA256.MatchesNegativeSet(disk.SHA256) == true)
return false;
// Filter on SHA-384
if (this.SHA384.MatchesPositiveSet(disk.SHA384) == false)
return false;
if (this.SHA384.MatchesNegativeSet(disk.SHA384) == true)
return false;
// Filter on SHA-512
if (this.SHA512.MatchesPositiveSet(disk.SHA512) == false)
return false;
if (this.SHA512.MatchesNegativeSet(disk.SHA512) == true)
return false;
// Filter on merge tag
if (this.MergeTag.MatchesPositiveSet(disk.MergeTag) == false)
return false;
if (this.MergeTag.MatchesNegativeSet(disk.MergeTag) == true)
return false;
// Filter on region
if (this.Region.MatchesPositiveSet(disk.Region) == false)
return false;
if (this.Region.MatchesNegativeSet(disk.Region) == true)
return false;
// Filter on index
if (this.Index.MatchesPositiveSet(disk.Index) == false)
return false;
if (this.Index.MatchesNegativeSet(disk.Index) == true)
return false;
// Filter on writable
if (this.Writable.MatchesNeutral(null, disk.Writable) == false)
return false;
// Filter on status
if (this.Status.MatchesPositive(ItemStatus.NULL, disk.ItemStatus) == false)
return false;
if (this.Status.MatchesNegative(ItemStatus.NULL, disk.ItemStatus) == true)
return false;
// Filter on optional
if (this.Optional.MatchesNeutral(null, disk.Optional) == false)
return false;
break;
case ItemType.Release:
Release release = (Release)item;
// Filter on region
if (this.Region.MatchesPositiveSet(release.Region) == false)
return false;
if (this.Region.MatchesNegativeSet(release.Region) == true)
return false;
// Filter on language
if (this.Language.MatchesPositiveSet(release.Language) == false)
return false;
if (this.Language.MatchesNegativeSet(release.Language) == true)
return false;
// Filter on date
if (this.Date.MatchesPositiveSet(release.Date) == false)
return false;
if (this.Date.MatchesNegativeSet(release.Date) == true)
return false;
// Filter on default
if (this.Default.MatchesNeutral(null, release.Default) == false)
return false;
break;
case ItemType.Rom:
Rom rom = (Rom)item;
// Filter on bios
if (this.Bios.MatchesPositiveSet(rom.Bios) == false)
return false;
if (this.Bios.MatchesNegativeSet(rom.Bios) == true)
return false;
// Filter on rom size
if (this.Size.MatchesNeutral(-1, rom.Size) == false)
return false;
else if (this.Size.MatchesPositive(-1, rom.Size) == false)
return false;
else if (this.Size.MatchesNegative(-1, rom.Size) == false)
return false;
// Filter on CRC
if (this.CRC.MatchesPositiveSet(rom.CRC) == false)
return false;
if (this.CRC.MatchesNegativeSet(rom.CRC) == true)
return false;
// Filter on MD5
if (this.MD5.MatchesPositiveSet(rom.MD5) == false)
return false;
if (this.MD5.MatchesNegativeSet(rom.MD5) == true)
return false;
#if NET_FRAMEWORK
// Filter on RIPEMD160
if (this.RIPEMD160.MatchesPositiveSet(rom.RIPEMD160) == false)
return false;
if (this.RIPEMD160.MatchesNegativeSet(rom.RIPEMD160) == true)
return false;
#endif
// Filter on SHA-1
if (this.SHA1.MatchesPositiveSet(rom.SHA1) == false)
return false;
if (this.SHA1.MatchesNegativeSet(rom.SHA1) == true)
return false;
// Filter on SHA-256
if (this.SHA256.MatchesPositiveSet(rom.SHA256) == false)
return false;
if (this.SHA256.MatchesNegativeSet(rom.SHA256) == true)
return false;
// Filter on SHA-384
if (this.SHA384.MatchesPositiveSet(rom.SHA384) == false)
return false;
if (this.SHA384.MatchesNegativeSet(rom.SHA384) == true)
return false;
// Filter on SHA-512
if (this.SHA512.MatchesPositiveSet(rom.SHA512) == false)
return false;
if (this.SHA512.MatchesNegativeSet(rom.SHA512) == true)
return false;
// Filter on merge tag
if (this.MergeTag.MatchesPositiveSet(rom.MergeTag) == false)
return false;
if (this.MergeTag.MatchesNegativeSet(rom.MergeTag) == true)
return false;
// Filter on region
if (this.Region.MatchesPositiveSet(rom.Region) == false)
return false;
if (this.Region.MatchesNegativeSet(rom.Region) == true)
return false;
// Filter on offset
if (this.Offset.MatchesPositiveSet(rom.Offset) == false)
return false;
if (this.Offset.MatchesNegativeSet(rom.Offset) == true)
return false;
// Filter on date
if (this.Date.MatchesPositiveSet(rom.Date) == false)
return false;
if (this.Date.MatchesNegativeSet(rom.Date) == true)
return false;
// Filter on status
if (this.Status.MatchesPositive(ItemStatus.NULL, rom.ItemStatus) == false)
return false;
if (this.Status.MatchesNegative(ItemStatus.NULL, rom.ItemStatus) == true)
return false;
// Filter on optional
if (this.Optional.MatchesNeutral(null, rom.Optional) == false)
return false;
// Filter on inverted
if (this.Inverted.MatchesNeutral(null, rom.Inverted) == false)
return false;
break;
case ItemType.Sample:
// Sample has no special fields
break;
}
#endregion
return true;
}
#endregion #endregion
#region Internal Splitting/Merging #region Internal Splitting/Merging