Perform mass cleanup

This is cleanup based on both new .NET functionality (in 6 and 7) as well as a ton of simplifications and things that were missed that were caught due to the cleanup.
This commit is contained in:
Matt Nadareski
2023-04-19 16:39:58 -04:00
parent fd5fd79b95
commit 728b5d6b27
95 changed files with 1353 additions and 1572 deletions

View File

@@ -90,7 +90,7 @@ namespace SabreTools.Filtering
/// <summary>
/// Logging object
/// </summary>
private readonly Logger logger = new Logger();
private readonly Logger logger = new();
#endregion
@@ -104,7 +104,7 @@ namespace SabreTools.Filtering
/// <returns>True if cleaning was successful, false on error</returns>
public bool ApplyCleaning(DatFile datFile, bool throwOnError = false)
{
InternalStopwatch watch = new InternalStopwatch("Applying cleaning steps to DAT");
InternalStopwatch watch = new("Applying cleaning steps to DAT");
try
{
@@ -212,7 +212,7 @@ namespace SabreTools.Filtering
if (datItem.GetName().Length > usableLength)
{
string ext = Path.GetExtension(datItem.GetName());
datItem.SetName(datItem.GetName().Substring(0, usableLength - ext.Length) + ext);
datItem.SetName(datItem.GetName()[..(usableLength - ext.Length)] + ext);
}
}
}
@@ -222,7 +222,7 @@ namespace SabreTools.Filtering
/// </summary>
/// <param name="game">Name of the game to be cleaned</param>
/// <returns>The cleaned name</returns>
internal string CleanGameName(string game)
internal static string CleanGameName(string game)
{
if (game == null)
return null;
@@ -247,7 +247,7 @@ namespace SabreTools.Filtering
try
{
// First we want to get a mapping for all games to description
ConcurrentDictionary<string, string> mapping = new ConcurrentDictionary<string, string>();
ConcurrentDictionary<string, string> mapping = new();
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
{
ConcurrentList<DatItem> items = datFile.Items[key];
@@ -262,7 +262,7 @@ namespace SabreTools.Filtering
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
{
ConcurrentList<DatItem> items = datFile.Items[key];
ConcurrentList<DatItem> newItems = new ConcurrentList<DatItem>();
ConcurrentList<DatItem> newItems = new();
foreach (DatItem item in items)
{
// Update machine name
@@ -301,7 +301,7 @@ namespace SabreTools.Filtering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
internal string NormalizeChars(string input)
internal static string NormalizeChars(string input)
{
if (input == null)
return null;
@@ -357,7 +357,7 @@ namespace SabreTools.Filtering
/// </summary>
/// <param name="s">Input string to clean</param>
/// <returns>Cleaned string</returns>
internal string RemoveUnicodeCharacters(string s)
internal static string RemoveUnicodeCharacters(string s)
{
if (s == null)
return null;
@@ -370,7 +370,7 @@ namespace SabreTools.Filtering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
internal string RussianToLatin(string input)
internal static string RussianToLatin(string input)
{
if (input == null)
return null;
@@ -405,7 +405,7 @@ namespace SabreTools.Filtering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
internal string SearchPattern(string input)
internal static string SearchPattern(string input)
{
if (input == null)
return null;
@@ -464,14 +464,13 @@ namespace SabreTools.Filtering
internal void SetOneGamePerRegion(DatFile datFile)
{
// If we have null region list, make it empty
if (RegionList == null)
RegionList = new List<string>();
RegionList ??= new List<string>();
// For sake of ease, the first thing we want to do is bucket by game
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None, norename: true);
// Then we want to get a mapping of all machines to parents
Dictionary<string, List<string>> parents = new Dictionary<string, List<string>>();
Dictionary<string, List<string>> parents = new();
foreach (string key in datFile.Items.Keys)
{
DatItem item = datFile.Items[key][0];
@@ -535,7 +534,7 @@ namespace SabreTools.Filtering
/// Ensure that all roms are in their own game (or at least try to ensure)
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
internal void SetOneRomPerGame(DatFile datFile)
internal static void SetOneRomPerGame(DatFile datFile)
{
// Because this introduces subfolders, we need to set the SuperDAT type
datFile.Header.Type = "SuperDAT";
@@ -555,7 +554,7 @@ namespace SabreTools.Filtering
/// Set internal names to match One Rom Per Game (ORPG) logic
/// </summary>
/// <param name="datItem">DatItem to run logic on</param>
internal void SetOneRomPerGame(DatItem datItem)
internal static void SetOneRomPerGame(DatItem datItem)
{
if (datItem.GetName() == null)
return;

View File

@@ -54,14 +54,14 @@ namespace SabreTools.Filtering
if (inputs == null || !inputs.Any())
return;
InternalStopwatch watch = new InternalStopwatch("Populating extras from list");
InternalStopwatch watch = new("Populating extras from list");
foreach (string input in inputs)
{
ExtraIniItem item = new ExtraIniItem();
ExtraIniItem item = new();
// If we don't even have a possible field and file combination
if (!input.Contains(":"))
if (!input.Contains(':'))
{
logger.Warning($"'{input}` is not a valid INI extras string. Valid INI extras strings are of the form 'key:value'. Please refer to README.1ST or the help feature for more details.");
return;
@@ -96,7 +96,7 @@ namespace SabreTools.Filtering
if (Items == null || !Items.Any())
return true;
InternalStopwatch watch = new InternalStopwatch("Applying extra mappings to DAT");
InternalStopwatch watch = new("Applying extra mappings to DAT");
try
{
@@ -125,7 +125,7 @@ namespace SabreTools.Filtering
combinedDatItemMaps.TryGetValue(machine, out Dictionary<DatItemField, string> datItemMappings);
// Create a setter with the new mappings
Setter setter = new Setter
Setter setter = new()
{
MachineMappings = machineMappings,
DatItemMappings = datItemMappings,

View File

@@ -45,7 +45,7 @@ namespace SabreTools.Filtering
public bool PopulateFromFile(string ini)
{
// Prepare all intenral variables
IniReader ir = new IniReader(ini) { ValidateRows = false };
IniReader ir = new(ini) { ValidateRows = false };
bool foundRootFolder = false;
// If we got a null reader, just return

View File

@@ -96,7 +96,7 @@ namespace SabreTools.Filtering
if (filters == null || filters.Count == 0)
return;
InternalStopwatch watch = new InternalStopwatch("Populating filters from list");
InternalStopwatch watch = new("Populating filters from list");
foreach (string filterPair in filters)
{
@@ -138,7 +138,7 @@ namespace SabreTools.Filtering
protected (string field, string value, bool negate) ProcessFilterPair(string filter)
{
// If we don't even have a possible filter pair
if (!filter.Contains(":"))
if (!filter.Contains(':'))
{
logger.Warning($"'{filter}` is not a valid filter string. Valid filter strings are of the form 'key:value'. Please refer to README.1ST or the help feature for more details.");
return (null, null, false);
@@ -163,7 +163,7 @@ namespace SabreTools.Filtering
/// <param name="filterItem">FilterItem to populate</param>
/// <param name="value">String value to add</param>
/// <param name="negate">True to set negative filter, false otherwise</param>
protected void SetBooleanFilter(FilterItem<bool?> filterItem, string value, bool negate)
protected static void SetBooleanFilter(FilterItem<bool?> filterItem, string value, bool negate)
{
if (negate || value.Equals("false", StringComparison.OrdinalIgnoreCase))
filterItem.Neutral = false;
@@ -177,7 +177,7 @@ namespace SabreTools.Filtering
/// <param name="filterItem">FilterItem to populate</param>
/// <param name="value">String value to add</param>
/// <param name="negate">True to set negative filter, false otherwise</param>
protected void SetDoubleFilter(FilterItem<double?> filterItem, string value, bool negate)
protected static void SetDoubleFilter(FilterItem<double?> filterItem, string value, bool negate)
{
bool? operation = null;
if (value.StartsWith(">"))
@@ -235,7 +235,7 @@ namespace SabreTools.Filtering
/// <param name="filterItem">FilterItem to populate</param>
/// <param name="value">String value to add</param>
/// <param name="negate">True to set negative filter, false otherwise</param>
protected void SetLongFilter(FilterItem<long?> filterItem, string value, bool negate)
protected static void SetLongFilter(FilterItem<long?> filterItem, string value, bool negate)
{
bool? operation = null;
if (value.StartsWith(">"))
@@ -294,7 +294,7 @@ namespace SabreTools.Filtering
/// <param name="filterItem">FilterItem to populate</param>
/// <param name="value">String value to add</param>
/// <param name="negate">True to set negative filter, false otherwise</param>
protected void SetStringFilter(FilterItem<string> filterItem, string value, bool negate)
protected static void SetStringFilter(FilterItem<string> filterItem, string value, bool negate)
{
if (negate)
filterItem.NegativeSet.Add(value);
@@ -382,7 +382,7 @@ namespace SabreTools.Filtering
if (!MachineFilter.HasFilters && !DatItemFilter.HasFilters)
return true;
InternalStopwatch watch = new InternalStopwatch("Applying filters to DAT");
InternalStopwatch watch = new("Applying filters to DAT");
// If we're filtering per machine, bucket by machine first
if (perMachine)

View File

@@ -110,7 +110,7 @@ namespace SabreTools.Filtering
/// <param name="def">Default value to check filter value</param>
/// <param name="value">Value to check</param>
/// <returns>True if the value was found in the supplied filter, null on default value, false otherwise</returns>
private bool? Matches(T single, T def, T value)
private static bool? Matches(T single, T def, T value)
{
// If the filter is default, we ignore
if (single.Equals(def))
@@ -129,12 +129,12 @@ namespace SabreTools.Filtering
/// <param name="set">Set to check against</param>
/// <param name="value">Value to check</param>
/// <returns>True if the value was found in the supplied filter, null on an empty set, false otherwise</returns>
private bool? MatchesSet(List<T> set, T value)
private static bool? MatchesSet(List<T> set, T value)
{
if (set.Count == 0)
return null;
if (this.FindValueInList(set, value))
if (FindValueInList(set, value))
return true;
return false;
@@ -146,7 +146,7 @@ namespace SabreTools.Filtering
/// <param name="haystack">List to search for the value in</param>
/// <param name="needle">Value to search the list for</param>
/// <returns>True if the value could be found, false otherwise</returns>
private bool FindValueInList(List<T> haystack, T needle)
private static bool FindValueInList(List<T> haystack, T needle)
{
bool found = false;
foreach (T straw in haystack)

View File

@@ -65,7 +65,7 @@ namespace SabreTools.Filtering
if (fields == null || fields.Count == 0)
return;
InternalStopwatch watch = new InternalStopwatch("Populating removals from list");
InternalStopwatch watch = new("Populating removals from list");
foreach (string field in fields)
{
@@ -102,7 +102,7 @@ namespace SabreTools.Filtering
if (DatHeaderRemover == null && DatItemRemover == null)
return;
InternalStopwatch watch = new InternalStopwatch("Applying removals to DAT");
InternalStopwatch watch = new("Applying removals to DAT");
// Remove DatHeader fields
if (DatHeaderRemover != null && DatHeaderRemover.DatHeaderFields.Any())

View File

@@ -26,7 +26,7 @@ namespace SabreTools.Filtering
/// <summary>
/// Logging object
/// </summary>
private static readonly Logger logger = new Logger();
private static readonly Logger logger = new();
#endregion
@@ -47,7 +47,7 @@ namespace SabreTools.Filtering
/// <returns>True if the DatFile was split, false on error</returns>
public bool ApplySplitting(DatFile datFile, bool useTags, bool throwOnError = false)
{
InternalStopwatch watch = new InternalStopwatch("Applying splitting to DAT");
InternalStopwatch watch = new("Applying splitting to DAT");
try
{
@@ -260,7 +260,7 @@ namespace SabreTools.Filtering
{
DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom);
if (datFile.Items[game].Where(i => i.GetName() == datItem.GetName()).Count() == 0 && !datFile.Items[game].Contains(datItem))
if (!datFile.Items[game].Where(i => i.GetName() == datItem.GetName()).Any() && !datFile.Items[game].Contains(datItem))
datFile.Items.Add(game, datItem);
}
}
@@ -308,7 +308,7 @@ namespace SabreTools.Filtering
if (deviceReferences.Any())
{
// Loop through all names and check the corresponding machines
List<string> newDeviceReferences = new List<string>();
List<string> newDeviceReferences = new();
foreach (string deviceReference in deviceReferences)
{
// If the machine doesn't exist then we continue
@@ -351,7 +351,7 @@ namespace SabreTools.Filtering
if (useSlotOptions && slotOptions.Any())
{
// Loop through all names and check the corresponding machines
List<string> newSlotOptions = new List<string>();
List<string> newSlotOptions = new();
foreach (string slotOption in slotOptions)
{
// If the machine doesn't exist then we continue
@@ -429,7 +429,7 @@ namespace SabreTools.Filtering
{
DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom);
if (datFile.Items[game].Where(i => i.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant()).Count() == 0
if (!datFile.Items[game].Where(i => i.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant()).Any()
&& !datFile.Items[game].Contains(datItem))
{
datFile.Items.Add(game, datItem);