Skip to content

Commit 492f5e6

Browse files
crhagluncrhaglun
andauthored
Add --noCustomStringMarshal (#312)
Co-authored-by: crhaglun <[email protected]>
1 parent baaba70 commit 492f5e6

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

FFmpeg.AutoGen.CppSharpUnsafeGenerator/CliOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public class CliOptions
5959
HelpText = "Print details during execution.")]
6060
public bool Verbose { get; set; }
6161

62+
[Option("noCustomStringMarshal",
63+
Default = false,
64+
HelpText = "Don't use custom string marshallers; all strings are marshalled as UnmanagedType.LPUTF8Str regardless of .NET target framework.")]
65+
public bool NoCustomStringMarshal { get; set; }
66+
6267
public static CliOptions ParseArgumentsStrict(string[] args)
6368
{
6469
var result = CommandLine.Parser.Default.ParseArguments<CliOptions>(args);

FFmpeg.AutoGen.CppSharpUnsafeGenerator/Processing/FunctionProcessor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ internal class FunctionProcessor
1212
{
1313
private const string ReturnMarshalAsConstCharPtr = "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ConstCharPtrMarshaler))]";
1414

15+
private const string ReturnMarshalAsLPUTF8Str = "[return: MarshalAs(UnmanagedType.LPUTF8Str)]";
16+
1517
private const string MarshalAsUTF8Macros =
1618
" \r\n" +
1719
" #if NETSTANDARD2_1_OR_GREATER\r\n" +
@@ -21,6 +23,8 @@ internal class FunctionProcessor
2123
" #endif\r\n" +
2224
" ";
2325

26+
private const string MarshalAsLPUTF8Str = "[MarshalAs(UnmanagedType.LPUTF8Str)]";
27+
2428

2529
private readonly ProcessingContext _context;
2630

@@ -126,7 +130,7 @@ private TypeDefinition GetParameterType(Type type, string name)
126130
PrimitiveType.Char => new TypeDefinition
127131
{
128132
Name = "string",
129-
Attributes = new[] { MarshalAsUTF8Macros }
133+
Attributes = new[] { _context.NoCustomStringMarshal ? MarshalAsLPUTF8Str : MarshalAsUTF8Macros }
130134
},
131135
PrimitiveType.Void => new TypeDefinition
132136
{
@@ -153,7 +157,7 @@ private TypeDefinition GetReturnType(Type type, string name)
153157
PrimitiveType.Char => new TypeDefinition
154158
{
155159
Name = "string",
156-
Attributes = new[] { ReturnMarshalAsConstCharPtr }
160+
Attributes = new[] { _context.NoCustomStringMarshal ? ReturnMarshalAsLPUTF8Str : ReturnMarshalAsConstCharPtr }
157161
},
158162
PrimitiveType.Void => new TypeDefinition
159163
{

FFmpeg.AutoGen.CppSharpUnsafeGenerator/Processing/ProcessingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal sealed record ProcessingContext
1212
public Dictionary<string, string> WellKnownEnumItems { get; init; } = new();
1313
public Dictionary<string, FunctionExport> FunctionExportMap { get; init; } = new();
1414
public List<IDefinition> Definitions { get; init; } = new();
15+
public bool NoCustomStringMarshal { get; init; } = false;
1516

1617
public void AddDefinition(IDefinition definition)
1718
{

FFmpeg.AutoGen.CppSharpUnsafeGenerator/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ internal static void Main(string[] args)
4848
FunctionExportMap = functionExports
4949
.GroupBy(x => x.Name)
5050
.Select(x => x.First()) // Eliminate duplicated names
51-
.ToDictionary(x => x.Name)
51+
.ToDictionary(x => x.Name),
52+
NoCustomStringMarshal = options.NoCustomStringMarshal,
5253
};
5354
var processor = new ASTProcessor(processingContext);
5455
astContexts.ForEach(processor.Process);

0 commit comments

Comments
 (0)