libraries: serialize library artboards as local artboards

Fixes https://github.com/orgs/rive-app/projects/15/views/1?pane=issue&itemId=104718681&issue=rive-app%7Crive%7C9356

Fixes https://github.com/rive-app/rive/issues/9324#issue-2966381868
### Overview
Currently we do not export LibraryArtboards to runtime, as C++ won't understand them. As such, currently, if you have ArtboardX that contains  NestedArtboard_ofALibraryArtboard, a NestedArtboard that points to ArtboardX won't work.

This PR makes it such that a NestedArtboard to ArtboardX would show. Moreover, its statemachine will play too.

### There are a few main changes
1. LibraryAsset: cached library artboards in LibraryAsset are referenced by ID instead of by name. This ID is the original artboard's ID in the library asset file, and is persisted in our schema as LibraryArtboard.artboardId. Same same with caching statemachines and linear animations. I've added LibraryStateMachine.animationId and LibraryLinearAnimation.animationId.
2. NestedArtboard: before this PR, there was logic that checks whether the source artboard was a LibraryArtboard or just an Artboard. Now, it's just Artboard, because the runtime exporter only exports Artboards.
3. Runtime exporter: I've created the idea of MappedContext, which maps a library identifier to its various remaps, like globalIndexMapper, nestedIds, eventIds, etc.

### The mapping
NestedArtboard.artboardId -> LibraryArtboard.id
LibraryArtboard.artboardId -> LibraryAsset's rev file's Artboard.id

NestedStateMachine.animationId -> LibraryStateMachine.id
LibraryStateMachine.animationId -> LibraryAsset's rev file's State Machine.id

### Order of export
Currently, when we export we only go over the host file, and the order looks like
Backboard_ofHost
FileAssets_ofHost
DataModels_ofHost
Artboard1_ofHost
Artboard1_components_ofHost (e.g. paints, nested artboards, state machines, etc.)

Now, I'm recursively also exporting the library files. So the order looks like
Backboard_ofHost
[XXX]
  **--FileAssets_ofLibrary
  --DataModels_ofLibrary
  --Artboard1_ofLibrary
  --Artboard1_components_ofLibrary**
FileAssets_ofHost
DataModels_ofHost
Artboard1_ofHost
Artboard1_components_ofHost

Basically, we insert the indented section, which represents the library file. And if Library uses other library assets, it should go into the [XXX] section. Tho we currently don't support this, as we don't decode library assets used from a library asset.
If we aggregate all the artboards, [0, total count) is the range of valid artboard indices in C++.

### Tests
I'll fix the TODO's in the tests

Diffs=
e75974b896 libraries: serialize library artboards as local artboards (#9419)

Co-authored-by: Susan Wang <susan@rive.app>
diff --git a/.rive_head b/.rive_head
index 11648ec..9f3326b 100644
--- a/.rive_head
+++ b/.rive_head
@@ -1 +1 @@
-689a455e677b68ac54ce7af3bbc6bd28b617a7cc
+e75974b8969006de706c0da14875e26efa6d1886
diff --git a/include/rive/nested_artboard.hpp b/include/rive/nested_artboard.hpp
index e1e12c7..bf44957 100644
--- a/include/rive/nested_artboard.hpp
+++ b/include/rive/nested_artboard.hpp
@@ -37,6 +37,7 @@
 
     void nest(Artboard* artboard);
     ArtboardInstance* artboardInstance() { return m_Instance.get(); }
+    Artboard* sourceArtboard() { return m_Artboard; }
 
     StatusCode import(ImportStack& importStack) override;
     Core* clone() const override;
@@ -78,4 +79,4 @@
 };
 } // namespace rive
 
