mirror of
https://github.com/amerkoleci/Vortice.Win32.git
synced 2026-01-14 16:16:04 +08:00
Improve Api usage and handle better code generation.
This commit is contained in:
@@ -14,6 +14,8 @@ public class ApiDataType
|
|||||||
{
|
{
|
||||||
public string Kind { get; set; }
|
public string Kind { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
public string TargetKind { get; set; }
|
||||||
|
public string Api { get; set; }
|
||||||
|
|
||||||
// Kind == Array
|
// Kind == Array
|
||||||
public ApiDataArrayShape Shape { get; set; }
|
public ApiDataArrayShape Shape { get; set; }
|
||||||
|
|||||||
@@ -11,9 +11,12 @@ public sealed class CodeWriter : IDisposable
|
|||||||
private readonly StreamWriter _writer;
|
private readonly StreamWriter _writer;
|
||||||
|
|
||||||
public int IndentLevel { get; private set; }
|
public int IndentLevel { get; private set; }
|
||||||
|
public string Api { get; }
|
||||||
|
|
||||||
public CodeWriter(string fileName, string ns, params string[] usingNamespaces)
|
public CodeWriter(string fileName, string api, string ns, params string[] usingNamespaces)
|
||||||
{
|
{
|
||||||
|
Api = api;
|
||||||
|
|
||||||
_indentStrings = new string[10];
|
_indentStrings = new string[10];
|
||||||
for (int i = 0; i < _indentStrings.Length; i++)
|
for (int i = 0; i < _indentStrings.Length; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
// Copyright © Amer Koleci and Contributors.
|
// Copyright © Amer Koleci and Contributors.
|
||||||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
// 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;
|
||||||
using System.Xml.Linq;
|
|
||||||
using Microsoft.VisualBasic.FileIO;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Generator;
|
namespace Generator;
|
||||||
@@ -32,7 +29,7 @@ public static class Program
|
|||||||
{"Single", "float" },
|
{"Single", "float" },
|
||||||
{"Double", "double" },
|
{"Double", "double" },
|
||||||
|
|
||||||
{ "BOOL", "Bool32" },
|
{ "Foundation.BOOL", "Bool32" },
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<string, string> s_knownTypesPrefixes = new()
|
private static readonly Dictionary<string, string> s_knownTypesPrefixes = new()
|
||||||
@@ -74,26 +71,43 @@ public static class Program
|
|||||||
|
|
||||||
foreach (string jsonFile in jsons)
|
foreach (string jsonFile in jsons)
|
||||||
{
|
{
|
||||||
string outputFolder = Path.Combine(outputPath, "Graphics");
|
|
||||||
if (!Directory.Exists(outputFolder))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(outputFolder);
|
|
||||||
}
|
|
||||||
|
|
||||||
string finalPath = Path.Combine(AppContext.BaseDirectory, "win32json", "api", jsonFile);
|
string finalPath = Path.Combine(AppContext.BaseDirectory, "win32json", "api", jsonFile);
|
||||||
string jsonData = File.ReadAllText(finalPath);
|
string jsonData = File.ReadAllText(finalPath);
|
||||||
ApiData? api = JsonConvert.DeserializeObject<ApiData>(jsonData);
|
ApiData? api = JsonConvert.DeserializeObject<ApiData>(jsonData);
|
||||||
Generate(api!, outputFolder);
|
Generate(api!, outputPath, jsonFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Generate(ApiData api, string outputPath)
|
private static void Generate(ApiData api, string outputPath, string jsonFile)
|
||||||
{
|
{
|
||||||
|
string[] splits = jsonFile.Split(".", StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
string folderRoot = splits[0];
|
||||||
|
string outputFolder = Path.Combine(outputPath, folderRoot);
|
||||||
|
|
||||||
|
if (!Directory.Exists(outputFolder))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(outputFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
string fileName = string.Empty;
|
||||||
|
for (int i = 1; i < splits.Length - 1; i++)
|
||||||
|
{
|
||||||
|
if(string.IsNullOrEmpty(fileName) == false)
|
||||||
|
{
|
||||||
|
fileName += ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName += splits[i];
|
||||||
|
}
|
||||||
|
string ns = fileName;
|
||||||
|
fileName += ".cs";
|
||||||
|
|
||||||
using var writer = new CodeWriter(
|
using var writer = new CodeWriter(
|
||||||
Path.Combine(outputPath, "Dxgi.Common.cs"),
|
Path.Combine(outputFolder, fileName),
|
||||||
"Win32.Graphics.Dxgi");
|
$"{folderRoot}.{ns}",
|
||||||
|
$"Win32.{folderRoot}.{ns}");
|
||||||
|
|
||||||
GenerateConstants(writer, api);
|
GenerateConstants(writer, api);
|
||||||
GenerateTypes(writer, api);
|
GenerateTypes(writer, api);
|
||||||
@@ -143,7 +157,7 @@ public static class Program
|
|||||||
|
|
||||||
string csTypeName = GetDataTypeName(enumType.Name, out string enumPrefix);
|
string csTypeName = GetDataTypeName(enumType.Name, out string enumPrefix);
|
||||||
string baseTypeName = GetTypeName(enumType.IntegerBase);
|
string baseTypeName = GetTypeName(enumType.IntegerBase);
|
||||||
AddCsMapping(enumType.Name, csTypeName);
|
AddCsMapping(writer.Api, enumType.Name, csTypeName);
|
||||||
|
|
||||||
writer.WriteLine($"/// <unmanaged>{enumType.Name}</unmanaged>");
|
writer.WriteLine($"/// <unmanaged>{enumType.Name}</unmanaged>");
|
||||||
using (writer.PushBlock($"public enum {csTypeName} : {baseTypeName}"))
|
using (writer.PushBlock($"public enum {csTypeName} : {baseTypeName}"))
|
||||||
@@ -166,7 +180,7 @@ public static class Program
|
|||||||
private static void GenerateStruct(CodeWriter writer, ApiType structType)
|
private static void GenerateStruct(CodeWriter writer, ApiType structType)
|
||||||
{
|
{
|
||||||
string csTypeName = GetDataTypeName(structType.Name, out string structPrefix);
|
string csTypeName = GetDataTypeName(structType.Name, out string structPrefix);
|
||||||
AddCsMapping(structType.Name, csTypeName);
|
AddCsMapping(writer.Api, structType.Name, csTypeName);
|
||||||
|
|
||||||
writer.WriteLine($"/// <unmanaged>{structType.Name}</unmanaged>");
|
writer.WriteLine($"/// <unmanaged>{structType.Name}</unmanaged>");
|
||||||
using (writer.PushBlock($"public partial struct {csTypeName}"))
|
using (writer.PushBlock($"public partial struct {csTypeName}"))
|
||||||
@@ -189,6 +203,7 @@ public static class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
fieldTypeName = GetTypeName(field.Type.Child);
|
fieldTypeName = GetTypeName(field.Type.Child);
|
||||||
|
fieldTypeName = NormalizeTypeName(writer.Api, fieldTypeName);
|
||||||
|
|
||||||
if (canUseFixed)
|
if (canUseFixed)
|
||||||
{
|
{
|
||||||
@@ -233,6 +248,7 @@ public static class Program
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
fieldTypeName = NormalizeTypeName(writer.Api, fieldTypeName);
|
||||||
writer.WriteLine($"public {fieldTypeName} {fieldValueName};");
|
writer.WriteLine($"public {fieldTypeName} {fieldValueName};");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,6 +267,14 @@ public static class Program
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string NormalizeTypeName(string api, string typeName)
|
||||||
|
{
|
||||||
|
if (!typeName.StartsWith(api))
|
||||||
|
return typeName;
|
||||||
|
|
||||||
|
return typeName.Replace(api + ".", "");
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetDataTypeName(string typeName, out string prefix)
|
private static string GetDataTypeName(string typeName, out string prefix)
|
||||||
{
|
{
|
||||||
prefix = typeName;
|
prefix = typeName;
|
||||||
@@ -414,7 +438,7 @@ public static class Program
|
|||||||
{
|
{
|
||||||
if (dataType.Kind == "ApiRef")
|
if (dataType.Kind == "ApiRef")
|
||||||
{
|
{
|
||||||
return GetTypeName(dataType.Name);
|
return GetTypeName($"{dataType.Api}.{dataType.Name}");
|
||||||
}
|
}
|
||||||
else if (dataType.Kind == "Array")
|
else if (dataType.Kind == "Array")
|
||||||
{
|
{
|
||||||
@@ -428,9 +452,9 @@ public static class Program
|
|||||||
return GetTypeName(dataType.Name);
|
return GetTypeName(dataType.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddCsMapping(string typeName, string csTypeName)
|
private static void AddCsMapping(string api, string typeName, string csTypeName)
|
||||||
{
|
{
|
||||||
s_csNameMappings[typeName] = csTypeName;
|
s_csNameMappings[$"{api}.{typeName}"] = $"{api}.{csTypeName}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetTypeName(string name)
|
private static string GetTypeName(string name)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ using System;
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace Win32.Graphics.Dxgi;
|
namespace Win32.Graphics.Dxgi.Common;
|
||||||
|
|
||||||
public static partial class Apis
|
public static partial class Apis
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright © Amer Koleci and Contributors.
|
// Copyright © Amer Koleci and Contributors.
|
||||||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
||||||
|
|
||||||
namespace Win32.Graphics.Dxgi;
|
namespace Win32.Graphics.Dxgi.Common;
|
||||||
|
|
||||||
public partial struct Rgb
|
public partial struct Rgb
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user