mirror of
https://github.com/SabreTools/MPF.git
synced 2026-07-02 17:24:48 +00:00
132 lines
4.4 KiB
C#
132 lines
4.4 KiB
C#
using System;
|
|
#if NET20 || NET35
|
|
using System.Collections.Generic;
|
|
#else
|
|
using System.Collections.Concurrent;
|
|
#endif
|
|
using System.Reflection;
|
|
using SabreTools.RedumpLib.Data;
|
|
|
|
namespace MPF.Core
|
|
{
|
|
public static class EnumExtensions
|
|
{
|
|
#region Convert to Long Name
|
|
|
|
/// <summary>
|
|
/// Long name method cache
|
|
/// </summary>
|
|
#if NET20 || NET35
|
|
private static readonly Dictionary<Type, MethodInfo?> LongNameMethods = [];
|
|
#else
|
|
private static readonly ConcurrentDictionary<Type, MethodInfo?> LongNameMethods = [];
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Get the string representation of a generic enumerable value
|
|
/// </summary>
|
|
/// <param name="value">Enum value to convert</param>
|
|
/// <returns>String representation of that value if possible, empty string on error</returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the string representation of the InternalProgram enum values
|
|
/// </summary>
|
|
/// <param name="prog">InternalProgram value to convert</param>
|
|
/// <returns>String representing the value, if possible</returns>
|
|
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",
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the string representation of the RedumperReadMethod enum values
|
|
/// </summary>
|
|
/// <param name="method">RedumperReadMethod value to convert</param>
|
|
/// <returns>String representing the value, if possible</returns>
|
|
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",
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the string representation of the RedumperSectorOrder enum values
|
|
/// </summary>
|
|
/// <param name="order">RedumperSectorOrder value to convert</param>
|
|
/// <returns>String representing the value, if possible</returns>
|
|
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
|
|
}
|
|
}
|