コード例 #1
0
        public static void Dump()
        {
            XElement root = XElement.Load(filename);
            var      lstDefinition = new List <StructDefinition>(); bool inside = false;

            TraverseDefinitions(root, lstDefinition, ref inside);

            using (var sw = new System.IO.StreamWriter("Structs.Sizes.gen.cs")) {
                sw.WriteLine($"var sizes = new int[] {leftBrace}");
                for (int i = 0; i < lstDefinition.Count; i++)
                {
                    StructDefinition definition = lstDefinition[i];
                    //sw.WriteLine(definition.raw);
                    string[] definitionLines = definition.Dump();

                    sw.WriteLine($"    // Struct: {i}");
                    sw.WriteLine($"    sizeof({definitionLines[0]}),");
                }
                sw.WriteLine($"{rightBrace};");
            }
            Console.WriteLine("Done");
        }
コード例 #2
0
ファイル: StructsParser.cs プロジェクト: bitzhuwei/ApiSpec
        public static void Dump()
        {
            XElement root = XElement.Load(filename);
            var      lstDefinition = new List <StructDefinition>(); bool inside = false;

            TraverseDefinitions(root, lstDefinition, ref inside);
            var lstItemComment = new List <StructItemComment>(); inside = false;

            TraverseItemComments(root, lstItemComment, ref inside);
            var lstComment = new List <string>(); inside = false;

            TraverseComments(root, lstComment, ref inside);
            //var lstItemDescription = new List<ItemDescription>(); inside = false;
            //TraverseDescriptions(root, lstItemDescription, ref inside);

            using (var sw = new System.IO.StreamWriter("Structs.gen.cs")) {
                for (int i = 0; i < lstDefinition.Count; i++)
                {
                    StructDefinition definition = lstDefinition[i];
                    //sw.WriteLine(definition.raw);
                    string[]                    definitionLines = definition.Dump();
                    StructItemComment           itemComment     = lstItemComment[i];
                    Dictionary <string, string> item2Comment    = itemComment.Dump();
                    //ItemDescription itemDescription = lstItemDescription[i];

                    sw.WriteLine($"// Struct: {i}");
                    string comment = lstComment[i];
                    sw.WriteLine($"/// <summary>{comment}");
                    // description is too long.
                    //foreach (var item in itemDescription.lstComment) {
                    //    string s = item.Replace("\r", "");
                    //    s = s.Replace("\n", "");
                    //    string c = RemoveBraces(s);
                    //    sw.WriteLine($"/// <para>{c}</para>");
                    //}
                    sw.WriteLine($"/// </summary>");
                    bool isUnion = false;
                    {
                        string line = definitionLines[0];
                        if (line.StartsWith("/*union*/"))
                        {
                            isUnion = true;
                        }
                        sw.WriteLine(line); // public struct XXX {
                    }
                    for (int j = 1; j < definitionLines.Length - 1; j++)
                    {
                        string line = definitionLines[j];
                        if (item2Comment != null)
                        {
                            string strComment = ParseItemComment(line, item2Comment);
                            if (strComment != string.Empty)
                            {
                                strComment = strComment.Replace("\r\n", "\n");
                                strComment = strComment.Replace("\r", "\n");
                                strComment = strComment.Replace("\n", $"{Environment.NewLine}    /// ");
                                strComment = Helper.RemoveBraces(strComment);
                                sw.WriteLine($"    /// <summary>{strComment}</summary>");
                            }
                        }
                        {
                            line = line.Trim();
                            var l = line.Replace("const char* ", "IntPtr ");
                            l = l.Replace("const*", "/*-const-*/ *");
                            l = l.Replace("const ", "/*-const-*/ ");
                            l = l.Replace(" const", " /*-const-*/");
                            l = l.Replace("size_t ", "Int32 ");
                            l = l.Replace("uint8_t* ", "byte* ");
                            l = l.Replace("uint8_t ", " byte ");
                            l = l.Replace("uint16_t* ", "UInt16* ");
                            l = l.Replace("uint16_t ", "UInt16 ");
                            l = l.Replace("uint32_t* ", "UInt32* ");
                            l = l.Replace("uint32_t ", "UInt32 ");
                            l = l.Replace("uint64_t* ", "UInt64* ");
                            l = l.Replace("uint64_t ", "UInt64 ");
                            l = l.Replace("int32_t* ", "Int32* ");
                            l = l.Replace("int32_t ", "Int32 ");
                            l = l.Replace("int64_t* ", "Int64* ");
                            l = l.Replace("int64_t ", "Int64 ");
                            l = l.Replace("struct ", "/*-struct-*/ ");
                            l = l.Replace(" object", " _object");
                            l = l.Replace(" event", " _event");
                            if (l.Contains("["))
                            {
                                l = "fixed " + l;
                            }
                            l = "public " + l;
                            if (isUnion)
                            {
                                l = "[FieldOffset(0)] " + l;
                            }
                            l = "    " + l;
                            sw.WriteLine(l);
                        }
                    }
                    {
                        string line = definitionLines[definitionLines.Length - 1];
                        sw.WriteLine(line); // }
                    }
                }
            }
            Console.WriteLine("Done");
        }
