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());
}
@@ -13,6 +13,7 @@ namespace FlaxEngine.Json.JsonCustomSerializers
internal class ExtendedDefaultContractResolver : DefaultContractResolver
{
private readonly Type _flaxType = typeof(Object);
private static readonly JsonConverter InterfaceObjectReferenceConverterInstance = new InterfaceObjectReferenceConverter();
private readonly Type[] AttributesIgnoreList =
{
@@ -34,6 +35,88 @@ namespace FlaxEngine.Json.JsonCustomSerializers
_attributesIgnoreList = isManagedOnly ? AttributesIgnoreListManaged : AttributesIgnoreList;
}
private static bool HasObjectInterfaceReferenceAttribute(IEnumerable<Attribute> attributes)
{
return attributes.Any(x => x is ScriptingObjectInterfaceReferenceAttribute || x is SoftObjectInterfaceReferenceAttribute);
}
private static Type GetCollectionItemType(Type type)
{
if (type.IsArray)
return type.GetElementType();
if (!type.IsGenericType || type == typeof(string))
return null;
var types = type.GetInterfaces().Concat(new[] { type });
var dictionaryType = types.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary<,>));
if (dictionaryType != null)
return dictionaryType.GetGenericArguments()[1];
var enumerableType = types.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>));
return enumerableType?.GetGenericArguments()[0];
}
private static void SetupInterfaceObjectReferenceItems(JsonContainerContract contract, Type itemType)
{
if (itemType?.IsInterface == true)
{
contract.ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize;
contract.ItemConverter = InterfaceObjectReferenceConverterInstance;
}
}
private void SetupObjectReferenceProperty(JsonProperty jsonProperty, Type type, IEnumerable<Attribute> attributes)
{
var hasObjectInterfaceReferenceAttribute = HasObjectInterfaceReferenceAttribute(attributes);
if (_flaxType.IsAssignableFrom(type) || (type.IsInterface && hasObjectInterfaceReferenceAttribute))
{
jsonProperty.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
jsonProperty.Converter = JsonSerializer.ObjectConverter;
}
if (hasObjectInterfaceReferenceAttribute && GetCollectionItemType(type)?.IsInterface == true)
{
jsonProperty.ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize;
jsonProperty.ItemConverter = JsonSerializer.ObjectConverter;
}
}
private sealed class InterfaceObjectReferenceConverter : JsonConverter
{
public override unsafe void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
if (value is Object obj)
{
var id = obj.ID;
writer.WriteValue(JsonSerializer.GetStringID(&id));
}
else if (value == null)
{
writer.WriteNull();
}
else
{
serializer.Serialize(writer, value, value.GetType());
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String && JsonSerializer.TryParseID((string)reader.Value, out var id))
{
return Object.Find(ref id, objectType, true);
}
if (reader.TokenType == JsonToken.Null)
return null;
// objectType is the same interface item type that selected this converter. Passing it back to
// Newtonsoft can cause this converter to be chosen again and recurse until the stack overflows.
return Newtonsoft.Json.Linq.JToken.Load(reader).ToObject<object>(serializer);
}
public override bool CanConvert(Type objectType)
{
return objectType.IsInterface;
}
}
/// <inheritdoc />
protected override JsonContract CreateContract(Type objectType)
{
@@ -55,11 +138,23 @@ namespace FlaxEngine.Json.JsonCustomSerializers
return contract;
}
/// <inheritdoc />
protected override JsonArrayContract CreateArrayContract(Type objectType)
{
var contract = base.CreateArrayContract(objectType);
SetupInterfaceObjectReferenceItems(contract, contract.CollectionItemType);
return contract;
}
/// <inheritdoc />
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
var contract = base.CreateDictionaryContract(objectType);
SetupInterfaceObjectReferenceItems(contract, contract.DictionaryValueType);
// Override contract to save enums keys as integer
var keyType = contract.DictionaryKeyType;
if ((keyType?.IsEnum ?? false) && keyType.GetCustomAttribute<EnumStringAttribute>() == null)
@@ -116,11 +211,7 @@ namespace FlaxEngine.Json.JsonCustomSerializers
jsonProperty.Writable = true;
jsonProperty.Readable = true;
if (_flaxType.IsAssignableFrom(f.FieldType))
{
jsonProperty.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
jsonProperty.Converter = JsonSerializer.ObjectConverter;
}
SetupObjectReferenceProperty(jsonProperty, f.FieldType, attributes);
result.Add(jsonProperty);
}
@@ -159,11 +250,7 @@ namespace FlaxEngine.Json.JsonCustomSerializers
jsonProperty.Writable = true;
jsonProperty.Readable = !isObsolete;
if (_flaxType.IsAssignableFrom(p.PropertyType))
{
jsonProperty.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
jsonProperty.Converter = JsonSerializer.ObjectConverter;
}
SetupObjectReferenceProperty(jsonProperty, p.PropertyType, attributes);
result.Add(jsonProperty);
}
+46 -56
View File
@@ -618,6 +618,31 @@ namespace FlaxEngine.Json
return id;
}
/// <summary>
/// Tries to parse the given object identifier represented in the internal serialization format.
/// </summary>
/// <param name="str">The ID string.</param>
/// <param name="id">The identifier.</param>
/// <returns>True if parsing succeeded, otherwise false.</returns>
public static unsafe bool TryParseID(string str, out Guid id)
{
id = Guid.Empty;
if (str == null || str.Length != 32)
return false;
GuidInterop g;
if (!TryParseHex(str, 0, 8, out g.A) ||
!TryParseHex(str, 8, 8, out g.B) ||
!TryParseHex(str, 16, 8, out g.C) ||
!TryParseHex(str, 24, 8, out g.D))
{
return false;
}
id = *(Guid*)&g;
return true;
}
/// <summary>
/// Parses the given object identifier represented in the internal serialization format.
/// </summary>
@@ -625,76 +650,40 @@ namespace FlaxEngine.Json
/// <param name="id">The identifier.</param>
public static unsafe void ParseID(string str, out Guid id)
{
GuidInterop g;
// Broken after VS 15.5
/*fixed (char* a = str)
{
char* b = a + 8;
char* c = b + 8;
char* d = c + 8;
ParseHex(a, 8, out g.A);
ParseHex(b, 8, out g.B);
ParseHex(c, 8, out g.C);
ParseHex(d, 8, out g.D);
}*/
// Temporary fix (not using raw char* pointer)
ParseHex(str, 0, 8, out g.A);
ParseHex(str, 8, 8, out g.B);
ParseHex(str, 16, 8, out g.C);
ParseHex(str, 24, 8, out g.D);
id = *(Guid*)&g;
TryParseID(str, out id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void ParseHex(char* str, int length, out uint result)
{
uint sum = 0;
char* p = str;
char* end = str + length;
if (*p == '0' && *(p + 1) == 'x')
p += 2;
while (p < end && *p != 0)
{
int c = *p - '0';
if (c < 0 || c > 9)
{
c = char.ToLower(*p) - 'a' + 10;
if (c < 10 || c > 15)
{
result = 0;
return;
}
}
sum = 16 * sum + (uint)c;
p++;
}
result = sum;
TryParseHex(new ReadOnlySpan<char>(str, length), out result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void ParseHex(string str, int start, int length, out uint result)
{
uint sum = 0;
int p = start;
int end = start + length;
TryParseHex(str, start, length, out result);
}
if (str.Length < end)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool TryParseHex(string str, int start, int length, out uint result)
{
if (str.Length < start + length)
{
result = 0;
return;
return false;
}
return TryParseHex(str.AsSpan(start, length), out result);
}
if (str[p] == '0' && str[p + 1] == 'x')
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool TryParseHex(ReadOnlySpan<char> str, out uint result)
{
uint sum = 0;
int p = 0;
int end = str.Length;
if (p + 1 < end && str[p] == '0' && str[p + 1] == 'x')
p += 2;
while (p < end && str[p] != 0)
@@ -707,7 +696,7 @@ namespace FlaxEngine.Json
if (c < 10 || c > 15)
{
result = 0;
return;
return false;
}
}
@@ -717,6 +706,7 @@ namespace FlaxEngine.Json
}
result = sum;
return p == end;
}
}
}
+16
View File
@@ -133,6 +133,14 @@ public:
v = ptr;
}
template<typename T>
FORCE_INLINE void Read(ScriptingObjectInterfaceReference<T>& v)
{
uint32 id[4];
ReadBytes(id, sizeof(id));
v = *(Guid*)id;
}
template<typename T>
FORCE_INLINE void Read(SoftObjectReference<T>& v)
{
@@ -141,6 +149,14 @@ public:
v.Set(*(Guid*)id);
}
template<typename T>
FORCE_INLINE void Read(SoftObjectInterfaceReference<T>& v)
{
uint32 id[4];
ReadBytes(id, sizeof(id));
v.Set(*(Guid*)id);
}
template<typename T>
FORCE_INLINE void Read(AssetReference<T>& v)
{
+49 -4
View File
@@ -14,8 +14,12 @@ struct VariantType;
template<typename T>
class ScriptingObjectReference;
template<typename T>
class ScriptingObjectInterfaceReference;
template<typename T>
class SoftObjectReference;
template<typename T>
class SoftObjectInterfaceReference;
template<typename T>
class AssetReference;
template<typename T>
class WeakAssetReference;
@@ -458,7 +462,6 @@ namespace Serialization
}
FLAXENGINE_API bool ShouldSerializeRef(const SceneObject* v, const SceneObject* other);
template<typename T>
inline typename TEnableIf<TAnd<TIsBaseOf<ScriptingObject, T>, TNot<TIsBaseOf<SceneObject, T>>>::Value, bool>::Type ShouldSerialize(const T* v, const void* otherObj)
{
@@ -474,7 +477,7 @@ namespace Serialization
{
Guid id;
Deserialize(stream, id, modifier);
modifier->IdsMapping.TryGet(id, id);
modifier->IdsMapping.TryGet(id, id);
v = (T*)::FindObject(id, T::GetStaticClass());
}
@@ -501,7 +504,28 @@ namespace Serialization
{
Guid id;
Deserialize(stream, id, modifier);
modifier->IdsMapping.TryGet(id, id);
modifier->IdsMapping.TryGet(id, id);
v = id;
}
// Scripting Interface Reference
template<typename T>
inline bool ShouldSerialize(const ScriptingObjectInterfaceReference<T>& v, const void* otherObj)
{
return !otherObj || ShouldSerializeRef(v.GetObject(), ((ScriptingObjectInterfaceReference<T>*)otherObj)->GetObject());
}
template<typename T>
inline void Serialize(ISerializable::SerializeStream& stream, const ScriptingObjectInterfaceReference<T>& v, const void* otherObj)
{
stream.Guid(v.GetID());
}
template<typename T>
inline void Deserialize(ISerializable::DeserializeStream& stream, ScriptingObjectInterfaceReference<T>& v, ISerializeModifier* modifier)
{
Guid id;
Deserialize(stream, id, modifier);
modifier->IdsMapping.TryGet(id, id);
v = id;
}
@@ -522,7 +546,28 @@ namespace Serialization
{
Guid id;
Deserialize(stream, id, modifier);
modifier->IdsMapping.TryGet(id, id);
modifier->IdsMapping.TryGet(id, id);
v = id;
}
// Soft Object Interface Reference
template<typename T>
inline bool ShouldSerialize(const SoftObjectInterfaceReference<T>& v, const void* otherObj)
{
return !otherObj || ShouldSerializeRef(v.GetObject(), ((SoftObjectInterfaceReference<T>*)otherObj)->GetObject());
}
template<typename T>
inline void Serialize(ISerializable::SerializeStream& stream, const SoftObjectInterfaceReference<T>& v, const void* otherObj)
{
stream.Guid(v.GetID());
}
template<typename T>
inline void Deserialize(ISerializable::DeserializeStream& stream, SoftObjectInterfaceReference<T>& v, ISerializeModifier* modifier)
{
Guid id;
Deserialize(stream, id, modifier);
modifier->IdsMapping.TryGet(id, id);
v = id;
}
+4
View File
@@ -17,8 +17,12 @@ class ScriptingObject;
template<typename T>
class ScriptingObjectReference;
template<typename T>
class ScriptingObjectInterfaceReference;
template<typename T>
class SoftObjectReference;
template<typename T>
class SoftObjectInterfaceReference;
template<typename T>
class AssetReference;
template<typename T>
class WeakAssetReference;
+18
View File
@@ -156,11 +156,29 @@ public:
{
Write(v.Get());
}
template<typename T>
FORCE_INLINE void Write(const ScriptingObjectInterfaceReference<T>& v)
{
uint32 id[4];
v.CopyID(id);
WriteBytes(id, sizeof(id));
}
template<typename T>
FORCE_INLINE void Write(const SoftObjectReference<T>& v)
{
Write(v.Get());
}
template<typename T>
FORCE_INLINE void Write(const SoftObjectInterfaceReference<T>& v)
{
uint32 id[4];
v.CopyID(id);
WriteBytes(id, sizeof(id));
}
template<typename T>
FORCE_INLINE void Write(const AssetReference<T>& v)
{
+6
View File
@@ -6,6 +6,8 @@
#include "Engine/Core/Math/Vector3.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Scripting/ScriptingObjectInterfaceReference.h"
#include "Engine/Scripting/SoftObjectInterfaceReference.h"
#include "Engine/Scripting/SerializableScriptingObject.h"
#include "Engine/Scripting/SoftTypeReference.h"
#include "Engine/Content/SceneReference.h"
@@ -177,6 +179,10 @@ public:
// Test struct
API_FIELD() TestStruct SimpleStruct;
// Test interface reference
API_FIELD() ScriptingObjectInterfaceReference<ITestInterface> InterfaceRef;
// Test soft interface reference
API_FIELD() SoftObjectInterfaceReference<ITestInterface> SoftInterfaceRef;
// Test event
API_EVENT() Delegate<int32, Float3, const String&, String&, TestStruct&, const Array<TestStruct>&, Array<TestStruct>&> SimpleEvent;