Initial workable WIC support and correct out parameters in other contexts.

This commit is contained in:
Amer Koleci
2022-09-16 12:04:06 +02:00
parent 3f9da136a9
commit 1942a804f8
7 changed files with 858 additions and 792 deletions

View File

@@ -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.Globalization;
using System.Text;
using Newtonsoft.Json;
@@ -61,8 +62,8 @@ public static class Program
{ "Foundation.CHAR", "byte" },
{ "Foundation.LUID", "Luid" },
{ "Foundation.LARGE_INTEGER", "LargeInterger" },
{ "Foundation.ULARGE_INTEGER", "ULargeInterger" },
{ "Foundation.LARGE_INTEGER", "LargeInteger" },
{ "Foundation.ULARGE_INTEGER", "ULargeInteger" },
{ "System.Com.IUnknown", "IUnknown" },
{ "System.Com.ISequentialStream", "Com.ISequentialStream" },
@@ -92,6 +93,8 @@ public static class Program
{ "Graphics.Direct2D.Common.D2D_VECTOR_4F", "Vector4" },
{ "Graphics.Direct2D.Common.D2D_SIZE_F", "System.Drawing.SizeF" },
{ "Graphics.Imaging.WICRect", "System.Drawing.Rectangle" },
// TODO: Understand those ->
{ "Foundation.RECT", "RawRect" },
{ "Foundation.RECTL", "RawRect" },
@@ -691,6 +694,21 @@ public static class Program
{ "WICColorContextType", "WICColorContext" },
{ "WICBitmapCreateCacheOption", "WICBitmap" },
{ "WICDecodeOptions", "WICDecodeMetadata" },
{ "WICBitmapEncoderCacheOption", "WICBitmapEncoder" },
{ "WICComponentType", "WIC" },
{ "WICComponentEnumerateOptions", "WICComponentEnumerate" },
{ "WICBitmapInterpolationMode", "WICBitmapInterpolation" },
{ "WICBitmapPaletteType", "WICBitmapPaletteType" },
{ "WICBitmapDitherType", "WICBitmapDitherType" },
{ "WICBitmapAlphaChannelOption", "WICBitmap" },
{ "WICBitmapTransformOptions", "WICBitmapTransform" },
{ "WICBitmapLockFlags", "WICBitmapLock" },
{ "WICBitmapDecoderCapabilities", "WICBitmapDecoderCapability" },
{ "WICProgressOperation", "WICProgressOperation" },
{ "WICProgressNotification", "WICProgressNotification" },
{ "WICComponentSigning", "WICComponent" },
{ "WICPixelFormatNumericRepresentation", "WICPixelFormatNumericRepresentation" },
{ "WICPlanarOptions", "WICPlanarOptions" },
};
private static readonly Dictionary<string, string> s_knownEnumValueNames = new()
@@ -834,6 +852,7 @@ public static class Program
// WIC
{ "IWICImagingFactory::CreateDecoderFromFilename::dwDesiredAccess", "NativeFileAccess" },
{ "IWICBitmap::Lock::flags", "WICBitmapLockFlags" },
};
private static readonly HashSet<string> s_visitedEnums = new();
@@ -1288,6 +1307,7 @@ public static class Program
{
string csTypeName;
string enumPrefix = string.Empty;
bool skipPrettify = false;
if (enumType.Name.StartsWith("WIC"))
{
@@ -1296,6 +1316,7 @@ public static class Program
if (s_knownTypesPrefixes.TryGetValue(enumType.Name, out string? knowPrefix))
{
enumPrefix = knowPrefix!;
skipPrettify = true;
}
}
else
@@ -1351,7 +1372,7 @@ public static class Program
using (writer.PushBlock($"public enum {csTypeName} : {baseTypeName}"))
{
if (isFlags &&
!enumType.Values.Any(item => GetEnumItemName(enumType, item, enumPrefix) == "None"))
!enumType.Values.Any(item => GetEnumItemName(enumType, item, enumPrefix, skipPrettify) == "None"))
{
writer.WriteLine("None = 0,");
}
@@ -1382,7 +1403,7 @@ public static class Program
continue;
}
string enumValueName = GetEnumItemName(enumType, enumItem, enumPrefix);
string enumValueName = GetEnumItemName(enumType, enumItem, enumPrefix, skipPrettify);
if (!autoGenerated)
{
@@ -1406,14 +1427,14 @@ public static class Program
writer.WriteLine();
}
private static string GetEnumItemName(ApiType enumType, ApiEnumValue enumItem, string enumPrefix)
private static string GetEnumItemName(ApiType enumType, ApiEnumValue enumItem, string enumPrefix, bool skipPrettify)
{
if (string.IsNullOrEmpty(enumPrefix))
{
return enumItem.Name;
}
string enumValueName = GetPrettyFieldName(enumItem.Name, enumPrefix);
string enumValueName = GetPrettyFieldName(enumItem.Name, enumPrefix, skipPrettify);
// D3D11 has some enum name "issues"
// D3D11_FILL_MODE -> D3D11_FILL_*
@@ -1421,7 +1442,7 @@ public static class Program
{
string[] parts = enumType.Name.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
enumPrefix = string.Join("_", parts.Take(parts.Length - 1));
enumValueName = GetPrettyFieldName(enumItem.Name, enumPrefix);
enumValueName = GetPrettyFieldName(enumItem.Name, enumPrefix, skipPrettify);
}
// D3D12 FLAGS/FLAG
@@ -1429,7 +1450,7 @@ public static class Program
if (enumValueName.StartsWith("D3D12_") && enumType.Name.EndsWith("FLAGS"))
{
enumPrefix = enumType.Name.Substring(0, enumType.Name.Length - 1);
enumValueName = GetPrettyFieldName(enumItem.Name, enumPrefix);
enumValueName = GetPrettyFieldName(enumItem.Name, enumPrefix, skipPrettify);
}
return enumValueName;
@@ -1446,7 +1467,8 @@ public static class Program
}
else
{
if (structType.Name.StartsWith("Dxc"))
if (structType.Name.StartsWith("Dxc") ||
structType.Name.StartsWith("WIC"))
{
csTypeName = structType.Name;
}
@@ -1490,7 +1512,7 @@ public static class Program
}
else
{
fieldValueName = GetPrettyFieldName(field.Name, structPrefix);
fieldValueName = GetPrettyFieldName(field.Name, structPrefix, false);
}
if (structType.Name == "D3D11_OMAC")
@@ -1615,7 +1637,7 @@ public static class Program
string fieldTypeName = GetTypeName(field.Type);
fieldTypeName = NormalizeTypeName(writer.Api, fieldTypeName);
string fieldName = GetPrettyFieldName(field.Name, structPrefix);
string fieldName = GetPrettyFieldName(field.Name, structPrefix, false);
writer.WriteLine("[UnscopedRef]");
if (fieldTypeName == "Array")
@@ -1796,7 +1818,7 @@ public static class Program
string parameterType = string.Empty;
if (method.Name == "GetFrame" && parameter.Name == "ppIBitmapFrame")
if (method.Name == "CreateSurface" && parameter.Name == "ppSurface")
{
}
@@ -1845,11 +1867,25 @@ public static class Program
parameterType += "*";
}
}
else if (parameter.Attrs.Any(item => item is string str && str == "RetVal"))
else if (parameterType.EndsWith("**") == false &&
parameter.Attrs.Any(item => item is string str && (str == "RetVal" || str == "Out")))
{
if (!IsPrimitive(parameter.Type))
if (parameter.Type.Child.Kind != "ApiRef")
{
parameterType += "*";
if (!IsPrimitive(parameter.Type))
{
parameterType += "*";
}
}
else
{
string apiName = GetApiName(parameter.Type.Child);
string fullTypeName = $"{apiName}.{parameter.Type.Child.Name}";
if (!IsPrimitive(parameter.Type) && !IsStruct(fullTypeName) && !IsEnum(fullTypeName))
{
parameterType += "*";
}
}
}
@@ -2050,7 +2086,7 @@ public static class Program
return prettyName;
}
private static string GetPrettyFieldName(string value, string enumPrefix)
private static string GetPrettyFieldName(string value, string enumPrefix, bool skipPrettify)
{
if (s_knownEnumValueNames.TryGetValue(value, out string? knownName))
{
@@ -2100,7 +2136,7 @@ public static class Program
continue;
}
if (s_preserveCaps.Contains(part))
if (s_preserveCaps.Contains(part) || skipPrettify)
{
sb.Append(part);
}
@@ -2370,13 +2406,24 @@ public static class Program
case "uint":
case "short":
case "ushort":
case "Bool32":
case "long":
case "ulong":
case "float":
case "double":
return true;
case "nint":
case "nuint":
case "IntPtr":
case "UIntPtr":
case "Guid":
return true;
case "Bool32":
case "LargeInteger":
case "ULargeInteger":
case "Luid":
case "HResult":
return true;
}