-#endif
\ No newline at end of file
+#endif
diff --git a/renderer/path_fiddle/path_fiddle.cpp b/renderer/path_fiddle/path_fiddle.cpp
index 857f554..ff6c2b4 100644
--- a/renderer/path_fiddle/path_fiddle.cpp
+++ b/renderer/path_fiddle/path_fiddle.cpp
@@ -101,6 +101,8 @@
     clear_scenes();
     for (size_t i = 0; i < count; ++i)
     {
+        // Tip: you can change the artboard shown here
+        // auto artboard = rivFile->artboardAt(2);
         auto artboard = rivFile->artboardDefault();
         std::unique_ptr<Scene> scene;
         if (stateMachine >= 0)
diff --git a/src/nested_artboard.cpp b/src/nested_artboard.cpp
index 222d94b..434e1b9 100644
--- a/src/nested_artboard.cpp
+++ b/src/nested_artboard.cpp
@@ -36,6 +36,7 @@
     {
         // We're just marking the source artboard so we can later instance from
         // it. No need to advance it or change any of its properties.
+        // E.g. at import time, we return here.
         return;
     }
     m_Artboard->frameOrigin(false);
@@ -377,4 +378,4 @@
     }
 
     return keepGoing;
-}
\ No newline at end of file
+}
diff --git a/tests/unit_tests/assets/double_library_with_image.riv b/tests/unit_tests/assets/double_library_with_image.riv
index a084c45..2717f39 100644
--- a/tests/unit_tests/assets/double_library_with_image.riv
+++ b/tests/unit_tests/assets/double_library_with_image.riv
Binary files differ
diff --git a/tests/unit_tests/assets/library_export_animation_test.riv b/tests/unit_tests/assets/library_export_animation_test.riv
index 2ff7114..668e9b2 100644
--- a/tests/unit_tests/assets/library_export_animation_test.riv
+++ b/tests/unit_tests/assets/library_export_animation_test.riv
Binary files differ
diff --git a/tests/unit_tests/assets/library_export_state_machine_test.riv b/tests/unit_tests/assets/library_export_state_machine_test.riv
index bc7bcaf..08fdedf 100644
--- a/tests/unit_tests/assets/library_export_state_machine_test.riv
+++ b/tests/unit_tests/assets/library_export_state_machine_test.riv
Binary files differ
diff --git a/tests/unit_tests/assets/library_export_test.riv b/tests/unit_tests/assets/library_export_test.riv
index fbb3eff..11d9250 100644
--- a/tests/unit_tests/assets/library_export_test.riv
+++ b/tests/unit_tests/assets/library_export_test.riv
Binary files differ
diff --git a/tests/unit_tests/assets/library_with_image.riv b/tests/unit_tests/assets/library_with_image.riv
index dbf32c7..bb0d095 100644
--- a/tests/unit_tests/assets/library_with_image.riv
+++ b/tests/unit_tests/assets/library_with_image.riv
Binary files differ
diff --git a/tests/unit_tests/runtime/library_asset_test.cpp b/tests/unit_tests/runtime/library_asset_test.cpp
index 8649f9c..bf3d951 100644
--- a/tests/unit_tests/runtime/library_asset_test.cpp
+++ b/tests/unit_tests/runtime/library_asset_test.cpp
@@ -8,6 +8,7 @@
 #include <rive/animation/nested_simple_animation.hpp>
 #include <rive/animation/nested_state_machine.hpp>
 #include <rive/relative_local_asset_loader.hpp>
+#include <rive/shapes/image.hpp>
 #include <utils/no_op_factory.hpp>
 #include <utils/no_op_renderer.hpp>
 #include "rive_file_reader.hpp"
