diff --git a/LuaScript/Compat/AviUtlCompatMap.cs b/LuaScript/Compat/AviUtlCompatMap.cs new file mode 100644 index 0000000..9e18bf8 --- /dev/null +++ b/LuaScript/Compat/AviUtlCompatMap.cs @@ -0,0 +1,131 @@ +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Xml.Linq; + +namespace LuaScript.Compat +{ + internal sealed class AviUtlCompatMap + { + private const string ResourceSuffix = "AviUtlCompatMap.xml"; + + private readonly Dictionary> _effects; + + public static AviUtlCompatMap Default { get; } = Load(); + + private AviUtlCompatMap(Dictionary> effects) + { + _effects = effects; + } + + public bool TryResolve(string name, AviUtlEngine target, out AviUtlEffectMapping mapping) + { + mapping = null!; + if (string.IsNullOrEmpty(name) || !_effects.TryGetValue(Normalize(name), out var candidates)) + return false; + + foreach (var candidate in candidates) + { + if (candidate.Engine.Includes(target)) + { + mapping = candidate; + return true; + } + } + return false; + } + + public static AviUtlEngine ResolveTarget(string? script) + { + if (string.IsNullOrEmpty(script)) + return AviUtlEngine.AviUtl; + + foreach (var raw in script.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n')) + { + var line = raw.Trim(); + if (!line.StartsWith("--!", System.StringComparison.Ordinal)) + continue; + var token = line[3..].Trim().ToLowerInvariant(); + if (token == "aviutl2") + return AviUtlEngine.AviUtl2; + if (token == "aviutl") + return AviUtlEngine.AviUtl; + } + return AviUtlEngine.AviUtl; + } + + private static AviUtlCompatMap Load() + { + var effects = new Dictionary>(StringComparer.OrdinalIgnoreCase); + using var stream = OpenResource(); + if (stream is null) + return new AviUtlCompatMap(effects); + + var document = XDocument.Load(stream); + var root = document.Root; + if (root is null) + return new AviUtlCompatMap(effects); + + foreach (var element in root.Elements("effect")) + { + var target = (string?)element.Attribute("target"); + if (string.IsNullOrWhiteSpace(target)) + continue; + + if (!AviUtlEngineExtensions.TryParse(((string?)element.Attribute("engine"))?.Trim().ToLowerInvariant(), out var engine)) + engine = AviUtlEngine.Both; + + var parameters = element.Elements("param").Select(ParseParameter).ToArray(); + var mapping = new AviUtlEffectMapping(engine, target.Trim(), parameters); + + foreach (var name in SplitNames((string?)element.Attribute("names"))) + { + if (!effects.TryGetValue(name, out var list)) + { + list = []; + effects[name] = list; + } + list.Add(mapping); + } + } + + return new AviUtlCompatMap(effects); + } + + private static AviUtlParameterMapping ParseParameter(XElement element) + { + var source = ((string?)element.Attribute("name") ?? string.Empty).Trim(); + var property = ((string?)element.Attribute("property") ?? string.Empty).Trim(); + double scale = ParseDouble((string?)element.Attribute("scale"), 1d); + double offset = ParseDouble((string?)element.Attribute("offset"), 0d); + return new AviUtlParameterMapping(source, property, scale, offset); + } + + private static IEnumerable SplitNames(string? names) + { + if (string.IsNullOrWhiteSpace(names)) + yield break; + foreach (var part in names.Split(',')) + { + var trimmed = part.Trim(); + if (trimmed.Length != 0) + yield return Normalize(trimmed); + } + } + + private static double ParseDouble(string? value, double fallback) => + double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double result) ? result : fallback; + + private static string Normalize(string value) => value.Trim().ToLowerInvariant(); + + private static Stream? OpenResource() + { + var assembly = typeof(AviUtlCompatMap).Assembly; + var name = assembly.GetManifestResourceNames() + .FirstOrDefault(n => n.EndsWith(ResourceSuffix, System.StringComparison.Ordinal)); + return name is null ? null : assembly.GetManifestResourceStream(name); + } + } +} diff --git a/LuaScript/Compat/AviUtlCompatMap.xml b/LuaScript/Compat/AviUtlCompatMap.xml new file mode 100644 index 0000000..76b6524 --- /dev/null +++ b/LuaScript/Compat/AviUtlCompatMap.xml @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LuaScript/Compat/AviUtlEffectMapping.cs b/LuaScript/Compat/AviUtlEffectMapping.cs new file mode 100644 index 0000000..6152f89 --- /dev/null +++ b/LuaScript/Compat/AviUtlEffectMapping.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; + +namespace LuaScript.Compat +{ + internal readonly record struct AviUtlParameterMapping(string Source, string Property, double Scale, double Offset) + { + public double Transform(double value) => value * Scale + Offset; + } + + internal sealed class AviUtlEffectMapping + { + private readonly Dictionary _parameters; + + public AviUtlEngine Engine { get; } + + public string TargetType { get; } + + public AviUtlEffectMapping(AviUtlEngine engine, string targetType, IEnumerable parameters) + { + Engine = engine; + TargetType = targetType; + _parameters = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var parameter in parameters) + _parameters[parameter.Source] = parameter; + } + + public bool TryGetParameter(string source, out AviUtlParameterMapping mapping) => + _parameters.TryGetValue(source, out mapping); + } +} diff --git a/LuaScript/Compat/AviUtlEngine.cs b/LuaScript/Compat/AviUtlEngine.cs new file mode 100644 index 0000000..73f5ff2 --- /dev/null +++ b/LuaScript/Compat/AviUtlEngine.cs @@ -0,0 +1,34 @@ +namespace LuaScript.Compat +{ + internal enum AviUtlEngine + { + AviUtl, + AviUtl2, + Both, + } + + internal static class AviUtlEngineExtensions + { + public static bool Includes(this AviUtlEngine scope, AviUtlEngine target) => + scope == AviUtlEngine.Both || scope == target; + + public static bool TryParse(string? value, out AviUtlEngine engine) + { + switch (value) + { + case "aviutl": + engine = AviUtlEngine.AviUtl; + return true; + case "aviutl2": + engine = AviUtlEngine.AviUtl2; + return true; + case "both": + engine = AviUtlEngine.Both; + return true; + default: + engine = AviUtlEngine.AviUtl; + return false; + } + } + } +}