using UnityEngine;
using System;
using System.Collections.Generic;
namespace Rive
{
///
/// Interface for out-of-band assets.
///
public interface IOutOfBandAsset
{
void Load();
void Unload();
}
///
/// Represents an out-of-band Rive asset.
///
/// Out-of-band assets are assets that are referenced in a Rive asset, but are not
/// part of the Rive asset itself. For example, images and fonts can be marked as
/// referenced and linked separately.
///
public abstract class OutOfBandAsset : ScriptableObject, IOutOfBandAsset
{
[HideInInspector]
[SerializeField]
private byte[] bytes;
[NonSerialized]
private int m_refCount;
[NonSerialized]
private IntPtr m_nativeAsset = IntPtr.Zero;
protected abstract IntPtr LoadNative(byte[] bytes);
protected abstract void UnloadNative(IntPtr nativePtr);
internal IntPtr NativeAsset { get { return m_nativeAsset; } }
///
/// The raw bytes of the out-of-band asset.
///
public byte[] Bytes { get { return bytes; } }
///
/// Create an out-of-band asset instance from the given bytes.
///
///
///
/// The created out-of-band asset instance.
public static T Create(byte[] bytes) where T : OutOfBandAsset
{
var asset = ScriptableObject.CreateInstance();
asset.Init(bytes);
return asset;
}
private void Init(byte[] bytes)
{
this.bytes = bytes;
}
internal void LoadIntoByteAssetMap(uint embeddedAssetId, EmbeddedAssetType embeddedAssetType, List assetMap)
{
// Write it into the asset map if the native asset was succesfully loaded.
if (NativeAsset != IntPtr.Zero)
{
var bytes = BitConverter.GetBytes(embeddedAssetId);
for (int j = 0; j < bytes.Length; j++)
{
assetMap.Add(bytes[j]);
}
bytes = BitConverter.GetBytes((ushort)embeddedAssetType);
for (int j = 0; j < bytes.Length; j++)
{
assetMap.Add(bytes[j]);
}
#if UNITY_WEBGL && !UNITY_EDITOR
// nint is incorrectly reported as 64 bit on wasm which would
// break Rive's native code.
bytes = BitConverter.GetBytes((int)NativeAsset);
#else
bytes = BitConverter.GetBytes((nint)NativeAsset);
#endif
for (int j = 0; j < bytes.Length; j++)
{
assetMap.Add(bytes[j]);
}
}
}
///
/// Load the out-of-band asset. Call this before using the asset.
///
public void Load()
{
if (m_refCount == 0)
{
m_nativeAsset = LoadNative(bytes);
}
m_refCount++;
}
///
/// Unload the out-of-band asset. This allows the engine to clean it up when it is not used by any more animations.
///
public void Unload()
{
m_refCount--;
if (m_refCount == 0)
{
IntPtr nativeAsset = m_nativeAsset;
m_nativeAsset = IntPtr.Zero;
UnloadNative(nativeAsset);
}
}
///
/// Check the reference count of the out-of-band asset.
///
///
internal int RefCount()
{
return m_refCount;
}
}
}