@@ -16,78 +17,89 @@
 
 TEST_CASE("File with library artboard loads", "[libraries]")
 {
-    // created by test in rive_core.
+    // created by library_import_export_test.dart in rive_core.
     auto file = ReadRiveFile("assets/library_export_test.riv");
 
+    // Libraries are always exported before host files.
+    // In this file, the first artboard (id=0) is the Artboard in the library
+    // file. The nested artboard that instances it is in the second Artboard.
     auto nestedArtboard =
-        file->artboard()->find<rive::NestedArtboard>("The nested artboard");
+        file->artboard(1)->find<rive::NestedArtboard>("The nested artboard");
     REQUIRE(nestedArtboard != nullptr);
     REQUIRE(nestedArtboard->name() == "The nested artboard");
     REQUIRE(nestedArtboard->x() == 1);
     REQUIRE(nestedArtboard->y() == 2);
-    REQUIRE(nestedArtboard->artboardId() == -1);
+    REQUIRE(nestedArtboard->artboardId() == 0);
 
-    // time to find the library asset somewhere
+    auto artboard = file->artboard(0);
+    REQUIRE(artboard->name() == "Rocket");
+    REQUIRE(artboard->width() == 512);
+    REQUIRE(artboard->height() == 513);
+
+    // The library asset is not exported to runtime.
     auto assets = file->assets();
     REQUIRE(assets.size() == 0);
-    // todo assert nested artboard structure
 }
 
 TEST_CASE("File with library animation loads", "[libraries]")
 {
-    // created by test in rive_core.
+    // created by library_import_export_test.dart in rive_core.
     auto file = ReadRiveFile("assets/library_export_animation_test.riv");
 
     auto nestedArtboard =
-        file->artboard()->find<rive::NestedArtboard>("The nested artboard");
+        file->artboard(1)->find<rive::NestedArtboard>("The nested artboard");
     REQUIRE(nestedArtboard != nullptr);
     REQUIRE(nestedArtboard->name() == "The nested artboard");
     REQUIRE(nestedArtboard->x() == 1);
     REQUIRE(nestedArtboard->y() == 2);
-    REQUIRE(nestedArtboard->artboardId() == -1);
 
-    auto nestedSimpleAnimation =
-        file->artboard()->find<rive::NestedSimpleAnimation>("");
-    REQUIRE(nestedSimpleAnimation != nullptr);
+    REQUIRE(nestedArtboard->artboardId() == 0);
+    REQUIRE(file->artboard(0)->animationCount() == 1);
+    auto simpleAnimation = file->artboard(0)->firstAnimation();
+    REQUIRE(simpleAnimation->name() == "LA Rocket");
+
+    auto nestedAnimations = nestedArtboard->nestedAnimations();
+    REQUIRE(nestedAnimations.size() == 1);
+    auto nestedSimpleAnimation = nestedAnimations[0];
     REQUIRE(nestedSimpleAnimation->name() == "");
-    REQUIRE(nestedSimpleAnimation->isPlaying() == true);
-    // // TODO: figure out what the id should be?
-    REQUIRE(nestedSimpleAnimation->animationId() == -1);
 
-    // // time to find the library asset somewhere
+    REQUIRE(nestedSimpleAnimation->animationId() == 0);
+    REQUIRE(nestedArtboard->nestedAnimations().size() == 1);
+
+    // The library asset is not exported to runtime.
     auto assets = file->assets();
     REQUIRE(assets.size() == 0);
-
-    REQUIRE(nestedArtboard->nestedAnimations().size() == 1);
-    // todo assert nested artboard has animation structure
 }
 
 TEST_CASE("File with library state machine loads", "[libraries]")
 {
-    // created by test in rive_core.
+    // created by library_import_export_test.dart in rive_core.
     auto file = ReadRiveFile("assets/library_export_state_machine_test.riv");
 
     auto nestedArtboard =
-        file->artboard()->find<rive::NestedArtboard>("The nested artboard");
+        file->artboard(1)->find<rive::NestedArtboard>("The nested artboard");
     REQUIRE(nestedArtboard != nullptr);
     REQUIRE(nestedArtboard->name() == "The nested artboard");
     REQUIRE(nestedArtboard->x() == 1);
     REQUIRE(nestedArtboard->y() == 2);
-    REQUIRE(nestedArtboard->artboardId() == -1);
 
-    auto nestedSimpleAnimation =
-        file->artboard()->find<rive::NestedStateMachine>("");
-    REQUIRE(nestedSimpleAnimation != nullptr);
-    REQUIRE(nestedSimpleAnimation->name() == "");
-    REQUIRE(nestedSimpleAnimation->animationId() == -1);
+    REQUIRE(nestedArtboard->artboardId() == 0);
+    REQUIRE(file->artboard(0)->stateMachineCount() == 1);
+    auto stateMachine = file->artboard(0)->firstStateMachine();
+    REQUIRE(stateMachine->name() == "SM Rocket");
+
+    auto nestedAnimations = nestedArtboard->nestedAnimations();
+    REQUIRE(nestedAnimations.size() == 1);
+    auto nestedStateMachine = nestedAnimations[0];
+    REQUIRE(nestedStateMachine->name() == "");
+
+    REQUIRE(nestedStateMachine->animationId() == 0);
+    REQUIRE(nestedArtboard->nestedAnimations().size() == 1);
 
     // time to find the library asset somewhere
     // make sure its loaded?
     auto assets = file->assets();
     REQUIRE(assets.size() == 0);
-
-    REQUIRE(nestedArtboard->nestedAnimations().size() == 1);
-    // todo assert nested artboard has state machine structure
 }
 
 // TODO reinstate, but right now we're missing fonts locally, need to get
