text regex source gen framework is briefly done. Going on ir2code

This commit is contained in:
Youmu Liu
2026-07-14 17:44:29 +08:00
parent bc90e340df
commit 4d2a4eeb18
32 changed files with 276 additions and 140 deletions
@@ -0,0 +1,192 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="CsTypeMapper"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace GenerateGL.CodeGen
{
internal static class CsTypeMapper
{
public static string MapDelegateParameter(
GlParam param
)
{
int pointerLevel = param.PointerLevel;
if (param.CType.Equals("GLchar", StringComparison.Ordinal) && pointerLevel > 0) {
return $"byte{new string('*', pointerLevel)}";
}
string mapped = MapType(param.CType, allowVoid:false);
if (pointerLevel > 0)
{
if (mapped == "void") {
return $"void{new string('*', pointerLevel)}";
}
return $"{mapped}{new string('*', pointerLevel)}";
}
return mapped;
}
public static string MapDelegateReturn(
string cType,
int pointerLevel
)
{
string mapped = MapType(cType, allowVoid:true);
if (pointerLevel > 0) {
return $"{mapped}{new string('*', pointerLevel)}";
}
return mapped;
}
public static string MapParameter(
GlParam param
)
{
if (param.PointerLevel == 0)
{
if (param.CType.Equals("GLenum", StringComparison.Ordinal) &&
LooksLikeShaderType(param.Name) &&
!param.IsPointer) {
return "ShaderType";
}
if (param.CType.Equals("GLuint", StringComparison.Ordinal) &&
LooksLikeHandle(param.Name) &&
!param.IsPointer) {
return "GlHandle";
}
}
int pointerLevel = param.PointerLevel;
if (param.CType.Equals("GLchar", StringComparison.Ordinal) && pointerLevel > 0) {
return $"byte{new string('*', pointerLevel)}";
}
string mapped = MapType(param.CType, allowVoid:false);
if (pointerLevel > 0)
{
if (mapped == "void") {
return $"void{new string('*', pointerLevel)}";
}
return $"{mapped}{new string('*', pointerLevel)}";
}
return mapped;
}
public static string MapReturn(
string cType,
int pointerLevel
)
{
string mapped = MapType(cType, allowVoid:true);
if (pointerLevel > 0) {
return $"{mapped}{new string('*', pointerLevel)}";
}
return mapped;
}
public static bool TryMapConstPointerToSpan(
GlParam param,
out string spanElementType
)
{
spanElementType = string.Empty;
if (!param.IsConst || param.PointerLevel != 1) {
return false;
}
string mapped = MapType(param.CType, allowVoid:false);
if (mapped is "void") {
return false;
}
spanElementType = mapped;
return true;
}
private static bool LooksLikeHandle(
string name
)
{
if (string.IsNullOrWhiteSpace(name)) {
return false;
}
return name.Contains("program", StringComparison.OrdinalIgnoreCase) ||
name.Contains("shader", StringComparison.OrdinalIgnoreCase) ||
name.Contains("buffer", StringComparison.OrdinalIgnoreCase) ||
name.Contains("texture", StringComparison.OrdinalIgnoreCase) ||
name.Contains("framebuffer", StringComparison.OrdinalIgnoreCase) ||
name.Contains("renderbuffer", StringComparison.OrdinalIgnoreCase) ||
name.Contains("vertexarray", StringComparison.OrdinalIgnoreCase) ||
name.Contains("query", StringComparison.OrdinalIgnoreCase) ||
name.Contains("sampler", StringComparison.OrdinalIgnoreCase) ||
name.Contains("transformfeedback", StringComparison.OrdinalIgnoreCase);
}
private static bool LooksLikeShaderType(
string name
)
{
if (string.IsNullOrWhiteSpace(name)) {
return false;
}
return name.Contains("shadertype", StringComparison.OrdinalIgnoreCase) ||
name.Equals("type", StringComparison.OrdinalIgnoreCase);
}
private static string MapType(
string cType,
bool allowVoid
)
{
return cType switch
{
"void" when allowVoid => "void",
"GLenum" or "GLbitfield" => "uint",
"GLboolean" => "byte",
"GLuint" or "GLhandleARB" => "uint",
"GLint" or "GLsizei" or "GLfixed" => "int",
"GLshort" => "short",
"GLbyte" => "sbyte",
"GLubyte" => "byte",
"GLushort" => "ushort",
"GLfloat" => "float",
"GLdouble" => "double",
"GLclampf" or "GLclampd" => "float",
"GLintptr" => "nint",
"GLsizeiptr" => "nuint",
"GLchar" => "byte",
"GLsync" => "GLsync",
_ => allowVoid && cType == "void" ? "void" : "IntPtr"
};
}
}
}
@@ -0,0 +1,422 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Generator"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace GenerateGL.CodeGen
{
internal sealed class Generator
{
private static readonly string[] TargetVersions = {
"3.3", "4.0", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6"
};
private static readonly FrozenDictionary<string, string> ReservedNames =
new Dictionary<string, string>(StringComparer.Ordinal) {
{ "string", "@string" }, { "class", "@class" }, { "namespace", "@namespace" }, { "checked", "@checked" }, { "unchecked", "@unchecked" }, { "ref", "@ref" }, { "out", "@out" }, { "in", "@in" }, { "object", "@object" }, { "event", "@event" }, { "params", "@params" }, { "base", "@base" }, { "this", "@this" }, { "void", "@void" }, { "int", "@int" }, { "uint", "@uint" }, { "long", "@long" }, { "ulong", "@ulong" }, { "short", "@short" }, { "ushort", "@ushort" }, { "float", "@float" }, { "double", "@double" }, { "decimal", "@decimal" }, { "bool", "@bool" }, { "byte", "@byte" }, { "sbyte", "@sbyte" }, { "char", "@char" }, { "struct", "@struct" }, { "public", "@public" }, { "private", "@private" }, { "protected", "@protected" }, { "internal", "@internal" }, { "fixed", "@fixed" }
}.ToFrozenDictionary(StringComparer.Ordinal);
private readonly IReadOnlyDictionary<string, GlCommand> _commands;
private readonly IReadOnlyList<GlFeature> _features;
private readonly string _outputRoot;
public Generator(
IReadOnlyDictionary<string, GlCommand> commands,
IReadOnlyList<GlFeature> features,
string outputRoot
)
{
_commands = commands;
_features = features;
_outputRoot = outputRoot;
}
public void Run()
{
if (Directory.Exists(_outputRoot)) {
Directory.Delete(_outputRoot, recursive:true);
}
Directory.CreateDirectory(_outputRoot);
List<GlCommand> all = new List<GlCommand>();
HashSet<string> previous = new HashSet<string>(StringComparer.Ordinal);
foreach (string v in TargetVersions)
{
IReadOnlyList<GlCommand> cmdsUpTo = CollectCommandsUpToVersion(v);
List<GlCommand> versionCmds = cmdsUpTo.Where(c => !previous.Contains(c.Name))
.ToList();
foreach (GlCommand cmd in versionCmds) {
previous.Add(cmd.Name);
}
all.AddRange(versionCmds);
WriteOpenGLFunctionFile(v, versionCmds);
WriteGlContextFile(v, versionCmds);
}
WriteMergedGlContextFile(all);
WriteCommandHistoryFile(BuildCommandHistory());
}
private IReadOnlyDictionary<string, List<CommandHistoryEntry>> BuildCommandHistory()
{
Dictionary<string, List<CommandHistoryEntry>> history = new(StringComparer.Ordinal);
IEnumerable<GlFeature> ordered = _features.Where(f => Version.TryParse(f.Version, out _))
.OrderBy(f => Version.Parse(f.Version));
foreach (GlFeature feature in ordered)
{
string version = feature.Version;
foreach (string name in feature.RequiresCommands.Distinct(StringComparer.Ordinal))
{
if (!_commands.ContainsKey(name) || ShouldSkip(name))
{
continue;
}
if (!history.TryGetValue(name, out List<CommandHistoryEntry>? list))
{
list = new List<CommandHistoryEntry>();
history[name] = list;
}
if (list.Count == 0 ||
list[^1].Action != CommandHistoryAction.Added ||
list[^1].Version != version) {
list.Add(new CommandHistoryEntry(version, CommandHistoryAction.Added));
}
}
foreach (string name in feature.RemovedCommands.Distinct(StringComparer.Ordinal))
{
if (!_commands.ContainsKey(name) || ShouldSkip(name))
{
continue;
}
if (!history.TryGetValue(name, out List<CommandHistoryEntry>? list))
{
list = new List<CommandHistoryEntry>();
history[name] = list;
}
if (list.Count == 0 ||
list[^1].Action != CommandHistoryAction.Removed ||
list[^1].Version != version) {
list.Add(new CommandHistoryEntry(version, CommandHistoryAction.Removed));
}
}
}
return history;
}
private IReadOnlyList<GlCommand> CollectCommandsUpToVersion(
string version
)
{
Version target = Version.Parse(version);
IEnumerable<GlFeature> ordered = _features.Where(f => Version.TryParse(f.Version, out Version? fv) &&
fv <= target)
.OrderBy(f => Version.Parse(f.Version));
HashSet<string> active = new HashSet<string>(StringComparer.Ordinal);
foreach (GlFeature feature in ordered)
{
foreach (string name in feature.RequiresCommands.Distinct(StringComparer.Ordinal))
{
if (_commands.ContainsKey(name)) {
active.Add(name);
}
}
foreach (string name in feature.RemovedCommands.Distinct(StringComparer.Ordinal)) {
active.Remove(name);
}
}
return active.Select(name => _commands.TryGetValue(name, out GlCommand? c) ? c : null)
.Where(c => c != null)!
.Cast<GlCommand>()
.ToList();
}
private static string DelegateSignature(
string ret,
IReadOnlyList<string> paramTypes
)
{
if (paramTypes.Count == 0) {
return $"delegate*<{ret}>";
}
return $"delegate*<{string.Join(", ", paramTypes)}, {ret}>";
}
private static string SafeName(
string name
)
{
if (string.IsNullOrWhiteSpace(name)) {
return "arg";
}
if (ReservedNames.TryGetValue(name, out string? mapped)) {
return mapped;
}
return name;
}
private static bool ShouldSkip(
string name
)
{
// No skipping; include legacy/fixed-function APIs as well.
return false;
}
private static string TrimGlPrefix(
string name
)
{
if (name.StartsWith("gl", StringComparison.Ordinal) && name.Length > 2) {
return name.Substring(2);
}
return name;
}
private void WriteCommandHistoryFile(
IReadOnlyDictionary<string, List<CommandHistoryEntry>> history
)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("// <auto-generated>");
sb.AppendLine($"// Generated by StgSharp.GenerateGL on {DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture)}");
sb.AppendLine("// Command availability history");
sb.AppendLine("// </auto-generated>");
sb.AppendLine("using System;\nusing System.Collections.Frozen;\nusing System.Collections.Generic;\n");
sb.AppendLine("namespace StgSharp.Graphics.OpenGL");
sb.AppendLine("{");
sb.AppendLine(" internal enum CommandHistoryAction { Added, Removed }");
sb.AppendLine();
sb.AppendLine(" internal readonly record struct CommandHistoryEntry(string Version, CommandHistoryAction Action);");
sb.AppendLine();
sb.AppendLine(" internal static class GlCommandHistory");
sb.AppendLine(" {");
sb.AppendLine(" internal static readonly FrozenDictionary<string, CommandHistoryEntry[]> History = new Dictionary<string, CommandHistoryEntry[]>(StringComparer.Ordinal)");
sb.AppendLine(" {");
foreach (KeyValuePair<string, List<CommandHistoryEntry>> kvp in history.OrderBy(k => k.Key, StringComparer.Ordinal))
{
string events = string.Join(", ", kvp.Value
.OrderBy(v => Version.Parse(v.Version))
.Select(v => $"new CommandHistoryEntry(\"{v.Version}\", CommandHistoryAction.{v.Action})"));
sb.AppendLine($" {{ \"{kvp.Key}\", new CommandHistoryEntry[] {{ {events} }} }},");
}
sb.AppendLine(" }.ToFrozenDictionary(StringComparer.Ordinal);");
sb.AppendLine(" }");
sb.AppendLine("}");
string fileName = Path.Combine(_outputRoot, "glCommandHistory.cs");
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
}
private void WriteGlContextFile(
string version,
IReadOnlyList<GlCommand> commands
)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("// <auto-generated>");
sb.AppendLine($"// Generated by StgSharp.GenerateGL on {DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture)}");
sb.AppendLine($"// Version: {version}");
sb.AppendLine("// </auto-generated>");
sb.AppendLine("using System;\nusing System.Runtime.CompilerServices;\n");
sb.AppendLine("namespace StgSharp.Graphics.OpenGL");
sb.AppendLine("{");
sb.AppendLine(" public unsafe partial struct OpenglContext");
sb.AppendLine(" {");
foreach (GlCommand cmd in commands.OrderBy(c => c!.Name, StringComparer.Ordinal))
{
string ret = CsTypeMapper.MapDelegateReturn(cmd.ReturnType, cmd.ReturnPointerLevel);
List<string> paramTypes = new List<string>();
foreach (GlParam p in cmd.Parameters)
{
string mapped = CsTypeMapper.MapDelegateParameter(p);
paramTypes.Add(mapped);
}
string decl = DelegateSignature(ret, paramTypes);
sb.AppendLine($" internal unsafe {decl} {cmd.Name};");
}
sb.AppendLine(" }");
sb.AppendLine("}");
string fileName = Path.Combine(_outputRoot, $"glContext.v{version.Replace('.', '_')}.cs");
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
}
private void WriteMergedGlContextFile(
IReadOnlyList<GlCommand> commands
)
{
List<GlCommand> distinct = commands.Where(c => !ShouldSkip(c.Name))
.GroupBy(c => c.Name, StringComparer.Ordinal)
.Select(g => g.First())
.OrderBy(c => c.Name, StringComparer.Ordinal)
.ToList();
StringBuilder sb = new StringBuilder();
sb.AppendLine("// <auto-generated>");
sb.AppendLine($"// Generated by StgSharp.GenerateGL on {DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture)}");
sb.AppendLine("// Aggregated GL3+ commands");
sb.AppendLine("// </auto-generated>");
sb.AppendLine("using System;\nusing System.Runtime.CompilerServices;\n");
sb.AppendLine("namespace StgSharp.Graphics.OpenGL");
sb.AppendLine("{");
sb.AppendLine(" public unsafe partial struct OpenglContext");
sb.AppendLine(" {");
foreach (GlCommand cmd in distinct)
{
string ret = CsTypeMapper.MapDelegateReturn(cmd.ReturnType, cmd.ReturnPointerLevel);
List<string> paramTypes = new List<string>();
foreach (GlParam p in cmd.Parameters)
{
string mapped = CsTypeMapper.MapDelegateParameter(p);
paramTypes.Add(mapped);
}
string decl = DelegateSignature(ret, paramTypes);
sb.AppendLine($" internal unsafe {decl} {cmd.Name};");
}
sb.AppendLine(" }");
sb.AppendLine("}");
string fileName = Path.Combine(_outputRoot, "glContext.cs");
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
}
private void WriteOpenGLFunctionFile(
string version,
IReadOnlyList<GlCommand> commands
)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("// <auto-generated>");
sb.AppendLine($"// Generated by StgSharp.GenerateGL on {DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture)}");
sb.AppendLine($"// Version: {version}");
sb.AppendLine("// </auto-generated>");
sb.AppendLine("using System;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\nusing StgSharp.Graphics.OpenGL;\n");
sb.AppendLine("namespace StgSharp.Graphics.OpenGL");
sb.AppendLine("{");
sb.AppendLine(" public unsafe partial class OpenGLFunction");
sb.AppendLine(" {");
foreach (GlCommand cmd in commands.OrderBy(c => c!.Name, StringComparer.Ordinal))
{
string ret = CsTypeMapper.MapReturn(cmd.ReturnType, cmd.ReturnPointerLevel);
string rawName = cmd.Name; // original glXxx
string methodName = TrimGlPrefix(rawName);
List<string> paramList = new List<string>();
List<string> callArgs = new List<string>();
List<(string Type,string Name)> pinned = new();
foreach (GlParam p in cmd.Parameters)
{
string pname = SafeName(p.Name);
if (CsTypeMapper.TryMapConstPointerToSpan(p, out string spanElementType))
{
paramList.Add($"ReadOnlySpan<{spanElementType}> {pname}");
pinned.Add((spanElementType, pname));
callArgs.Add($"{pname}Ptr");
} else
{
string mapped = CsTypeMapper.MapParameter(p);
paramList.Add($"{mapped} {pname}");
if (mapped == "GlHandle")
{
callArgs.Add($"{pname}.Value");
} else
{
callArgs.Add(pname);
}
}
}
string signature = string.Join(", ", paramList);
string call = string.Join(", ", callArgs);
sb.AppendLine($" [MethodImpl(MethodImplOptions.AggressiveInlining)]");
sb.AppendLine($" public {ret} {methodName}({signature})");
sb.AppendLine(" {");
string prefix = ret == "void" ? string.Empty : "return ";
if (pinned.Count == 0)
{
sb.AppendLine($" {prefix}Context.{rawName}({call});");
} else
{
string indent = " ";
foreach ((string Type, string Name) pin in pinned)
{
sb.AppendLine($"{indent}fixed ({pin.Type}* {pin.Name}Ptr = {pin.Name})");
sb.AppendLine($"{indent}{{");
indent += " ";
}
sb.AppendLine($"{indent}{prefix}Context.{rawName}({call});");
for (int i = 0; i < pinned.Count; i++)
{
indent = indent.Substring(0, indent.Length - 4);
sb.AppendLine($"{indent}}}");
}
}
sb.AppendLine(" }");
sb.AppendLine();
}
sb.AppendLine(" }");
sb.AppendLine("}");
string fileName = Path.Combine(_outputRoot, $"glFunction.v{version.Replace('.', '_')}.cs");
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
}
private enum CommandHistoryAction
{
Added,
Removed
}
private readonly record struct CommandHistoryEntry(
string Version,
CommandHistoryAction Action
);
}
}
@@ -0,0 +1,184 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="GlXmlParser"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace GenerateGL.CodeGen
{
internal static class GlXmlParser
{
public static (IReadOnlyDictionary<string, GlCommand> Commands, IReadOnlyList<GlFeature> Features) Parse(
string xmlPath
)
{
XDocument doc = XDocument.Load(xmlPath, LoadOptions.PreserveWhitespace);
Dictionary<string, GlCommand> commands = ParseCommands(doc);
List<GlFeature> features = ParseFeatures(doc);
return (commands, features);
}
private static string BuildCType(
XElement element
)
{
return BuildCType(element, out _, out _, out _);
}
private static string BuildCType(
XElement element,
out int pointerLevel,
out bool isPointer,
out bool isConst
)
{
// CType is usually text parts + <ptype>
IEnumerable<string> parts = element.Nodes()
.Select(n => n switch
{
XText t => t.Value,
XElement el when el.Name == "ptype" => el.Value,
_ => string.Empty
});
string raw = string.Concat(parts).Trim();
pointerLevel = raw.Count(ch => ch == '*');
isPointer = pointerLevel > 0;
isConst = raw.Contains("const", StringComparison.OrdinalIgnoreCase);
string cleaned = raw.Replace("const", string.Empty, StringComparison.OrdinalIgnoreCase)
.Replace("*", string.Empty)
.Trim();
return cleaned;
}
private static Dictionary<string, GlCommand> ParseCommands(
XDocument doc
)
{
Dictionary<string, GlCommand> dict = new Dictionary<string, GlCommand>(StringComparer.Ordinal);
IEnumerable<XElement> commandElements = doc.Root?.Element("commands")?.Elements("command") ??
Enumerable.Empty<XElement>();
foreach (XElement cmd in commandElements)
{
XElement? proto = cmd.Element("proto");
if (proto is null)
{
continue;
}
string name = proto.Element("name")?.Value ?? string.Empty;
string returnType = BuildCType(proto, out int retPtrLevel, out _,
out bool retIsConst);
List<GlParam> parameters = new List<GlParam>();
foreach (XElement p in cmd.Elements("param"))
{
string pName = p.Element("name")?.Value ?? string.Empty;
string cType = BuildCType(p, out int pointerLevel, out _, out bool isConst);
parameters.Add(new GlParam(pName, cType, pointerLevel, isConst));
}
if (!string.IsNullOrEmpty(name)) {
dict[name] = new GlCommand(name, returnType, retPtrLevel, retIsConst,
parameters);
}
}
return dict;
}
private static List<GlFeature> ParseFeatures(
XDocument doc
)
{
List<GlFeature> list = new List<GlFeature>();
IEnumerable<XElement> featureElements = doc.Root?.Elements("feature") ??
Enumerable.Empty<XElement>();
foreach (XElement feature in featureElements)
{
string? api = feature.Attribute("api")?.Value;
if (!string.Equals(api, "gl", StringComparison.Ordinal))
{
continue;
}
string? profile = feature.Attribute("profile")?.Value;
// Only core (or no profile for legacy) to stay aligned with core profile
if (!string.IsNullOrEmpty(profile) &&
!string.Equals(profile, "core", StringComparison.Ordinal))
{
continue;
}
string version = feature.Attribute("number")?.Value ?? string.Empty;
List<string> requires = new List<string>();
foreach (XElement req in feature.Elements("require"))
{
string? reqProfile = req.Attribute("profile")?.Value;
if (!string.IsNullOrEmpty(reqProfile) &&
!string.Equals(reqProfile, "core", StringComparison.Ordinal))
{
continue;
}
foreach (XElement cmd in req.Elements("command"))
{
string? name = cmd.Attribute("name")?.Value;
if (!string.IsNullOrEmpty(name)) {
requires.Add(name);
}
}
}
List<string> removes = new List<string>();
foreach (XElement rem in feature.Elements("remove"))
{
string? remProfile = rem.Attribute("profile")?.Value;
if (!string.IsNullOrEmpty(remProfile) &&
!string.Equals(remProfile, "core", StringComparison.Ordinal))
{
continue;
}
foreach (XElement cmd in rem.Elements("command"))
{
string? name = cmd.Attribute("name")?.Value;
if (!string.IsNullOrEmpty(name)) {
removes.Add(name);
}
}
}
list.Add(new GlFeature(version, requires, removes));
}
return list;
}
}
}
@@ -0,0 +1,57 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Models"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System.Collections.Generic;
namespace GenerateGL.CodeGen
{
internal sealed record GlCommand(
string Name,
string ReturnType,
int ReturnPointerLevel,
bool ReturnIsConst,
IReadOnlyList<GlParam> Parameters
);
internal sealed record GlParam(
string Name,
string CType,
int PointerLevel,
bool IsConst
)
{
public bool IsPointer => PointerLevel > 0;
}
internal sealed record GlFeature(
string Version,
IReadOnlyList<string> RequiresCommands,
IReadOnlyList<string> RemovedCommands
);
}