diff --git a/SabreTools.RedumpLib.Test/ExtensionsTests.cs b/SabreTools.RedumpLib.Test/ExtensionsTests.cs index 1a37222..cdefe08 100644 --- a/SabreTools.RedumpLib.Test/ExtensionsTests.cs +++ b/SabreTools.RedumpLib.Test/ExtensionsTests.cs @@ -15,8 +15,8 @@ namespace SabreTools.RedumpLib.Test /// /// DiscType values that map to MediaType /// - private static readonly DiscType?[] _mappableDiscTypes = new DiscType?[] - { + private static readonly DiscType?[] _mappableDiscTypes = + [ DiscType.BD25, DiscType.BD33, DiscType.BD50, @@ -35,13 +35,13 @@ namespace SabreTools.RedumpLib.Test DiscType.NintendoWiiUOpticalDiscSL, DiscType.UMDSL, DiscType.UMDDL, - }; + ]; /// /// MediaType values that map to DiscType /// - private static readonly MediaType?[] _mappableMediaTypes = new MediaType?[] - { + private static readonly MediaType?[] _mappableMediaTypes = + [ MediaType.BluRay, MediaType.CDROM, MediaType.DVD, @@ -51,7 +51,7 @@ namespace SabreTools.RedumpLib.Test MediaType.NintendoWiiOpticalDisc, MediaType.NintendoWiiUOpticalDisc, MediaType.UMD, - }; + ]; /// /// Check that every supported system has some set of MediaTypes supported @@ -101,9 +101,9 @@ namespace SabreTools.RedumpLib.Test foreach (DiscType? discType in Enum.GetValues(typeof(DiscType))) { if (_mappableDiscTypes.Contains(discType)) - testData.Add(new object?[] { discType, false }); + testData.Add([discType, false]); else - testData.Add(new object?[] { discType, true }); + testData.Add([discType, true]); } return testData; @@ -118,7 +118,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null } }; foreach (RedumpSystem? redumpSystem in Enum.GetValues(typeof(RedumpSystem))) { - testData.Add(new object?[] { redumpSystem }); + testData.Add([redumpSystem]); } return testData; @@ -135,9 +135,9 @@ namespace SabreTools.RedumpLib.Test foreach (MediaType? mediaType in Enum.GetValues(typeof(MediaType))) { if (_mappableMediaTypes.Contains(mediaType)) - testData.Add(new object?[] { mediaType, false }); + testData.Add([mediaType, false]); else - testData.Add(new object?[] { mediaType, true }); + testData.Add([mediaType, true]); } return testData; @@ -173,7 +173,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, true } }; foreach (DiscCategory? discCategory in Enum.GetValues(typeof(DiscCategory))) { - testData.Add(new object?[] { discCategory, false }); + testData.Add([discCategory, false]); } return testData; @@ -210,9 +210,9 @@ namespace SabreTools.RedumpLib.Test foreach (DiscType? discType in Enum.GetValues(typeof(DiscType))) { if (discType == DiscType.NONE) - testData.Add(new object?[] { discType, true }); + testData.Add([discType, true]); else - testData.Add(new object?[] { discType, false }); + testData.Add([discType, false]); } return testData; @@ -346,7 +346,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, true } }; foreach (Language? language in Enum.GetValues(typeof(Language))) { - testData.Add(new object?[] { language, false }); + testData.Add([language, false]); } return testData; @@ -382,7 +382,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, true } }; foreach (LanguageSelection? languageSelection in Enum.GetValues(typeof(LanguageSelection))) { - testData.Add(new object?[] { languageSelection, false }); + testData.Add([languageSelection, false]); } return testData; @@ -435,7 +435,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, true } }; foreach (MediaType? mediaType in Enum.GetValues(typeof(MediaType))) { - testData.Add(new object?[] { mediaType, false }); + testData.Add([mediaType, false]); } return testData; @@ -515,7 +515,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, true } }; foreach (Region? region in Enum.GetValues(typeof(Region))) { - testData.Add(new object?[] { region, false }); + testData.Add([region, false]); } return testData; @@ -568,7 +568,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, true } }; foreach (SiteCode? siteCode in Enum.GetValues(typeof(SiteCode))) { - testData.Add(new object?[] { siteCode, false }); + testData.Add([siteCode, false]); } return testData; @@ -631,7 +631,7 @@ namespace SabreTools.RedumpLib.Test if (redumpSystem.IsMarker()) continue; - testData.Add(new object?[] { redumpSystem, false }); + testData.Add([redumpSystem, false]); } return testData; @@ -668,9 +668,9 @@ namespace SabreTools.RedumpLib.Test foreach (SystemCategory? systemCategory in Enum.GetValues(typeof(SystemCategory))) { if (systemCategory == SystemCategory.NONE) - testData.Add(new object?[] { systemCategory, true }); + testData.Add([systemCategory, true]); else - testData.Add(new object?[] { systemCategory, false }); + testData.Add([systemCategory, false]); } return testData; @@ -706,7 +706,7 @@ namespace SabreTools.RedumpLib.Test var testData = new List() { new object?[] { null, false } }; foreach (YesNo? yesNo in Enum.GetValues(typeof(YesNo))) { - testData.Add(new object?[] { yesNo, false }); + testData.Add([yesNo, false]); } return testData; diff --git a/SabreTools.RedumpLib/Data/Extensions.cs b/SabreTools.RedumpLib/Data/Extensions.cs index 44f1869..250d2aa 100644 --- a/SabreTools.RedumpLib/Data/Extensions.cs +++ b/SabreTools.RedumpLib/Data/Extensions.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Generic; -#if NET40_OR_GREATER || NETCOREAPP -using System.Linq; -#endif using SabreTools.RedumpLib.Attributes; namespace SabreTools.RedumpLib.Data @@ -928,65 +925,35 @@ namespace SabreTools.RedumpLib.Data /// Language represented by the string, if possible public static Language? ToLanguage(string lang) { - var languages = (Language?[])Enum.GetValues(typeof(Language)); + lang = lang.ToLowerInvariant(); + var languages = (Language[])Enum.GetValues(typeof(Language)); // Check ISO 639-1 codes -#if NET20 || NET35 - var languageMapping = new Dictionary(); - foreach (var l in languages) - { - if (l.TwoLetterCode() == null) - continue; - - languageMapping[l.TwoLetterCode() ?? string.Empty] = l; - } -#else - Dictionary languageMapping = Array.FindAll(languages, l => l.TwoLetterCode() != null) - .ToDictionary(l => l.TwoLetterCode() ?? string.Empty, l => l); -#endif - - if (languageMapping.ContainsKey(lang)) - return languageMapping[lang]; + int index = Array.FindIndex(languages, l => lang == l.TwoLetterCode()); + if (index > -1) + return languages[index]; // Check standard ISO 639-2 codes -#if NET20 || NET35 - languageMapping = new Dictionary(); - foreach (var l in languages) - { - if (l.ThreeLetterCode() == null) - continue; - - languageMapping[l.ThreeLetterCode() ?? string.Empty] = l; - } -#else - languageMapping = Array.FindAll(languages, l => l.ThreeLetterCode() != null) - .ToDictionary(l => l.ThreeLetterCode() ?? string.Empty, l => l); -#endif - - if (languageMapping.ContainsKey(lang)) - return languageMapping[lang]; + index = Array.FindIndex(languages, r => lang == r.ThreeLetterCode()); + if (index > -1) + return languages[index]; // Check alternate ISO 639-2 codes -#if NET20 || NET35 - languageMapping = new Dictionary(); - foreach (var l in languages) - { - if (l.ThreeLetterCodeAlt() == null) - continue; - - languageMapping[l.ThreeLetterCodeAlt() ?? string.Empty] = l; - } -#else - languageMapping = Array.FindAll(languages, l => l.ThreeLetterCodeAlt() != null) - .ToDictionary(l => l.ThreeLetterCodeAlt() ?? string.Empty, l => l); -#endif - - if (languageMapping.ContainsKey(lang)) - return languageMapping[lang]; + index = Array.FindIndex(languages, r => lang == r.ThreeLetterCodeAlt()); + if (index > -1) + return languages[index]; return null; } + /// + /// Get the ISO 639-2 code for each known language + /// + /// + /// + public static string? ThreeLetterCode(this Language language) + => (AttributeHelper.GetAttribute(language) as LanguageAttribute)?.ThreeLetterCode; + /// /// Get the ISO 639-2 code for each known language /// @@ -995,6 +962,14 @@ namespace SabreTools.RedumpLib.Data public static string? ThreeLetterCode(this Language? language) => (AttributeHelper.GetAttribute(language) as LanguageAttribute)?.ThreeLetterCode; + /// + /// Get the ISO 639-2 alternate code for each known language + /// + /// + /// + public static string? ThreeLetterCodeAlt(this Language language) + => (AttributeHelper.GetAttribute(language) as LanguageAttribute)?.ThreeLetterCodeAlt; + /// /// Get the ISO 639-2 alternate code for each known language /// @@ -1003,6 +978,14 @@ namespace SabreTools.RedumpLib.Data public static string? ThreeLetterCodeAlt(this Language? language) => (AttributeHelper.GetAttribute(language) as LanguageAttribute)?.ThreeLetterCodeAlt; + /// + /// Get the ISO 639-1 code for each known language + /// + /// + /// + public static string? TwoLetterCode(this Language language) + => (AttributeHelper.GetAttribute(language) as LanguageAttribute)?.TwoLetterCode; + /// /// Get the ISO 639-1 code for each known language /// @@ -1090,14 +1073,32 @@ namespace SabreTools.RedumpLib.Data /// /// /// - public static string? LongName(this Region? region) => AttributeHelper.GetAttribute(region)?.LongName; + public static string? LongName(this Region region) + => AttributeHelper.GetAttribute(region)?.LongName; + + /// + /// Get the Redump longnames for each known region + /// + /// + /// + public static string? LongName(this Region? region) + => AttributeHelper.GetAttribute(region)?.LongName; /// /// Get the Redump shortnames for each known region /// /// /// - public static string? ShortName(this Region? region) => AttributeHelper.GetAttribute(region)?.ShortName; + public static string? ShortName(this Region region) + => AttributeHelper.GetAttribute(region)?.ShortName; + + /// + /// Get the Redump shortnames for each known region + /// + /// + /// + public static string? ShortName(this Region? region) + => AttributeHelper.GetAttribute(region)?.ShortName; /// /// Get the Region enum value for a given string @@ -1107,25 +1108,15 @@ namespace SabreTools.RedumpLib.Data public static Region? ToRegion(string region) { region = region.ToLowerInvariant(); - var regions = (Region?[])Enum.GetValues(typeof(Region)); + var regions = (Region[])Enum.GetValues(typeof(Region)); - // Check ISO 3166-1 alpha-2 codes -#if NET20 || NET35 - var regionMapping = new Dictionary(); - foreach (var r in regions) - { - if (r.ShortName() == null) - continue; + int index = Array.FindIndex(regions, r => region == r.ShortName()); + if (index > -1) + return regions[index]; - regionMapping[r.ShortName()?.ToLowerInvariant() ?? string.Empty] = r; - } -#else - Dictionary regionMapping = Array.FindAll(regions, r => r.ShortName() != null) - .ToDictionary(r => r.ShortName()?.ToLowerInvariant() ?? string.Empty, r => r); -#endif - - if (regionMapping.ContainsKey(region)) - return regionMapping[region]; + index = Array.FindIndex(regions, r => region == r.LongName()); + if (index > -1) + return regions[index]; return null; } @@ -1442,64 +1433,146 @@ namespace SabreTools.RedumpLib.Data /// /// /// - public static string? LongName(this RedumpSystem? system) => AttributeHelper.GetAttribute(system)?.LongName; + public static string? LongName(this RedumpSystem system) + => AttributeHelper.GetAttribute(system)?.LongName; + + /// + /// Get the Redump longnames for each known system + /// + /// + /// + public static string? LongName(this RedumpSystem? system) + => AttributeHelper.GetAttribute(system)?.LongName; /// /// Get the Redump shortnames for each known system /// /// /// - public static string? ShortName(this RedumpSystem? system) => AttributeHelper.GetAttribute(system)?.ShortName; + public static string? ShortName(this RedumpSystem system) + => AttributeHelper.GetAttribute(system)?.ShortName; + + /// + /// Get the Redump shortnames for each known system + /// + /// + /// + public static string? ShortName(this RedumpSystem? system) + => AttributeHelper.GetAttribute(system)?.ShortName; /// /// Determine the category of a system /// - public static SystemCategory GetCategory(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.Category ?? SystemCategory.NONE; + public static SystemCategory GetCategory(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.Category ?? SystemCategory.NONE; /// /// Determine if a system is available in Redump yet /// - public static bool IsAvailable(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.Available ?? false; + public static bool IsAvailable(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.Available ?? false; + + /// + /// Determine if a system is available in Redump yet + /// + public static bool IsAvailable(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.Available ?? false; /// /// Determine if a system is restricted to dumpers /// - public static bool IsBanned(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.IsBanned ?? false; + public static bool IsBanned(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.IsBanned ?? false; + + /// + /// Determine if a system is restricted to dumpers + /// + public static bool IsBanned(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.IsBanned ?? false; /// /// Determine if a system has a CUE pack /// - public static bool HasCues(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasCues ?? false; + public static bool HasCues(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasCues ?? false; + + /// + /// Determine if a system has a CUE pack + /// + public static bool HasCues(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasCues ?? false; /// /// Determine if a system has a DAT /// - public static bool HasDat(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasDat ?? false; + public static bool HasDat(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasDat ?? false; + + /// + /// Determine if a system has a DAT + /// + public static bool HasDat(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasDat ?? false; /// /// Determine if a system has a decrypted keys pack /// - public static bool HasDkeys(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasDkeys ?? false; + public static bool HasDkeys(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasDkeys ?? false; + + /// + /// Determine if a system has a decrypted keys pack + /// + public static bool HasDkeys(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasDkeys ?? false; /// /// Determine if a system has a GDI pack /// - public static bool HasGdi(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasGdi ?? false; + public static bool HasGdi(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasGdi ?? false; + + /// + /// Determine if a system has a GDI pack + /// + public static bool HasGdi(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasGdi ?? false; /// /// Determine if a system has a keys pack /// - public static bool HasKeys(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasKeys ?? false; + public static bool HasKeys(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasKeys ?? false; + + /// + /// Determine if a system has a keys pack + /// + public static bool HasKeys(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasKeys ?? false; /// /// Determine if a system has an LSD pack /// - public static bool HasLsd(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasLsd ?? false; + public static bool HasLsd(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasLsd ?? false; + + /// + /// Determine if a system has an LSD pack + /// + public static bool HasLsd(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasLsd ?? false; /// /// Determine if a system has an SBI pack /// - public static bool HasSbi(this RedumpSystem? system) => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasSbi ?? false; + public static bool HasSbi(this RedumpSystem system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasSbi ?? false; + + /// + /// Determine if a system has an SBI pack + /// + public static bool HasSbi(this RedumpSystem? system) + => (AttributeHelper.GetAttribute(system) as SystemAttribute)?.HasSbi ?? false; /// /// Get the RedumpSystem enum value for a given string diff --git a/SabreTools.RedumpLib/Web/Packs.cs b/SabreTools.RedumpLib/Web/Packs.cs index d5d7149..f7fa3db 100644 --- a/SabreTools.RedumpLib/Web/Packs.cs +++ b/SabreTools.RedumpLib/Web/Packs.cs @@ -17,7 +17,7 @@ namespace SabreTools.RedumpLib.Web /// True to use named subfolders to store downloads, false to store directly in the output directory public static async Task DownloadPacks(RedumpClient rc, string? outDir, bool useSubfolders) { - var systems = (RedumpSystem?[])Enum.GetValues(typeof(RedumpSystem)); + var systems = (RedumpSystem[])Enum.GetValues(typeof(RedumpSystem)); await rc.DownloadPacks(Constants.PackCuesUrl, Array.FindAll(systems, s => s.HasCues()), "CUEs", outDir, useSubfolders ? "cue" : null); await rc.DownloadPacks(Constants.PackDatfileUrl, Array.FindAll(systems, s => s.HasDat()), "DATs", outDir, useSubfolders ? "dat" : null); @@ -39,7 +39,10 @@ namespace SabreTools.RedumpLib.Web /// True to use named subfolders to store downloads, false to store directly in the output directory public static async Task DownloadPacksForSystem(RedumpClient rc, RedumpSystem? system, string? outDir, bool useSubfolders) { - var systemAsArray = new RedumpSystem?[] { system }; + if (system == null) + return false; + + var systemAsArray = new RedumpSystem[] { system.Value }; if (system.HasCues()) await rc.DownloadPacks(Constants.PackCuesUrl, systemAsArray, "CUEs", outDir, useSubfolders ? "cue" : null); diff --git a/SabreTools.RedumpLib/Web/RedumpClient.cs b/SabreTools.RedumpLib/Web/RedumpClient.cs index 9f88e81..de62006 100644 --- a/SabreTools.RedumpLib/Web/RedumpClient.cs +++ b/SabreTools.RedumpLib/Web/RedumpClient.cs @@ -725,7 +725,7 @@ namespace SabreTools.RedumpLib.Web /// Base URL to download using /// Systems to download packs for /// Name of the pack that is downloading - public async Task> DownloadPacks(string url, RedumpSystem?[] systems, string title) + public async Task> DownloadPacks(string url, RedumpSystem[] systems, string title) { var packsDictionary = new Dictionary(); @@ -733,7 +733,7 @@ namespace SabreTools.RedumpLib.Web foreach (var system in systems) { // If the system is invalid, we can't do anything - if (system == null || !system.IsAvailable()) + if (!system.IsAvailable()) continue; // If we didn't have credentials @@ -748,7 +748,7 @@ namespace SabreTools.RedumpLib.Web Console.Write($"\r{longName}{new string(' ', Console.BufferWidth - longName!.Length - 1)}"); byte[]? pack = await DownloadSinglePack(url, system); if (pack != null) - packsDictionary.Add(system.Value, pack); + packsDictionary.Add(system, pack); } Console.Write($"\rComplete!{new string(' ', Console.BufferWidth - 10)}"); @@ -765,13 +765,13 @@ namespace SabreTools.RedumpLib.Web /// Name of the pack that is downloading /// Output directory to save data to /// Named subfolder for the pack, used optionally - public async Task DownloadPacks(string url, RedumpSystem?[] systems, string title, string? outDir, string? subfolder) + public async Task DownloadPacks(string url, RedumpSystem[] systems, string title, string? outDir, string? subfolder) { Console.WriteLine($"Downloading {title}"); foreach (var system in systems) { // If the system is invalid, we can't do anything - if (system == null || !system.IsAvailable()) + if (!system.IsAvailable()) continue; // If we didn't have credentials