Matroska-Solution / Matroska / MatroskaSpecification.tt
Code · 157 lines · 5803 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157<#@ 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;

//--------------------------------------------------------------------------------------------------------
// <auto-generated>
//     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".
// </auto-generated>
//--------------------------------------------------------------------------------------------------------
namespace Matroska;

/// <summary>
/// Contains the EBML elements specified by the Matroska project (https://matroska.org/technical/specs/index.html)
/// </summary>
[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<VInt, ElementDescriptor> _elementDescriptorsByVInt = new Dictionary<VInt, ElementDescriptor>();
    private static readonly Dictionary<ulong, ElementDescriptor> _elementDescriptorsByIdentifier = new Dictionary<ulong, ElementDescriptor>();

    /// <summary>
    /// Gets a dictionary of all Matroska elements.
    /// </summary>
    public static IReadOnlyDictionary<VInt, ElementDescriptor> ElementDescriptors => _elementDescriptorsByVInt;

    /// <summary>
    /// Gets a dictionary of all Matroska elements.
    /// </summary>
    public static IReadOnlyDictionary<ulong, ElementDescriptor> 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;
    }
#>
    /// <summary><#=(element.Required ? "[Required] " : "")#><#=element.Documentation.Replace("\n", "").Replace("      ", "")#></summary>
    public const ulong <#=element.Name#> = <#=element.Id#>;
    public static readonly ElementDescriptor <#=element.Name#>Descriptor = new ElementDescriptor(<#=element.Name#>, nameof(<#=element.Name#>), ElementType.<#=elementType#>);

<#
    }
#>
    #endregion
}