Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to use UnmanagedType.LPUTF8Str for all string marshalling #312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions FFmpeg.AutoGen.CppSharpUnsafeGenerator/CliOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class CliOptions
HelpText = "Print details during execution.")]
public bool Verbose { get; set; }

[Option("noCustomStringMarshal",
Default = false,
HelpText = "Don't use custom string marshallers; all strings are marshalled as UnmanagedType.LPUTF8Str regardless of .NET target framework.")]
public bool NoCustomStringMarshal { get; set; }

public static CliOptions ParseArgumentsStrict(string[] args)
{
var result = CommandLine.Parser.Default.ParseArguments<CliOptions>(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal class FunctionProcessor
{
private const string ReturnMarshalAsConstCharPtr = "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ConstCharPtrMarshaler))]";

private const string ReturnMarshalAsLPUTF8Str = "[return: MarshalAs(UnmanagedType.LPUTF8Str)]";

private const string MarshalAsUTF8Macros =
" \r\n" +
" #if NETSTANDARD2_1_OR_GREATER\r\n" +
Expand All @@ -21,6 +23,8 @@ internal class FunctionProcessor
" #endif\r\n" +
" ";

private const string MarshalAsLPUTF8Str = "[MarshalAs(UnmanagedType.LPUTF8Str)]";


private readonly ProcessingContext _context;

Expand Down Expand Up @@ -126,7 +130,7 @@ private TypeDefinition GetParameterType(Type type, string name)
PrimitiveType.Char => new TypeDefinition
{
Name = "string",
Attributes = new[] { MarshalAsUTF8Macros }
Attributes = new[] { _context.NoCustomStringMarshal ? MarshalAsLPUTF8Str : MarshalAsUTF8Macros }
},
PrimitiveType.Void => new TypeDefinition
{
Expand All @@ -153,7 +157,7 @@ private TypeDefinition GetReturnType(Type type, string name)
PrimitiveType.Char => new TypeDefinition
{
Name = "string",
Attributes = new[] { ReturnMarshalAsConstCharPtr }
Attributes = new[] { _context.NoCustomStringMarshal ? ReturnMarshalAsLPUTF8Str : ReturnMarshalAsConstCharPtr }
},
PrimitiveType.Void => new TypeDefinition
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal sealed record ProcessingContext
public Dictionary<string, string> WellKnownEnumItems { get; init; } = new();
public Dictionary<string, FunctionExport> FunctionExportMap { get; init; } = new();
public List<IDefinition> Definitions { get; init; } = new();
public bool NoCustomStringMarshal { get; init; } = false;

public void AddDefinition(IDefinition definition)
{
Expand Down
3 changes: 2 additions & 1 deletion FFmpeg.AutoGen.CppSharpUnsafeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal static void Main(string[] args)
FunctionExportMap = functionExports
.GroupBy(x => x.Name)
.Select(x => x.First()) // Eliminate duplicated names
.ToDictionary(x => x.Name)
.ToDictionary(x => x.Name),
NoCustomStringMarshal = options.NoCustomStringMarshal,
};
var processor = new ASTProcessor(processingContext);
astContexts.ForEach(processor.Process);
Expand Down