using System; #if NET20 || NET35 using System.Collections.Generic; #else using System.Collections.Concurrent; #endif using System.Reflection; using SabreTools.RedumpLib.Data; using RedumperReadMethod = MPF.ExecutionContexts.Redumper.ReadMethod; using RedumperSectorOrder = MPF.ExecutionContexts.Redumper.SectorOrder; namespace MPF.Frontend { public static class EnumExtensions { #region Convert to Long Name /// /// Long name method cache /// #if NET20 || NET35 private static readonly Dictionary LongNameMethods = []; #else private static readonly ConcurrentDictionary LongNameMethods = []; #endif /// /// Get the string representation of a generic enumerable value /// /// Enum value to convert /// String representation of that value if possible, empty string on error public static string GetLongName(Enum value) { try { var sourceType = value.GetType(); sourceType = Nullable.GetUnderlyingType(sourceType) ?? sourceType; if (!LongNameMethods.TryGetValue(sourceType, out var method)) { method = typeof(Extensions).GetMethod("LongName", [typeof(Nullable<>).MakeGenericType(sourceType)]); method ??= typeof(EnumExtensions).GetMethod("LongName", [typeof(Nullable<>).MakeGenericType(sourceType)]); #if NET20 || NET35 LongNameMethods[sourceType] = method; #else LongNameMethods.TryAdd(sourceType, method); #endif } if (method != null) return method.Invoke(null, new[] { value }) as string ?? string.Empty; else return string.Empty; } catch { // Converter is not implemented for the given type return string.Empty; } } /// /// Get the string representation of the InternalProgram enum values /// /// InternalProgram value to convert /// String representing the value, if possible public static string LongName(this InternalProgram? prog) { return (prog) switch { #region Dumping support InternalProgram.Aaru => "Aaru", InternalProgram.DiscImageCreator => "DiscImageCreator", InternalProgram.Redumper => "Redumper", #endregion #region Verification support only InternalProgram.CleanRip => "CleanRip", InternalProgram.PS3CFW => "PS3 CFW", InternalProgram.UmdImageCreator => "UmdImageCreator", InternalProgram.XboxBackupCreator => "XboxBackupCreator", #endregion InternalProgram.NONE => "Unknown", _ => "Unknown", }; } /// /// Get the string representation of the RedumperReadMethod enum values /// /// RedumperReadMethod value to convert /// String representing the value, if possible public static string LongName(this RedumperReadMethod? method) { return (method) switch { RedumperReadMethod.D8 => "D8", RedumperReadMethod.BE => "BE", RedumperReadMethod.BE_CDDA => "BE_CDDA", RedumperReadMethod.NONE => "Default", _ => "Unknown", }; } /// /// Get the string representation of the RedumperSectorOrder enum values /// /// RedumperSectorOrder value to convert /// String representing the value, if possible public static string LongName(this RedumperSectorOrder? order) { return (order) switch { RedumperSectorOrder.DATA_C2_SUB => "DATA_C2_SUB", RedumperSectorOrder.DATA_SUB_C2 => "DATA_SUB_C2", RedumperSectorOrder.DATA_SUB => "DATA_SUB", RedumperSectorOrder.DATA_C2 => "DATA_C2", RedumperSectorOrder.NONE => "Default", _ => "Unknown", }; } #endregion #region Convert from String /// /// Get the RedumperReadMethod enum value for a given string /// /// String value to convert /// RedumperReadMethod represented by the string, if possible public static RedumperReadMethod ToRedumperReadMethod(this string? method) { return (method?.ToLowerInvariant()) switch { "d8" => RedumperReadMethod.D8, "be" => RedumperReadMethod.BE, "be_cdda" or "be cdda" or "be-cdda" or "becdda" => RedumperReadMethod.BE_CDDA, _ => RedumperReadMethod.NONE, }; } /// /// Get the RedumperSectorOrder enum value for a given string /// /// String value to convert /// RedumperSectorOrder represented by the string, if possible public static RedumperSectorOrder ToRedumperSectorOrder(this string? order) { return (order?.ToLowerInvariant()) switch { "data_c2_sub" or "data c2 sub" or "data-c2-sub" or "datac2sub" => RedumperSectorOrder.DATA_C2_SUB, "data_sub_c2" or "data sub c2" or "data-sub-c2" or "datasubc2" => RedumperSectorOrder.DATA_SUB_C2, "data_sub" or "data sub" or "data-sub" or "datasub" => RedumperSectorOrder.DATA_SUB, "data_c2" or "data c2" or "data-c2" or "datac2" => RedumperSectorOrder.DATA_C2, _ => RedumperSectorOrder.NONE, }; } #endregion #region Functionality Support /// /// Get if a system requires an anti-modchip scan /// public static bool SupportsAntiModchipScans(this RedumpSystem? system) { return system switch { RedumpSystem.SonyPlayStation => true, _ => false, }; } /// /// Get if a system requires a copy protection scan /// public static bool SupportsCopyProtectionScans(this RedumpSystem? system) { return system switch { RedumpSystem.AppleMacintosh => true, RedumpSystem.EnhancedCD => true, RedumpSystem.IBMPCcompatible => true, RedumpSystem.PalmOS => true, RedumpSystem.PocketPC => true, RedumpSystem.RainbowDisc => true, RedumpSystem.SonyElectronicBook => true, _ => false, }; } #endregion } }