Fix using C#-only structures as Visual Script properties

This commit is contained in:
Wojtek Figat
2021-07-30 10:34:21 +02:00
parent 752dc30cc5
commit e970d53787
3 changed files with 29 additions and 2 deletions

View File

@@ -289,7 +289,7 @@ void VisualScriptExecutor::ProcessGroupPacking(Box* box, Node* node, Value& valu
if (mclass)
{
// Fallback to C#-only types
auto instance = (MonoObject*)structureValue.AsUint;
auto instance = (MonoObject*)structureValue;
CHECK(instance);
if (structureValue.Type.Type != VariantType::ManagedObject || mono_object_get_class(instance) != mclass->GetNative())
{

View File

@@ -22,6 +22,8 @@
#include "Engine/Core/Math/Transform.h"
#include "Engine/Scripting/Scripting.h"
#include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Scripting/ManagedCLR/MClass.h"
#include "Engine/Scripting/ManagedCLR/MCore.h"
#include "Engine/Scripting/ManagedCLR/MUtils.h"
#include "Engine/Utilities/Crc.h"
#include "Engine/Utilities/StringConverter.h"
@@ -3107,6 +3109,30 @@ void Variant::AllocStructure()
AsBlob.Data = Allocator::Allocate(AsBlob.Length);
*((int16*)AsBlob.Data) = 0;
}
else if (const auto mclass = Scripting::FindClass(typeName))
{
// Fallback to C#-only types
MCore::AttachThread();
auto instance = mclass->CreateInstance();
if (instance)
{
#if 0
void* data = mono_object_unbox(instance);
int32 instanceSize = mono_class_instance_size(mclass->GetNative());
AsBlob.Length = instanceSize - (int32)((uintptr)data - (uintptr)instance);
AsBlob.Data = Allocator::Allocate(AsBlob.Length);
Platform::MemoryCopy(AsBlob.Data, data, AsBlob.Length);
#else
Type.Type = VariantType::ManagedObject;
AsUint = mono_gchandle_new(instance, true);
#endif
}
else
{
AsBlob.Data = nullptr;
AsBlob.Length = 0;
}
}
else
{
if (typeName.Length() != 0)

View File

@@ -237,7 +237,7 @@ VariantType MUtils::UnboxVariantType(MonoType* monoType)
// TODO: support any structure unboxing
// TODO: unbox other types as generic ManagedObject type
LOG(Error, "Invalid managed type to unbox {0}.{1}", String(mono_class_get_namespace(klass)), String(mono_class_get_name(klass)));
LOG(Error, "Invalid managed type to unbox {0}", String(GetClassFullname(klass)));
return VariantType();
}
@@ -342,6 +342,7 @@ Variant MUtils::UnboxVariant(MonoObject* value)
type.Struct.Unbox(v.AsBlob.Data, value);
return v;
}
return Variant(value);
}
if (klass == mono_array_class_get(mono_get_byte_class(), 1))
{