using System; using System.Collections.Generic; using Vortice.Direct2D1; using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.Player.Video; using YukkuriMovieMaker.Plugin; using YukkuriMovieMaker.Plugin.Effects; using LuaScript.Compat; namespace LuaScript { internal sealed class VideoEffectChain : IDisposable { private sealed class Node { public required IVideoEffect Model; public required IVideoEffectProcessor Processor; public required AviUtlEffectMapping? Mapping; public required int RequestIndex; } private static Dictionary? _typeRegistry; private static Dictionary? _nameRegistry; private readonly IGraphicsDevicesAndContext _devices; private readonly List _nodes = []; private string[] _signature = []; private AviUtlEngine _signatureTarget = AviUtlEngine.Both; public VideoEffectChain(IGraphicsDevicesAndContext devices) { _devices = devices; } public ID2D1Image Apply(ID2D1Image source, IReadOnlyList requests, EffectDescription baseDesc, AviUtlEngine target, ref DrawDescription drawDescription) { try { SyncNodes(requests, target); } catch { return source; } var current = source; var desc = drawDescription; foreach (var node in _nodes) { try { ApplyParameters(node.Model, node.Mapping, requests[node.RequestIndex].Arguments); node.Processor.SetInput(current); var effectDescription = new EffectDescription(baseDesc, desc, baseDesc.InputIndex, baseDesc.InputCount, baseDesc.GroupIndex, baseDesc.GroupCount); desc = node.Processor.Update(effectDescription); current = node.Processor.Output; } catch { } } drawDescription = desc; return current; } private void SyncNodes(IReadOnlyList requests, AviUtlEngine target) { if (SignatureMatches(requests, target)) return; DisposeNodes(); _signatureTarget = target; _signature = new string[requests.Count]; for (int i = 0; i < requests.Count; i++) _signature[i] = requests[i].Name; for (int i = 0; i < requests.Count; i++) { Type? type; AviUtlEffectMapping? mapping; if (AviUtlCompatMap.Default.TryResolve(requests[i].Name, target, out var resolved) && ResolveType(resolved.TargetType) is { } mappedType) { mapping = resolved; type = mappedType; } else { mapping = null; type = ResolveName(requests[i].Name); } if (type is null) continue; IVideoEffect? model = null; try { model = Activator.CreateInstance(type) as IVideoEffect; } catch { } if (model is null) continue; IVideoEffectProcessor processor; try { processor = model.CreateVideoEffect(_devices); } catch { continue; } _nodes.Add(new Node { Model = model, Processor = processor, Mapping = mapping, RequestIndex = i }); } } private bool SignatureMatches(IReadOnlyList requests, AviUtlEngine target) { if (_signatureTarget != target || _signature.Length != requests.Count) return false; for (int i = 0; i < requests.Count; i++) { if (!string.Equals(_signature[i], requests[i].Name, StringComparison.Ordinal)) return false; } return true; } private static Type? ResolveType(string targetType) { EnsureRegistries(); return _typeRegistry!.TryGetValue(Normalize(targetType), out var type) ? type : null; } private static Type? ResolveName(string name) { EnsureRegistries(); return _nameRegistry!.TryGetValue(Normalize(name), out var type) ? type : null; } private static void EnsureRegistries() { if (_typeRegistry is not null && _nameRegistry is not null) return; var byType = new Dictionary(StringComparer.Ordinal); var byName = new Dictionary(StringComparer.Ordinal); try { foreach (var factory in EffectFactories.VideoEffectFactories) { try { var type = factory.EffectType; if (type is null) continue; byType.TryAdd(Normalize(type.Name), type); Register(byName, factory.Name, type); foreach (var keyword in factory.Keywords) Register(byName, keyword, type); } catch { } } } catch { } _typeRegistry = byType; _nameRegistry = byName; } private static void Register(Dictionary map, string? key, Type type) { if (string.IsNullOrWhiteSpace(key)) return; map.TryAdd(Normalize(key), type); } private static string Normalize(string value) => ParameterBinder.Normalize(value); private static void ApplyParameters(IVideoEffect model, AviUtlEffectMapping? mapping, IReadOnlyList> arguments) { if (arguments.Count == 0) return; if (mapping is null) { ParameterBinder.ApplyArguments(model, arguments); return; } var properties = ParameterBinder.GetProperties(model.GetType()); foreach (var (key, value) in arguments) { if (!mapping.TryGetParameter(key, out var parameter) || !properties.TryGetValue(Normalize(parameter.Property), out var property)) continue; try { ParameterBinder.SetValue(model, property, value, parameter); } catch { } } } private void DisposeNodes() { foreach (var node in _nodes) { try { node.Processor.ClearInput(); } catch { } node.Processor.Dispose(); } _nodes.Clear(); } public void Dispose() => DisposeNodes(); } }