@@ -129,22 +141,51 @@
 // we are literally just loading files here.
 TEST_CASE("File with library including image", "[libraries]")
 {
-    // TODO: create this file in the editor tests.
+    // created by library_import_export_test.dart in rive_core.
     auto file = ReadRiveFile("assets/library_with_image.riv");
 
-    // library, image and text
     auto assets = file->assets();
-    REQUIRE(assets.size() == 0);
-    // TODO: asset image
+    REQUIRE(assets.size() == 1);
+
+    auto nestedArtboard =
+        file->artboard(1)->find<rive::NestedArtboard>("The instance");
+    REQUIRE(nestedArtboard != nullptr);
+
+    auto sourceArtboard = nestedArtboard->sourceArtboard();
+    REQUIRE(sourceArtboard != nullptr);
+
+    auto images = sourceArtboard->find<rive::Image>();
+    REQUIRE(images.size() == 1);
+    REQUIRE(images[0]->assetId() == 0);
+    REQUIRE(images[0]->imageAsset() != nullptr);
 }
 
 TEST_CASE("File with multiple libraries including image", "[libraries]")
 {
-    // TODO: create this file in the editor tests.
+    // created by library_import_export_test.dart in rive_core.
     auto file = ReadRiveFile("assets/double_library_with_image.riv");
 
-    // library, image and text
     auto assets = file->assets();
-    REQUIRE(assets.size() == 0);
-    // TODO: asset multiple images
+    REQUIRE(assets.size() == 2);
+
+    auto nestedArtboard1 =
+        file->artboard(2)->find<rive::NestedArtboard>("The nested artboard");
+    auto nestedArtboard2 = file->artboard(2)->find<rive::NestedArtboard>(
+        "Another nested artboard");
+    REQUIRE(nestedArtboard1 != nullptr);
+    REQUIRE(nestedArtboard2 != nullptr);
+
+    auto sourceArtboard1 = nestedArtboard1->sourceArtboard();
+    REQUIRE(sourceArtboard1 != nullptr);
+    auto images1 = sourceArtboard1->find<rive::Image>();
+    REQUIRE(images1.size() == 1);
+    REQUIRE(images1[0]->imageAsset() != nullptr);
+    REQUIRE(images1[0]->imageAsset()->name() == "MyFirstImageAsset");
+
+    auto sourceArtboard2 = nestedArtboard2->sourceArtboard();
+    REQUIRE(sourceArtboard2 != nullptr);
+    auto images2 = sourceArtboard2->find<rive::Image>();
+    REQUIRE(images2.size() == 1);
+    REQUIRE(images2[0]->imageAsset() != nullptr);
+    REQUIRE(images2[0]->imageAsset()->name() == "MyOtherImageAsset");
 }