パラメータ適用を共有バインダーへ抽出

このコミットが含まれているのは:
routersys committed 2026-07-04 22:00:08 +09:00
1 親 48f6711f1a
コミット f5fd6cebdb
2ファイル変更
+151 -117

検索条件に一致するファイルはありません

+12 -117
ファイルの表示
@@ -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<string, Type>? _typeRegistry;
private static Dictionary<string, Type>? _nameRegistry;
private static readonly Dictionary<Type, Dictionary<string, PropertyInfo>> _propertyCache = [];
private readonly IGraphicsDevicesAndContext _devices;
private readonly List<Node> _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<KeyValuePair<string, object>> 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<string, PropertyInfo> GetProperties(Type type)
{
if (_propertyCache.TryGetValue(type, out var cached))
return cached;
var map = new Dictionary<string, PropertyInfo>(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<DisplayAttribute>()?.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)
+139
ファイルの表示
@@ -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<Type, Dictionary<string, PropertyInfo>> s_propertyCache = [];
public static string Normalize(string value) => value.Trim().ToLowerInvariant();
public static Dictionary<string, PropertyInfo> GetProperties(Type type)
{
lock (s_propertyCache)
{
if (s_propertyCache.TryGetValue(type, out var cached))
return cached;
}
var map = new Dictionary<string, PropertyInfo>(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<DisplayAttribute>()?.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<KeyValuePair<string, object>> 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));
}
}
}