private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { SpaceSeparatedList(cw, spec.Constants, constant => { ConstantHelpers.WriteConstant(cw, tnm, constant); }); cw.WriteLine(); SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { if (Configuration.GenerateCalliStubs) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } else { cw.WriteLine($"IntPtr {command.Name}_nativePtr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); using (cw.PushBlock($"if ({command.Name}_nativePtr != IntPtr.Zero)")) { cw.WriteLine($"{command.Name}_ptr = Marshal.GetDelegateForFunctionPointer<{command.Name}_delegate>({command.Name}_nativePtr);"); } using (cw.PushBlock("else")) { string invocation = string.Join(", ", command.Parameters.Select(pd => Util.NormalizeFieldName(pd.Name))); cw.WriteLine($"{command.Name}_ptr = ({invocation}) => {{ throw CreateMissingFunctionException(); }};"); } } } } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } } }
private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { Util.SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Constants.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { ConstantHelpers.WriteAllConstants(cw, tnm, spec.Constants); } } }