//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="ManagedNameResolver" // Project: StgSharp // Copyright (c) Nitload. // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace GenerateGL.CodeGen { internal static partial class ManagedNameResolver { private static readonly HashSet TypedSuffixFamilies = new( StringComparer.Ordinal) { "ClearBuffer", "ClearNamedFramebuffer", "GetBufferParameter", "GetNamedBufferParameter", "GetQueryBufferObject", "GetQueryObject", "GetSamplerParameter", "GetTexLevelParameter", "GetTexParameter", "GetTextureLevelParameter", "GetTextureParameter", "GetUniform", "GetnUniform", "GetVertexAttrib", "PatchParameter", "PixelStore", "PointParameter", "SamplerParameter", "TexParameter", "TextureParameter" }; private static readonly Dictionary ExactCandidates = new Dictionary(StringComparer.Ordinal) { { "BlendEquationi", [ "BlendEquation" ] }, { "BlendEquationSeparatei", [ "BlendEquationSeparate" ] }, { "BlendFunci", [ "BlendFunc" ] }, { "BlendFuncSeparatei", [ "BlendFuncSeparate" ] }, { "ClearDepthf", [ "ClearDepth" ] }, { "ColorMaski", [ "ColorMask" ] }, { "DepthRangef", [ "DepthRange" ] }, { "Disablei", [ "Disable" ] }, { "Enablei", [ "Enable" ] }, { "FramebufferParameteri", [ "FramebufferParameter" ] }, { "GetStringi", [ "GetString" ] }, { "IsEnabledi", [ "IsEnabled" ] }, { "NamedFramebufferParameteri", [ "NamedFramebufferParameter" ] }, { "ProgramParameteri", [ "ProgramParameter" ] }, { "ScissorIndexedv", [ "ScissorIndexed" ] }, { "ViewportIndexedf", [ "ViewportIndexed" ] }, { "ViewportIndexedfv", [ "ViewportIndexed" ] } }; public static IReadOnlyDictionary Resolve( IEnumerable commands ) { Dictionary result = new(StringComparer.Ordinal); HashSet usedSignatures = new(StringComparer.Ordinal); foreach (GlCommand command in commands.OrderBy(c => c.Name, StringComparer.Ordinal)) { string signature = BuildParameterSignature(command); foreach (string candidate in GetCandidates(command.Name)) { if (usedSignatures.Add($"{candidate}({signature})")) { result.Add(command.Name, candidate); break; } } } return result; } private static string BuildParameterSignature( GlCommand command ) { ManagedCommandPlan plan = ManagedCommandPlan.Create(command); return string.Join(",", plan.PublicParameters.Select(p => p.PublicType)); } private static IEnumerable GetCandidates( string glName ) { string name = TrimGlPrefix(glName); Match vertexAttrib = VertexAttribRegex().Match(name); if (vertexAttrib.Success) { string stem = vertexAttrib.Groups[1].Value; string dimension = vertexAttrib.Groups[2].Value; bool normalized = vertexAttrib.Groups[3].Success; yield return normalized ? $"{stem}Normalized" : stem; yield return normalized ? $"{stem}{dimension}Normalized" : $"{stem}{dimension}"; yield return name; yield break; } Match typed = TypedSuffixRegex().Match(name); if (typed.Success && TypedSuffixFamilies.Contains(typed.Groups[1].Value)) { string stem = typed.Groups[1].Value; string suffix = typed.Groups[2].Value; if (suffix.StartsWith('I')) { yield return $"{stem}I"; } yield return stem; yield return name; yield break; } if (ExactCandidates.TryGetValue(name, out string[]? candidates)) { foreach (string candidate in candidates) { yield return candidate; } } yield return name; } private static string TrimGlPrefix( string name ) { return name.StartsWith("gl", StringComparison.Ordinal) && name.Length > 2 ? name.Substring(2) : name; } [GeneratedRegex("^(VertexAttrib(?:I|L|P)?)([1-4])(N)?(?:ui|ub|us|i|b|s|f|d)(v)?$", RegexOptions.CultureInvariant)] private static partial Regex VertexAttribRegex(); [GeneratedRegex("^(.+?)(Iuiv|Iiv|ui64v|i64v|uiv|ui|iv|fi|i|fv|f|dv|d)$", RegexOptions.CultureInvariant)] private static partial Regex TypedSuffixRegex(); } }