From 74a65b7a8b4d8503cfdb2a6f1e16b6ebbc62fc64 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 4 Jul 2026 11:01:44 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B7=E3=82=A7=E3=83=BC=E3=83=80=E3=83=BC?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=81=AE=E8=A7=A3=E6=9E=90=E5=99=A8=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripting/AviUtlPixelShaderLibrary.cs | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 LuaScript.Compat/Scripting/AviUtlPixelShaderLibrary.cs diff --git a/LuaScript.Compat/Scripting/AviUtlPixelShaderLibrary.cs b/LuaScript.Compat/Scripting/AviUtlPixelShaderLibrary.cs new file mode 100644 index 0000000..795f650 --- /dev/null +++ b/LuaScript.Compat/Scripting/AviUtlPixelShaderLibrary.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; + +namespace LuaScript.Compat +{ + internal sealed class AviUtlPixelShaderLibrary + { + private const string SectionMarker = "--[[pixelshader@"; + + public static AviUtlPixelShaderLibrary Empty { get; } = new(null); + + private readonly Dictionary? _shaders; + + private AviUtlPixelShaderLibrary(Dictionary? shaders) => _shaders = shaders; + + public int Count => _shaders?.Count ?? 0; + + public static AviUtlPixelShaderLibrary Parse(string? source) + { + if (string.IsNullOrEmpty(source)) + return Empty; + + Dictionary? shaders = null; + int index = 0; + while (index < source.Length) + { + int lineStart = index; + int lineBreak = source.IndexOf('\n', index); + index = lineBreak < 0 ? source.Length : lineBreak + 1; + + int i = ScriptParserHelper.SkipSpaces(source, lineStart); + if (!MatchesMarker(source, i)) + continue; + i += SectionMarker.Length; + + int nameStart = i; + while (i < source.Length && IsEntryPointChar(source[i])) + i++; + if (i == nameStart || i >= source.Length || source[i] != ':') + continue; + + string name = source[nameStart..i]; + int bodyStart = i + 1; + int bodyEnd = source.IndexOf("]]", bodyStart, StringComparison.Ordinal); + if (bodyEnd < 0) + continue; + + shaders ??= new Dictionary(StringComparer.Ordinal); + shaders.TryAdd(name, source[bodyStart..bodyEnd]); + index = bodyEnd + 2; + } + + return shaders is null ? Empty : new AviUtlPixelShaderLibrary(shaders); + } + + public bool TryGet(string name, out string hlsl) + { + hlsl = string.Empty; + if (_shaders is null || string.IsNullOrEmpty(name)) + return false; + + int at = name.IndexOf('@'); + string local = at < 0 ? name : name[..at]; + if (_shaders.TryGetValue(local, out var found)) + { + hlsl = found; + return true; + } + return false; + } + + public static string EntryPointOf(string name) + { + int at = name.IndexOf('@'); + return at < 0 ? name : name[..at]; + } + + private static bool MatchesMarker(string text, int index) => + index + SectionMarker.Length <= text.Length && + string.CompareOrdinal(text, index, SectionMarker, 0, SectionMarker.Length) == 0; + + private static bool IsEntryPointChar(char c) => + ScriptParserHelper.IsNameChar(c) || c == '_'; + } +}