mirror of
https://github.com/amerkoleci/Vortice.Win32.git
synced 2026-01-14 16:16:04 +08:00
Generator: Completed initial step of Com Types generation and various improvements.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
#nullable disable
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace Generator;
|
||||
|
||||
public class ApiDataArrayShape
|
||||
@@ -20,6 +22,11 @@ public class ApiDataType
|
||||
// Kind == Array
|
||||
public ApiDataArrayShape Shape { get; set; }
|
||||
public ApiDataType Child { get; set; }
|
||||
|
||||
// Kind == LPArray
|
||||
public bool NullNullTerm { get; set; }
|
||||
public int CountParamIndex { get; set; }
|
||||
public int CountConst { get; set; }
|
||||
}
|
||||
|
||||
public class ApiDataConstant
|
||||
@@ -46,6 +53,7 @@ public class ApiParameter
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public ApiDataType Type { get; set; }
|
||||
public List<object> Attrs { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ApiFunction
|
||||
@@ -55,6 +63,34 @@ public class ApiFunction
|
||||
public ApiDataType ReturnType { get; set; }
|
||||
public IList<ApiParameter> Params { get; set; } = new List<ApiParameter>();
|
||||
public List<object> Attrs { get; set; }
|
||||
|
||||
private string _toString = default;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_toString))
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
builder.Append(ReturnType.Name).Append(' ');
|
||||
builder.Append(Name).Append('(');
|
||||
int parameterIndex = 0;
|
||||
foreach (var parameter in Params)
|
||||
{
|
||||
// TODO: Handle PointerTo, Array etc
|
||||
builder.Append(parameter.Type.Name).Append(' ').Append(parameter.Name);
|
||||
if (parameterIndex < Params.Count - 1)
|
||||
{
|
||||
builder.Append(", ");
|
||||
}
|
||||
parameterIndex++;
|
||||
}
|
||||
|
||||
builder.Append(')');
|
||||
_toString = builder.ToString();
|
||||
}
|
||||
|
||||
return _toString;
|
||||
}
|
||||
}
|
||||
|
||||
public class ApiType
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
// Copyright © Amer Koleci and Contributors.
|
||||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace Generator;
|
||||
|
||||
public sealed class CodeWriter : IDisposable
|
||||
{
|
||||
private readonly string _fileName;
|
||||
private bool _shouldIndent = true;
|
||||
private readonly string[] _indentStrings;
|
||||
private string _indentString = "";
|
||||
private readonly StreamWriter _writer;
|
||||
private readonly StringBuilder _builder = new();
|
||||
|
||||
public int IndentLevel { get; private set; }
|
||||
public string Api { get; }
|
||||
@@ -16,6 +19,7 @@ public sealed class CodeWriter : IDisposable
|
||||
|
||||
public CodeWriter(string fileName, string api, string docFileName, string ns, params string[] usingNamespaces)
|
||||
{
|
||||
_fileName = fileName;
|
||||
Api = api;
|
||||
DocFileName = docFileName;
|
||||
|
||||
@@ -25,40 +29,40 @@ public sealed class CodeWriter : IDisposable
|
||||
_indentStrings[i] = new string('\t', i);
|
||||
}
|
||||
|
||||
_writer = File.CreateText(fileName);
|
||||
_writer.WriteLine("// ------------------------------------------------------------------------------");
|
||||
_writer.WriteLine("// <auto-generated>");
|
||||
_writer.WriteLine("// This code was generated by a tool.");
|
||||
_writer.WriteLine("//");
|
||||
_writer.WriteLine("// Changes to this file may cause incorrect behavior and will be lost if");
|
||||
_writer.WriteLine("// the code is regenerated.");
|
||||
_writer.WriteLine("// </auto-generated>");
|
||||
_writer.WriteLine("// ------------------------------------------------------------------------------");
|
||||
_writer.WriteLine();
|
||||
_builder.AppendLine("// ------------------------------------------------------------------------------");
|
||||
_builder.AppendLine("// <auto-generated>");
|
||||
_builder.AppendLine("// This code was generated by a tool.");
|
||||
_builder.AppendLine("//");
|
||||
_builder.AppendLine("// Changes to this file may cause incorrect behavior and will be lost if");
|
||||
_builder.AppendLine("// the code is regenerated.");
|
||||
_builder.AppendLine("// </auto-generated>");
|
||||
_builder.AppendLine("// ------------------------------------------------------------------------------");
|
||||
_builder.AppendLine();
|
||||
|
||||
_writer.WriteLine($"using System;");
|
||||
_writer.WriteLine($"using System.Diagnostics;");
|
||||
_writer.WriteLine($"using System.Runtime.CompilerServices;");
|
||||
_writer.WriteLine($"using System.Diagnostics.CodeAnalysis;");
|
||||
_builder.AppendLine($"using System;");
|
||||
_builder.AppendLine($"using System.Diagnostics;");
|
||||
_builder.AppendLine($"using System.Runtime.CompilerServices;");
|
||||
_builder.AppendLine($"using System.Diagnostics.CodeAnalysis;");
|
||||
|
||||
foreach (string usingNamespace in usingNamespaces)
|
||||
{
|
||||
_writer.WriteLine($"using {usingNamespace};");
|
||||
_builder.AppendLine($"using {usingNamespace};");
|
||||
}
|
||||
_writer.WriteLine();
|
||||
_builder.AppendLine();
|
||||
|
||||
_writer.WriteLine("#if !NET6_0_OR_GREATER");
|
||||
_writer.WriteLine("using MemoryMarshal = Win32.MemoryMarshal;");
|
||||
_writer.WriteLine("#endif");
|
||||
_writer.WriteLine();
|
||||
_builder.AppendLine("#if !NET6_0_OR_GREATER");
|
||||
_builder.AppendLine("using MemoryMarshal = Win32.MemoryMarshal;");
|
||||
_builder.AppendLine("#endif");
|
||||
_builder.AppendLine();
|
||||
|
||||
_writer.WriteLine($"namespace {ns};");
|
||||
_writer.WriteLine();
|
||||
_builder.AppendLine($"namespace {ns};");
|
||||
_builder.AppendLine();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_writer.Dispose();
|
||||
string content = _builder.ToString();
|
||||
File.WriteAllText(_fileName, content);
|
||||
}
|
||||
|
||||
public void Write(char chr)
|
||||
@@ -73,20 +77,20 @@ public sealed class CodeWriter : IDisposable
|
||||
|
||||
public void WriteLine()
|
||||
{
|
||||
_writer.WriteLine();
|
||||
_builder.AppendLine();
|
||||
_shouldIndent = true;
|
||||
}
|
||||
|
||||
public void WriteLine(string @string)
|
||||
{
|
||||
WriteIndented(@string);
|
||||
_writer.WriteLine();
|
||||
_builder.AppendLine();
|
||||
_shouldIndent = true;
|
||||
}
|
||||
|
||||
public void WriteLineUndindented(string @string)
|
||||
{
|
||||
_writer.WriteLine(@string);
|
||||
_builder.AppendLine(@string);
|
||||
_shouldIndent = true;
|
||||
}
|
||||
|
||||
@@ -139,22 +143,22 @@ public sealed class CodeWriter : IDisposable
|
||||
{
|
||||
if (_shouldIndent)
|
||||
{
|
||||
_writer.Write(_indentString);
|
||||
_builder.Append(_indentString);
|
||||
_shouldIndent = false;
|
||||
}
|
||||
|
||||
_writer.Write(chr);
|
||||
_builder.Append(chr);
|
||||
}
|
||||
|
||||
private void WriteIndented(string @string)
|
||||
{
|
||||
if (_shouldIndent)
|
||||
{
|
||||
_writer.Write(_indentString);
|
||||
_builder.Append(_indentString);
|
||||
_shouldIndent = false;
|
||||
}
|
||||
|
||||
_writer.Write(@string);
|
||||
_builder.Append(@string);
|
||||
}
|
||||
|
||||
private class CodeBlock : IDisposable
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright © Amer Koleci and Contributors.
|
||||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
@@ -66,6 +67,7 @@ public static class Program
|
||||
{ "Foundation.SIZE", "System.Drawing.Size" },
|
||||
|
||||
{ "Graphics.Gdi.HMONITOR", "IntPtr" },
|
||||
{ "Graphics.Gdi.HDC", "IntPtr" },
|
||||
};
|
||||
|
||||
private static readonly Dictionary<string, string> s_knownTypesPrefixes = new()
|
||||
@@ -130,6 +132,9 @@ public static class Program
|
||||
{ "DXGI_SWAP_CHAIN_DESC1::Flags", "DXGI_SWAP_CHAIN_FLAG" },
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> s_visitedEnums = new();
|
||||
private static readonly HashSet<string> s_visitedStructs = new();
|
||||
|
||||
private static bool s_generateUnmanagedDocs = true;
|
||||
|
||||
public static int Main(string[] args)
|
||||
@@ -443,7 +448,10 @@ public static class Program
|
||||
foreach (ApiType enumType in api.Types.Where(item => item.Kind.ToLowerInvariant() == "enum"))
|
||||
{
|
||||
GenerateEnum(writer, enumType, false);
|
||||
|
||||
s_visitedEnums.Add($"{writer.Api}.{enumType.Name}");
|
||||
}
|
||||
|
||||
writer.WriteLine($"#endregion Enums");
|
||||
writer.WriteLine();
|
||||
|
||||
@@ -504,6 +512,8 @@ public static class Program
|
||||
foreach (ApiType structType in api.Types.Where(item => item.Kind.ToLowerInvariant() == "struct"))
|
||||
{
|
||||
GenerateStruct(writer, structType);
|
||||
|
||||
s_visitedStructs.Add($"{writer.Api}.{structType.Name}");
|
||||
}
|
||||
writer.WriteLine($"#endregion Structs");
|
||||
writer.WriteLine();
|
||||
@@ -512,10 +522,35 @@ public static class Program
|
||||
writer.WriteLine($"#region COM Types");
|
||||
foreach (ApiType comType in api.Types.Where(item => item.Kind.ToLowerInvariant() == "com"))
|
||||
{
|
||||
GenerateComType(writer, comType);
|
||||
//if (comType.Name != "IDXGIObject" &&
|
||||
// comType.Name != "IDXGIDeviceSubObject")
|
||||
//{
|
||||
// break;
|
||||
//}
|
||||
|
||||
// Generate methods
|
||||
List<KeyValuePair<ApiFunction, string>> methodsToGenerate = new();
|
||||
ApiType iterateType = comType;
|
||||
while (iterateType.Interface != null && iterateType.Interface.Name != "IUnknown")
|
||||
{
|
||||
iterateType = api.Types.First(item => item.Name == iterateType.Interface.Name);
|
||||
|
||||
foreach (var method in iterateType.Methods)
|
||||
{
|
||||
methodsToGenerate.Add(new(method, iterateType.Name));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach (var method in comType.Methods)
|
||||
{
|
||||
methodsToGenerate.Add(new(method, comType.Name));
|
||||
}
|
||||
|
||||
GenerateComType(api, writer, comType, methodsToGenerate);
|
||||
}
|
||||
writer.WriteLine($"#endregion COM Types");
|
||||
writer.WriteLine();
|
||||
|
||||
writer.WriteLine($"#endregion Com Types");
|
||||
}
|
||||
|
||||
private static void GenerateEnum(CodeWriter writer, ApiType enumType, bool autoGenerated)
|
||||
@@ -671,20 +706,20 @@ public static class Program
|
||||
|
||||
writer.WriteLine($"public {unsafePrefix}{fieldTypeName} {fieldValueName};");
|
||||
}
|
||||
|
||||
writer.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
writer.WriteLine();
|
||||
}
|
||||
|
||||
private static void GenerateComType(CodeWriter writer, ApiType comType)
|
||||
private static void GenerateComType(
|
||||
ApiData api,
|
||||
CodeWriter writer,
|
||||
ApiType comType,
|
||||
List<KeyValuePair<ApiFunction, string>> methodsToGenerate)
|
||||
{
|
||||
if (comType.Name != "IDXGIObject" /*&&
|
||||
comType.Name != "IDXGIDeviceSubObject"*/)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string csTypeName = comType.Name;
|
||||
//AddCsMapping(writer.Api, comType.Name, csTypeName);
|
||||
|
||||
@@ -701,7 +736,6 @@ public static class Program
|
||||
using (writer.PushBlock($"public unsafe partial struct {csTypeName} : {csTypeName}.Interface"))
|
||||
{
|
||||
// Generate IID
|
||||
writer.WriteLine($"[NativeTypeName(\"const GUID\")]");
|
||||
using (writer.PushBlock($"public static ref readonly Guid IID_{csTypeName}"))
|
||||
{
|
||||
writer.WriteLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]");
|
||||
@@ -756,7 +790,17 @@ public static class Program
|
||||
writer.WriteLine();
|
||||
|
||||
int vtblIndex = 0;
|
||||
if (comType.Interface.Name == "IUnknown")
|
||||
|
||||
bool generateIUnknown = false;
|
||||
var iterateType = comType;
|
||||
|
||||
while (iterateType != null)
|
||||
{
|
||||
generateIUnknown = iterateType.Interface.Name == "IUnknown";
|
||||
iterateType = api.Types.FirstOrDefault(item => item.Name == iterateType.Interface.Name);
|
||||
}
|
||||
|
||||
if (generateIUnknown)
|
||||
{
|
||||
writer.WriteLine("/// <inheritdoc cref=\"IUnknown.QueryInterface\" />");
|
||||
writer.WriteLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]");
|
||||
@@ -791,8 +835,11 @@ public static class Program
|
||||
vtblIndex = 3;
|
||||
}
|
||||
|
||||
foreach (var method in comType.Methods)
|
||||
foreach (var methodPair in methodsToGenerate)
|
||||
{
|
||||
var method = methodPair.Key;
|
||||
string docName = methodPair.Value;
|
||||
|
||||
// TODO: Handle inherit
|
||||
string returnType = GetTypeName(method.ReturnType);
|
||||
|
||||
@@ -801,12 +848,43 @@ public static class Program
|
||||
StringBuilder argumentsNameBuilder = new();
|
||||
int parameterIndex = 0;
|
||||
|
||||
if (method.Name == "SetEvictionPriority")
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
foreach (var parameter in method.Params)
|
||||
{
|
||||
string parameterType = GetTypeName(parameter.Type);
|
||||
bool asPointer = false;
|
||||
if (parameter.Type.Kind == "ApiRef")
|
||||
{
|
||||
string fullTypeName = $"{parameter.Type.Api}.{parameter.Type.Name}";
|
||||
if (!IsEnum(fullTypeName))
|
||||
{
|
||||
asPointer = true;
|
||||
}
|
||||
//string typeName = GetTypeName($"{dataType.Api}.{dataType.Name}");
|
||||
}
|
||||
|
||||
string parameterType = GetTypeName(parameter.Type, asPointer);
|
||||
parameterType = NormalizeTypeName(writer.Api, parameterType);
|
||||
string parameterName = parameter.Name;
|
||||
|
||||
bool isOptional = parameter.Attrs.Any(item => item is string str && str == "Optional");
|
||||
if (parameter.Attrs.Any(item => item is string str && str == "ComOutPtr"))
|
||||
{
|
||||
if (!IsPrimitive(parameter.Type))
|
||||
{
|
||||
parameterType += "*";
|
||||
}
|
||||
}
|
||||
|
||||
argumentBuilder.Append(parameterType).Append(' ').Append(parameterName);
|
||||
if (isOptional == true)
|
||||
{
|
||||
//argumentBuilder.Append(" = default");
|
||||
}
|
||||
|
||||
argumentsTypesBuilder.Append(parameterType);
|
||||
argumentsNameBuilder.Append(parameterName);
|
||||
|
||||
@@ -827,13 +905,30 @@ public static class Program
|
||||
returnMarshalType = "int";
|
||||
}
|
||||
|
||||
argumentsTypesBuilder.Append(", ").Append(returnMarshalType);
|
||||
if (method.Params.Count > 0)
|
||||
{
|
||||
argumentsTypesBuilder.Append(", ");
|
||||
}
|
||||
|
||||
argumentsTypesBuilder.Append(returnMarshalType);
|
||||
|
||||
string argumentsString = argumentBuilder.ToString();
|
||||
string argumentTypesString = argumentsTypesBuilder.ToString();
|
||||
string argumentNamesString = argumentsNameBuilder.ToString();
|
||||
if (method.Params.Count > 0)
|
||||
{
|
||||
argumentNamesString = ", " + argumentNamesString;
|
||||
}
|
||||
|
||||
if (comType.Name == docName)
|
||||
{
|
||||
writer.WriteLine($"/// <include file='../{writer.DocFileName}.xml' path='doc/member[@name=\"{comType.Name}::{method.Name}\"]/*' />");
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteLine($"/// <inheritdoc cref=\"{docName}.{method.Name}\" />");
|
||||
}
|
||||
|
||||
writer.WriteLine($"/// <include file='../{writer.DocFileName}.xml' path='doc/member[@name=\"{comType.Name}::{method.Name}\"]/*' />");
|
||||
writer.WriteLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]");
|
||||
writer.WriteLine($"[VtblIndex({vtblIndex})]");
|
||||
using (writer.PushBlock($"public {returnType} {method.Name}({argumentsString})"))
|
||||
@@ -841,14 +936,13 @@ public static class Program
|
||||
writer.WriteLineUndindented("#if NET6_0_OR_GREATER");
|
||||
if (returnType != "void")
|
||||
writer.Write("return ");
|
||||
writer.WriteLine($"((delegate* unmanaged<{comType.Name}*, {argumentTypesString}>)(lpVtbl[{vtblIndex}]))(({comType.Name}*)Unsafe.AsPointer(ref this), {argumentNamesString});");
|
||||
writer.WriteLine($"((delegate* unmanaged<{comType.Name}*, {argumentTypesString}>)(lpVtbl[{vtblIndex}]))(({comType.Name}*)Unsafe.AsPointer(ref this){argumentNamesString});");
|
||||
writer.WriteLineUndindented("#else");
|
||||
if (returnType != "void")
|
||||
writer.Write("return ");
|
||||
writer.WriteLine($"((delegate* unmanaged[Stdcall]<{comType.Name}*, {argumentTypesString}>)(lpVtbl[{vtblIndex}]))(({comType.Name}*)Unsafe.AsPointer(ref this), {argumentNamesString});");
|
||||
writer.WriteLine($"((delegate* unmanaged[Stdcall]<{comType.Name}*, {argumentTypesString}>)(lpVtbl[{vtblIndex}]))(({comType.Name}*)Unsafe.AsPointer(ref this){argumentNamesString});");
|
||||
writer.WriteLineUndindented("#endif");
|
||||
}
|
||||
|
||||
writer.WriteLine();
|
||||
|
||||
vtblIndex++;
|
||||
@@ -857,7 +951,7 @@ public static class Program
|
||||
using (writer.PushBlock($"public interface Interface : {comType.Interface.Name}.Interface"))
|
||||
{
|
||||
}
|
||||
writer.WriteLine();
|
||||
//writer.WriteLine();
|
||||
}
|
||||
|
||||
writer.WriteLine();
|
||||
@@ -877,7 +971,9 @@ public static class Program
|
||||
private static string NormalizeTypeName(string api, string typeName)
|
||||
{
|
||||
if (!typeName.StartsWith(api))
|
||||
{
|
||||
return typeName;
|
||||
}
|
||||
|
||||
return typeName.Replace(api + ".", "");
|
||||
}
|
||||
@@ -1084,17 +1180,21 @@ public static class Program
|
||||
return $"new Guid({a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}, {j}, {k})";
|
||||
}
|
||||
|
||||
|
||||
private static string GetTypeName(ApiDataType dataType)
|
||||
private static string GetTypeName(ApiDataType dataType, bool asPointer = false)
|
||||
{
|
||||
if (dataType.Kind == "ApiRef")
|
||||
{
|
||||
return GetTypeName($"{dataType.Api}.{dataType.Name}");
|
||||
string typeName = GetTypeName($"{dataType.Api}.{dataType.Name}");
|
||||
return asPointer ? typeName + "*" : typeName;
|
||||
}
|
||||
else if (dataType.Kind == "Array")
|
||||
{
|
||||
return "Array";
|
||||
}
|
||||
else if (dataType.Kind == "LPArray")
|
||||
{
|
||||
return GetTypeName(dataType.Child) + "*";
|
||||
}
|
||||
else if (dataType.Kind == "PointerTo")
|
||||
{
|
||||
return GetTypeName(dataType.Child) + "*";
|
||||
@@ -1103,6 +1203,48 @@ public static class Program
|
||||
return GetTypeName(dataType.Name);
|
||||
}
|
||||
|
||||
private static bool IsPrimitive(ApiDataType dataType)
|
||||
{
|
||||
if (dataType.Kind == "ApiRef")
|
||||
{
|
||||
string apiRefType = GetTypeName($"{dataType.Api}.{dataType.Name}");
|
||||
}
|
||||
else if (dataType.Kind == "PointerTo")
|
||||
{
|
||||
return IsPrimitive(dataType.Child);
|
||||
}
|
||||
|
||||
if (dataType.Kind != "Native")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string typeName = GetTypeName(dataType.Name);
|
||||
switch (typeName)
|
||||
{
|
||||
case "void":
|
||||
case "int":
|
||||
case "uint":
|
||||
return true;
|
||||
|
||||
case "nint":
|
||||
case "nuint":
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsEnum(string typeName)
|
||||
{
|
||||
return s_visitedEnums.Contains(typeName);
|
||||
}
|
||||
|
||||
private static bool IsStruct(string typeName)
|
||||
{
|
||||
return s_visitedStructs.Contains(typeName);
|
||||
}
|
||||
|
||||
private static void AddCsMapping(string api, string typeName, string csTypeName)
|
||||
{
|
||||
s_csNameMappings[$"{api}.{typeName}"] = $"{api}.{csTypeName}";
|
||||
|
||||
Reference in New Issue
Block a user