mirror of
https://github.com/SabreTools/MPF.git
synced 2026-07-02 17:24:48 +00:00
179 lines
6.0 KiB
C#
179 lines
6.0 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
|
|
|
|
#region Convert From String
|
|
|
|
/// <summary>
|
|
/// Get the InternalProgram enum value for a given string
|
|
/// </summary>
|
|
/// <param name="internalProgram">String value to convert</param>
|
|
/// <returns>InternalProgram represented by the string, if possible</returns>
|
|
public static InternalProgram ToInternalProgram(string? internalProgram)
|
|
{
|
|
return (internalProgram?.ToLowerInvariant()) switch
|
|
{
|
|
// Dumping support
|
|
"aaru"
|
|
or "chef"
|
|
or "dichef"
|
|
or "discimagechef" => InternalProgram.Aaru,
|
|
"creator"
|
|
or "dic"
|
|
or "dicreator"
|
|
or "discimagecreator" => InternalProgram.DiscImageCreator,
|
|
"rd"
|
|
or "redumper" => InternalProgram.Redumper,
|
|
|
|
// Verification support only
|
|
"cleanrip"
|
|
or "cr" => InternalProgram.CleanRip,
|
|
"ps3cfw"
|
|
or "ps3"
|
|
or "getkey"
|
|
or "managunz"
|
|
or "multiman" => InternalProgram.PS3CFW,
|
|
"uic"
|
|
or "umd"
|
|
or "umdcreator"
|
|
or "umdimagecreator" => InternalProgram.UmdImageCreator,
|
|
"xbc"
|
|
or "xbox"
|
|
or "xbox360"
|
|
or "xboxcreator"
|
|
or "xboxbackupcreator" => InternalProgram.XboxBackupCreator,
|
|
|
|
_ => InternalProgram.NONE,
|
|
};
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|