From f5fd6cebdb4ede97c872926381c23b03061ac9b2 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 4 Jul 2026 22:00:08 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC=E3=82=BF?= =?UTF-8?q?=E9=81=A9=E7=94=A8=E3=82=92=E5=85=B1=E6=9C=89=E3=83=90=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E3=83=80=E3=83=BC=E3=81=B8=E6=8A=BD=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/Drawing/VideoEffectChain.cs | 129 ++-------------------- LuaScript/Drawing/Ymm4PropertyBinder.cs | 139 ++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 117 deletions(-) create mode 100644 LuaScript/Drawing/Ymm4PropertyBinder.cs diff --git a/LuaScript/Drawing/VideoEffectChain.cs b/LuaScript/Drawing/VideoEffectChain.cs index d475365..76c6bd4 100644 --- a/LuaScript/Drawing/VideoEffectChain.cs +++ b/LuaScript/Drawing/VideoEffectChain.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Globalization; -using System.Reflection; -using System.Windows.Media; using Vortice.Direct2D1; using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.Player.Video; @@ -23,11 +19,8 @@ namespace LuaScript public required int RequestIndex; } - private static readonly AviUtlParameterMapping Identity = new(string.Empty, string.Empty, 1d, 0d); - private static Dictionary? _typeRegistry; private static Dictionary? _nameRegistry; - private static readonly Dictionary> _propertyCache = []; private readonly IGraphicsDevicesAndContext _devices; private readonly List _nodes = []; @@ -190,34 +183,29 @@ namespace LuaScript map.TryAdd(Normalize(key), type); } - private static string Normalize(string value) => value.Trim().ToLowerInvariant(); + private static string Normalize(string value) => Ymm4PropertyBinder.Normalize(value); private static void ApplyParameters(IVideoEffect model, AviUtlEffectMapping? mapping, IReadOnlyList> arguments) { if (arguments.Count == 0) return; - var properties = GetProperties(model.GetType()); + if (mapping is null) + { + Ymm4PropertyBinder.ApplyArguments(model, arguments); + return; + } + + var properties = Ymm4PropertyBinder.GetProperties(model.GetType()); foreach (var (key, value) in arguments) { - PropertyInfo? property; - AviUtlParameterMapping parameter; - if (mapping is not null) - { - if (!mapping.TryGetParameter(key, out parameter) || - !properties.TryGetValue(Normalize(parameter.Property), out property)) - continue; - } - else - { - if (!properties.TryGetValue(Normalize(key), out property)) - continue; - parameter = Identity; - } + if (!mapping.TryGetParameter(key, out var parameter) || + !properties.TryGetValue(Normalize(parameter.Property), out var property)) + continue; try { - SetValue(model, property, value, parameter); + Ymm4PropertyBinder.SetValue(model, property, value, parameter); } catch { @@ -225,99 +213,6 @@ namespace LuaScript } } - private static Dictionary GetProperties(Type type) - { - if (_propertyCache.TryGetValue(type, out var cached)) - return cached; - - var map = new Dictionary(StringComparer.Ordinal); - foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) - { - if (property.GetIndexParameters().Length != 0) - continue; - bool usable = property.PropertyType == typeof(Animation) || property.GetSetMethod() is not null; - if (!usable) - continue; - - map.TryAdd(Normalize(property.Name), property); - try - { - var display = property.GetCustomAttribute()?.GetName(); - if (!string.IsNullOrWhiteSpace(display)) - map.TryAdd(Normalize(display), property); - } - catch - { - } - } - - _propertyCache[type] = map; - return map; - } - - private static void SetValue(IVideoEffect model, PropertyInfo property, object value, AviUtlParameterMapping parameter) - { - var type = property.PropertyType; - - if (type == typeof(Animation)) - { - if (property.GetValue(model) is Animation animation) - animation.CopyFrom(new Animation(parameter.Transform(ToDouble(value)))); - return; - } - if (type == typeof(bool)) - { - property.SetValue(model, ToBool(value)); - return; - } - if (type.IsEnum) - { - property.SetValue(model, ToEnum(type, value)); - return; - } - if (type == typeof(Color)) - { - property.SetValue(model, ToColor(value)); - return; - } - if (type == typeof(double) || type == typeof(float) || type == typeof(int) || type == typeof(long) || type == typeof(short)) - { - property.SetValue(model, Convert.ChangeType(parameter.Transform(ToDouble(value)), type, CultureInfo.InvariantCulture)); - return; - } - if (type == typeof(string)) - property.SetValue(model, value?.ToString()); - } - - private static double ToDouble(object value) => value switch - { - double d => d, - bool b => b ? 1d : 0d, - string s => double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out var r) ? r : 0d, - _ => 0d, - }; - - private static bool ToBool(object value) => value switch - { - bool b => b, - double d => d != 0d, - string s => s.Equals("true", StringComparison.OrdinalIgnoreCase) || s == "1", - _ => false, - }; - - private static object ToEnum(Type type, object value) - { - if (value is string s && Enum.TryParse(type, s, true, out var parsed)) - return parsed; - return Enum.ToObject(type, (int)ToDouble(value)); - } - - private static Color ToColor(object value) - { - int rgb = (int)ToDouble(value); - return Color.FromRgb((byte)((rgb >> 16) & 0xFF), (byte)((rgb >> 8) & 0xFF), (byte)(rgb & 0xFF)); - } - private void DisposeNodes() { foreach (var node in _nodes) diff --git a/LuaScript/Drawing/Ymm4PropertyBinder.cs b/LuaScript/Drawing/Ymm4PropertyBinder.cs new file mode 100644 index 0000000..0753cd0 --- /dev/null +++ b/LuaScript/Drawing/Ymm4PropertyBinder.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Globalization; +using System.Reflection; +using System.Windows.Media; +using LuaScript.Compat; +using YukkuriMovieMaker.Commons; + +namespace LuaScript +{ + internal static class Ymm4PropertyBinder + { + public static readonly AviUtlParameterMapping Identity = new(string.Empty, string.Empty, 1d, 0d); + + private static readonly Dictionary> s_propertyCache = []; + + public static string Normalize(string value) => value.Trim().ToLowerInvariant(); + + public static Dictionary GetProperties(Type type) + { + lock (s_propertyCache) + { + if (s_propertyCache.TryGetValue(type, out var cached)) + return cached; + } + + var map = new Dictionary(StringComparer.Ordinal); + foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) + { + if (property.GetIndexParameters().Length != 0) + continue; + bool usable = property.PropertyType == typeof(Animation) || property.GetSetMethod() is not null; + if (!usable) + continue; + + map.TryAdd(Normalize(property.Name), property); + try + { + var display = property.GetCustomAttribute()?.GetName(); + if (!string.IsNullOrWhiteSpace(display)) + map.TryAdd(Normalize(display), property); + } + catch + { + } + } + + lock (s_propertyCache) + { + s_propertyCache[type] = map; + } + return map; + } + + public static void ApplyArguments(object model, IReadOnlyList> arguments) + { + if (arguments.Count == 0) + return; + + var properties = GetProperties(model.GetType()); + foreach (var (key, value) in arguments) + { + if (!properties.TryGetValue(Normalize(key), out var property)) + continue; + try + { + SetValue(model, property, value, Identity); + } + catch + { + } + } + } + + public static void SetValue(object model, PropertyInfo property, object value, AviUtlParameterMapping parameter) + { + var type = property.PropertyType; + + if (type == typeof(Animation)) + { + if (property.GetValue(model) is Animation animation) + animation.CopyFrom(new Animation(parameter.Transform(ToDouble(value)))); + return; + } + if (type == typeof(bool)) + { + property.SetValue(model, ToBool(value)); + return; + } + if (type.IsEnum) + { + property.SetValue(model, ToEnum(type, value)); + return; + } + if (type == typeof(Color)) + { + property.SetValue(model, ToColor(value)); + return; + } + if (type == typeof(double) || type == typeof(float) || type == typeof(int) || type == typeof(long) || type == typeof(short)) + { + property.SetValue(model, Convert.ChangeType(parameter.Transform(ToDouble(value)), type, CultureInfo.InvariantCulture)); + return; + } + if (type == typeof(string)) + property.SetValue(model, value?.ToString()); + } + + private static double ToDouble(object value) => value switch + { + double d => d, + bool b => b ? 1d : 0d, + string s => double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out var r) ? r : 0d, + _ => 0d, + }; + + private static bool ToBool(object value) => value switch + { + bool b => b, + double d => d != 0d, + string s => s.Equals("true", StringComparison.OrdinalIgnoreCase) || s == "1", + _ => false, + }; + + private static object ToEnum(Type type, object value) + { + if (value is string s && Enum.TryParse(type, s, true, out var parsed)) + return parsed; + return Enum.ToObject(type, (int)ToDouble(value)); + } + + private static Color ToColor(object value) + { + int rgb = (int)ToDouble(value); + return Color.FromRgb((byte)((rgb >> 16) & 0xFF), (byte)((rgb >> 8) & 0xFF), (byte)(rgb & 0xFF)); + } + } +}