Add ScriptingObjectReference for Read/WriteStream and scripting interop as an array

This commit is contained in:
Wojciech Figat
2022-11-09 10:48:22 +01:00
parent c41d67c4ac
commit c0f596a00e
4 changed files with 53 additions and 11 deletions

View File

@@ -257,6 +257,38 @@ 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>>
{
MonoObject* Box(const ScriptingObjectReference<T>& data, MonoClass* klass)
{
return data.GetManagedInstance();
}
void Unbox(ScriptingObjectReference<T>& result, MonoObject* data)
{
result = (T*)ScriptingObject::ToNative(data);
}
void ToManagedArray(MonoArray* result, const Span<ScriptingObjectReference<T>>& data)
{
for (int32 i = 0; i < data.Length(); i++)
mono_array_setref(result, i, data[i].GetManagedInstance());
}
template<typename AllocationType = HeapAllocation>
void ToNativeArray(Array<ScriptingObjectReference<T>, AllocationType>& result, MonoArray* data, int32 length)
{
result.Resize(length);
for (int32 i = 0; i < length; i++)
result[i] = (T*)ScriptingObject::ToNative(mono_array_get(data, MonoObject*, i));
}
};
// Converter for Asset References.
template<typename T>
class AssetReference;

View File

@@ -5,10 +5,6 @@
#include "Stream.h"
#include "Engine/Core/Templates.h"
struct CommonValue;
struct Variant;
struct VariantType;
class ISerializable;
extern FLAXENGINE_API class ScriptingObject* FindObject(const Guid& id, class MClass* type);
/// <summary>
@@ -156,6 +152,14 @@ public:
data = (T*)::FindObject(*(Guid*)id, T::GetStaticClass());
}
template<typename T>
FORCE_INLINE void Read(ScriptingObjectReference<T>& v)
{
T* ptr;
Read(ptr);
v = ptr;
}
/// <summary>
/// Read data array
/// </summary>

View File

@@ -8,9 +8,15 @@
#define FILESTREAM_BUFFER_SIZE 4096
#define STREAM_MAX_STRING_LENGTH (4*1024) // 4 kB
// Forward declarations
class ReadStream;
class WriteStream;
struct CommonValue;
struct Variant;
struct VariantType;
class ISerializable;
class ScriptingObject;
template<typename T>
class ScriptingObjectReference;
/// <summary>
/// Base class for all data streams (memory streams, file streams etc.)

View File

@@ -5,12 +5,6 @@
#include "Stream.h"
#include "Engine/Core/Templates.h"
struct CommonValue;
struct Variant;
struct VariantType;
class ISerializable;
class ScriptingObject;
/// <summary>
/// Base class for all data write streams
/// </summary>
@@ -177,6 +171,12 @@ public:
WriteBytes(id, sizeof(id));
}
template<typename T>
FORCE_INLINE void Write(const ScriptingObjectReference<T>& v)
{
Write(v.Get());
}
template<typename T, typename AllocationType = HeapAllocation>
void Write(const Array<T, AllocationType>& data)
{