<#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" encoding="utf-8" #> <#@ Assembly Name="System.Core.dll" #> <#@ Assembly Name="System.Xml.dll" #> <#@ Assembly Name="System.Xml.Linq.dll" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Reflection" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Xml.Linq" #> <# string XmlFile = "ebml.xml"; string MatroskaXmlFile = "ebml_matroska.xml"; XNamespace ns = "urn:ietf:rfc:8794"; #> using System.Collections.Generic; using System.CodeDom.Compiler; using System.Linq; using System.Reflection; using NEbml.Core; //-------------------------------------------------------------------------------------------------------- // // This C# code class was auto-generated at <#= DateTime.Now #>. // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // // Based on "https://github.com/ietf-wg-cellar/matroska-specification/blob/master/ebml_matroska.xml". // //-------------------------------------------------------------------------------------------------------- namespace Matroska; /// /// Contains the EBML elements specified by the Matroska project (https://matroska.org/technical/specs/index.html) /// [GeneratedCode("MatroskaSpecification.tt", "1.0.0.0")] internal static class MatroskaSpecification { <# string FixDocumentation(string d) { return d.Replace(" ", " ").Trim(); } var elementsEbml = XElement.Load(Path.Combine(Path.GetDirectoryName(this.Host.TemplateFile), XmlFile)).Elements() .Select(e => new { Name = e.Attribute("name").Value.Replace("CRC-32", "CRC32"), Path = e.Attribute("path").Value, Id = e.Attribute("id").Value, ElementType = e.Attribute("type").Value, Required = (e.Attribute("minOccurs") != null ? int.Parse(e.Attribute("minOccurs").Value) : 0) > 0, Documentation = e.Element(ns + "documentation") != null ? FixDocumentation(e.Element(ns + "documentation").Value) : "" }); var matroskaElementsToSkip = new [] { "EBMLMaxIDLength", "EBMLMaxSizeLength" }; var elementsMatroska = XElement.Load(Path.Combine(Path.GetDirectoryName(this.Host.TemplateFile), MatroskaXmlFile)).Elements() .Select(e => new { Name = e.Attribute("name").Value, Path = e.Attribute("path").Value, Id = e.Attribute("id").Value, ElementType = e.Attribute("type").Value, Required = (e.Attribute("minOccurs") != null ? int.Parse(e.Attribute("minOccurs").Value) : 0) > 0, Documentation = e.Element(ns + "documentation") != null ? FixDocumentation(e.Element(ns + "documentation").Value) : "" }) .Where(e => !matroskaElementsToSkip.Contains(e.Name)).ToArray(); var elements = elementsEbml.Union(elementsMatroska).OrderBy(e => e.Path).ToList(); #> #region Helpers private static readonly Dictionary _elementDescriptorsByVInt = new Dictionary(); private static readonly Dictionary _elementDescriptorsByIdentifier = new Dictionary(); /// /// Gets a dictionary of all Matroska elements. /// public static IReadOnlyDictionary ElementDescriptors => _elementDescriptorsByVInt; /// /// Gets a dictionary of all Matroska elements. /// public static IReadOnlyDictionary ElementDescriptorsByIdentifier => _elementDescriptorsByIdentifier; static MatroskaSpecification() { var fields = typeof(MatroskaSpecification).GetFields(BindingFlags.Public | BindingFlags.Static); foreach (FieldInfo field in fields.Where(ft => ft.FieldType == typeof(ElementDescriptor))) { var value = (ElementDescriptor)field.GetValue(null); _elementDescriptorsByVInt.Add(value.Identifier, value); } foreach (FieldInfo field in fields.Where(ft => ft.FieldType == typeof(ulong))) { var identifier = (ulong)field.GetValue(null); _elementDescriptorsByIdentifier.Add(identifier, _elementDescriptorsByVInt[VInt.FromEncoded(identifier)]); } } #endregion #region Definitions <# foreach (var element in elements) { string elementType; switch (element.ElementType) { case "master": elementType = "MasterElement"; break; case "utf-8": elementType = "Utf8String"; break; case "string": elementType = "AsciiString"; break; case "binary": elementType = "Binary"; break; case "date": elementType = "Date"; break; case "float": elementType = "Float"; break; case "uinteger": elementType = "UnsignedInteger"; break; case "integer": elementType = "SignedInteger"; break; default: elementType = element.ElementType; break; } #> /// <#=(element.Required ? "[Required] " : "")#><#=element.Documentation.Replace("\n", "").Replace(" ", "")#> public const ulong <#=element.Name#> = <#=element.Id#>; public static readonly ElementDescriptor <#=element.Name#>Descriptor = new ElementDescriptor(<#=element.Name#>, nameof(<#=element.Name#>), ElementType.<#=elementType#>); <# } #> #endregion }