Merge branch 'GoaLitiuM-dotnet7' into dotnet7

This commit is contained in:
Wojciech Figat
2022-12-28 09:07:26 +01:00
12 changed files with 156 additions and 158 deletions

View File

@@ -26,6 +26,9 @@ bool DeployDataStep::Perform(CookingData& data)
Platform::Sleep(10); Platform::Sleep(10);
} }
FileSystem::CreateDirectory(contentDir); FileSystem::CreateDirectory(contentDir);
#if USE_NETCORE
// TODO: Optionally copy all files needed for self-contained deployment
#else
const auto srcMono = depsRoot / TEXT("Mono"); const auto srcMono = depsRoot / TEXT("Mono");
const auto dstMono = data.DataOutputPath / TEXT("Mono"); const auto dstMono = data.DataOutputPath / TEXT("Mono");
if (!FileSystem::DirectoryExists(dstMono)) if (!FileSystem::DirectoryExists(dstMono))
@@ -42,6 +45,7 @@ bool DeployDataStep::Perform(CookingData& data)
return true; return true;
} }
} }
#endif
// Deploy engine data for the target platform // Deploy engine data for the target platform
if (data.Tools->OnDeployBinaries(data)) if (data.Tools->OnDeployBinaries(data))

View File

@@ -616,7 +616,14 @@ namespace FlaxEditor.CustomEditors
else else
{ {
// Default // Default
obj = JsonConvert.DeserializeObject(text, TypeUtils.GetType(Values.Type), JsonSerializer.Settings); try
{
obj = JsonConvert.DeserializeObject(text, TypeUtils.GetType(Values.Type), JsonSerializer.Settings);
}
catch
{
obj = null;
}
} }
if (obj == null || Values.Type.IsInstanceOfType(obj)) if (obj == null || Values.Type.IsInstanceOfType(obj))

View File

