152 行
5.9 KiB
C#
152 行
5.9 KiB
C#
using System.Collections.Concurrent;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Resources;
|
|
using YukkuriMovieMaker.Commons;
|
|
using YukkuriMovieMaker.Controls;
|
|
using YukkuriMovieMaker.Plugin;
|
|
using YukkuriMovieMaker.Plugin.Brush;
|
|
using YukkuriMovieMaker.Plugin.Shape;
|
|
using YukkuriMovieMaker.Plugin.Tachie;
|
|
using YukkuriMovieMaker.Plugin.Transition;
|
|
using YukkuriMovieMaker.Plugin.Voice;
|
|
|
|
namespace UndoTree;
|
|
|
|
internal static class UndoTreeNames
|
|
{
|
|
const string TypePrefix = "Type_";
|
|
const string MemberPrefix = "Member_";
|
|
static readonly string HostNamespace = typeof(IPlugin).Namespace!.Split('.')[0];
|
|
|
|
const int MinimumPrefixLength = 4;
|
|
|
|
static readonly ConcurrentDictionary<Type, IPlugin?> Plugins = new();
|
|
|
|
static readonly ParameterFamily[] Families =
|
|
[
|
|
new([typeof(IAudioSpectrumParameter)], [typeof(IAudioSpectrumPlugin)], "AudioSpectrumParameterOf"),
|
|
new([typeof(IShapeParameter)], [typeof(IShapePlugin)], "ShapeParameterOf"),
|
|
new([typeof(IBrushParameter)], [typeof(IBrushPlugin)], "BrushParameterOf"),
|
|
new([typeof(ITransitionParameter)], [typeof(ITransitionPlugin)], "TransitionParameterOf"),
|
|
new([typeof(IVoiceParameter)], [typeof(IVoicePlugin)], "VoiceParameterOf"),
|
|
new([typeof(ITachieCharacterParameter)], [typeof(ITachiePlugin)], "TachieCharacterParameterOf"),
|
|
new([typeof(ITachieFaceParameter)], [typeof(ITachiePlugin)], "TachieFaceParameterOf"),
|
|
new([typeof(ITachieItemParameter)], [typeof(ITachiePlugin)], "TachieItemParameterOf"),
|
|
];
|
|
|
|
internal static Func<IEnumerable<IPlugin>> LoadedPlugins { get; set; } = () => PluginLoader.Plugins;
|
|
|
|
public static string OfType(Type type, CultureInfo? culture = null)
|
|
=> (IsHost(type) ? Find(TypePrefix + type.Name, culture) : null) ?? OfParameter(type, culture) ?? type.Name;
|
|
|
|
public static string OfMember(Type? owner, string member, CultureInfo? culture = null) => (owner is not null && IsHost(owner) ? Find(MemberPrefix + member, culture) : null) ?? member;
|
|
|
|
public static string OfPlugin(Type type)
|
|
=> Plugins.GetOrAdd(type, CreatePlugin)?.Name is { Length: > 0 } name ? name : OfType(type);
|
|
|
|
public static string OfEnum(Enum value)
|
|
{
|
|
if (value is AnimationType animation && AnimationName(animation) is { } name)
|
|
return name;
|
|
|
|
var type = value.GetType();
|
|
return string.Join(", ", value.ToString().Split(", ").Select(field => DisplayName(type, field)));
|
|
}
|
|
|
|
internal static IEnumerable<string> FamilyKeys => Families.Select(family => family.Key);
|
|
|
|
internal static string? FamilyKeyOf(Type type) => FamilyOf(type)?.Key;
|
|
|
|
static ParameterFamily? FamilyOf(Type type)
|
|
=> Families.FirstOrDefault(family => family.Parameters.Any(parameter => parameter.IsAssignableFrom(type)));
|
|
|
|
static string? OfParameter(Type type, CultureInfo? culture)
|
|
{
|
|
if (FamilyOf(type) is not { } family)
|
|
return null;
|
|
|
|
try
|
|
{
|
|
return PluginOf(type, family)?.Name is { Length: > 0 } name && Find(family.Key, culture) is { } template ? string.Format(template, name) : null;
|
|
}
|
|
catch (Exception exception) when (exception is not OutOfMemoryException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static IPlugin? PluginOf(Type parameter, ParameterFamily family)
|
|
{
|
|
var candidates = LoadedPlugins()
|
|
.Where(plugin => plugin.GetType().Assembly == parameter.Assembly && family.Plugins.Any(kind => kind.IsInstanceOfType(plugin)))
|
|
.ToArray();
|
|
if (candidates.Length <= 1)
|
|
return candidates.FirstOrDefault();
|
|
|
|
var scored = candidates
|
|
.Select(plugin => (Plugin: plugin, Length: CommonPrefixLength(parameter.Name, plugin.GetType().Name)))
|
|
.OrderByDescending(item => item.Length)
|
|
.ToArray();
|
|
return scored[0].Length >= MinimumPrefixLength && scored[0].Length > scored[1].Length ? scored[0].Plugin : null;
|
|
}
|
|
|
|
static int CommonPrefixLength(string first, string second)
|
|
{
|
|
var length = 0;
|
|
while (length < first.Length && length < second.Length && char.ToUpperInvariant(first[length]) == char.ToUpperInvariant(second[length]))
|
|
length++;
|
|
|
|
return length;
|
|
}
|
|
|
|
static bool IsHost(Type type) => type.Namespace is { } name && (name == HostNamespace || name.StartsWith(HostNamespace + ".", StringComparison.Ordinal));
|
|
|
|
static string? Find(string key, CultureInfo? culture)
|
|
=> Texts.ResourceManager.GetString(key, culture ?? Texts.Culture) is { Length: > 0 } text ? text : null;
|
|
|
|
static IPlugin? CreatePlugin(Type type)
|
|
{
|
|
if (!typeof(IPlugin).IsAssignableFrom(type) || type.IsAbstract || type.GetConstructor(Type.EmptyTypes) is null)
|
|
return null;
|
|
|
|
try
|
|
{
|
|
return Activator.CreateInstance(type) as IPlugin;
|
|
}
|
|
catch (Exception exception) when (exception is TargetInvocationException or MemberAccessException or InvalidOperationException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static string? AnimationName(AnimationType animation)
|
|
{
|
|
try
|
|
{
|
|
return AnimationTypeToToolTipConverter.ConvertText(animation, false);
|
|
}
|
|
catch (Exception exception) when (exception is NotImplementedException or ArgumentException or InvalidOperationException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
sealed record ParameterFamily(Type[] Parameters, Type[] Plugins, string Key);
|
|
|
|
static string DisplayName(Type type, string field)
|
|
{
|
|
try
|
|
{
|
|
if (type.GetField(field)?.GetCustomAttribute<DisplayAttribute>()?.GetName() is { Length: > 0 } name)
|
|
return name;
|
|
}
|
|
catch (Exception exception) when (exception is InvalidOperationException or MissingManifestResourceException or AmbiguousMatchException)
|
|
{
|
|
}
|
|
|
|
return field;
|
|
}
|
|
}
|