コード例 #3
0
        public static void Dump()
        {
            XElement root = XElement.Load(filename);
            var      lstDefinition = new List <StructDefinition>(); bool inside = false;

            TraverseDefinitions(root, lstDefinition, ref inside);

            using (var sw = new System.IO.StreamWriter("Structs.Marshal.gen.cs")) {
                for (int i = 0; i < lstDefinition.Count; i++)
                {
                    StructDefinition definition = lstDefinition[i];
                    //sw.WriteLine(definition.raw);
                    string[] definitionLines = definition.Dump();

                    sw.WriteLine($"// Struct: {i}");
                    if (definitionLines[1] != string.Empty)
                    {
                        sw.WriteLine($"public unsafe partial struct {definitionLines[0]} {leftBrace}");
                        {
                            sw.WriteLine($"    /// <summary>");
                            sw.WriteLine($"    /// Allocate <paramref name=\"count\"/> instances of <see cref=\"{definitionLines[0]}\"/> in unmanaged memory.");
                            sw.WriteLine($"    /// <para>The 'sType' member is already set up.</para>");
                            sw.WriteLine($"    /// </summary>");
                            sw.WriteLine($"    /// <param name=\"count\">how many items to allocate?</param>");
                            sw.WriteLine($"    /// <returns>A pointer to the instances of <see cref=\"{definitionLines[0]}\"/>.</returns>");
                            sw.WriteLine($"    public static {definitionLines[0]}* Alloc(int count = 1) {leftBrace}");
                            {
                                sw.WriteLine($"        if (count <= 0) {leftBrace} return null; {rightBrace}");
                                sw.WriteLine($"        ");
                                sw.WriteLine($"        int size = sizeof({definitionLines[0]});");
                                sw.WriteLine($"        var info = ({definitionLines[0]}*)Marshal.AllocHGlobal(size * count);");
                                sw.WriteLine($"        for (int i = 0; i < count; i++) {leftBrace} Marshal.Copy(Vk.zeros, 0, (IntPtr)(&(info[i])), size); { rightBrace}");
                                string sType = definitionLines[1] != "0" ? "VkStructureType." + definitionLines[1] : "0";
                                sw.WriteLine($"        for (int i = 0; i < count; i++) {leftBrace} info[i].sType = {sType}; {rightBrace}");
                                sw.WriteLine();
                                if (definitionLines[1] == "0" &&
                                    (definitionLines[0] != "VkBaseInStructure") &&
                                    (definitionLines[0] != "VkBaseOutStructure")
                                    )
                                {
                                    sw.WriteLine($"        throw new Exception(\"No suitable sType found!\");");
                                }
                                sw.WriteLine($"        return info;");
                            }
                            sw.WriteLine($"    {rightBrace}");
                        }
                        sw.WriteLine($"{rightBrace}");
                        sw.WriteLine();
                    }
                    else
                    {
                        sw.WriteLine($"public unsafe partial struct {definitionLines[0]} {leftBrace}");
                        {
                            sw.WriteLine($"    /// <summary>");
                            sw.WriteLine($"    /// Allocate <paramref name=\"count\"/> instances of <see cref=\"{definitionLines[0]}\"/> in unmanaged memory.");
                            sw.WriteLine($"    /// <para>No 'sType' member exists in this struct.</para>");
                            sw.WriteLine($"    /// </summary>");
                            sw.WriteLine($"    /// <param name=\"count\">how many items to allocate?</param>");
                            sw.WriteLine($"    /// <returns>A pointer to the instances of <see cref=\"{definitionLines[0]}\"/>.</returns>");
                            sw.WriteLine($"    public static {definitionLines[0]}* Alloc(int count = 1) {leftBrace}");
                            {
                                sw.WriteLine($"        if (count <= 0) {leftBrace} return null; {rightBrace}");
                                sw.WriteLine($"        ");
                                sw.WriteLine($"        int size = sizeof({definitionLines[0]});");
                                sw.WriteLine($"        var info = ({definitionLines[0]}*)Marshal.AllocHGlobal(size * count);");
                                sw.WriteLine($"        for (int i = 0; i < count; i++) {leftBrace} Marshal.Copy(Vk.zeros, 0, (IntPtr)(&(info[i])), size); {rightBrace}");
                                sw.WriteLine();
                                sw.WriteLine($"        return info;");
                            }
                            sw.WriteLine($"    {rightBrace}");
                        }
                        sw.WriteLine($"{rightBrace}");
                        sw.WriteLine();
                    }
                }
            }
            Console.WriteLine("Done");
        }