@@ -15,6 +15,7 @@ using System.Runtime.InteropServices.Marshalling;
using FlaxEngine.Visject; using FlaxEngine.Visject;
using System.Buffers; using System.Buffers;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Text;
#pragma warning disable 1591 #pragma warning disable 1591
@@ -181,9 +182,11 @@ namespace FlaxEngine
return managedArray; return managedArray;
} }
internal static ManagedArray AllocateNewArray<T>(T* ptr, int length) where T : unmanaged => new ManagedArray(ptr, length, Unsafe.SizeOf<T>()); internal static ManagedArray AllocateNewArray(int length, int elementSize)
=> new ManagedArray((IntPtr)NativeInterop.NativeAlloc(length, elementSize), length, elementSize);
internal static ManagedArray AllocateNewArray(IntPtr ptr, int length, int elementSize) => new ManagedArray(ptr.ToPointer(), length, elementSize); internal static ManagedArray AllocateNewArray(IntPtr ptr, int length, int elementSize)
=> new ManagedArray(ptr, length, elementSize);
/// <summary> /// <summary>
/// Returns an instance of ManagedArray from shared pool. /// Returns an instance of ManagedArray from shared pool.
@@ -198,6 +201,19 @@ namespace FlaxEngine
return managedArray; return managedArray;
} }
/// <summary>
/// Returns an instance of ManagedArray from shared pool.
/// </summary>
/// <remarks>
/// The resources must be released by calling FreePooled() instead of Free()-method.
/// </remarks>
internal static ManagedArray AllocatePooledArray<T>(int length) where T : unmanaged
{
ManagedArray managedArray = ManagedArrayPool.Get();
managedArray.Allocate((IntPtr)NativeInterop.NativeAlloc(length, Unsafe.SizeOf<T>()), length, Unsafe.SizeOf<T>());
return managedArray;
}
internal ManagedArray(Array arr) => WrapArray(arr); internal ManagedArray(Array arr) => WrapArray(arr);
internal void WrapArray(Array arr) internal void WrapArray(Array arr)
@@ -224,7 +240,7 @@ namespace FlaxEngine
private ManagedArray() { } private ManagedArray() { }
private ManagedArray(void* ptr, int length, int elementSize) => Allocate(new IntPtr(ptr), length, elementSize); private ManagedArray(IntPtr ptr, int length, int elementSize) => Allocate(ptr, length, elementSize);
~ManagedArray() ~ManagedArray()
{ {
@@ -242,7 +258,7 @@ namespace FlaxEngine
} }
if (unmanagedData != IntPtr.Zero) if (unmanagedData != IntPtr.Zero)
{ {
Marshal.FreeHGlobal(unmanagedData); NativeInterop.NativeFree(unmanagedData.ToPointer());
unmanagedData = IntPtr.Zero; unmanagedData = IntPtr.Zero;
} }
} }
@@ -608,7 +624,7 @@ namespace FlaxEngine
return null; return null;
} }
numElements = managed.Length; numElements = managed.Length;
ManagedArray managedArray = ManagedArray.AllocatePooledArray((TUnmanagedElement*)Marshal.AllocHGlobal(sizeof(TUnmanagedElement) * managed.Length), managed.Length); ManagedArray managedArray = ManagedArray.AllocatePooledArray<TUnmanagedElement>(managed.Length);
var ptr = GCHandle.ToIntPtr(GCHandle.Alloc(managedArray)); var ptr = GCHandle.ToIntPtr(GCHandle.Alloc(managedArray));
return (TUnmanagedElement*)ptr; return (TUnmanagedElement*)ptr;
} }
@@ -644,7 +660,7 @@ namespace FlaxEngine
if (managed == null) if (managed == null)
return; return;
managedArray = managed; managedArray = managed;
unmanagedArray = ManagedArray.AllocatePooledArray((TUnmanagedElement*)Marshal.AllocHGlobal(sizeof(TUnmanagedElement) * managed.Length), managed.Length); unmanagedArray = ManagedArray.AllocatePooledArray<TUnmanagedElement>(managed.Length);
handle = GCHandle.Alloc(unmanagedArray); handle = GCHandle.Alloc(unmanagedArray);
} }
@@ -692,7 +708,7 @@ namespace FlaxEngine
return null; return null;
} }
numElements = managed.Length; numElements = managed.Length;
ManagedArray managedArray = ManagedArray.AllocatePooledArray((TUnmanagedElement*)Marshal.AllocHGlobal(sizeof(TUnmanagedElement) * managed.Length), managed.Length); ManagedArray managedArray = ManagedArray.AllocatePooledArray<TUnmanagedElement>(managed.Length);
IntPtr handle = GCHandle.ToIntPtr(GCHandle.Alloc(managedArray)); IntPtr handle = GCHandle.ToIntPtr(GCHandle.Alloc(managedArray));
return (TUnmanagedElement*)handle; return (TUnmanagedElement*)handle;
} }
@@ -869,6 +885,35 @@ namespace FlaxEngine
nativeLibraryPaths[moduleName] = modulePath; nativeLibraryPaths[moduleName] = modulePath;
} }
internal static void* NativeAlloc(int byteCount)
{
return NativeMemory.AlignedAlloc((UIntPtr)byteCount, 16);
}
internal static void* NativeAlloc(int elementCount, int elementSize)
{
return NativeMemory.AlignedAlloc((UIntPtr)(elementCount * elementSize), 16);
}
internal static IntPtr NativeAllocStringAnsi(string str)
{
if (str is null)
return IntPtr.Zero;
int length = str.Length + 1;
void* ptr = NativeMemory.AlignedAlloc((UIntPtr)length, 16);
Span<byte> byteSpan = new Span<byte>(ptr, length);
Encoding.ASCII.GetBytes(str, byteSpan);
byteSpan[length - 1] = 0;
return (IntPtr)ptr;
}
internal static void NativeFree(void* ptr)
{
NativeMemory.AlignedFree(ptr);
}
internal static T[] GCHandleArrayToManagedArray<T>(ManagedArray ptrArray) internal static T[] GCHandleArrayToManagedArray<T>(ManagedArray ptrArray)
{ {
Span<IntPtr> span = ptrArray.GetSpan<IntPtr>(); Span<IntPtr> span = ptrArray.GetSpan<IntPtr>();
@@ -1530,7 +1575,7 @@ namespace FlaxEngine
Assembly assembly = Unsafe.As<Assembly>(GCHandle.FromIntPtr(assemblyHandle).Target); Assembly assembly = Unsafe.As<Assembly>(GCHandle.FromIntPtr(assemblyHandle).Target);
var assemblyTypes = GetAssemblyTypes(assembly); var assemblyTypes = GetAssemblyTypes(assembly);
NativeClassDefinitions* arr = (NativeClassDefinitions*)Marshal.AllocCoTaskMem(Unsafe.SizeOf<NativeClassDefinitions>() * assemblyTypes.Length).ToPointer(); NativeClassDefinitions* arr = (NativeClassDefinitions*)NativeAlloc(assemblyTypes.Length, Unsafe.SizeOf<NativeClassDefinitions>());
for (int i = 0; i < assemblyTypes.Length; i++) for (int i = 0; i < assemblyTypes.Length; i++)
{ {
@@ -1545,9 +1590,9 @@ namespace FlaxEngine
NativeClassDefinitions managedClass = new NativeClassDefinitions() NativeClassDefinitions managedClass = new NativeClassDefinitions()
{ {
typeHandle = GCHandle.ToIntPtr(typeHandle), typeHandle = GCHandle.ToIntPtr(typeHandle),
name = Marshal.StringToCoTaskMemAnsi(type.Name), name = NativeAllocStringAnsi(type.Name),
fullname = Marshal.StringToCoTaskMemAnsi(type.FullName), fullname = NativeAllocStringAnsi(type.FullName),
@namespace = Marshal.StringToCoTaskMemAnsi(type.Namespace ?? ""), @namespace = NativeAllocStringAnsi(type.Namespace ?? ""),
typeAttributes = (uint)type.Attributes, typeAttributes = (uint)type.Attributes,
}; };
Unsafe.Write(ptr.ToPointer(), managedClass); Unsafe.Write(ptr.ToPointer(), managedClass);
@@ -1565,9 +1610,9 @@ namespace FlaxEngine
*managedClass = new NativeClassDefinitions() *managedClass = new NativeClassDefinitions()
{ {
typeHandle = GCHandle.ToIntPtr(classTypeHandle), typeHandle = GCHandle.ToIntPtr(classTypeHandle),
name = Marshal.StringToCoTaskMemAnsi(type.Name), name = NativeAllocStringAnsi(type.Name),
fullname = Marshal.StringToCoTaskMemAnsi(type.FullName), fullname = NativeAllocStringAnsi(type.FullName),
@namespace = Marshal.StringToCoTaskMemAnsi(type.Namespace ?? ""), @namespace = NativeAllocStringAnsi(type.Namespace ?? ""),
typeAttributes = (uint)type.Attributes, typeAttributes = (uint)type.Attributes,
}; };
*assemblyHandle = GCHandle.ToIntPtr(GetAssemblyHandle(type.Assembly)); *assemblyHandle = GCHandle.ToIntPtr(GetAssemblyHandle(type.Assembly));
@@ -1586,13 +1631,13 @@ namespace FlaxEngine
foreach (MethodInfo method in instanceMethods) foreach (MethodInfo method in instanceMethods)
methods.Add(method); methods.Add(method);
NativeMethodDefinitions* arr = (NativeMethodDefinitions*)Marshal.AllocCoTaskMem(Unsafe.SizeOf<NativeMethodDefinitions>() * methods.Count).ToPointer(); NativeMethodDefinitions* arr = (NativeMethodDefinitions*)NativeAlloc(methods.Count, Unsafe.SizeOf<NativeMethodDefinitions>());
for (int i = 0; i < methods.Count; i++) for (int i = 0; i < methods.Count; i++)
{ {
IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativeMethodDefinitions>() * i); IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativeMethodDefinitions>() * i);
NativeMethodDefinitions classMethod = new NativeMethodDefinitions() NativeMethodDefinitions classMethod = new NativeMethodDefinitions()
{ {
name = Marshal.StringToCoTaskMemAnsi(methods[i].Name), name = NativeAllocStringAnsi(methods[i].Name),
numParameters = methods[i].GetParameters().Length, numParameters = methods[i].GetParameters().Length,
methodAttributes = (uint)methods[i].Attributes, methodAttributes = (uint)methods[i].Attributes,
}; };
@@ -1621,11 +1666,9 @@ namespace FlaxEngine
Type type = Unsafe.As<Type>(GCHandle.FromIntPtr(typeHandle).Target); Type type = Unsafe.As<Type>(GCHandle.FromIntPtr(typeHandle).Target);
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
NativeFieldDefinitions* arr = (NativeFieldDefinitions*)Marshal.AllocCoTaskMem(Unsafe.SizeOf<NativeFieldDefinitions>() * fields.Length).ToPointer(); NativeFieldDefinitions* arr = (NativeFieldDefinitions*)NativeAlloc(fields.Length, Unsafe.SizeOf<NativeFieldDefinitions>());
for (int i = 0; i < fields.Length; i++) for (int i = 0; i < fields.Length; i++)
{ {
IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativeFieldDefinitions>() * i);
FieldHolder fieldHolder = new FieldHolder(fields[i], type); FieldHolder fieldHolder = new FieldHolder(fields[i], type);
GCHandle fieldHandle = GCHandle.Alloc(fieldHolder); GCHandle fieldHandle = GCHandle.Alloc(fieldHolder);
@@ -1636,12 +1679,12 @@ namespace FlaxEngine
NativeFieldDefinitions classField = new NativeFieldDefinitions() NativeFieldDefinitions classField = new NativeFieldDefinitions()
{ {
name = Marshal.StringToCoTaskMemAnsi(fieldHolder.field.Name), name = NativeAllocStringAnsi(fieldHolder.field.Name),
fieldHandle = GCHandle.ToIntPtr(fieldHandle), fieldHandle = GCHandle.ToIntPtr(fieldHandle),
fieldTypeHandle = GCHandle.ToIntPtr(GetTypeGCHandle(fieldHolder.field.FieldType)), fieldTypeHandle = GCHandle.ToIntPtr(GetTypeGCHandle(fieldHolder.field.FieldType)),
fieldAttributes = (uint)fieldHolder.field.Attributes, fieldAttributes = (uint)fieldHolder.field.Attributes,
}; };
Unsafe.Write(ptr.ToPointer(), classField); Unsafe.Write(IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativeFieldDefinitions>() * i).ToPointer(), classField);
} }
*classFields = arr; *classFields = arr;
*classFieldsCount = fields.Length; *classFieldsCount = fields.Length;
@@ -1653,7 +1696,7 @@ namespace FlaxEngine
Type type = Unsafe.As<Type>(GCHandle.FromIntPtr(typeHandle).Target); Type type = Unsafe.As<Type>(GCHandle.FromIntPtr(typeHandle).Target);
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
NativePropertyDefinitions* arr = (NativePropertyDefinitions*)Marshal.AllocCoTaskMem(Unsafe.SizeOf<NativePropertyDefinitions>() * properties.Length).ToPointer(); NativePropertyDefinitions* arr = (NativePropertyDefinitions*)NativeAlloc(properties.Length, Unsafe.SizeOf<NativePropertyDefinitions>());
for (int i = 0; i < properties.Length; i++) for (int i = 0; i < properties.Length; i++)
{ {
IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativePropertyDefinitions>() * i); IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativePropertyDefinitions>() * i);
@@ -1663,7 +1706,7 @@ namespace FlaxEngine
NativePropertyDefinitions classProperty = new NativePropertyDefinitions() NativePropertyDefinitions classProperty = new NativePropertyDefinitions()
{ {
name = Marshal.StringToCoTaskMemAnsi(properties[i].Name), name = NativeAllocStringAnsi(properties[i].Name),
}; };
if (getterMethod != null) if (getterMethod != null)
{ {
@@ -1688,7 +1731,7 @@ namespace FlaxEngine
object[] attributeValues = type.GetCustomAttributes(false); object[] attributeValues = type.GetCustomAttributes(false);
Type[] attributeTypes = type.GetCustomAttributes(false).Select(x => x.GetType()).ToArray(); Type[] attributeTypes = type.GetCustomAttributes(false).Select(x => x.GetType()).ToArray();
NativeAttributeDefinitions* arr = (NativeAttributeDefinitions*)Marshal.AllocCoTaskMem(Unsafe.SizeOf<NativeAttributeDefinitions>() * attributeTypes.Length).ToPointer(); NativeAttributeDefinitions* arr = (NativeAttributeDefinitions*)NativeAlloc(attributeTypes.Length, Unsafe.SizeOf<NativeAttributeDefinitions>());
for (int i = 0; i < attributeTypes.Length; i++) for (int i = 0; i < attributeTypes.Length; i++)
{ {
IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativeAttributeDefinitions>() * i); IntPtr ptr = IntPtr.Add(new IntPtr(arr), Unsafe.SizeOf<NativeAttributeDefinitions>() * i);
@@ -1702,7 +1745,7 @@ namespace FlaxEngine
NativeAttributeDefinitions classAttribute = new NativeAttributeDefinitions() NativeAttributeDefinitions classAttribute = new NativeAttributeDefinitions()
{ {
name = Marshal.StringToCoTaskMemAnsi(attributeTypes[i].Name), name = NativeAllocStringAnsi(attributeTypes[i].Name),
attributeTypeHandle = GCHandle.ToIntPtr(attributeTypeHandle), attributeTypeHandle = GCHandle.ToIntPtr(attributeTypeHandle),
attributeHandle = GCHandle.ToIntPtr(attributeHandle), attributeHandle = GCHandle.ToIntPtr(attributeHandle),
}; };
@@ -1733,13 +1776,11 @@ namespace FlaxEngine
//foreach (Type interfaceType in type.BaseType.GetInterfaces()) //foreach (Type interfaceType in type.BaseType.GetInterfaces())
// interfaces.Remove(interfaceType.FullName); // interfaces.Remove(interfaceType.FullName);
IntPtr arr = Marshal.AllocCoTaskMem(IntPtr.Size * interfaces.Length); IntPtr arr = (IntPtr)NativeAlloc(interfaces.Length, IntPtr.Size);
for (int i = 0; i < interfaces.Length; i++) for (int i = 0; i < interfaces.Length; i++)
{ {
IntPtr ptr = IntPtr.Add(arr, IntPtr.Size * i);
GCHandle handle = GetTypeGCHandle(interfaces[i]); GCHandle handle = GetTypeGCHandle(interfaces[i]);
Marshal.WriteIntPtr(ptr, GCHandle.ToIntPtr(handle)); Unsafe.Write<IntPtr>(IntPtr.Add(arr, IntPtr.Size * i).ToPointer(), GCHandle.ToIntPtr(handle));
} }
*classInterfaces = arr; *classInterfaces = arr;
*classInterfacesCount = interfaces.Length; *classInterfacesCount = interfaces.Length;
@@ -1759,11 +1800,11 @@ namespace FlaxEngine
{ {
MethodHolder methodHolder = Unsafe.As<MethodHolder>(GCHandle.FromIntPtr(methodHandle).Target); MethodHolder methodHolder = Unsafe.As<MethodHolder>(GCHandle.FromIntPtr(methodHandle).Target);
Type returnType = methodHolder.method.ReturnType; Type returnType = methodHolder.method.ReturnType;
IntPtr arr = Marshal.AllocCoTaskMem(IntPtr.Size * methodHolder.parameterTypes.Length); IntPtr arr = (IntPtr)NativeAlloc(methodHolder.parameterTypes.Length, IntPtr.Size);
for (int i = 0; i < methodHolder.parameterTypes.Length; i++) for (int i = 0; i < methodHolder.parameterTypes.Length; i++)
{ {
GCHandle typeHandle = GetTypeGCHandle(methodHolder.parameterTypes[i]); GCHandle typeHandle = GetTypeGCHandle(methodHolder.parameterTypes[i]);
Marshal.WriteIntPtr(IntPtr.Add(new IntPtr(arr), IntPtr.Size * i), GCHandle.ToIntPtr(typeHandle)); Unsafe.Write<IntPtr>(IntPtr.Add(new IntPtr(arr), IntPtr.Size * i).ToPointer(), GCHandle.ToIntPtr(typeHandle));
} }
*typeHandles = arr; *typeHandles = arr;
} }
@@ -1847,7 +1888,7 @@ namespace FlaxEngine
Type marshalledType = ArrayFactory.GetMarshalledType(type); Type marshalledType = ArrayFactory.GetMarshalledType(type);
if (marshalledType.IsValueType) if (marshalledType.IsValueType)
{ {
ManagedArray managedArray = ManagedArray.AllocateNewArray(Marshal.AllocHGlobal(Marshal.SizeOf(marshalledType) * (int)size), (int)size, Marshal.SizeOf(marshalledType)); ManagedArray managedArray = ManagedArray.AllocateNewArray((int)size, Marshal.SizeOf(marshalledType));
return GCHandle.ToIntPtr(GCHandle.Alloc(managedArray/*, GCHandleType.Weak*/)); return GCHandle.ToIntPtr(GCHandle.Alloc(managedArray/*, GCHandleType.Weak*/));
} }
else else
@@ -2045,8 +2086,10 @@ namespace FlaxEngine
return (bool)returnObject ? boolTruePtr : boolFalsePtr; return (bool)returnObject ? boolTruePtr : boolFalsePtr;
else if (methodHolder.method.ReturnType == typeof(Type)) else if (methodHolder.method.ReturnType == typeof(Type))
return GCHandle.ToIntPtr(GetTypeGCHandle(Unsafe.As<Type>(returnObject))); return GCHandle.ToIntPtr(GetTypeGCHandle(Unsafe.As<Type>(returnObject)));
else if (methodHolder.method.ReturnType == typeof(object[])) else if (methodHolder.method.ReturnType.IsArray && ArrayFactory.GetMarshalledType(methodHolder.method.ReturnType.GetElementType()) == methodHolder.method.ReturnType.GetElementType())
return GCHandle.ToIntPtr(GCHandle.Alloc(ManagedArray.WrapNewArray(ManagedArrayToGCHandleArray(Unsafe.As<object[]>(returnObject))), GCHandleType.Weak)); return GCHandle.ToIntPtr(GCHandle.Alloc(ManagedArray.WrapNewArray(Unsafe.As<Array>(returnObject)), GCHandleType.Weak));
else if (methodHolder.method.ReturnType.IsArray)
return GCHandle.ToIntPtr(GCHandle.Alloc(ManagedArray.WrapNewArray(ManagedArrayToGCHandleArray(Unsafe.As<Array>(returnObject))), GCHandleType.Weak));
else else
return GCHandle.ToIntPtr(GCHandle.Alloc(returnObject, GCHandleType.Weak)); return GCHandle.ToIntPtr(GCHandle.Alloc(returnObject, GCHandleType.Weak));
} }
@@ -2109,8 +2152,8 @@ namespace FlaxEngine
firstAssemblyLoaded = true; firstAssemblyLoaded = true;
Assembly flaxEngineAssembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.GetName().Name == "FlaxEngine.CSharp"); Assembly flaxEngineAssembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.GetName().Name == "FlaxEngine.CSharp");
*assemblyName = Marshal.StringToCoTaskMemAnsi(flaxEngineAssembly.GetName().Name); *assemblyName = NativeAllocStringAnsi(flaxEngineAssembly.GetName().Name);
*assemblyFullName = Marshal.StringToCoTaskMemAnsi(flaxEngineAssembly.FullName); *assemblyFullName = NativeAllocStringAnsi(flaxEngineAssembly.FullName);
return GCHandle.ToIntPtr(GetAssemblyHandle(flaxEngineAssembly)); return GCHandle.ToIntPtr(GetAssemblyHandle(flaxEngineAssembly));
} }
string assemblyPath = Marshal.PtrToStringAnsi(assemblyPath_); string assemblyPath = Marshal.PtrToStringAnsi(assemblyPath_);
@@ -2118,8 +2161,8 @@ namespace FlaxEngine
Assembly assembly = scriptingAssemblyLoadContext.LoadFromAssemblyPath(assemblyPath); Assembly assembly = scriptingAssemblyLoadContext.LoadFromAssemblyPath(assemblyPath);
NativeLibrary.SetDllImportResolver(assembly, NativeLibraryImportResolver); NativeLibrary.SetDllImportResolver(assembly, NativeLibraryImportResolver);
*assemblyName = Marshal.StringToCoTaskMemAnsi(assembly.GetName().Name); *assemblyName = NativeAllocStringAnsi(assembly.GetName().Name);
*assemblyFullName = Marshal.StringToCoTaskMemAnsi(assembly.FullName); *assemblyFullName = NativeAllocStringAnsi(assembly.FullName);
return GCHandle.ToIntPtr(GetAssemblyHandle(assembly)); return GCHandle.ToIntPtr(GetAssemblyHandle(assembly));
} }
@@ -2132,8 +2175,8 @@ namespace FlaxEngine
firstAssemblyLoaded = true; firstAssemblyLoaded = true;
Assembly flaxEngineAssembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.GetName().Name == "FlaxEngine.CSharp"); Assembly flaxEngineAssembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.GetName().Name == "FlaxEngine.CSharp");
*assemblyName = Marshal.StringToCoTaskMemAnsi(flaxEngineAssembly.GetName().Name); *assemblyName = NativeAllocStringAnsi(flaxEngineAssembly.GetName().Name);
*assemblyFullName = Marshal.StringToCoTaskMemAnsi(flaxEngineAssembly.FullName); *assemblyFullName = NativeAllocStringAnsi(flaxEngineAssembly.FullName);
return GCHandle.ToIntPtr(GetAssemblyHandle(flaxEngineAssembly)); return GCHandle.ToIntPtr(GetAssemblyHandle(flaxEngineAssembly));
} }
@@ -2149,8 +2192,8 @@ namespace FlaxEngine
// Assemblies loaded via streams have no Location: https://github.com/dotnet/runtime/issues/12822 // Assemblies loaded via streams have no Location: https://github.com/dotnet/runtime/issues/12822
AssemblyLocations.Add(assembly.FullName, assemblyPath); AssemblyLocations.Add(assembly.FullName, assemblyPath);
*assemblyName = Marshal.StringToCoTaskMemAnsi(assembly.GetName().Name); *assemblyName = NativeAllocStringAnsi(assembly.GetName().Name);
*assemblyFullName = Marshal.StringToCoTaskMemAnsi(assembly.FullName); *assemblyFullName = NativeAllocStringAnsi(assembly.FullName);
return GCHandle.ToIntPtr(GetAssemblyHandle(assembly)); return GCHandle.ToIntPtr(GetAssemblyHandle(assembly));
} }
@@ -2162,15 +2205,15 @@ namespace FlaxEngine
if (assembly == null) if (assembly == null)
assembly = scriptingAssemblyLoadContext.Assemblies.FirstOrDefault(x => x.GetName().Name == name); assembly = scriptingAssemblyLoadContext.Assemblies.FirstOrDefault(x => x.GetName().Name == name);
*assemblyName = Marshal.StringToCoTaskMemAnsi(assembly.GetName().Name); *assemblyName = NativeAllocStringAnsi(assembly.GetName().Name);
*assemblyFullName = Marshal.StringToCoTaskMemAnsi(assembly.FullName); *assemblyFullName = NativeAllocStringAnsi(assembly.FullName);
return GCHandle.ToIntPtr(GetAssemblyHandle(assembly)); return GCHandle.ToIntPtr(GetAssemblyHandle(assembly));
} }
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
internal static IntPtr GetRuntimeInformation() internal static IntPtr GetRuntimeInformation()
{ {
return Marshal.StringToCoTaskMemAnsi(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription); return NativeAllocStringAnsi(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
} }
[UnmanagedCallersOnly] [UnmanagedCallersOnly]

View File

@@ -59,8 +59,10 @@ namespace FlaxEngine
return (bool)(object)returnValue ? boolTruePtr : boolFalsePtr; return (bool)(object)returnValue ? boolTruePtr : boolFalsePtr;
else if (typeof(TRet) == typeof(Type)) else if (typeof(TRet) == typeof(Type))
return returnValue != null ? GCHandle.ToIntPtr(GetTypeGCHandle(Unsafe.As<Type>(returnValue))) : IntPtr.Zero; return returnValue != null ? GCHandle.ToIntPtr(GetTypeGCHandle(Unsafe.As<Type>(returnValue))) : IntPtr.Zero;
else if (typeof(TRet) == typeof(object[])) else if (typeof(TRet).IsArray && ArrayFactory.GetMarshalledType(typeof(TRet).GetElementType()) == typeof(TRet).GetElementType())
return returnValue != null ? GCHandle.ToIntPtr(GCHandle.Alloc(ManagedArray.WrapNewArray(ManagedArrayToGCHandleArray(Unsafe.As<object[]>(returnValue))), GCHandleType.Weak)) : IntPtr.Zero; return returnValue != null ? GCHandle.ToIntPtr(GCHandle.Alloc(ManagedArray.WrapNewArray(Unsafe.As<Array>(returnValue)), GCHandleType.Weak)) : IntPtr.Zero;
else if (typeof(TRet).IsArray)
return returnValue != null ? GCHandle.ToIntPtr(GCHandle.Alloc(ManagedArray.WrapNewArray(ManagedArrayToGCHandleArray(Unsafe.As<Array>(returnValue))), GCHandleType.Weak)) : IntPtr.Zero;
else else
return returnValue != null ? GCHandle.ToIntPtr(GCHandle.Alloc(returnValue, GCHandleType.Weak)) : IntPtr.Zero; return returnValue != null ? GCHandle.ToIntPtr(GCHandle.Alloc(returnValue, GCHandleType.Weak)) : IntPtr.Zero;
} }

