Merge branch 'dev/ObjectInterfaceReferences' of https://github.com/ifromstone/FlaxEngine into ifromstone-dev/ObjectInterfaceReferences

# Conflicts:
#	Source/Engine/Scripting/Internal/ManagedDictionary.cpp
This commit is contained in:
2026-09-12 12:40:59 +02:00
24 changed files with 1144 additions and 93 deletions
@@ -0,0 +1,14 @@
// Copyright (c) Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
/// <summary>
/// Marks a generated interface property as a native scripting object interface reference.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class ScriptingObjectInterfaceReferenceAttribute : Attribute
{
}
}
@@ -0,0 +1,14 @@
// Copyright (c) Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
/// <summary>
/// Marks a generated interface property as a native soft object interface reference.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class SoftObjectInterfaceReferenceAttribute : Attribute
{
}
}
@@ -37,7 +37,7 @@ struct FLAXENGINE_API VTableFunctionInjector
#if USE_NETCORE
#define ADD_INTERNAL_CALL(fullName, method)
#define DEFINE_INTERNAL_CALL(returnType) extern "C" DLLEXPORT returnType
#define DEFINE_INTERNAL_CALL(returnType) extern "C" DLLEXPORT USED returnType
#else
extern "C" FLAXENGINE_API void mono_add_internal_call(const char* name, const void* method);
#define ADD_INTERNAL_CALL(fullName, method) mono_add_internal_call(fullName, (const void*)method)
@@ -278,6 +278,10 @@ struct MConverter<T, typename TEnableIf<TIsBaseOf<class ScriptingObject, T>::Val
// Converter for ScriptingObject References.
template<typename T>
class ScriptingObjectReference;
template<typename T>
class ScriptingObjectInterfaceReference;
template<typename T>
class SoftObjectInterfaceReference;
template<typename T>
struct MConverter<ScriptingObjectReference<T>>
@@ -311,6 +315,50 @@ struct MConverter<ScriptingObjectReference<T>>
}
};
template<typename TReference, typename TInterface>
struct MInterfaceReferenceConverter
{
MObject* Box(const TReference& data, const MClass* klass)
{
return data.GetManagedInstance();
}
void Unbox(TReference& result, MObject* data)
{
result = ScriptingObject::ToInterface<TInterface>(ScriptingObject::ToNative(data));
}
void ToManagedArray(MArray* result, const Span<TReference>& data)
{
if (data.Length() == 0)
return;
MObject** objects = (MObject**)Allocator::Allocate(data.Length() * sizeof(MObject*));
for (int32 i = 0; i < data.Length(); i++)
objects[i] = data[i].GetManagedInstance();
MCore::GC::WriteArrayRef(result, Span<MObject*>(objects, data.Length()));
Allocator::Free(objects);
}
void ToNativeArray(Span<TReference>& result, const MArray* data)
{
MObject** dataPtr = MCore::Array::GetAddress<MObject*>(data);
for (int32 i = 0; i < result.Length(); i++)
result.Get()[i] = ScriptingObject::ToInterface<TInterface>(ScriptingObject::ToNative(dataPtr[i]));
}
};
// Converter for Scripting Interface References.
template<typename T>
struct MConverter<ScriptingObjectInterfaceReference<T>> : MInterfaceReferenceConverter<ScriptingObjectInterfaceReference<T>, T>
{
};
// Converter for Soft Object Interface References.
template<typename T>
struct MConverter<SoftObjectInterfaceReference<T>> : MInterfaceReferenceConverter<SoftObjectInterfaceReference<T>, T>
{
};
// Converter for Asset References.
template<typename T>
class AssetReference;
+8 -2
View File
@@ -717,6 +717,12 @@ DEFINE_INTERNAL_CALL(MString*) ObjectInternal_GetTypeName(ScriptingObject* obj)
return MUtils::ToString(obj->GetType().Fullname);
}
FORCE_INLINE bool ObjectInternal_MatchesType(ScriptingObject* obj, MClass* klass)
{
return !klass ||
(klass->IsInterface() ? obj->GetClass()->HasInterface(klass) : obj->Is(klass));
}
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject* type, bool skipLog = false)
{
if (!id->IsValid())
@@ -732,7 +738,7 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject*
}
if (obj)
{
if (klass && !obj->Is(klass))
if (!ObjectInternal_MatchesType(obj, klass))
{
if (!skipLog)
{
@@ -762,7 +768,7 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject*
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_TryFindObject(Guid* id, MTypeObject* type)
{
ScriptingObject* obj = Scripting::TryFindObject(*id);
if (obj && !obj->Is(MUtils::GetClass(type)))
if (obj && !ObjectInternal_MatchesType(obj, MUtils::GetClass(type)))
obj = nullptr;
return obj ? obj->GetOrCreateManagedInstance() : nullptr;
}
@@ -0,0 +1,195 @@
// Copyright (c) Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h"
/// <summary>
/// The scene object interface reference.
/// </summary>
/// <typeparam name="T">The type of the scripting interface.</typeparam>
template<typename T>
API_CLASS(InBuild) class ScriptingObjectInterfaceReference : public ScriptingObjectReferenceBase
{
typedef ScriptingObjectInterfaceReferenceHelper<T> Helper;
public:
typedef ScriptingObjectInterfaceReference<T> Type;
public:
/// <summary>
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
/// </summary>
ScriptingObjectInterfaceReference()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
/// </summary>
/// <param name="obj">The object to link.</param>
ScriptingObjectInterfaceReference(SceneObject* obj)
: ScriptingObjectReferenceBase(Helper::IsValidObject(obj) ? obj : nullptr)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
/// </summary>
/// <param name="interfaceObj">The interface object to link.</param>
ScriptingObjectInterfaceReference(T* interfaceObj)
: ScriptingObjectReferenceBase(Helper::GetSceneObject(interfaceObj))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
/// </summary>
/// <param name="other">The other property.</param>
ScriptingObjectInterfaceReference(const ScriptingObjectInterfaceReference& other)
: ScriptingObjectReferenceBase(other._object)
{
}
ScriptingObjectInterfaceReference(ScriptingObjectInterfaceReference&& other) noexcept
: ScriptingObjectReferenceBase(MoveTemp(other))
{
}
/// <summary>
/// Finalizes an instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
/// </summary>
~ScriptingObjectInterfaceReference()
{
}
public:
FORCE_INLINE bool operator==(SceneObject* other) const
{
return _object == other;
}
FORCE_INLINE bool operator!=(SceneObject* other) const
{
return _object != other;
}
FORCE_INLINE bool operator==(T* other) const
{
return Get() == other;
}
FORCE_INLINE bool operator!=(T* other) const
{
return Get() != other;
}
FORCE_INLINE bool operator==(const ScriptingObjectInterfaceReference& other) const
{
return _object == other._object;
}
FORCE_INLINE bool operator!=(const ScriptingObjectInterfaceReference& other) const
{
return _object != other._object;
}
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(SceneObject* other)
{
OnSet(Helper::IsValidObject(other) ? other : nullptr);
return *this;
}
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(T* other)
{
OnSet(Helper::GetSceneObject(other));
return *this;
}
ScriptingObjectInterfaceReference& operator=(const ScriptingObjectInterfaceReference& other)
{
OnSet(other._object);
return *this;
}
ScriptingObjectInterfaceReference& operator=(ScriptingObjectInterfaceReference&& other) noexcept
{
ScriptingObjectReferenceBase::operator=(MoveTemp(other));
return *this;
}
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(const Guid& id)
{
OnSet(Helper::FindSceneObject(id));
return *this;
}
/// <summary>
/// Implicit conversion to the interface.
/// </summary>
FORCE_INLINE operator T*() const
{
return Get();
}
/// <summary>
/// Implicit conversion to boolean value.
/// </summary>
FORCE_INLINE operator bool() const
{
return _object != nullptr;
}
/// <summary>
/// Interface accessor.
/// </summary>
FORCE_INLINE T* operator->() const
{
return Get();
}
/// <summary>
/// Gets the interface pointer.
/// </summary>
FORCE_INLINE T* Get() const
{
return ScriptingObject::ToInterface<T>(_object);
}
/// <summary>
/// Gets the referenced object.
/// </summary>
FORCE_INLINE SceneObject* GetObject() const
{
return static_cast<SceneObject*>(_object);
}
/// <summary>
/// Copies the object ID into the raw storage.
/// </summary>
FORCE_INLINE void CopyID(uint32 id[4]) const
{
memset(id, 0, sizeof(uint32) * 4);
if (_object)
{
const Guid value = GetID();
memcpy(id, &value, sizeof(uint32) * 4);
}
}
/// <summary>
/// Gets the object as a given type (static cast).
/// </summary>
template<typename U>
FORCE_INLINE U* As() const
{
return static_cast<U*>(_object);
}
};
template<typename T>
uint32 GetHash(const ScriptingObjectInterfaceReference<T>& key)
{
return GetHash(key.GetID());
}
@@ -0,0 +1,30 @@
// Copyright (c) Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Scripting/ScriptingObjectReference.h"
#include "Engine/Level/SceneObject.h"
/// <summary>
/// Utility methods for scene object interface references.
/// </summary>
/// <typeparam name="T">The type of the scripting interface.</typeparam>
template<typename T>
struct ScriptingObjectInterfaceReferenceHelper
{
FORCE_INLINE static bool IsValidObject(const SceneObject* obj)
{
return !obj || obj->GetType().GetInterface(T::TypeInitializer) != nullptr;
}
FORCE_INLINE static SceneObject* GetSceneObject(T* interfaceObj)
{
return ScriptingObject::Cast<SceneObject>(ScriptingObject::FromInterface<T>(interfaceObj));
}
FORCE_INLINE static SceneObject* FindSceneObject(const Guid& id)
{
SceneObject* obj = static_cast<SceneObject*>(FindObject(id, SceneObject::GetStaticClass()));
return IsValidObject(obj) ? obj : nullptr;
}
};
@@ -0,0 +1,258 @@
// Copyright (c) Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Scripting/SoftObjectReference.h"
#include "Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h"
/// <summary>
/// The scene object soft interface reference. Objects gets referenced on use (ID reference is resolving it).
/// </summary>
/// <typeparam name="T">The type of the scripting interface.</typeparam>
template<typename T>
API_CLASS(InBuild) class SoftObjectInterfaceReference : public SoftObjectReferenceBase
{
typedef ScriptingObjectInterfaceReferenceHelper<T> Helper;
public:
typedef SoftObjectInterfaceReference<T> Type;
public:
/// <summary>
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
/// </summary>
SoftObjectInterfaceReference()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
/// </summary>
/// <param name="obj">The object to link.</param>
SoftObjectInterfaceReference(SceneObject* obj)
{
OnSet(Helper::IsValidObject(obj) ? obj : nullptr);
}
/// <summary>
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
/// </summary>
/// <param name="interfaceObj">The interface object to link.</param>
SoftObjectInterfaceReference(T* interfaceObj)
{
OnSet(Helper::GetSceneObject(interfaceObj));
}
/// <summary>
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
/// </summary>
/// <param name="other">The other property.</param>
SoftObjectInterfaceReference(const SoftObjectInterfaceReference& other)
{
OnSet(other.GetID());
}
/// <summary>
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
/// </summary>
/// <param name="other">The other property.</param>
SoftObjectInterfaceReference(SoftObjectInterfaceReference&& other)
{
OnSet(other.GetID());
other.OnSet(nullptr);
}
/// <summary>
/// Finalizes an instance of the <see cref="SoftObjectInterfaceReference"/> class.
/// </summary>
~SoftObjectInterfaceReference()
{
}
public:
FORCE_INLINE bool operator==(SceneObject* other)
{
return GetObject() == other;
}
FORCE_INLINE bool operator!=(SceneObject* other)
{
return GetObject() != other;
}
FORCE_INLINE bool operator==(T* other)
{
return Get() == other;
}
FORCE_INLINE bool operator!=(T* other)
{
return Get() != other;
}
FORCE_INLINE bool operator==(const SoftObjectInterfaceReference& other)
{
return GetID() == other.GetID();
}
FORCE_INLINE bool operator!=(const SoftObjectInterfaceReference& other)
{
return GetID() != other.GetID();
}
SoftObjectInterfaceReference& operator=(const SoftObjectInterfaceReference& other)
{
if (this != &other)
OnSet(other.GetID());
return *this;
}
SoftObjectInterfaceReference& operator=(SoftObjectInterfaceReference&& other)
{
if (this != &other)
{
OnSet(other.GetID());
other.OnSet(nullptr);
}
return *this;
}
FORCE_INLINE SoftObjectInterfaceReference& operator=(SceneObject* other)
{
OnSet(Helper::IsValidObject(other) ? other : nullptr);
return *this;
}
FORCE_INLINE SoftObjectInterfaceReference& operator=(T* other)
{
OnSet(Helper::GetSceneObject(other));
return *this;
}
FORCE_INLINE SoftObjectInterfaceReference& operator=(const Guid& id)
{
OnSet(id);
return *this;
}
/// <summary>
/// Implicit conversion to the interface.
/// </summary>
FORCE_INLINE operator T*() const
{
return Get();
}
/// <summary>
/// Implicit conversion to boolean value.
/// </summary>
FORCE_INLINE operator bool() const
{
return Get() != nullptr;
}
/// <summary>
/// Interface accessor.
/// </summary>
FORCE_INLINE T* operator->() const
{
return Get();
}
/// <summary>
/// Gets the object as a given type (static cast).
/// </summary>
template<typename U>
FORCE_INLINE U* As() const
{
return static_cast<U*>(GetObject());
}
public:
/// <summary>
/// Gets the interface pointer.
/// </summary>
FORCE_INLINE T* Get() const
{
return ScriptingObject::ToInterface<T>(GetObject());
}
/// <summary>
/// Gets the referenced object.
/// </summary>
SceneObject* GetObject() const
{
if (!_object)
const_cast<SoftObjectInterfaceReference*>(this)->OnResolve(SceneObject::GetStaticClass());
return Helper::IsValidObject(static_cast<SceneObject*>(_object)) ? static_cast<SceneObject*>(_object) : nullptr;
}
/// <summary>
/// Gets managed instance object (or null if no object linked).
/// </summary>
MObject* GetManagedInstance() const
{
auto object = GetObject();
return object ? object->GetOrCreateManagedInstance() : nullptr;
}
/// <summary>
/// Determines whether object is assigned and managed instance of the object is alive.
/// </summary>
bool HasManagedInstance() const
{
auto object = GetObject();
return object && object->HasManagedInstance();
}
/// <summary>
/// Gets the managed instance object or creates it if missing or null if not assigned.
/// </summary>
MObject* GetOrCreateManagedInstance() const
{
auto object = GetObject();
return object ? object->GetOrCreateManagedInstance() : nullptr;
}
/// <summary>
/// Copies the object ID into the raw storage.
/// </summary>
FORCE_INLINE void CopyID(uint32 id[4]) const
{
const Guid value = GetID();
memcpy(id, &value, sizeof(uint32) * 4);
}
/// <summary>
/// Sets the object.
/// </summary>
/// <param name="id">The object ID. Uses Scripting to find the registered object of the given ID.</param>
FORCE_INLINE void Set(const Guid& id)
{
OnSet(id);
}
/// <summary>
/// Sets the object.
/// </summary>
/// <param name="object">The object.</param>
FORCE_INLINE void Set(SceneObject* object)
{
OnSet(Helper::IsValidObject(object) ? object : nullptr);
}
/// <summary>
/// Sets the object.
/// </summary>
/// <param name="interfaceObj">The interface object.</param>
FORCE_INLINE void Set(T* interfaceObj)
{
OnSet(Helper::GetSceneObject(interfaceObj));
}
};
template<typename T>
uint32 GetHash(const SoftObjectInterfaceReference<T>& key)
{
return GetHash(key.GetID());
}