public static void Dump() { XElement root = XElement.Load(filename); var lstDefinition = new List <Definition>(); bool inside = false; TraverseDefinitions(root, lstDefinition, ref inside); var lstItemComment = new List <ItemComment>(); 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("Commands.Delegates.gen.cs")) { sw.WriteLine("const string VulkanLibrary = \"vulkan-1\";"); for (int i = 0, t = 0; i < lstDefinition.Count; i++) { Definition definition = lstDefinition[i]; //sw.WriteLine(definition.raw); string[] definitionLines = definition.Dump(); ItemComment itemComment = lstItemComment[i]; Dictionary <string, string> item2Comment = itemComment.Dump(); //ItemDescription itemDescription = lstItemDescription[i]; { string line = definitionLines[0]; if (line.StartsWith("public static extern")) { continue; } sw.WriteLine($"// Command: {i}"); sw.WriteLine($"// Delegate: {t}"); t++; } 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 = Helper.RemoveBraces(s); // sw.WriteLine($"/// <para>{c}</para>"); //} sw.WriteLine($"/// </summary>"); // /// <param name="device"></param> for (int j = 1; j < definitionLines.Length; j++) { string line = definitionLines[j]; if (item2Comment != null) { string key; string strComment = ParseItemComment(line, item2Comment, out key); 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(string.Format("/// <param name=\"{0}\">{1}</param>", key, strComment)); } } } { string line = definitionLines[0]; line = line.Trim(); if (line.StartsWith("public static extern")) { sw.WriteLine("[DllImport(VulkanLibrary, CallingConvention = CallingConvention.Winapi)]"); } 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("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"); sw.WriteLine(l); // public static extern VkResult vkAcquireFullScreenExclusiveModeEXT( ... } for (int j = 1; j < definitionLines.Length; j++) { string line = definitionLines[j]; 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("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"); l = l.Replace("Display* ", "/*Display*-*/IntPtr "); l = l.Replace("AHardwareBuffer** ", "/*AHardwareBuffer**-*/IntPtr "); l = l.Replace("AHardwareBuffer* ", "/*AHardwareBuffer*-*/IntPtr "); l = l.Replace("wl_display* ", "/*wl_display*-*/IntPtr "); l = l.Replace("xcb_connection_t* ", "/*xcb_connection_t*-*/IntPtr "); l = l.Replace("xcb_visualid_t ", "/*xcb_visualid_t*/IntPtr "); l = l.Replace("VisualID ", "/*VisualID*/IntPtr "); l = l.Replace("RROutput ", "/*RROutput*/IntPtr "); l = " " + l; sw.WriteLine(l); } } } Console.WriteLine("Done"); }
public static void Dump() { XElement root = XElement.Load(filename); var lstDefinition = new List <Definition>(); bool inside = false; TraverseDefinitions(root, lstDefinition, ref inside); var lstItemComment = new List <ItemComment>(); 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("Commands.GetInstanceProcAddr.gen.cs")) { for (int i = 0, t = 0; i < lstDefinition.Count; i++) { Definition definition = lstDefinition[i]; //sw.WriteLine(definition.raw); string[] definitionLines = definition.Dump(); ItemComment itemComment = lstItemComment[i]; Dictionary <string, string> item2Comment = itemComment.Dump(); //ItemDescription itemDescription = lstItemDescription[i]; { string line = definitionLines[0]; if (line.StartsWith("public static extern")) { continue; } sw.WriteLine($"// Command: {i}"); sw.WriteLine($"// GetInstanceProcAddr: {t}"); t++; } 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 = Helper.RemoveBraces(s); // sw.WriteLine($"/// <para>{c}</para>"); //} sw.WriteLine($"/// </summary>"); // /// <param name="device"></param> for (int j = 1; j < definitionLines.Length; j++) { string line = definitionLines[j]; if (item2Comment != null) { string key; string strComment = ParseItemComment(line, item2Comment, out key); 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(string.Format("/// <param name=\"{0}\">{1}</param>", key, strComment)); } } } { string line = definitionLines[0]; line = line.Trim(); if (line.StartsWith("public static extern")) { sw.WriteLine("[DllImport(VulkanLibrary, CallingConvention = CallingConvention.Winapi)]"); } 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("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"); l = l.Replace(" vk", " "); sw.WriteLine(l); // public static extern VkResult vkAcquireFullScreenExclusiveModeEXT( ... } { sw.WriteLine(" this VkInstance instance,"); } for (int j = 1; j < definitionLines.Length; j++) { string line = definitionLines[j]; if (line.Contains("VkInstance")) { continue; } 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("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"); l = l.Replace("Display* ", "/*Display*-*/IntPtr "); l = l.Replace("AHardwareBuffer** ", "/*AHardwareBuffer**-*/IntPtr "); l = l.Replace("AHardwareBuffer* ", "/*AHardwareBuffer*-*/IntPtr "); l = l.Replace("wl_display* ", "/*wl_display*-*/IntPtr "); l = l.Replace("xcb_connection_t* ", "/*xcb_connection_t*-*/IntPtr "); l = l.Replace("xcb_visualid_t ", "/*xcb_visualid_t*/IntPtr "); l = l.Replace("VisualID ", "/*VisualID*/IntPtr "); l = l.Replace("RROutput ", "/*RROutput*/IntPtr "); if (j == definitionLines.Length - 1) { l = l.Replace(";", " {"); } l = " " + l; sw.WriteLine(l); } string funcName = string.Empty; string returnType = string.Empty; { char[] chars = new char[] { ',', '(', ')', ';', ' ' }; string[] parts = definitionLines[0].Split(chars, StringSplitOptions.RemoveEmptyEntries); returnType = parts[2]; funcName = parts[3]; } string leftBigBrace = "{", rightBigBrace = "}"; { //IntPtr procHandle = vkAPI.vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT"); sw.WriteLine($"IntPtr addr = vkAPI.vkGetInstanceProcAddr(instance, \"{funcName}\");"); //delCreateDebugReportCallbackEXT = (vkCreateDebugReportCallbackEXT)Marshal.GetDelegateForFunctionPointer(procHandle, typeof(vkCreateDebugReportCallbackEXT)); sw.WriteLine($"var func = ({funcName})Marshal.GetDelegateForFunctionPointer(addr, typeof({funcName}));"); sw.WriteLine(); sw.WriteLine($"if (func != null) {leftBigBrace}"); string starter = (returnType != "void") ? "return " : ""; sw.Write($"{starter}func("); char[] chars = new char[] { ',', '(', ')', ';', ' ' }; for (int j = 1; j < definitionLines.Length; j++) { string[] parts = definitionLines[j].Trim().Split(chars, StringSplitOptions.RemoveEmptyEntries); string paramName = parts[parts.Length - 1]; paramName = paramName.Replace("object", "_object"); paramName = paramName.Replace("event", "_event"); sw.Write(paramName); if (j != definitionLines.Length - 1) { sw.Write(", "); } } sw.WriteLine($");"); sw.WriteLine($"{rightBigBrace}"); if (returnType == "VkResult") { sw.WriteLine($"else {leftBigBrace}"); sw.WriteLine($"return VkResult.ErrorExtensionNotPresent;"); sw.WriteLine($"{rightBigBrace}"); } else if (returnType == "VkDeviceAddress") { sw.WriteLine($"else {leftBigBrace}"); sw.WriteLine($"return 0;"); sw.WriteLine($"{rightBigBrace}"); } } sw.WriteLine("}"); //private static vkCreateDebugReportCallbackEXT delCreateDebugReportCallbackEXT; //sw.WriteLine($"private static {funcName} del{funcName};"); sw.WriteLine(); } } Console.WriteLine("Done"); }