Support ancient .NET in Filter

This commit is contained in:
Matt Nadareski
2024-02-28 20:14:45 -05:00
parent e43f21a31a
commit 6cf574f677
6 changed files with 70 additions and 21 deletions

View File

@@ -36,15 +36,15 @@ namespace SabreTools.Filter
public static bool RemoveField(DictionaryBase? dictionaryBase, string? fieldName)
{
// If the item or field name are missing, we can't do anything
if (dictionaryBase == null || string.IsNullOrWhiteSpace(fieldName))
if (dictionaryBase == null || string.IsNullOrEmpty(fieldName))
return false;
// If the key doesn't exist, then it's already removed
if (!dictionaryBase.ContainsKey(fieldName))
if (!dictionaryBase.ContainsKey(fieldName!))
return true;
// Remove the key
dictionaryBase.Remove(fieldName);
dictionaryBase.Remove(fieldName!);
return true;
}
@@ -54,7 +54,7 @@ namespace SabreTools.Filter
public static bool SetField(DictionaryBase? dictionaryBase, string? fieldName, object value)
{
// If the item or field name are missing, we can't do anything
if (dictionaryBase == null || string.IsNullOrWhiteSpace(fieldName))
if (dictionaryBase == null || string.IsNullOrEmpty(fieldName))
return false;
// Retrieve the list of valid fields for the item and validate
@@ -63,7 +63,7 @@ namespace SabreTools.Filter
return false;
// Set the field with the new value
dictionaryBase[fieldName] = value;
dictionaryBase[fieldName!] = value;
return true;
}

View File

@@ -14,17 +14,29 @@ namespace SabreTools.Filter
/// <summary>
/// Key name for the filter
/// </summary>
#if NETFRAMEWORK || NETCOREAPP3_1
public string[] Key { get; private set; }
#else
public string[] Key { get; init; }
#endif
/// <summary>
/// Value to match in the filter
/// </summary>
#if NETFRAMEWORK || NETCOREAPP3_1
public string? Value { get; private set; }
#else
public string? Value { get; init; }
#endif
/// <summary>
/// Operation on how to match the filter
/// </summary>
#if NETFRAMEWORK || NETCOREAPP3_1
public Operation Operation { get; private set; }
#else
public Operation Operation { get; init; }
#endif
public FilterObject(string filterString)
{
@@ -322,11 +334,19 @@ namespace SabreTools.Filter
return false;
// If we find a special character, try parsing as regex
#if NETFRAMEWORK
if (value.Contains("^")
|| value.Contains("$")
|| value.Contains("*")
|| value.Contains("?")
|| value.Contains("+"))
#else
if (value.Contains('^')
|| value.Contains('$')
|| value.Contains('*')
|| value.Contains('?')
|| value.Contains('+'))
#endif
{
try
{
@@ -348,10 +368,10 @@ namespace SabreTools.Filter
private bool? ConvertToBoolean(string? value)
{
// If we don't have a valid string, we can't do anything
if (string.IsNullOrWhiteSpace(value))
if (string.IsNullOrEmpty(value))
return null;
return value.ToLowerInvariant() switch
return value!.ToLowerInvariant() switch
{
"true" or "yes" => true,
"false" or "no" => false,

View File

@@ -12,7 +12,7 @@ namespace SabreTools.Filter
public static (string?, string?) ParseFilterId(string itemFieldString)
{
// If we don't have a filter ID, we can't do anything
if (string.IsNullOrWhiteSpace(itemFieldString))
if (string.IsNullOrEmpty(itemFieldString))
return (null, null);
// If we only have one part, we can't do anything
@@ -29,23 +29,23 @@ namespace SabreTools.Filter
public static (string?, string?) ParseFilterId(string itemName, string? fieldName)
{
// If we don't have a filter ID, we can't do anything
if (string.IsNullOrWhiteSpace(itemName) || string.IsNullOrWhiteSpace(fieldName))
if (string.IsNullOrEmpty(itemName) || string.IsNullOrEmpty(fieldName))
return (null, null);
// Return santized values based on the split ID
return itemName.ToLowerInvariant() switch
{
// Header
"header" => ParseHeaderFilterId(fieldName),
"header" => ParseHeaderFilterId(fieldName!),
// Machine
"game" => ParseMachineFilterId(fieldName),
"machine" => ParseMachineFilterId(fieldName),
"resource" => ParseMachineFilterId(fieldName),
"set" => ParseMachineFilterId(fieldName),
"game" => ParseMachineFilterId(fieldName!),
"machine" => ParseMachineFilterId(fieldName!),
"resource" => ParseMachineFilterId(fieldName!),
"set" => ParseMachineFilterId(fieldName!),
// DatItem
_ => ParseDatItemFilterId(itemName, fieldName),
_ => ParseDatItemFilterId(itemName, fieldName!),
};
}

View File

@@ -12,7 +12,11 @@ namespace SabreTools.Filter
/// <summary>
/// Set of filters to be run against an object
/// </summary>
#if NETFRAMEWORK || NETCOREAPP3_1
public FilterObject[] Filters { get; private set; }
#else
public FilterObject[] Filters { get; init; }
#endif
public FilterRunner(FilterObject[]? filters)
{

View File

@@ -1,10 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- Assembly Properties -->
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64</RuntimeIdentifiers>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.1.2</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
<Copyright>Copyright (c)2016-2023 Matt Nadareski</Copyright>
<PackageProjectUrl>https://github.com/SabreTools/</PackageProjectUrl>
<RepositoryUrl>https://github.com/SabreTools/SabreTools</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<ItemGroup>

View File

@@ -21,12 +21,18 @@ namespace SabreTools.Filter
if (fields == null)
return null;
#if NET20 || NET35 || NET40
// TODO: Figure out how to do this, try using the following:
// https://learn.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=net-8.0
return null;
#else
return fields
.Where(f => f.IsLiteral && !f.IsInitOnly)
.Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute)))
.Select(f => f.GetRawConstantValue() as string)
.Where(v => v != null)
.ToArray()!;
#endif
}
/// <summary>
@@ -34,7 +40,7 @@ namespace SabreTools.Filter
/// </summary>
public static Type? GetDatItemType(string? itemType)
{
if (string.IsNullOrWhiteSpace(itemType))
if (string.IsNullOrEmpty(itemType))
return null;
return AppDomain.CurrentDomain.GetAssemblies()
@@ -51,7 +57,13 @@ namespace SabreTools.Filter
if (type == null)
return null;
#if NET20 || NET35 || NET40
// TODO: Figure out how to do this, try using the following:
// https://learn.microsoft.com/en-us/dotnet/api/system.reflection.customattributedata?view=net-8.0
return null;
#else
return type.GetCustomAttribute<XmlRootAttribute>()?.ElementName;
#endif
}
}
}