Merge remote-tracking branch 'origin/master' into 1.13
# Conflicts: # Source/Engine/GraphicsDevice/Vulkan/GPUSwapChainVulkan.cpp # Source/Engine/Renderer/Utils/GlobalSignDistanceFieldPass.cpp # Source/Engine/Scripting/Internal/InternalCalls.h # Source/Engine/Scripting/Internal/ManagedDictionary.cpp # Source/Engine/Scripting/Internal/ManagedDictionary.h # Source/Engine/Scripting/ScriptingObject.cpp # Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "ManagedDictionary.h"
|
||||
|
||||
#if USE_CSHARP
|
||||
#include "Engine/Scripting/BinaryModule.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MClass.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MMethod.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MAssembly.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MException.h"
|
||||
#include "Engine/Scripting/Internal/StdTypesContainer.h"
|
||||
|
||||
Dictionary<ManagedDictionary::KeyValueType, MType*> ManagedDictionary::CachedTypes;
|
||||
#if !USE_MONO_AOT
|
||||
ManagedDictionary::MakeGenericTypeThunk ManagedDictionary::MakeGenericType;
|
||||
@@ -15,4 +21,157 @@ MMethod* ManagedDictionary::CreateInstance;
|
||||
MMethod* ManagedDictionary::AddDictionaryItem;
|
||||
MMethod* ManagedDictionary::GetDictionaryKeys;
|
||||
#endif
|
||||
|
||||
ManagedDictionary::ManagedDictionary(MObject* instance)
|
||||
{
|
||||
Instance = instance;
|
||||
|
||||
#if !USE_MONO_AOT
|
||||
// Cache the thunks of the dictionary helper methods
|
||||
if (MakeGenericType == nullptr)
|
||||
{
|
||||
MClass* scriptingClass = Scripting::GetStaticClass();
|
||||
CHECK(scriptingClass);
|
||||
|
||||
MMethod* makeGenericTypeMethod = scriptingClass->GetMethod("MakeGenericType", 2);
|
||||
CHECK(makeGenericTypeMethod);
|
||||
MakeGenericType = (MakeGenericTypeThunk)makeGenericTypeMethod->GetThunk();
|
||||
|
||||
MMethod* createInstanceMethod = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
|
||||
CHECK(createInstanceMethod);
|
||||
CreateInstance = (CreateInstanceThunk)createInstanceMethod->GetThunk();
|
||||
|
||||
MMethod* addDictionaryItemMethod = scriptingClass->GetMethod("AddDictionaryItem", 3);
|
||||
CHECK(addDictionaryItemMethod);
|
||||
AddDictionaryItem = (AddDictionaryItemThunk)addDictionaryItemMethod->GetThunk();
|
||||
|
||||
MMethod* getDictionaryKeysItemMethod = scriptingClass->GetMethod("GetDictionaryKeys", 1);
|
||||
CHECK(getDictionaryKeysItemMethod);
|
||||
GetDictionaryKeys = (GetDictionaryKeysThunk)getDictionaryKeysItemMethod->GetThunk();
|
||||
}
|
||||
#else
|
||||
if (MakeGenericType == nullptr)
|
||||
{
|
||||
MClass* scriptingClass = Scripting::GetStaticClass();
|
||||
CHECK(scriptingClass);
|
||||
|
||||
MakeGenericType = scriptingClass->GetMethod("MakeGenericType", 2);
|
||||
CHECK(MakeGenericType);
|
||||
|
||||
CreateInstance = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
|
||||
CHECK(CreateInstance);
|
||||
|
||||
AddDictionaryItem = scriptingClass->GetMethod("AddDictionaryItem", 3);
|
||||
CHECK(AddDictionaryItem);
|
||||
|
||||
GetDictionaryKeys = scriptingClass->GetMethod("GetDictionaryKeys", 1);
|
||||
CHECK(GetDictionaryKeys);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
MType* ManagedDictionary::GetClass(MType* keyType, MType* valueType)
|
||||
{
|
||||
// Check if the generic type was generated earlier
|
||||
KeyValueType cacheKey = { keyType, valueType };
|
||||
MType* dictionaryType;
|
||||
if (CachedTypes.TryGet(cacheKey, dictionaryType))
|
||||
return dictionaryType;
|
||||
|
||||
MType* genericType = MUtils::GetType(StdTypesContainer::Instance()->DictionaryClass);
|
||||
MArray* genericArgs = MCore::Array::New(MCore::TypeCache::IntPtr, 2);
|
||||
MType** genericArgsPtr = MCore::Array::GetAddress<MType*>(genericArgs);
|
||||
genericArgsPtr[0] = keyType;
|
||||
genericArgsPtr[1] = valueType;
|
||||
|
||||
MObject* exception = nullptr;
|
||||
#if !USE_MONO_AOT
|
||||
dictionaryType = MakeGenericType(nullptr, genericType, genericArgs, &exception);
|
||||
#else
|
||||
void* params[2];
|
||||
params[0] = genericType;
|
||||
params[1] = genericArgs;
|
||||
dictionaryType = (MType*)MakeGenericType->Invoke(nullptr, params, &exception);
|
||||
#endif
|
||||
if (exception)
|
||||
{
|
||||
MException ex(exception);
|
||||
ex.Log(LogType::Error, TEXT(""));
|
||||
return nullptr;
|
||||
}
|
||||
CachedTypes.Add(cacheKey, dictionaryType);
|
||||
return dictionaryType;
|
||||
}
|
||||
|
||||
ManagedDictionary ManagedDictionary::New(MType* keyType, MType* valueType)
|
||||
{
|
||||
ManagedDictionary result;
|
||||
MType* dictionaryType = GetClass(keyType, valueType);
|
||||
if (!dictionaryType)
|
||||
return result;
|
||||
|
||||
MObject* exception = nullptr;
|
||||
#if !USE_MONO_AOT
|
||||
MObject* instance = CreateInstance(nullptr, dictionaryType, nullptr, &exception);
|
||||
#else
|
||||
void* params[2];
|
||||
params[0] = dictionaryType;
|
||||
params[1] = nullptr;
|
||||
MObject* instance = CreateInstance->Invoke(nullptr, params, &exception);
|
||||
#endif
|
||||
if (exception)
|
||||
{
|
||||
MException ex(exception);
|
||||
ex.Log(LogType::Error, TEXT(""));
|
||||
return result;
|
||||
}
|
||||
|
||||
result.Instance = instance;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ManagedDictionary::Add(MObject* key, MObject* value)
|
||||
{
|
||||
CHECK(Instance);
|
||||
|
||||
MObject* exception = nullptr;
|
||||
#if !USE_MONO_AOT
|
||||
AddDictionaryItem(nullptr, Instance, key, value, &exception);
|
||||
#else
|
||||
void* params[3];
|
||||
params[0] = Instance;
|
||||
params[1] = key;
|
||||
params[2] = value;
|
||||
AddDictionaryItem->Invoke(Instance, params, &exception);
|
||||
#endif
|
||||
if (exception)
|
||||
{
|
||||
MException ex(exception);
|
||||
ex.Log(LogType::Error, TEXT(""));
|
||||
}
|
||||
}
|
||||
|
||||
MArray* ManagedDictionary::GetKeys() const
|
||||
{
|
||||
CHECK_RETURN(Instance, nullptr);
|
||||
#if !USE_MONO_AOT
|
||||
return GetDictionaryKeys(nullptr, Instance, nullptr);
|
||||
#else
|
||||
void* params[1];
|
||||
params[0] = Instance;
|
||||
return (MArray*)GetDictionaryKeys->Invoke(nullptr, params, nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
MObject* ManagedDictionary::GetValue(MObject* key) const
|
||||
{
|
||||
CHECK_RETURN(Instance, nullptr);
|
||||
MClass* klass = MCore::Object::GetClass(Instance);
|
||||
MMethod* getItemMethod = klass->GetMethod("System.Collections.IDictionary.get_Item", 1);
|
||||
CHECK_RETURN(getItemMethod, nullptr);
|
||||
void* params[1];
|
||||
params[0] = key;
|
||||
return getItemMethod->Invoke(Instance, params, nullptr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,13 +5,7 @@
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Scripting/Scripting.h"
|
||||
#if USE_CSHARP
|
||||
#include "Engine/Scripting/BinaryModule.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MUtils.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MClass.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MMethod.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MAssembly.h"
|
||||
#include "Engine/Scripting/ManagedCLR/MException.h"
|
||||
#include "Engine/Scripting/Internal/StdTypesContainer.h"
|
||||
#include "Engine/Core/Collections/Dictionary.h"
|
||||
|
||||
/// <summary>
|
||||
@@ -57,53 +51,7 @@ private:
|
||||
public:
|
||||
MObject* Instance;
|
||||
|
||||
ManagedDictionary(MObject* instance = nullptr)
|
||||
{
|
||||
Instance = instance;
|
||||
|
||||
#if !USE_MONO_AOT
|
||||
// Cache the thunks of the dictionary helper methods
|
||||
if (MakeGenericType == nullptr)
|
||||
{
|
||||
MClass* scriptingClass = Scripting::GetStaticClass();
|
||||
CHECK(scriptingClass);
|
||||
|
||||
MMethod* makeGenericTypeMethod = scriptingClass->GetMethod("MakeGenericType", 2);
|
||||
CHECK(makeGenericTypeMethod);
|
||||
MakeGenericType = (MakeGenericTypeThunk)makeGenericTypeMethod->GetThunk();
|
||||
|
||||
MMethod* createInstanceMethod = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
|
||||
CHECK(createInstanceMethod);
|
||||
CreateInstance = (CreateInstanceThunk)createInstanceMethod->GetThunk();
|
||||
|
||||
MMethod* addDictionaryItemMethod = scriptingClass->GetMethod("AddDictionaryItem", 3);
|
||||
CHECK(addDictionaryItemMethod);
|
||||
AddDictionaryItem = (AddDictionaryItemThunk)addDictionaryItemMethod->GetThunk();
|
||||
|
||||
MMethod* getDictionaryKeysItemMethod = scriptingClass->GetMethod("GetDictionaryKeys", 1);
|
||||
CHECK(getDictionaryKeysItemMethod);
|
||||
GetDictionaryKeys = (GetDictionaryKeysThunk)getDictionaryKeysItemMethod->GetThunk();
|
||||
}
|
||||
#else
|
||||
if (MakeGenericType == nullptr)
|
||||
{
|
||||
MClass* scriptingClass = Scripting::GetStaticClass();
|
||||
CHECK(scriptingClass);
|
||||
|
||||
MakeGenericType = scriptingClass->GetMethod("MakeGenericType", 2);
|
||||
CHECK(MakeGenericType);
|
||||
|
||||
CreateInstance = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
|
||||
CHECK(CreateInstance);
|
||||
|
||||
AddDictionaryItem = scriptingClass->GetMethod("AddDictionaryItem", 3);
|
||||
CHECK(AddDictionaryItem);
|
||||
|
||||
GetDictionaryKeys = scriptingClass->GetMethod("GetDictionaryKeys", 1);
|
||||
CHECK(GetDictionaryKeys);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
ManagedDictionary(MObject* instance = nullptr);
|
||||
|
||||
template<typename KeyType, typename ValueType>
|
||||
static MObject* ToManaged(const Dictionary<KeyType, ValueType>& data, MType* keyType, MType* valueType)
|
||||
@@ -154,109 +102,15 @@ public:
|
||||
return result;
|
||||
}
|
||||
|
||||
static MType* GetClass(MType* keyType, MType* valueType)
|
||||
{
|
||||
// Check if the generic type was generated earlier
|
||||
KeyValueType cacheKey = { keyType, valueType };
|
||||
MType* dictionaryType;
|
||||
if (CachedTypes.TryGet(cacheKey, dictionaryType))
|
||||
return dictionaryType;
|
||||
static MType* GetClass(MType* keyType, MType* valueType);
|
||||
|
||||
MType* genericType = MUtils::GetType(StdTypesContainer::Instance()->DictionaryClass);
|
||||
MArray* genericArgs = MCore::Array::New(MCore::TypeCache::IntPtr, 2);
|
||||
MType** genericArgsPtr = MCore::Array::GetAddress<MType*>(genericArgs);
|
||||
genericArgsPtr[0] = keyType;
|
||||
genericArgsPtr[1] = valueType;
|
||||
static ManagedDictionary New(MType* keyType, MType* valueType);
|
||||
|
||||
MObject* exception = nullptr;
|
||||
#if !USE_MONO_AOT
|
||||
dictionaryType = MakeGenericType(nullptr, genericType, genericArgs, &exception);
|
||||
#else
|
||||
void* params[2];
|
||||
params[0] = genericType;
|
||||
params[1] = genericArgs;
|
||||
dictionaryType = (MType*)MakeGenericType->Invoke(nullptr, params, &exception);
|
||||
#endif
|
||||
if (exception)
|
||||
{
|
||||
MException ex(exception);
|
||||
ex.Log(LogType::Error, TEXT(""));
|
||||
return nullptr;
|
||||
}
|
||||
CachedTypes.Add(cacheKey, dictionaryType);
|
||||
return dictionaryType;
|
||||
}
|
||||
void Add(MObject* key, MObject* value);
|
||||
|
||||
static ManagedDictionary New(MType* keyType, MType* valueType)
|
||||
{
|
||||
ManagedDictionary result;
|
||||
MType* dictionaryType = GetClass(keyType, valueType);
|
||||
if (!dictionaryType)
|
||||
return result;
|
||||
MArray* GetKeys() const;
|
||||
|
||||
MObject* exception = nullptr;
|
||||
#if !USE_MONO_AOT
|
||||
MObject* instance = CreateInstance(nullptr, dictionaryType, nullptr, &exception);
|
||||
#else
|
||||
void* params[2];
|
||||
params[0] = dictionaryType;
|
||||
params[1] = nullptr;
|
||||
MObject* instance = CreateInstance->Invoke(nullptr, params, &exception);
|
||||
#endif
|
||||
if (exception)
|
||||
{
|
||||
MException ex(exception);
|
||||
ex.Log(LogType::Error, TEXT(""));
|
||||
return result;
|
||||
}
|
||||
|
||||
result.Instance = instance;
|
||||
return result;
|
||||
}
|
||||
|
||||
void Add(MObject* key, MObject* value)
|
||||
{
|
||||
CHECK(Instance);
|
||||
|
||||
MObject* exception = nullptr;
|
||||
#if !USE_MONO_AOT
|
||||
AddDictionaryItem(nullptr, Instance, key, value, &exception);
|
||||
#else
|
||||
void* params[3];
|
||||
params[0] = Instance;
|
||||
params[1] = key;
|
||||
params[2] = value;
|
||||
AddDictionaryItem->Invoke(Instance, params, &exception);
|
||||
#endif
|
||||
if (exception)
|
||||
{
|
||||
MException ex(exception);
|
||||
ex.Log(LogType::Error, TEXT(""));
|
||||
}
|
||||
}
|
||||
|
||||
MArray* GetKeys() const
|
||||
{
|
||||
CHECK_RETURN(Instance, nullptr);
|
||||
#if !USE_MONO_AOT
|
||||
return GetDictionaryKeys(nullptr, Instance, nullptr);
|
||||
#else
|
||||
void* params[1];
|
||||
params[0] = Instance;
|
||||
return (MArray*)GetDictionaryKeys->Invoke(nullptr, params, nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
MObject* GetValue(MObject* key) const
|
||||
{
|
||||
CHECK_RETURN(Instance, nullptr);
|
||||
MClass* klass = MCore::Object::GetClass(Instance);
|
||||
MMethod* getItemMethod = klass->GetMethod("System.Collections.IDictionary.get_Item", 1);
|
||||
CHECK_RETURN(getItemMethod, nullptr);
|
||||
void* params[1];
|
||||
params[0] = key;
|
||||
return getItemMethod->Invoke(Instance, params, nullptr);
|
||||
}
|
||||
MObject* GetValue(MObject* key) const;
|
||||
};
|
||||
|
||||
inline uint32 GetHash(const ManagedDictionary::KeyValueType& other)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include "MUtils.h"
|
||||
#include "MClass.h"
|
||||
#include "MCore.h"
|
||||
#include "MMethod.h"
|
||||
#include "MAssembly.h"
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Core/Types/DataContainer.h"
|
||||
#include "Engine/Core/Types/Version.h"
|
||||
@@ -19,6 +21,7 @@
|
||||
#include "Engine/Core/Math/Ray.h"
|
||||
#include "Engine/Scripting/Scripting.h"
|
||||
#include "Engine/Scripting/ScriptingObject.h"
|
||||
#include "Engine/Scripting/BinaryModule.h"
|
||||
#include "Engine/Scripting/Internal/StdTypesContainer.h"
|
||||
#include "Engine/Scripting/Internal/ManagedDictionary.h"
|
||||
#include "Engine/Utilities/StringConverter.h"
|
||||
|
||||
@@ -278,57 +278,25 @@ struct MConverter<T, typename TEnableIf<TIsBaseOf<class ScriptingObject, T>::Val
|
||||
// Converter for ScriptingObject References.
|
||||
template<typename T>
|
||||
class ScriptingObjectReference;
|
||||
|
||||
template<typename T>
|
||||
struct MConverter<ScriptingObjectReference<T>>
|
||||
{
|
||||
MObject* Box(const ScriptingObjectReference<T>& data, const MClass* klass)
|
||||
{
|
||||
return data.GetManagedInstance();
|
||||
}
|
||||
|
||||
void Unbox(ScriptingObjectReference<T>& result, MObject* data)
|
||||
{
|
||||
result = (T*)ScriptingObject::ToNative(data);
|
||||
}
|
||||
|
||||
void ToManagedArray(MArray* result, const Span<ScriptingObjectReference<T>>& 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<ScriptingObjectReference<T>>& result, const MArray* data)
|
||||
{
|
||||
MObject** dataPtr = MCore::Array::GetAddress<MObject*>(data);
|
||||
for (int32 i = 0; i < result.Length(); i++)
|
||||
result.Get()[i] = (T*)ScriptingObject::ToNative(dataPtr[i]);
|
||||
}
|
||||
};
|
||||
|
||||
// Converter for Asset References.
|
||||
class ScriptingObjectInterfaceReference;
|
||||
template<typename T>
|
||||
class AssetReference;
|
||||
|
||||
template<typename T>
|
||||
struct MConverter<AssetReference<T>>
|
||||
template<typename Reference, typename Object>
|
||||
struct MObjectReferenceConverter
|
||||
{
|
||||
MObject* Box(const AssetReference<T>& data, const MClass* klass)
|
||||
MObject* Box(const Reference& data, const MClass* klass)
|
||||
{
|
||||
return data.GetManagedInstance();
|
||||
}
|
||||
|
||||
void Unbox(AssetReference<T>& result, MObject* data)
|
||||
void Unbox(Reference& result, MObject* data)
|
||||
{
|
||||
result = (T*)ScriptingObject::ToNative(data);
|
||||
result = (Object*)ScriptingObject::ToNative(data);
|
||||
}
|
||||
|
||||
void ToManagedArray(MArray* result, const Span<AssetReference<T>>& data)
|
||||
void ToManagedArray(MArray* result, const Span<Reference>& data)
|
||||
{
|
||||
if (data.Length() == 0)
|
||||
return;
|
||||
@@ -339,14 +307,29 @@ struct MConverter<AssetReference<T>>
|
||||
Allocator::Free(objects);
|
||||
}
|
||||
|
||||
void ToNativeArray(Span<AssetReference<T>>& result, const MArray* data)
|
||||
void ToNativeArray(Span<Reference>& result, const MArray* data)
|
||||
{
|
||||
MObject** dataPtr = MCore::Array::GetAddress<MObject*>(data);
|
||||
for (int32 i = 0; i < result.Length(); i++)
|
||||
result.Get()[i] = (T*)ScriptingObject::ToNative(dataPtr[i]);
|
||||
result.Get()[i] = (Object*)ScriptingObject::ToNative(dataPtr[i]);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct MConverter<ScriptingObjectReference<T>> : MObjectReferenceConverter<ScriptingObjectReference<T>, T>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct MConverter<ScriptingObjectInterfaceReference<T>> : MObjectReferenceConverter<ScriptingObjectInterfaceReference<T>, ScriptingObject>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct MConverter<AssetReference<T>> : MObjectReferenceConverter<AssetReference<T>, T>
|
||||
{
|
||||
};
|
||||
|
||||
// TODO: use MarshalAs=Guid on SoftAssetReference to pass guid over bindings and not load asset in glue code
|
||||
template<typename T>
|
||||
class SoftAssetReference;
|
||||
|
||||
@@ -712,6 +712,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, MType* type, bool skipLog = false)
|
||||
{
|
||||
if (!id->IsValid())
|
||||
@@ -727,7 +733,7 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MType* type,
|
||||
}
|
||||
if (obj)
|
||||
{
|
||||
if (klass && !obj->Is(klass))
|
||||
if (!ObjectInternal_MatchesType(obj, klass))
|
||||
{
|
||||
if (!skipLog)
|
||||
{
|
||||
@@ -757,7 +763,7 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MType* type,
|
||||
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_TryFindObject(Guid* id, MType* 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,188 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
#if FLAX_EDITOR
|
||||
using System.Globalization;
|
||||
using System.ComponentModel;
|
||||
#endif
|
||||
|
||||
namespace FlaxEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// The scripting object reference with interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the scripting interface.</typeparam>
|
||||
#if FLAX_EDITOR
|
||||
[CustomEditor(typeof(FlaxEditor.CustomEditors.Editors.ScriptingObjectInterfaceReferenceEditor))]
|
||||
[TypeConverter(typeof(TypeConverters.ScriptingObjectInterfaceReferenceConverter))]
|
||||
#endif
|
||||
public struct ScriptingObjectInterfaceReference<T> : IComparable, IComparable<ScriptingObjectInterfaceReference<T>> where T : class
|
||||
{
|
||||
private Object _object;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the referenced object that implements the interface.
|
||||
/// </summary>
|
||||
public Object Object
|
||||
{
|
||||
get => _object;
|
||||
set => _object = value != null && value is T ? value : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the referenced object that implements the interface.
|
||||
/// </summary>
|
||||
[NoSerialize]
|
||||
public T Interface
|
||||
{
|
||||
get => _object as T;
|
||||
set
|
||||
{
|
||||
var obj = value as Object;
|
||||
if (value == null || obj != null)
|
||||
_object = obj;
|
||||
else
|
||||
throw new InvalidCastException($"Cannot use object of type {value.GetType().FullName} for ScriptingObjectInterfaceReference<{typeof(T).FullName}>. It needs to inherit from {typeof(Object).FullName}.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference{T}"/> structure.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to link.</param>
|
||||
public ScriptingObjectInterfaceReference(Object obj)
|
||||
{
|
||||
Object = obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference{T}"/> structure.
|
||||
/// </summary>
|
||||
/// <param name="interfaceObj">The interface object to link.</param>
|
||||
public ScriptingObjectInterfaceReference(T interfaceObj)
|
||||
{
|
||||
Interface = interfaceObj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator to typed interface.
|
||||
/// </summary>
|
||||
/// <param name="value">Reference</param>
|
||||
/// <returns>Interface</returns>
|
||||
public static explicit operator T(ScriptingObjectInterfaceReference<T> value)
|
||||
{
|
||||
return value._object as T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator from object to reference.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to link.</param>
|
||||
/// <returns>Reference</returns>
|
||||
public static explicit operator ScriptingObjectInterfaceReference<T>(T obj)
|
||||
{
|
||||
return new ScriptingObjectInterfaceReference<T>(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator to object.
|
||||
/// </summary>
|
||||
/// <param name="value">Reference</param>
|
||||
/// <returns>Object</returns>
|
||||
public static implicit operator Object(ScriptingObjectInterfaceReference<T> value)
|
||||
{
|
||||
return value._object;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator from object to reference.
|
||||
/// </summary>
|
||||
/// <param name="obj">Object</param>
|
||||
/// <returns>Reference</returns>
|
||||
public static implicit operator ScriptingObjectInterfaceReference<T>(Object obj)
|
||||
{
|
||||
return new ScriptingObjectInterfaceReference<T>(obj);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return _object?.ToString() ?? "<null>";
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Object.GetUnmanagedPtr(_object).GetHashCode();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CompareTo(object obj)
|
||||
{
|
||||
if (obj is ScriptingObjectInterfaceReference<T> other)
|
||||
return CompareTo(other);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CompareTo(ScriptingObjectInterfaceReference<T> other)
|
||||
{
|
||||
return Object.GetUnmanagedPtr(_object).CompareTo(Object.GetUnmanagedPtr(other._object));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if FLAX_EDITOR
|
||||
namespace FlaxEngine.TypeConverters
|
||||
{
|
||||
internal class ScriptingObjectInterfaceReferenceConverter : TypeConverter
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
||||
{
|
||||
if (sourceType == typeof(string))
|
||||
return true;
|
||||
return base.CanConvertFrom(context, sourceType);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
|
||||
{
|
||||
if (destinationType == typeof(string))
|
||||
return false;
|
||||
return base.CanConvertTo(context, destinationType);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
||||
{
|
||||
if (value is string str && context is DummyTypeDescriptorContext internalContext)
|
||||
{
|
||||
var type = internalContext.CurrentType;
|
||||
Json.JsonSerializer.ParseID(str, out var id);
|
||||
var obj = Object.Find(ref id, type.GetGenericArguments()[0]);
|
||||
var objectField = type.GetField("_object", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
||||
value = Activator.CreateInstance(type);
|
||||
objectField.SetValue(value, obj);
|
||||
return value;
|
||||
}
|
||||
return base.ConvertFrom(context, culture, value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override unsafe object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
||||
{
|
||||
if (destinationType == typeof(string))
|
||||
{
|
||||
var objectField = value.GetType().GetField("_object", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
||||
var obj = objectField.GetValue(value) as Object;
|
||||
if (obj == null)
|
||||
return string.Empty;
|
||||
var id = obj.ID;
|
||||
return Json.JsonSerializer.GetStringID(&id);
|
||||
}
|
||||
return base.ConvertTo(context, culture, value, destinationType);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,193 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ScriptingObjectReference.h"
|
||||
|
||||
/// <summary>
|
||||
/// The scripting object reference with interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the scripting interface.</typeparam>
|
||||
template<typename T>
|
||||
API_CLASS(Template, MarshalAs=ScriptingObject*) class ScriptingObjectInterfaceReference : public ScriptingObjectReferenceBase
|
||||
{
|
||||
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(ScriptingObject* obj)
|
||||
: ScriptingObjectReferenceBase(IsValid(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(ScriptingObject::FromInterface<T>(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==(ScriptingObject* other) const
|
||||
{
|
||||
return _object == other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(ScriptingObject* 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=(ScriptingObject* other)
|
||||
{
|
||||
OnSet(IsValid(other) ? other : nullptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(T* other)
|
||||
{
|
||||
OnSet(ScriptingObject::FromInterface<T>(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(const ScriptingObjectInterfaceReference& other)
|
||||
{
|
||||
OnSet(other._object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(ScriptingObjectInterfaceReference&& other) noexcept
|
||||
{
|
||||
ScriptingObjectReferenceBase::operator=(MoveTemp(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
ScriptingObjectInterfaceReference& operator=(const Guid& id)
|
||||
{
|
||||
ScriptingObject* obj = FindObject(id, ScriptingObject::GetStaticClass());
|
||||
OnSet(IsValid(obj) ? obj : nullptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion to the interface.
|
||||
/// </summary>
|
||||
FORCE_INLINE operator T*() const
|
||||
{
|
||||
return Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion to the object.
|
||||
/// </summary>
|
||||
FORCE_INLINE operator ScriptingObject*() const
|
||||
{
|
||||
return _object;
|
||||
}
|
||||
|
||||
/// <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 ScriptingObject* GetObject() const
|
||||
{
|
||||
return _object;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets managed instance object.
|
||||
/// </summary>
|
||||
FORCE_INLINE MObject* GetManagedInstance() const
|
||||
{
|
||||
return _object ? _object->GetOrCreateManagedInstance() : nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
FORCE_INLINE static bool IsValid(const ScriptingObject* obj)
|
||||
{
|
||||
return !obj || obj->GetType().GetInterface(T::TypeInitializer);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
uint32 GetHash(const ScriptingObjectInterfaceReference<T>& key)
|
||||
{
|
||||
return GetHash(key.GetID());
|
||||
}
|
||||
@@ -72,7 +72,7 @@ public:
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets managed instance object (or null if no object linked).
|
||||
/// Gets managed instance object.
|
||||
/// </summary>
|
||||
FORCE_INLINE MObject* GetManagedInstance() const
|
||||
{
|
||||
|
||||
@@ -233,7 +233,7 @@ public:
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets managed instance object (or null if no object linked).
|
||||
/// Gets managed instance object.
|
||||
/// </summary>
|
||||
MObject* GetManagedInstance() const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user