Add support for using pointer in MarshalAs in scripting types

This commit is contained in:
Wojtek Figat
2024-02-15 10:47:45 +01:00
parent db7dfdb0b1
commit f730657518
7 changed files with 25 additions and 9 deletions

View File

@@ -22,7 +22,7 @@ namespace Flax.Build.Bindings
public string[] Comment;
public bool IsInBuild;
public bool IsDeprecated;
public string MarshalAs;
public TypeInfo MarshalAs;
internal bool IsInited;
internal TypedefInfo Instigator;

View File

@@ -197,7 +197,7 @@ namespace Flax.Build.Bindings
if (apiType != null)
{
if (apiType.MarshalAs != null)
return UsePassByReference(buildData, new TypeInfo(apiType.MarshalAs), caller);
return UsePassByReference(buildData, apiType.MarshalAs, caller);
// Skip for scripting objects
if (apiType.IsScriptingObject)

View File

@@ -439,7 +439,7 @@ namespace Flax.Build.Bindings
}
if (apiType.MarshalAs != null)
return GenerateCSharpManagedToNativeType(buildData, new TypeInfo(apiType.MarshalAs), caller);
return GenerateCSharpManagedToNativeType(buildData, apiType.MarshalAs, caller);
if (apiType.IsScriptingObject || apiType.IsInterface)
return "IntPtr";
}
@@ -531,7 +531,7 @@ namespace Flax.Build.Bindings
{
var apiType = FindApiTypeInfo(buildData, functionInfo.ReturnType, caller);
if (apiType != null && apiType.MarshalAs != null)
returnValueType = GenerateCSharpNativeToManaged(buildData, new TypeInfo(apiType.MarshalAs), caller);
returnValueType = GenerateCSharpNativeToManaged(buildData, apiType.MarshalAs, caller);
else
returnValueType = GenerateCSharpNativeToManaged(buildData, functionInfo.ReturnType, caller);
}

View File

@@ -19,7 +19,7 @@ namespace Flax.Build.Bindings
partial class BindingsGenerator
{
private static readonly Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();
private const int CacheVersion = 21;
private const int CacheVersion = 22;
internal static void Write(BinaryWriter writer, string e)
{

View File

@@ -597,7 +597,7 @@ namespace Flax.Build.Bindings
CppReferencesFiles.Add(apiType.File);
if (apiType.MarshalAs != null)
return GenerateCppWrapperNativeToManaged(buildData, new TypeInfo(apiType.MarshalAs), caller, out type, functionInfo);
return GenerateCppWrapperNativeToManaged(buildData, apiType.MarshalAs, caller, out type, functionInfo);
// Scripting Object
if (apiType.IsScriptingObject)
@@ -801,7 +801,7 @@ namespace Flax.Build.Bindings
if (apiType != null)
{
if (apiType.MarshalAs != null)
return GenerateCppWrapperManagedToNative(buildData, new TypeInfo(apiType.MarshalAs), caller, out type, out apiType, functionInfo, out needLocalVariable);
return GenerateCppWrapperManagedToNative(buildData, apiType.MarshalAs, caller, out type, out apiType, functionInfo, out needLocalVariable);
// Scripting Object (for non-pod types converting only, other API converts managed to unmanaged object in C# wrapper code)
if (CppNonPodTypesConvertingGeneration && apiType.IsScriptingObject && typeInfo.IsPtr)

View File

@@ -185,6 +185,11 @@ namespace Flax.Build.Bindings
tag.Value = tag.Value.Substring(1, tag.Value.Length - 2);
if (tag.Value.Contains("\\\""))
tag.Value = tag.Value.Replace("\\\"", "\"");
token = context.Tokenizer.NextToken();
if (token.Type == TokenType.Multiply)
tag.Value += token.Value;
else
context.Tokenizer.PreviousToken();
parameters.Add(tag);
break;
case TokenType.Whitespace:
@@ -647,7 +652,7 @@ namespace Flax.Build.Bindings
desc.Namespace = tag.Value;
break;
case "marshalas":
desc.MarshalAs = tag.Value;
desc.MarshalAs = TypeInfo.FromString(tag.Value);
break;
case "tag":
ParseTag(ref desc.Tags, tag);
@@ -1236,7 +1241,7 @@ namespace Flax.Build.Bindings
desc.Namespace = tag.Value;
break;
case "marshalas":
desc.MarshalAs = tag.Value;
desc.MarshalAs = TypeInfo.FromString(tag.Value);
break;
case "tag":
ParseTag(ref desc.Tags, tag);

View File

@@ -180,6 +180,17 @@ namespace Flax.Build.Bindings
return sb.ToString();
}
public static TypeInfo FromString(string text)
{
var result = new TypeInfo(text);
if (result.Type.EndsWith('*'))
{
result.IsPtr = true;
result.Type = result.Type.Substring(0, result.Type.Length - 1);
}
return result;
}
public string ToString(bool canRef = true)
{
var sb = new StringBuilder(64);