Refactor abstract classes handling for scripting types creation (eg. Script or GPUResource) under dotnet7

This commit is contained in:
Wojciech Figat
2023-01-02 10:35:39 +01:00
parent 6a8483a898
commit 00d960d61e
4 changed files with 9 additions and 24 deletions

View File

@@ -12,7 +12,6 @@ using System.Runtime.CompilerServices;
using FlaxEngine.Assertions;
using FlaxEngine.Utilities;
using System.Runtime.InteropServices.Marshalling;
using FlaxEngine.Visject;
using System.Buffers;
using System.Collections.Concurrent;
using System.Text;
@@ -1845,10 +1844,12 @@ namespace FlaxEngine
internal static IntPtr NewObject(IntPtr typeHandle)
{
Type type = Unsafe.As<Type>(GCHandle.FromIntPtr(typeHandle).Target);
if (type == typeof(Script))
if (type.IsAbstract)
{
// FIXME: Script is an abstract type which can not be instantiated
type = typeof(VisjectScript);
// Dotnet doesn't allow to instantiate abstract type thus allow to use generated mock class usage (eg. for Script or GPUResource) for generated abstract types
var abstractWrapper = type.GetNestedType("AbstractWrapper", BindingFlags.NonPublic);
if (abstractWrapper != null)
type = abstractWrapper;
}
object value = RuntimeHelpers.GetUninitializedObject(type);
return GCHandle.ToIntPtr(GCHandle.Alloc(value));

View File

@@ -1,6 +0,0 @@
#include "VisjectScript.h"
VisjectScript::VisjectScript(const SpawnParams& params)
: Script(params)
{
}

View File

@@ -1,14 +0,0 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Scripting/Script.h"
/// <summary>
///
/// </summary>
API_CLASS(Namespace = "FlaxEngine.Visject") class FLAXENGINE_API VisjectScript : public Script
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE(VisjectScript);
};

View File

@@ -1227,6 +1227,10 @@ namespace Flax.Build.Bindings
GenerateCSharpManagedTypeInternals(buildData, classInfo, contents, indent);
// Abstract wrapper (to ensure C# class can be created for Visual Scripts, see NativeInterop.NewObject)
if (classInfo.IsAbstract)
contents.AppendLine().Append(indent).Append("[Unmanaged] private sealed class AbstractWrapper : ").Append(classInfo.Name).AppendLine(" { }");
// Nested types
foreach (var apiTypeInfo in classInfo.Children)
{