2222namespace yup
2323{
2424
25+ namespace
26+ {
27+
28+ // ==============================================================================
29+
30+ class LambdaAssetLoader : public rive ::FileAssetLoader
31+ {
32+ public:
33+ LambdaAssetLoader (const ArtboardFile::AssetLoadCallback& assetCallback)
34+ : assetCallback (assetCallback)
35+ {
36+ }
37+
38+ bool loadContents (rive::FileAsset& asset,
39+ rive::Span<const uint8_t > inBandBytes,
40+ rive::Factory* factory) override
41+ {
42+ jassert (factory != nullptr );
43+
44+ ArtboardFile::AssetInfo assetInfo;
45+ assetInfo.uniqueName = String (asset.uniqueName ());
46+ assetInfo.uniquePath = String (asset.uniqueFilename ());
47+ assetInfo.extension = String (asset.fileExtension ());
48+
49+ return assetCallback (assetInfo, Span<const uint8>{ inBandBytes.data (), inBandBytes.size () }, *factory);
50+ }
51+
52+ private:
53+ ArtboardFile::AssetLoadCallback assetCallback;
54+ };
55+
56+ } // namespace
57+
2558// ==============================================================================
2659
2760ArtboardFile::ArtboardFile (std::unique_ptr<rive::File> rivFile)
@@ -44,6 +77,11 @@ rive::File* ArtboardFile::getRiveFile()
4477// ==============================================================================
4578
4679ArtboardFile::LoadResult ArtboardFile::load (const File& file, rive::Factory& factory)
80+ {
81+ return load (file, factory, nullptr );
82+ }
83+
84+ ArtboardFile::LoadResult ArtboardFile::load (const File& file, rive::Factory& factory, const AssetLoadCallback& assetCallback)
4785{
4886 if (! file.existsAsFile ())
4987 return LoadResult::fail (" Failed to find artboard file to load" );
@@ -52,19 +90,39 @@ ArtboardFile::LoadResult ArtboardFile::load (const File& file, rive::Factory& fa
5290 if (is == nullptr || ! is->openedOk ())
5391 return LoadResult::fail (" Failed to open artboard file for reading" );
5492
55- return load (*is, factory);
93+ return load (*is, factory, assetCallback );
5694}
5795
96+ // ==============================================================================
97+
5898ArtboardFile::LoadResult ArtboardFile::load (InputStream& is, rive::Factory& factory)
99+ {
100+ return load (is, factory, nullptr );
101+ }
102+
103+ ArtboardFile::LoadResult ArtboardFile::load (InputStream& is, rive::Factory& factory, const AssetLoadCallback& assetCallback)
59104{
60105 yup::MemoryBlock mb;
61106 is.readIntoMemoryBlock (mb);
62107
63108 rive::ImportResult result;
64- auto rivFile = rive::File::import (
65- { static_cast <const uint8_t *> (mb.getData ()), mb.getSize () },
66- std::addressof (factory),
67- std::addressof (result));
109+ std::unique_ptr<rive::File> rivFile;
110+
111+ if (assetCallback != nullptr )
112+ {
113+ rivFile = rive::File::import (
114+ { static_cast <const uint8_t *> (mb.getData ()), mb.getSize () },
115+ std::addressof (factory),
116+ std::addressof (result),
117+ rive::make_rcp<LambdaAssetLoader> (assetCallback));
118+ }
119+ else
120+ {
121+ rivFile = rive::File::import (
122+ { static_cast <const uint8_t *> (mb.getData ()), mb.getSize () },
123+ std::addressof (factory),
124+ std::addressof (result));
125+ }
68126
69127 if (result == rive::ImportResult::malformed)
70128 return LoadResult::fail (" Malformed artboard file" );
0 commit comments