//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // 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 Commands, IReadOnlyList Features) Parse( string xmlPath ) { XDocument doc = XDocument.Load(xmlPath, LoadOptions.PreserveWhitespace); Dictionary commands = ParseCommands(doc); List features = ParseFeatures(doc); return (commands, features); } private static string BuildCType( XElement element ) { return BuildCType(element, out _, out _); } private static string BuildCType( XElement element, out bool isPointer, out bool isConst ) { // CType is usually text parts + IEnumerable 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(); isPointer = raw.Contains('*'); isConst = raw.Contains("const", StringComparison.OrdinalIgnoreCase); string cleaned = raw.Replace("const", string.Empty, StringComparison.OrdinalIgnoreCase) .Replace("*", string.Empty) .Trim(); return cleaned; } private static Dictionary ParseCommands( XDocument doc ) { Dictionary dict = new Dictionary(StringComparer.Ordinal); IEnumerable commandElements = doc.Root?.Element("commands")?.Elements("command") ?? Enumerable.Empty(); 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); List parameters = new List(); foreach (XElement p in cmd.Elements("param")) { string pName = p.Element("name")?.Value ?? string.Empty; string cType = BuildCType(p, out bool isPtr, out bool isConst); parameters.Add(new GlParam(pName, cType, isPtr, isConst)); } if (!string.IsNullOrEmpty(name)) { dict[name] = new GlCommand(name, returnType, parameters); } } return dict; } private static List ParseFeatures( XDocument doc ) { List list = new List(); IEnumerable featureElements = doc.Root?.Elements("feature") ?? Enumerable.Empty(); 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; // Keep core; also accept missing profile (core by default) if (!string.IsNullOrEmpty(profile) && !string.Equals(profile, "core", StringComparison.Ordinal)) { continue; } string version = feature.Attribute("number")?.Value ?? string.Empty; List requires = new List(); foreach (XElement req in feature.Elements("require")) { foreach (XElement cmd in req.Elements("command")) { string? name = cmd.Attribute("name")?.Value; if (!string.IsNullOrEmpty(name)) { requires.Add(name); } } } list.Add(new GlFeature(version, requires)); } return list; } } }