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

View File

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

View File

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

View File

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

View File

@@ -1,10 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks> <!-- Assembly Properties -->
<LangVersion>latest</LangVersion> <TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable> <RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64</RuntimeIdentifiers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <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> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -21,12 +21,18 @@ namespace SabreTools.Filter
if (fields == null) if (fields == null)
return 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 return fields
.Where(f => f.IsLiteral && !f.IsInitOnly) .Where(f => f.IsLiteral && !f.IsInitOnly)
.Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute))) .Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute)))
.Select(f => f.GetRawConstantValue() as string) .Select(f => f.GetRawConstantValue() as string)
.Where(v => v != null) .Where(v => v != null)
.ToArray()!; .ToArray()!;
#endif
} }
/// <summary> /// <summary>
@@ -34,7 +40,7 @@ namespace SabreTools.Filter
/// </summary> /// </summary>
public static Type? GetDatItemType(string? itemType) public static Type? GetDatItemType(string? itemType)
{ {
if (string.IsNullOrWhiteSpace(itemType)) if (string.IsNullOrEmpty(itemType))
return null; return null;
return AppDomain.CurrentDomain.GetAssemblies() return AppDomain.CurrentDomain.GetAssemblies()
@@ -51,7 +57,13 @@ namespace SabreTools.Filter
if (type == null) if (type == null)
return 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; return type.GetCustomAttribute<XmlRootAttribute>()?.ElementName;
#endif
} }
} }
} }