Fix `Unsafe_` method generation in SDL_ttf

This commit is contained in:
Susko3 2025-06-24 11:00:53 +02:00
parent bfa753159c
commit 88efab5deb
4 changed files with 15 additions and 11 deletions

View File

@ -5,7 +5,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -142,7 +141,10 @@ using System;
if (gm.RequiredChanges.HasFlag(Changes.ChangeReturnTypeToString))
{
expr = SyntaxFactory.InvocationExpression(
SyntaxFactory.IdentifierName("PtrToStringUTF8"))
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("SDL3"),
SyntaxFactory.IdentifierName("PtrToStringUTF8")))
.WithArguments(new[]
{
SyntaxFactory.Argument(makeFunctionCall(gm)),

View File

@ -27,8 +27,7 @@ namespace SDL.Tests
try
{
Assert.That(font != null, SDL_GetError);
string? name = PtrToStringUTF8(TTF_GetFontFamilyName(font)); // TODO: fix Unsafe_ generation
Assert.That(name, Is.EqualTo("Times New Roman"));
Assert.That(TTF_GetFontFamilyName(font), Is.EqualTo("Times New Roman"));
}
finally
{

View File

@ -16,6 +16,7 @@ Usage:
Example:
- python generate_bindings.py
- python generate_bindings.py SDL3/SDL_audio.h
- python generate_bindings.py SDL3_ttf/SDL_ttf.h
- python generate_bindings.py SDL_audio.h
- python generate_bindings.py SDL_audio
- python generate_bindings.py audio
@ -365,7 +366,10 @@ def generate_platform_specific_headers(sdl_api, header: Header, platforms):
def get_string_returning_functions(sdl_api):
for f in sdl_api:
if f["retval"] in ("const char*", "char*"):
yield f
yield f["name"]
yield "TTF_GetFontFamilyName"
yield "TTF_GetFontStyleName"
def should_skip(solo_headers: list[Header], header: Header):
@ -397,8 +401,7 @@ def main():
str_ret_funcs = list(get_string_returning_functions(sdl_api))
if str_ret_funcs:
base_command.append("--remap")
for func in str_ret_funcs:
name = func["name"]
for name in str_ret_funcs:
# add unsafe prefix to `const char *` functions so that the source generator can make friendly overloads with the unprefixed name.
base_command.append(f"{name}={unsafe_prefix}{name}")

View File

@ -257,13 +257,13 @@ namespace SDL
[return: NativeTypeName("bool")]
public static extern SDLBool TTF_FontIsScalable([NativeTypeName("const TTF_Font *")] TTF_Font* font);
[DllImport("SDL3_ttf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport("SDL3_ttf", CallingConvention = CallingConvention.Cdecl, EntryPoint = "TTF_GetFontFamilyName", ExactSpelling = true)]
[return: NativeTypeName("const char *")]
public static extern byte* TTF_GetFontFamilyName([NativeTypeName("const TTF_Font *")] TTF_Font* font);
public static extern byte* Unsafe_TTF_GetFontFamilyName([NativeTypeName("const TTF_Font *")] TTF_Font* font);
[DllImport("SDL3_ttf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport("SDL3_ttf", CallingConvention = CallingConvention.Cdecl, EntryPoint = "TTF_GetFontStyleName", ExactSpelling = true)]
[return: NativeTypeName("const char *")]
public static extern byte* TTF_GetFontStyleName([NativeTypeName("const TTF_Font *")] TTF_Font* font);
public static extern byte* Unsafe_TTF_GetFontStyleName([NativeTypeName("const TTF_Font *")] TTF_Font* font);
[DllImport("SDL3_ttf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: NativeTypeName("bool")]