View File

@@ -20,7 +20,7 @@ namespace FlaxEngine
return; return;
var temp = CollectionsMarshal.AsSpan(customActors).ToArray(); // FIXME var temp = CollectionsMarshal.AsSpan(customActors).ToArray(); // FIXME
var tempCount = temp.Length; var tempCount = temp.Length;
Internal_DrawSceneDepth(FlaxEngine.Object.GetUnmanagedPtr(context), FlaxEngine.Object.GetUnmanagedPtr(task), FlaxEngine.Object.GetUnmanagedPtr(output), ref temp, ref tempCount); Internal_DrawSceneDepth(FlaxEngine.Object.GetUnmanagedPtr(context), FlaxEngine.Object.GetUnmanagedPtr(task), FlaxEngine.Object.GetUnmanagedPtr(output), temp, ref tempCount);
} }
} }
} }

View File

@@ -129,26 +129,10 @@ void CoreCLR::RegisterNativeLibrary(const char* moduleName, const char* modulePa
void* CoreCLR::Allocate(int size) void* CoreCLR::Allocate(int size)
{ {
#if PLATFORM_WINDOWS return Platform::Allocate(size, 16);
void* ptr = CoTaskMemAlloc(size);
#else
void* ptr = malloc(size);
#endif
#if COMPILE_WITH_PROFILER
Platform::OnMemoryAlloc(ptr, size);
#endif
return ptr;
} }
void CoreCLR::Free(void* ptr) void CoreCLR::Free(void* ptr)
{ {
#if COMPILE_WITH_PROFILER Platform::Free(ptr);
Platform::OnMemoryFree(ptr);
#endif
#if PLATFORM_WINDOWS
CoTaskMemFree(ptr);
#else
free(ptr);
#endif
} }

View File

@@ -93,11 +93,11 @@ public:
CoreCLR::CallStaticMethod<void, void*, NativeClassDefinitions**, int*>(GetManagedClassesPtr, _assemblyHandle, &managedClasses, &classCount); CoreCLR::CallStaticMethod<void, void*, NativeClassDefinitions**, int*>(GetManagedClassesPtr, _assemblyHandle, &managedClasses, &classCount);
for (int i = 0; i < classCount; i++) for (int i = 0; i < classCount; i++)
{ {
CoreCLRClass* mci = New<CoreCLRClass>(managedClasses[i].typeHandle, StringAnsi(managedClasses[i].name), StringAnsi(managedClasses[i].fullname), StringAnsi(managedClasses[i].namespace_), managedClasses[i].typeAttributes, this); CoreCLRClass* klass = New<CoreCLRClass>(managedClasses[i].typeHandle, StringAnsi(managedClasses[i].name), StringAnsi(managedClasses[i].fullname), StringAnsi(managedClasses[i].namespace_), managedClasses[i].typeAttributes, this);
_classes.Add(mci); _classes.Add(klass);
ASSERT(managedClasses[i].typeHandle != nullptr); ASSERT(managedClasses[i].typeHandle != nullptr);
classHandles.Add(managedClasses[i].typeHandle, mci); classHandles.Add(managedClasses[i].typeHandle, klass);
CoreCLR::Free((void*)managedClasses[i].name); CoreCLR::Free((void*)managedClasses[i].name);
CoreCLR::Free((void*)managedClasses[i].fullname); CoreCLR::Free((void*)managedClasses[i].fullname);
@@ -168,6 +168,8 @@ public:
: _typeHandle(typeHandle), _name(name), _fullname(fullname), _namespace(namespace_), _typeAttributes(typeAttributes), _image(image) : _typeHandle(typeHandle), _name(name), _fullname(fullname), _namespace(namespace_), _typeAttributes(typeAttributes), _image(image)
{ {
_typeToken = TypeTokenPool++; _typeToken = TypeTokenPool++;
_monoType = 0;
_size = 0;
} }
~CoreCLRClass() ~CoreCLRClass()
@@ -368,6 +370,7 @@ public:
CoreCLRMethod(StringAnsi name, int numParams, void* methodHandle, uint32 flags, CoreCLRClass* klass) CoreCLRMethod(StringAnsi name, int numParams, void* methodHandle, uint32 flags, CoreCLRClass* klass)
:_name(name), _numParams(numParams), _methodHandle(methodHandle), _methodAttributes(flags), _class(klass) :_name(name), _numParams(numParams), _methodHandle(methodHandle), _methodAttributes(flags), _class(klass)
{ {
_returnType = nullptr;
} }
const StringAnsi& GetName() const const StringAnsi& GetName() const
@@ -481,8 +484,12 @@ public:
{ {
if (getter != nullptr) if (getter != nullptr)
_getMethod = New<CoreCLRMethod>(StringAnsi(_name + "Get"), 1, getter, getterFlags, klass); _getMethod = New<CoreCLRMethod>(StringAnsi(_name + "Get"), 1, getter, getterFlags, klass);
else
_getMethod = nullptr;
if (setter != nullptr) if (setter != nullptr)
_setMethod = New<CoreCLRMethod>(StringAnsi(_name + "Set"), 1, setter, setterFlags, klass); _setMethod = New<CoreCLRMethod>(StringAnsi(_name + "Set"), 1, setter, setterFlags, klass);
else
_setMethod = nullptr;
} }
const StringAnsi& GetName() const const StringAnsi& GetName() const
@@ -617,12 +624,12 @@ void* CoreCLR::GetCustomAttribute(void* klass, void* attribClass)
} }
Array<void*> CoreCLR::GetCustomAttributes(void* klass) Array<void*> CoreCLR::GetCustomAttributes(void* klass)
{ {
Array<CoreCLRCustomAttribute*> attrib = ((CoreCLRClass*)klass)->GetCustomAttributes(); Array<CoreCLRCustomAttribute*> attribs = ((CoreCLRClass*)klass)->GetCustomAttributes();
Array<void*> attributes; Array<void*> attributes;
attributes.Resize(attrib.Count(), false); attributes.Resize(attribs.Count(), false);
for (int i = 0; i < attrib.Count(); i++) for (int i = 0; i < attribs.Count(); i++)
attributes.Add(attrib[i]->GetHandle()); attributes.Add(attribs[i]->GetHandle());
return attributes; return attributes;
} }
@@ -754,8 +761,7 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoObject* mono_value_box(MonoDomain* domain, Mo
MONO_API void mono_value_copy(void* dest, /*const*/ void* src, MonoClass* klass) MONO_API void mono_value_copy(void* dest, /*const*/ void* src, MonoClass* klass)
{ {
CoreCLRClass* mci = (CoreCLRClass*)klass; Platform::MemoryCopy(dest, src, ((CoreCLRClass*)klass)->GetSize());
Platform::MemoryCopy(dest, src, mci->GetSize());
} }
MONO_API MONO_RT_EXTERNAL_ONLY MonoClass* mono_object_get_class(MonoObject* obj) MONO_API MONO_RT_EXTERNAL_ONLY MonoClass* mono_object_get_class(MonoObject* obj)
@@ -763,10 +769,9 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoClass* mono_object_get_class(MonoObject* obj)
static void* GetObjectTypePtr = CoreCLR::GetStaticMethodPointer(TEXT("GetObjectType")); static void* GetObjectTypePtr = CoreCLR::GetStaticMethodPointer(TEXT("GetObjectType"));
void* classHandle = CoreCLR::CallStaticMethod<void*, void*>(GetObjectTypePtr, obj); void* classHandle = CoreCLR::CallStaticMethod<void*, void*>(GetObjectTypePtr, obj);
CoreCLRClass* mi = GetOrCreateClass((void*)classHandle); CoreCLRClass* klass = GetOrCreateClass((void*)classHandle);
ASSERT(klass != nullptr)
ASSERT(mi != nullptr) return (MonoClass*)klass;
return (MonoClass*)mi;
} }
MONO_API void* mono_object_unbox(MonoObject* obj) MONO_API void* mono_object_unbox(MonoObject* obj)
@@ -794,25 +799,17 @@ MONO_API MonoMethod* mono_object_get_virtual_method(MonoObject* obj, MonoMethod*
MONO_API MONO_RT_EXTERNAL_ONLY MonoObject* mono_runtime_invoke(MonoMethod* method, void* obj, void** params, MonoObject** exc) MONO_API MONO_RT_EXTERNAL_ONLY MonoObject* mono_runtime_invoke(MonoMethod* method, void* obj, void** params, MonoObject** exc)
{ {
CoreCLRMethod* mi = (CoreCLRMethod*)method;
void* methodPtr = mi->GetMethodHandle();
ASSERT(methodPtr != nullptr);
static void* InvokeMethodPtr = CoreCLR::GetStaticMethodPointer(TEXT("InvokeMethod")); static void* InvokeMethodPtr = CoreCLR::GetStaticMethodPointer(TEXT("InvokeMethod"));
MonoObject* execTmp = nullptr; MonoObject* execTmp = nullptr;
if (!exc) if (!exc)
exc = &execTmp; exc = &execTmp;
return (MonoObject*)CoreCLR::CallStaticMethod<void*, void*, void*, void*, void*>(InvokeMethodPtr, obj, methodPtr, params, exc); return (MonoObject*)CoreCLR::CallStaticMethod<void*, void*, void*, void*, void*>(InvokeMethodPtr, obj, ((CoreCLRMethod*)method)->GetMethodHandle(), params, exc);
} }
MONO_API MONO_RT_EXTERNAL_ONLY void* mono_method_get_unmanaged_thunk(MonoMethod* method) MONO_API MONO_RT_EXTERNAL_ONLY void* mono_method_get_unmanaged_thunk(MonoMethod* method)
{ {
CoreCLRMethod* mi = (CoreCLRMethod*)method;
void* methodPtr = mi->GetMethodHandle();
ASSERT(methodPtr != nullptr);
static void* GetMethodUnmanagedFunctionPointerPtr = CoreCLR::GetStaticMethodPointer(TEXT("GetMethodUnmanagedFunctionPointer")); static void* GetMethodUnmanagedFunctionPointerPtr = CoreCLR::GetStaticMethodPointer(TEXT("GetMethodUnmanagedFunctionPointer"));
return CoreCLR::CallStaticMethod<void*, void*>(GetMethodUnmanagedFunctionPointerPtr, methodPtr); return CoreCLR::CallStaticMethod<void*, void*>(GetMethodUnmanagedFunctionPointerPtr, ((CoreCLRMethod*)method)->GetMethodHandle());
} }
MONO_API void mono_field_set_value(MonoObject* obj, MonoClassField* field, void* value) MONO_API void mono_field_set_value(MonoObject* obj, MonoClassField* field, void* value)
@@ -1108,8 +1105,7 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoCustomAttrInfo* mono_custom_attrs_from_method
MONO_API MONO_RT_EXTERNAL_ONLY MonoCustomAttrInfo* mono_custom_attrs_from_class(MonoClass* klass) MONO_API MONO_RT_EXTERNAL_ONLY MonoCustomAttrInfo* mono_custom_attrs_from_class(MonoClass* klass)
{ {
CoreCLRClass* mi = (CoreCLRClass*)klass; MonoCustomAttrInfo* info = (MonoCustomAttrInfo*)New<Array<CoreCLRCustomAttribute*>>(((CoreCLRClass*)klass)->GetCustomAttributes());
MonoCustomAttrInfo* info = (MonoCustomAttrInfo*)New<Array<CoreCLRCustomAttribute*>>(mi->GetCustomAttributes());
return info; return info;
} }
@@ -1154,8 +1150,8 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoObject* mono_custom_attrs_get_attr(MonoCustom
MONO_API void mono_custom_attrs_free(MonoCustomAttrInfo* ainfo) MONO_API void mono_custom_attrs_free(MonoCustomAttrInfo* ainfo)
{ {
Array<CoreCLRCustomAttribute*>* mcai = (Array<CoreCLRCustomAttribute*>*)ainfo; Array<CoreCLRCustomAttribute*>* attribs = (Array<CoreCLRCustomAttribute*>*)ainfo;
Delete(mcai); Delete(attribs);
} }
MONO_API MONO_RT_EXTERNAL_ONLY MonoType* mono_reflection_type_get_type(MonoReflectionType* reftype) MONO_API MONO_RT_EXTERNAL_ONLY MonoType* mono_reflection_type_get_type(MonoReflectionType* reftype)
@@ -1179,8 +1175,7 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoClass* mono_class_from_name(MonoImage* image,
StringAnsi name_space(name_space_); StringAnsi name_space(name_space_);
StringAnsi name(name_); StringAnsi name(name_);
CoreCLRAssembly* mi = (CoreCLRAssembly*)image; for (auto klass : ((CoreCLRAssembly*)image)->GetClasses())
for (auto klass : mi->GetClasses())
{ {
if (klass->GetNamespace() == name_space && klass->GetName() == name) if (klass->GetNamespace() == name_space && klass->GetName() == name)
return (MonoClass*)klass; return (MonoClass*)klass;
@@ -1201,8 +1196,7 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoClass* mono_array_class_get(MonoClass* elemen
MONO_API MONO_RT_EXTERNAL_ONLY MonoClassField* mono_class_get_field_from_name(MonoClass* klass, const char* name) MONO_API MONO_RT_EXTERNAL_ONLY MonoClassField* mono_class_get_field_from_name(MonoClass* klass, const char* name)
{ {
CoreCLRClass* mi = (CoreCLRClass*)klass; for (auto field : ((CoreCLRClass*)klass)->GetFields())
for (auto field : mi->GetFields())
{ {
if (field->GetName() == name) if (field->GetName() == name)
return (MonoClassField*)field; return (MonoClassField*)field;
@@ -1212,8 +1206,7 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoClassField* mono_class_get_field_from_name(Mo
MONO_API MonoProperty* mono_class_get_property_from_name(MonoClass* klass, const char* name) MONO_API MonoProperty* mono_class_get_property_from_name(MonoClass* klass, const char* name)
{ {
CoreCLRClass* mi = (CoreCLRClass*)klass; for (auto prop : ((CoreCLRClass*)klass)->GetProperties())
for (auto prop : mi->GetProperties())
{ {
if (prop->GetName() == name) if (prop->GetName() == name)
return (MonoProperty*)prop; return (MonoProperty*)prop;
@@ -1246,8 +1239,8 @@ MONO_API mono_bool mono_class_is_subclass_of(MonoClass* klass, MonoClass* klassc
MONO_API char* mono_type_get_name(MonoType* type) MONO_API char* mono_type_get_name(MonoType* type)
{ {
CoreCLRClass* mci = (CoreCLRClass*)mono_type_get_class(type); CoreCLRClass* klass = (CoreCLRClass*)mono_type_get_class(type);
return StringAnsi(mci->GetFullname()).Get(); return StringAnsi(klass->GetFullname()).Get();
} }
MONO_API MonoImage* mono_class_get_image(MonoClass* klass) MONO_API MonoImage* mono_class_get_image(MonoClass* klass)
@@ -1495,8 +1488,7 @@ MONO_API mono_bool mono_type_is_reference(MonoType* type)
MONO_API MonoType* mono_signature_get_return_type(MonoMethodSignature* sig) MONO_API MonoType* mono_signature_get_return_type(MonoMethodSignature* sig)
{ {
CoreCLRMethod* mi = (CoreCLRMethod*)sig; return (MonoType*)((CoreCLRMethod*)sig)->GetReturnType();
return (MonoType*)mi->GetReturnType();
} }
MONO_API MonoType* mono_signature_get_params(MonoMethodSignature* sig, void** iter) MONO_API MonoType* mono_signature_get_params(MonoMethodSignature* sig, void** iter)
@@ -1514,15 +1506,13 @@ MONO_API MonoType* mono_signature_get_params(MonoMethodSignature* sig, void** it
MONO_API uint32 mono_signature_get_param_count(MonoMethodSignature* sig) MONO_API uint32 mono_signature_get_param_count(MonoMethodSignature* sig)
{ {
CoreCLRMethod* mi = (CoreCLRMethod*)sig; return ((CoreCLRMethod*)sig)->GetNumParameters();
return mi->GetNumParameters();
} }
MONO_API mono_bool mono_signature_param_is_out(MonoMethodSignature* sig, int param_num) MONO_API mono_bool mono_signature_param_is_out(MonoMethodSignature* sig, int param_num)
{ {
CoreCLRMethod* mi = (CoreCLRMethod*)sig;
static void* GetMethodParameterIsOutPtr = CoreCLR::GetStaticMethodPointer(TEXT("GetMethodParameterIsOut")); static void* GetMethodParameterIsOutPtr = CoreCLR::GetStaticMethodPointer(TEXT("GetMethodParameterIsOut"));
return CoreCLR::CallStaticMethod<bool, void*, int>(GetMethodParameterIsOutPtr, mi->GetMethodHandle(), param_num); return CoreCLR::CallStaticMethod<bool, void*, int>(GetMethodParameterIsOutPtr, ((CoreCLRMethod*)sig)->GetMethodHandle(), param_num);
} }
MONO_API int mono_type_stack_size(MonoType* type, int* alignment) MONO_API int mono_type_stack_size(MonoType* type, int* alignment)

View File

@@ -615,7 +615,6 @@ namespace MUtils
FORCE_INLINE bool* ToBoolArray(const BitArray<>& data) FORCE_INLINE bool* ToBoolArray(const BitArray<>& data)
{ {
bool* arr = (bool*)CoreCLR::Allocate(data.Count() * sizeof(bool)); bool* arr = (bool*)CoreCLR::Allocate(data.Count() * sizeof(bool));
//memcpy(arr, data.Get(), data.Count() * sizeof(bool));
for (int i = 0; i < data.Count(); i++) for (int i = 0; i < data.Count(); i++)
arr[i] = data[i]; arr[i] = data[i];
return arr; return arr;

View File

@@ -172,11 +172,7 @@ namespace Flax.Build.Bindings
// Skip for collections // Skip for collections
if ((typeInfo.Type == "Array" || typeInfo.Type == "Span" || typeInfo.Type == "DataContainer" || typeInfo.Type == "Dictionary" || typeInfo.Type == "HashSet") && typeInfo.GenericArgs != null) if ((typeInfo.Type == "Array" || typeInfo.Type == "Span" || typeInfo.Type == "DataContainer" || typeInfo.Type == "Dictionary" || typeInfo.Type == "HashSet") && typeInfo.GenericArgs != null)
#if !USE_NETCORE
return false; return false;
#else
return true;
#endif
// Skip for special types // Skip for special types
if (typeInfo.GenericArgs == null) if (typeInfo.GenericArgs == null)

View File

@@ -478,6 +478,7 @@ namespace Flax.Build.Bindings
} }
#if USE_NETCORE #if USE_NETCORE
var returnNativeType = GenerateCSharpManagedToNativeType(buildData, functionInfo.ReturnType, caller);
string returnMarshalType = ""; string returnMarshalType = "";
if (returnValueType == "bool") if (returnValueType == "bool")
returnMarshalType = "MarshalAs(UnmanagedType.U1)"; returnMarshalType = "MarshalAs(UnmanagedType.U1)";
@@ -492,6 +493,12 @@ namespace Flax.Build.Bindings
// Interfaces are not supported by NativeMarshallingAttribute, marshal the parameter // Interfaces are not supported by NativeMarshallingAttribute, marshal the parameter
returnMarshalType = $"MarshalUsing(typeof({returnValueType}Marshaller))"; returnMarshalType = $"MarshalUsing(typeof({returnValueType}Marshaller))";
} }
else if (functionInfo.ReturnType.Type == "MonoArray")
returnMarshalType = "MarshalUsing(typeof(FlaxEngine.SystemArrayMarshaller))";
else if (functionInfo.ReturnType.Type == "Array" || functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "DataContainer" || functionInfo.ReturnType.Type == "BytesContainer" || returnNativeType == "Array")
returnMarshalType = $"MarshalUsing(typeof(FlaxEngine.ArrayMarshaller<,>), CountElementName = nameof(__returnCount))";
else if (functionInfo.ReturnType.Type == "Dictionary")
returnMarshalType = $"MarshalUsing(typeof(FlaxEngine.DictionaryMarshaller<,>), ConstantElementCount = 0)";
else if (returnValueType == "byte[]") else if (returnValueType == "byte[]")
returnMarshalType = $"MarshalUsing(typeof(FlaxEngine.ArrayMarshaller<,>), CountElementName = \"__returnCount\")"; returnMarshalType = $"MarshalUsing(typeof(FlaxEngine.ArrayMarshaller<,>), CountElementName = \"__returnCount\")";
else if (returnValueType == "bool[]") else if (returnValueType == "bool[]")
@@ -576,7 +583,7 @@ namespace Flax.Build.Bindings
string parameterMarshalType = ""; string parameterMarshalType = "";
if (parameterInfo.IsOut && parameterInfo.DefaultValue == "var __resultAsRef") if (parameterInfo.IsOut && parameterInfo.DefaultValue == "var __resultAsRef")
{ {
if (functionInfo.Glue.UseResultReferenceCount) if (parameterInfo.Type.Type == "Array" || parameterInfo.Type.Type == "Span" || parameterInfo.Type.Type == "DataContainer" || parameterInfo.Type.Type == "BytesContainer")
parameterMarshalType = $"MarshalUsing(typeof(FlaxEngine.ArrayMarshaller<,>), CountElementName = \"{parameterInfo.Name}Count\")"; parameterMarshalType = $"MarshalUsing(typeof(FlaxEngine.ArrayMarshaller<,>), CountElementName = \"{parameterInfo.Name}Count\")";
else if (parameterInfo.Type.Type == "Dictionary") else if (parameterInfo.Type.Type == "Dictionary")
parameterMarshalType = $"MarshalUsing(typeof(FlaxEngine.DictionaryMarshaller<,>), ConstantElementCount = 0)"; parameterMarshalType = $"MarshalUsing(typeof(FlaxEngine.DictionaryMarshaller<,>), ConstantElementCount = 0)";
@@ -819,7 +826,6 @@ namespace Flax.Build.Bindings
{ {
marshallerName = classInfo.Name + "Marshaller"; marshallerName = classInfo.Name + "Marshaller";
contents.Append(indent).AppendLine($"[NativeMarshalling(typeof({marshallerName}))]"); contents.Append(indent).AppendLine($"[NativeMarshalling(typeof({marshallerName}))]");
//contents.Append(indent).AppendLine($"[NativeMarshalling(typeof(FlaxEngine.GCHandleMarshaller2<{classInfo.Name}>.GCHandleMarshaller3<{classInfo.Name}>))]");
} }
#endif #endif
contents.Append(indent); contents.Append(indent);

View File

@@ -879,6 +879,7 @@ namespace Flax.Build.Bindings
CustomParameters = new List<FunctionInfo.ParameterInfo>(), CustomParameters = new List<FunctionInfo.ParameterInfo>(),
}; };
bool returnTypeIsContainer = false;
var returnValueConvert = GenerateCppWrapperNativeToManaged(buildData, functionInfo.ReturnType, caller, out var returnValueType, functionInfo); var returnValueConvert = GenerateCppWrapperNativeToManaged(buildData, functionInfo.ReturnType, caller, out var returnValueType, functionInfo);
if (functionInfo.Glue.UseReferenceForResult) if (functionInfo.Glue.UseReferenceForResult)
{ {
@@ -894,26 +895,11 @@ namespace Flax.Build.Bindings
}, },
IsOut = true, IsOut = true,
}); });
#if USE_NETCORE
if (functionInfo.ReturnType.Type == "Array" || functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "DataContainer")
{
functionInfo.Glue.UseResultReferenceCount = true;
functionInfo.Glue.CustomParameters.Add(new FunctionInfo.ParameterInfo
{
Name = "__resultAsRefCount",
DefaultValue = "var __resultAsRefCount",
Type = new TypeInfo
{
Type = "int"
},
IsOut = true,
});
}
#endif
} }
#if USE_NETCORE #if USE_NETCORE
else if (functionInfo.ReturnType.Type == "BitArray" || functionInfo.ReturnType.Type == "BytesContainer") else if (functionInfo.ReturnType.Type == "Array" || functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "DataContainer" || functionInfo.ReturnType.Type == "BitArray" || functionInfo.ReturnType.Type == "BytesContainer")
{ {
returnTypeIsContainer = true;
functionInfo.Glue.CustomParameters.Add(new FunctionInfo.ParameterInfo functionInfo.Glue.CustomParameters.Add(new FunctionInfo.ParameterInfo
{ {
Name = "__returnCount", Name = "__returnCount",
@@ -1048,22 +1034,14 @@ namespace Flax.Build.Bindings
} }
#if USE_NETCORE #if USE_NETCORE
string callBegin2 = ""; string callReturnCount = "";
if (functionInfo.Glue.UseResultReferenceCount) if (returnTypeIsContainer)
{ {
callBegin2 = " "; callReturnCount = " ";
if (functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "BytesContainer") if (functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "BytesContainer")
callBegin2 += "*__resultAsRefCount = {0}.Length();"; callReturnCount += "*__returnCount = {0}.Length();";
else else
callBegin2 += "*__resultAsRefCount = {0}.Count();"; callReturnCount += "*__returnCount = {0}.Count();";
}
else if (functionInfo.ReturnType.Type == "BitArray" || functionInfo.ReturnType.Type == "BytesContainer")
{
callBegin2 = " ";
if (functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "BytesContainer")
callBegin2 += "*__returnCount = {0}.Length();";
else
callBegin2 += "*__returnCount = {0}.Count();";
} }
#endif #endif
string call; string call;
@@ -1102,16 +1080,6 @@ namespace Flax.Build.Bindings
} }
else else
{ {
#if USE_NETCORE
// FIXME
if (parameterInfo.Type.Type == "Span" ||
parameterInfo.Type.Type == "Array" ||
parameterInfo.Type.Type == "DataContainer" ||
parameterInfo.Type.Type == "Dictionary")
{
name = '*' + name;
}
#endif
// Convert value // Convert value
param += string.Format(CppParamsWrappersCache[i], name); param += string.Format(CppParamsWrappersCache[i], name);
} }
@@ -1155,11 +1123,11 @@ namespace Flax.Build.Bindings
} }
#if USE_NETCORE #if USE_NETCORE
if (!string.IsNullOrEmpty(callBegin2)) if (!string.IsNullOrEmpty(callReturnCount))
{ {
contents.Append(" ").Append("const auto& callTemp = ").Append(string.Format(callFormat, call, callParams)).Append(";").AppendLine(); contents.Append(" ").Append("const auto& callTemp = ").Append(string.Format(callFormat, call, callParams)).Append(";").AppendLine();
call = "callTemp"; call = "callTemp";
contents.Append(string.Format(callBegin2, call)); contents.Append(string.Format(callReturnCount, call));
contents.AppendLine(); contents.AppendLine();
contents.Append(callBegin); contents.Append(callBegin);
} }

View File

@@ -60,7 +60,6 @@ namespace Flax.Build.Bindings
public struct GlueInfo public struct GlueInfo
{ {
public bool UseReferenceForResult; public bool UseReferenceForResult;
public bool UseResultReferenceCount;
public List<ParameterInfo> CustomParameters; public List<ParameterInfo> CustomParameters;
} }