// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using System.Runtime.CompilerServices; namespace FlaxEngine { /// /// Json asset reference utility. References resource with a typed data type. /// /// Type of the asset instance type. #if FLAX_EDITOR [CustomEditor(typeof(FlaxEditor.CustomEditors.Editors.AssetRefEditor))] #endif public struct JsonAssetReference : IComparable, IComparable>, IEquatable> { /// /// Gets or sets the referenced asset. /// public JsonAsset Asset; /// /// Gets the instance of the serialized object from the json asset data. Cached internally. /// public T Instance => (T)Asset?.Instance; /// /// Initializes a new instance of the structure. /// /// The Json Asset. public JsonAssetReference(JsonAsset asset) { Asset = asset; } /// /// Implicit cast operator. /// public static implicit operator JsonAsset(JsonAssetReference value) { return value.Asset; } /// /// Implicit cast operator. /// public static implicit operator IntPtr(JsonAssetReference value) { return Object.GetUnmanagedPtr(value.Asset); } /// /// Implicit cast operator. /// public static implicit operator JsonAssetReference(JsonAsset value) { return new JsonAssetReference(value); } /// /// Implicit cast operator. /// public static implicit operator JsonAssetReference(IntPtr valuePtr) { 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 bool Equals(JsonAssetReference other) { return Asset == other.Asset; } /// public int CompareTo(JsonAssetReference other) { return Object.GetUnmanagedPtr(Asset).CompareTo(Object.GetUnmanagedPtr(other.Asset)); } /// public override bool Equals(object obj) { return obj is JsonAssetReference other && Asset == other.Asset; } /// public override string ToString() { return Asset?.ToString(); } /// public int CompareTo(object obj) { return obj is JsonAssetReference other ? CompareTo(other) : 1; } /// public override int GetHashCode() { return (Asset != null ? Asset.GetHashCode() : 0); } } }