diff --git a/Attributes/AttributeHelper.cs b/Attributes/AttributeHelper.cs index 74fc700..5fe5c75 100644 --- a/Attributes/AttributeHelper.cs +++ b/Attributes/AttributeHelper.cs @@ -23,7 +23,7 @@ namespace SabreTools.RedumpLib.Attributes // If the value returns a null on ToString, just return null string? valueStr = value?.ToString(); - if (string.IsNullOrWhiteSpace(valueStr)) + if (string.IsNullOrEmpty(valueStr)) return null; // Get the member info array diff --git a/Builder.cs b/Builder.cs index 66d26c0..c9ef1a0 100644 --- a/Builder.cs +++ b/Builder.cs @@ -25,7 +25,7 @@ namespace SabreTools.RedumpLib public static SubmissionInfo? CreateFromFile(string? path) { // If the path is invalid - if (string.IsNullOrWhiteSpace(path)) + if (string.IsNullOrEmpty(path)) return null; // If the file doesn't exist @@ -61,7 +61,7 @@ namespace SabreTools.RedumpLib }; // No disc data means we can't parse it - if (string.IsNullOrWhiteSpace(discData)) + if (string.IsNullOrEmpty(discData)) return null; try @@ -272,11 +272,11 @@ namespace SabreTools.RedumpLib var match = Constants.TitleRegex.Match(discData); if (match.Success) { - string title = WebUtility.HtmlDecode(match.Groups[1].Value); + string? title = WebUtility.HtmlDecode(match.Groups[1].Value); // If we have parenthesis, title is everything before the first one - int firstParenLocation = title.IndexOf(" ("); - if (firstParenLocation >= 0) + int firstParenLocation = title?.IndexOf(" (") ?? -1; + if (title != null && firstParenLocation >= 0) { info.CommonDiscInfo!.Title = title.Substring(0, firstParenLocation); var subMatches = Constants.DiscNumberLetterRegex.Matches(title); @@ -378,7 +378,9 @@ namespace SabreTools.RedumpLib foreach (Match submatch in matches.Cast()) { - tempDumpers.Add(WebUtility.HtmlDecode(submatch.Groups[1].Value)); + string? dumper = WebUtility.HtmlDecode(submatch.Groups[1].Value); + if (dumper != null) + tempDumpers.Add(dumper); } info.DumpersAndStatus.Dumpers = [.. tempDumpers]; @@ -395,7 +397,7 @@ namespace SabreTools.RedumpLib // Process the old comments block string oldComments = info.CommonDiscInfo.Comments + (string.IsNullOrEmpty(info.CommonDiscInfo.Comments) ? string.Empty : "\n") - + WebUtility.HtmlDecode(match.Groups[1].Value) + + (WebUtility.HtmlDecode(match.Groups[1].Value) ?? string.Empty) .Replace("\r\n", "\n") .Replace("
\n", "\n") .Replace("
", string.Empty) @@ -416,7 +418,7 @@ namespace SabreTools.RedumpLib string commentLine = commentsSeparated[i].Trim(); // If we have an empty line, we want to treat this as intentional - if (string.IsNullOrWhiteSpace(commentLine)) + if (string.IsNullOrEmpty(commentLine)) { addToLast = false; lastSiteCode = null; @@ -494,7 +496,7 @@ namespace SabreTools.RedumpLib { if (addToLast && lastSiteCode != null) { - if (!string.IsNullOrWhiteSpace(info.CommonDiscInfo.CommentsSpecialFields![lastSiteCode.Value])) + if (!string.IsNullOrEmpty(info.CommonDiscInfo.CommentsSpecialFields![lastSiteCode.Value])) info.CommonDiscInfo.CommentsSpecialFields[lastSiteCode.Value] += "\n"; info.CommonDiscInfo.CommentsSpecialFields[lastSiteCode.Value] += commentLine; @@ -520,7 +522,7 @@ namespace SabreTools.RedumpLib // Process the old contents block string oldContents = info.CommonDiscInfo.Contents + (string.IsNullOrEmpty(info.CommonDiscInfo.Contents) ? string.Empty : "\n") - + WebUtility.HtmlDecode(match.Groups[1].Value) + + (WebUtility.HtmlDecode(match.Groups[1].Value) ?? string.Empty) .Replace("\r\n", "\n") .Replace("
\n", "\n") .Replace("
", string.Empty) @@ -541,7 +543,7 @@ namespace SabreTools.RedumpLib string contentLine = contentsSeparated[i].Trim(); // If we have an empty line, we want to treat this as intentional - if (string.IsNullOrWhiteSpace(contentLine)) + if (string.IsNullOrEmpty(contentLine)) { addToLast = false; lastSiteCode = null; @@ -582,7 +584,7 @@ namespace SabreTools.RedumpLib { if (addToLast && lastSiteCode != null) { - if (!string.IsNullOrWhiteSpace(info.CommonDiscInfo.ContentsSpecialFields![lastSiteCode.Value])) + if (!string.IsNullOrEmpty(info.CommonDiscInfo.ContentsSpecialFields![lastSiteCode.Value])) info.CommonDiscInfo.ContentsSpecialFields[lastSiteCode.Value] += "\n"; info.CommonDiscInfo.ContentsSpecialFields[lastSiteCode.Value] += contentLine; @@ -668,19 +670,19 @@ namespace SabreTools.RedumpLib if (info.CommonDiscInfo != null && seed.CommonDiscInfo != null) { // Info that only overwrites if supplied - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.Title)) info.CommonDiscInfo.Title = seed.CommonDiscInfo.Title; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.ForeignTitleNonLatin)) info.CommonDiscInfo.ForeignTitleNonLatin = seed.CommonDiscInfo.ForeignTitleNonLatin; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.DiscNumberLetter)) info.CommonDiscInfo.DiscNumberLetter = seed.CommonDiscInfo.DiscNumberLetter; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.DiscTitle)) info.CommonDiscInfo.DiscTitle = seed.CommonDiscInfo.DiscTitle; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.Title)) info.CommonDiscInfo.Title = seed.CommonDiscInfo.Title; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.ForeignTitleNonLatin)) info.CommonDiscInfo.ForeignTitleNonLatin = seed.CommonDiscInfo.ForeignTitleNonLatin; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.DiscNumberLetter)) info.CommonDiscInfo.DiscNumberLetter = seed.CommonDiscInfo.DiscNumberLetter; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.DiscTitle)) info.CommonDiscInfo.DiscTitle = seed.CommonDiscInfo.DiscTitle; if (seed.CommonDiscInfo.Category != null) info.CommonDiscInfo.Category = seed.CommonDiscInfo.Category; if (seed.CommonDiscInfo.Region != null) info.CommonDiscInfo.Region = seed.CommonDiscInfo.Region; if (seed.CommonDiscInfo.Languages != null) info.CommonDiscInfo.Languages = seed.CommonDiscInfo.Languages; if (seed.CommonDiscInfo.LanguageSelection != null) info.CommonDiscInfo.LanguageSelection = seed.CommonDiscInfo.LanguageSelection; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.Serial)) info.CommonDiscInfo.Serial = seed.CommonDiscInfo.Serial; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.Barcode)) info.CommonDiscInfo.Barcode = seed.CommonDiscInfo.Barcode; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.Comments)) info.CommonDiscInfo.Comments = seed.CommonDiscInfo.Comments; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.Serial)) info.CommonDiscInfo.Serial = seed.CommonDiscInfo.Serial; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.Barcode)) info.CommonDiscInfo.Barcode = seed.CommonDiscInfo.Barcode; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.Comments)) info.CommonDiscInfo.Comments = seed.CommonDiscInfo.Comments; if (seed.CommonDiscInfo.CommentsSpecialFields != null) info.CommonDiscInfo.CommentsSpecialFields = seed.CommonDiscInfo.CommentsSpecialFields; - if (!string.IsNullOrWhiteSpace(seed.CommonDiscInfo.Contents)) info.CommonDiscInfo.Contents = seed.CommonDiscInfo.Contents; + if (!string.IsNullOrEmpty(seed.CommonDiscInfo.Contents)) info.CommonDiscInfo.Contents = seed.CommonDiscInfo.Contents; if (seed.CommonDiscInfo.ContentsSpecialFields != null) info.CommonDiscInfo.ContentsSpecialFields = seed.CommonDiscInfo.ContentsSpecialFields; // Info that always overwrites @@ -708,18 +710,18 @@ namespace SabreTools.RedumpLib if (info.VersionAndEditions != null && seed.VersionAndEditions != null) { // Info that only overwrites if supplied - if (!string.IsNullOrWhiteSpace(seed.VersionAndEditions.Version)) info.VersionAndEditions.Version = seed.VersionAndEditions.Version; - if (!string.IsNullOrWhiteSpace(seed.VersionAndEditions.OtherEditions)) info.VersionAndEditions.OtherEditions = seed.VersionAndEditions.OtherEditions; + if (!string.IsNullOrEmpty(seed.VersionAndEditions.Version)) info.VersionAndEditions.Version = seed.VersionAndEditions.Version; + if (!string.IsNullOrEmpty(seed.VersionAndEditions.OtherEditions)) info.VersionAndEditions.OtherEditions = seed.VersionAndEditions.OtherEditions; } if (info.CopyProtection != null && seed.CopyProtection != null) { // Info that only overwrites if supplied - if (!string.IsNullOrWhiteSpace(seed.CopyProtection.Protection)) info.CopyProtection.Protection = seed.CopyProtection.Protection; + if (!string.IsNullOrEmpty(seed.CopyProtection.Protection)) info.CopyProtection.Protection = seed.CopyProtection.Protection; } } - #endregion +#endregion #region Helpers @@ -730,7 +732,7 @@ namespace SabreTools.RedumpLib /// Processed text block, if possible private static string ReplaceHtmlWithSiteCodes(this string text) { - if (string.IsNullOrWhiteSpace(text)) + if (string.IsNullOrEmpty(text)) return text; foreach (SiteCode? siteCode in Enum.GetValues(typeof(SiteCode))) diff --git a/Formatter.cs b/Formatter.cs index c3d9257..f1c64cd 100644 --- a/Formatter.cs +++ b/Formatter.cs @@ -173,11 +173,11 @@ namespace SabreTools.RedumpLib } // Copy Protection section - if (!string.IsNullOrWhiteSpace(info.CopyProtection?.Protection) + if (!string.IsNullOrEmpty(info.CopyProtection?.Protection) || (info.CopyProtection?.AntiModchip != null && info.CopyProtection.AntiModchip != YesNo.NULL) || (info.CopyProtection?.LibCrypt != null && info.CopyProtection.LibCrypt != YesNo.NULL) - || !string.IsNullOrWhiteSpace(info.CopyProtection?.LibCryptData) - || !string.IsNullOrWhiteSpace(info.CopyProtection?.SecuROMData)) + || !string.IsNullOrEmpty(info.CopyProtection?.LibCryptData) + || !string.IsNullOrEmpty(info.CopyProtection?.SecuROMData)) { output.Add(""); output.Add("Copy Protection:"); if (info.CommonDiscInfo?.System == RedumpSystem.SonyPlayStation) @@ -197,7 +197,7 @@ namespace SabreTools.RedumpLib // AddIfExists(output, Template.OtherDumpersField, info.OtherDumpers); // Tracks and Write Offsets section - if (!string.IsNullOrWhiteSpace(info.TracksAndWriteOffsets?.ClrMameProData)) + if (!string.IsNullOrEmpty(info.TracksAndWriteOffsets?.ClrMameProData)) { output.Add(""); output.Add("Tracks and Write Offsets:"); AddIfExists(output, Template.DATField, info.TracksAndWriteOffsets!.ClrMameProData + "\n", 1); @@ -240,7 +240,7 @@ namespace SabreTools.RedumpLib string? last = null; for (int i = 0; i < output.Count;) { - if (output[i] == last && string.IsNullOrWhiteSpace(last)) + if (output[i] == last && string.IsNullOrEmpty(last)) { output.RemoveAt(i); } @@ -279,9 +279,10 @@ namespace SabreTools.RedumpLib // Add all special fields before any comments info.CommonDiscInfo.Comments = string.Join( "\n", OrderCommentTags(info.CommonDiscInfo.CommentsSpecialFields) - .Where(kvp => !string.IsNullOrWhiteSpace(kvp.Value)) + .Where(kvp => !string.IsNullOrEmpty(kvp.Value)) .Select(FormatSiteTag) .Where(s => !string.IsNullOrEmpty(s)) + .ToArray() ) + "\n" + info.CommonDiscInfo.Comments; // Normalize newlines @@ -304,9 +305,10 @@ namespace SabreTools.RedumpLib // Add all special fields before any contents info.CommonDiscInfo.Contents = string.Join( "\n", OrderContentTags(info.CommonDiscInfo.ContentsSpecialFields) - .Where(kvp => !string.IsNullOrWhiteSpace(kvp.Value)) + .Where(kvp => !string.IsNullOrEmpty(kvp.Value)) .Select(FormatSiteTag) .Where(s => !string.IsNullOrEmpty(s)) + .ToArray() ) + "\n" + info.CommonDiscInfo.Contents; // Normalize newlines @@ -421,7 +423,7 @@ namespace SabreTools.RedumpLib if (value == null || value.Count == 0) return; - AddIfExists(output, key, string.Join(", ", value.Select(o => o.ToString())), indent); + AddIfExists(output, key, string.Join(", ", value.Select(o => o.ToString()).ToArray()), indent); } /// diff --git a/SabreTools.RedumpLib.csproj b/SabreTools.RedumpLib.csproj index f1d2898..411a9e8 100644 --- a/SabreTools.RedumpLib.csproj +++ b/SabreTools.RedumpLib.csproj @@ -26,6 +26,11 @@ + + + + + diff --git a/Validator.cs b/Validator.cs index 24a3c86..0cfdde7 100644 --- a/Validator.cs +++ b/Validator.cs @@ -90,7 +90,7 @@ namespace SabreTools.RedumpLib #endif { // If there is an invalid query - if (string.IsNullOrWhiteSpace(query)) + if (string.IsNullOrEmpty(query)) return null; var ids = new List(); diff --git a/Web/RedumpHttpClient.cs b/Web/RedumpHttpClient.cs index 1ac3aac..f9451c2 100644 --- a/Web/RedumpHttpClient.cs +++ b/Web/RedumpHttpClient.cs @@ -46,7 +46,7 @@ namespace SabreTools.RedumpLib.Web public async static Task<(bool?, string?)> ValidateCredentials(string username, string password) { // If options are invalid or we're missing something key, just return - if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) + if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return (false, null); // Try logging in with the supplied credentials otherwise @@ -70,16 +70,16 @@ namespace SabreTools.RedumpLib.Web public async Task Login(string username, string password) { // Credentials verification - if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password)) + if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) { Console.WriteLine("Credentials entered, will attempt Redump login..."); } - else if (!string.IsNullOrWhiteSpace(username) && string.IsNullOrWhiteSpace(password)) + else if (!string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password)) { Console.WriteLine("Only a username was specified, will not attempt Redump login..."); return false; } - else if (string.IsNullOrWhiteSpace(username)) + else if (string.IsNullOrEmpty(username)) { Console.WriteLine("No credentials entered, will not attempt Redump login..."); return false; @@ -107,7 +107,7 @@ namespace SabreTools.RedumpLib.Web if (response?.Content != null) responseContent = await response.Content.ReadAsStringAsync(); - if (string.IsNullOrWhiteSpace(responseContent)) + if (string.IsNullOrEmpty(responseContent)) { Console.WriteLine($"An error occurred while trying to log in on attempt {i}: No response"); continue; @@ -362,7 +362,7 @@ namespace SabreTools.RedumpLib.Web try { // If no output directory is defined, use the current directory instead - if (string.IsNullOrWhiteSpace(outDir)) + if (string.IsNullOrEmpty(outDir)) outDir = Environment.CurrentDirectory; string tempfile = Path.Combine(outDir, "tmp" + Guid.NewGuid().ToString()); @@ -421,7 +421,7 @@ namespace SabreTools.RedumpLib.Web public async Task DownloadSingleSiteID(int id, string? outDir, bool rename) { // If no output directory is defined, use the current directory instead - if (string.IsNullOrWhiteSpace(outDir)) + if (string.IsNullOrEmpty(outDir)) outDir = Environment.CurrentDirectory; string paddedId = id.ToString().PadLeft(6, '0'); @@ -583,7 +583,7 @@ namespace SabreTools.RedumpLib.Web public async Task DownloadSingleWIPID(int id, string? outDir, bool rename) { // If no output directory is defined, use the current directory instead - if (string.IsNullOrWhiteSpace(outDir)) + if (string.IsNullOrEmpty(outDir)) outDir = Environment.CurrentDirectory; string paddedId = id.ToString().PadLeft(6, '0'); @@ -684,7 +684,7 @@ namespace SabreTools.RedumpLib.Web // If the system is unknown, we can't do anything string? longName = system.LongName(); - if (string.IsNullOrWhiteSpace(longName)) + if (string.IsNullOrEmpty(longName)) continue; Console.Write($"\r{longName}{new string(' ', Console.BufferWidth - longName.Length - 1)}"); @@ -722,7 +722,7 @@ namespace SabreTools.RedumpLib.Web // If the system is unknown, we can't do anything string? longName = system.LongName(); - if (string.IsNullOrWhiteSpace(longName)) + if (string.IsNullOrEmpty(longName)) continue; Console.Write($"\r{longName}{new string(' ', Console.BufferWidth - longName.Length - 1)}"); @@ -794,14 +794,14 @@ namespace SabreTools.RedumpLib.Web private static void MoveOrDelete(string tempfile, string? newfile, string outDir, string? subfolder) { // If we don't have a file to move to, just delete the temp file - if (string.IsNullOrWhiteSpace(newfile)) + if (string.IsNullOrEmpty(newfile)) { File.Delete(tempfile); return; } // If we have a subfolder, create it and update the newfile name - if (!string.IsNullOrWhiteSpace(subfolder)) + if (!string.IsNullOrEmpty(subfolder)) { if (!Directory.Exists(Path.Combine(outDir, subfolder))) Directory.CreateDirectory(Path.Combine(outDir, subfolder)); diff --git a/Web/RedumpWebClient.cs b/Web/RedumpWebClient.cs index baa722e..eccca4b 100644 --- a/Web/RedumpWebClient.cs +++ b/Web/RedumpWebClient.cs @@ -37,7 +37,7 @@ namespace SabreTools.RedumpLib.Web // If we don't have the response header we care about string headerValue = ResponseHeaders.Get("Content-Disposition"); - if (string.IsNullOrWhiteSpace(headerValue)) + if (string.IsNullOrEmpty(headerValue)) return null; // Extract the filename from the value @@ -60,7 +60,7 @@ namespace SabreTools.RedumpLib.Web public static (bool?, string?) ValidateCredentials(string username, string password) { // If options are invalid or we're missing something key, just return - if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) + if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return (false, null); // Try logging in with the supplied credentials otherwise @@ -83,23 +83,23 @@ namespace SabreTools.RedumpLib.Web public bool? Login(string username, string password) { // Credentials verification - if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password)) + if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) { Console.WriteLine("Credentials entered, will attempt Redump login..."); } - else if (!string.IsNullOrWhiteSpace(username) && string.IsNullOrWhiteSpace(password)) + else if (!string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password)) { Console.WriteLine("Only a username was specified, will not attempt Redump login..."); return false; } - else if (string.IsNullOrWhiteSpace(username)) + else if (string.IsNullOrEmpty(username)) { Console.WriteLine("No credentials entered, will not attempt Redump login..."); return false; } // HTTP encode the password -#if NET40 +#if NET35 || NET40 password = Uri.EscapeUriString(password); #else password = WebUtility.UrlEncode(password); @@ -392,7 +392,7 @@ namespace SabreTools.RedumpLib.Web try { // If no output directory is defined, use the current directory instead - if (string.IsNullOrWhiteSpace(outDir)) + if (string.IsNullOrEmpty(outDir)) outDir = Environment.CurrentDirectory; string tempfile = Path.Combine(outDir, "tmp" + Guid.NewGuid().ToString()); @@ -455,7 +455,7 @@ namespace SabreTools.RedumpLib.Web public bool DownloadSingleSiteID(int id, string? outDir, bool rename) { // If no output directory is defined, use the current directory instead - if (string.IsNullOrWhiteSpace(outDir)) + if (string.IsNullOrEmpty(outDir)) outDir = Environment.CurrentDirectory; string paddedId = id.ToString().PadLeft(6, '0'); @@ -635,7 +635,7 @@ namespace SabreTools.RedumpLib.Web public bool DownloadSingleWIPID(int id, string? outDir, bool rename) { // If no output directory is defined, use the current directory instead - if (string.IsNullOrWhiteSpace(outDir)) + if (string.IsNullOrEmpty(outDir)) outDir = Environment.CurrentDirectory; string paddedId = id.ToString().PadLeft(6, '0'); @@ -745,7 +745,7 @@ namespace SabreTools.RedumpLib.Web // If the system is unknown, we can't do anything string? longName = system.LongName(); - if (string.IsNullOrWhiteSpace(longName)) + if (string.IsNullOrEmpty(longName)) continue; Console.Write($"\r{longName}{new string(' ', Console.BufferWidth - longName!.Length - 1)}"); @@ -783,7 +783,7 @@ namespace SabreTools.RedumpLib.Web // If the system is unknown, we can't do anything string? longName = system.LongName(); - if (string.IsNullOrWhiteSpace(longName)) + if (string.IsNullOrEmpty(longName)) continue; Console.Write($"\r{longName}{new string(' ', Console.BufferWidth - longName!.Length - 1)}"); @@ -803,9 +803,9 @@ namespace SabreTools.RedumpLib.Web /// Optional subfolder to append to the path private static void MoveOrDelete(string tempfile, string? newfile, string outDir, string? subfolder) { - if (!string.IsNullOrWhiteSpace(newfile)) + if (!string.IsNullOrEmpty(newfile)) { - if (!string.IsNullOrWhiteSpace(subfolder)) + if (!string.IsNullOrEmpty(subfolder)) { if (!Directory.Exists(Path.Combine(outDir, subfolder))) Directory.CreateDirectory(Path.Combine(outDir, subfolder));