From b5e23f0096a6438659f362bce115a00101bb80d5 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 15 Feb 2024 11:46:17 +0100 Subject: [PATCH] Improve JsonAssetReference --- Source/Engine/AI/BehaviorKnowledgeSelector.cs | 4 +- Source/Engine/Content/JsonAssetReference.cs | 38 ++++++++++++++++--- Source/Engine/Content/JsonAssetReference.h | 9 +++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Source/Engine/AI/BehaviorKnowledgeSelector.cs b/Source/Engine/AI/BehaviorKnowledgeSelector.cs index 5c642e92a..cdcaf6c40 100644 --- a/Source/Engine/AI/BehaviorKnowledgeSelector.cs +++ b/Source/Engine/AI/BehaviorKnowledgeSelector.cs @@ -146,7 +146,7 @@ namespace FlaxEngine public string Path; /// - /// Initializes a new instance of the structure. + /// Initializes a new instance of the structure. /// /// The selector path. public BehaviorKnowledgeSelector(string path) @@ -155,7 +155,7 @@ namespace FlaxEngine } /// - /// Initializes a new instance of the structure. + /// Initializes a new instance of the structure. /// /// The other selector. public BehaviorKnowledgeSelector(BehaviorKnowledgeSelectorAny other) diff --git a/Source/Engine/Content/JsonAssetReference.cs b/Source/Engine/Content/JsonAssetReference.cs index adfaa5179..7c8a8902f 100644 --- a/Source/Engine/Content/JsonAssetReference.cs +++ b/Source/Engine/Content/JsonAssetReference.cs @@ -1,6 +1,7 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; +using System.Runtime.CompilerServices; namespace FlaxEngine { @@ -19,13 +20,9 @@ namespace FlaxEngine public JsonAsset Asset; /// - /// Gets the instance of the Json Asset. Null if unset. + /// Gets the instance of the serialized object from the json asset data. Cached internally. /// - /// instance of the Json Asset or null if unset. - public T Get() - { - return (T)Asset?.Instance; - } + public T Instance => (T)Asset?.Instance; /// /// Initializes a new instance of the structure. @@ -68,6 +65,35 @@ namespace FlaxEngine return new JsonAssetReference(Object.FromUnmanagedPtr(valuePtr) as JsonAsset); } + /// + /// Checks if the object exists (reference is not null and the unmanaged object pointer is valid). + /// + /// The object to check. + /// True if object is valid, otherwise false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator bool(JsonAssetReference obj) + { + return obj.Asset; + } + + /// + /// Checks whether the two objects are equal. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(JsonAssetReference left, JsonAssetReference right) + { + return left.Asset == right.Asset; + } + + /// + /// Checks whether the two objects are not equal. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(JsonAssetReference left, JsonAssetReference right) + { + return left.Asset != right.Asset; + } + /// public override string ToString() { diff --git a/Source/Engine/Content/JsonAssetReference.h b/Source/Engine/Content/JsonAssetReference.h index 82fbe47f1..d84c44926 100644 --- a/Source/Engine/Content/JsonAssetReference.h +++ b/Source/Engine/Content/JsonAssetReference.h @@ -12,6 +12,15 @@ template API_STRUCT(NoDefault, Template, MarshalAs=JsonAsset*) struct JsonAssetReference : AssetReference { + /// + /// Gets the deserialized native object instance of the given type. Returns null if asset is not loaded or loaded object has different type. + /// + /// The asset instance object or null. + FORCE_INLINE T* GetInstance() const + { + return _asset ? Get()->GetInstance() : nullptr; + } + JsonAssetReference& operator=(JsonAsset* asset) noexcept { OnSet(asset);