diff --git a/Cargo.lock b/Cargo.lock index 6f4ac730c9..d16817c1af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2436,6 +2436,8 @@ dependencies = [ "kurbo", "log", "math-parser", + "migration-formats", + "migration-nodes", "node-macro", "num_enum", "once_cell", diff --git a/editor/Cargo.toml b/editor/Cargo.toml index 2cb9e8b5a5..7e1e743675 100644 --- a/editor/Cargo.toml +++ b/editor/Cargo.toml @@ -22,7 +22,9 @@ gpu = ["interpreted-executor/gpu", "dep:wgpu-executor"] # Local dependencies graphite-proc-macros = { workspace = true } graph-craft = { workspace = true } -document-format = { workspace = true } +document-format = { workspace = true, features = ["migrations"] } +migration-formats = { workspace = true } +migration-nodes = { workspace = true } document-graph-storage = { workspace = true, features = ["conversion"] } document-container = { workspace = true } graphene-hash = { workspace = true } diff --git a/editor/src/lib.rs b/editor/src/lib.rs index 1a1cb35c32..ba892308bc 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -23,3 +23,7 @@ pub mod utility_traits; pub mod utility_types; pub use document_container::store as document_store; + +// Referenced so the linker keeps their self-registering migrations, which run as documents open +use migration_formats as _; +use migration_nodes as _; diff --git a/editor/src/messages/portfolio/document_storage_io.rs b/editor/src/messages/portfolio/document_storage_io.rs index 1781c14877..5784c59838 100644 --- a/editor/src/messages/portfolio/document_storage_io.rs +++ b/editor/src/messages/portfolio/document_storage_io.rs @@ -241,27 +241,38 @@ async fn open_storage_or_fallback_to_legacy(container: AnyContainer, document_id /// Decoded proto-node declarations for every node `gdd` or its history references, from the global cache. async fn load_declarations(gdd: Option<&GddV1>, resources: &ResourcesHandle) -> Declarations { - match gdd { - Some(gdd) => gdd.declarations(resources).await, - None => Declarations::new(), + let Some(gdd) = gdd else { return Declarations::new() }; + + // Migrations store the declarations they introduce in the container, which the cache hasn't seen yet + if gdd.migrated() { + hydrate_resources(gdd, resources).await; + } + gdd.declarations(resources).await +} + +/// Copy every resource stored in `gdd`'s container into the global cache. +async fn hydrate_resources(gdd: &GddV1, byte_store: &ResourcesHandle) { + let hashes = match gdd.resource_hashes().await { + Ok(hashes) => hashes, + Err(error) => { + log::error!("Listing the resources of a document failed: {error}"); + return; + } + }; + + for hash in hashes { + match gdd.read_resource(&hash).await { + Ok(holder) => { + byte_store.store(holder.as_slice()); + } + Err(error) => log::error!("Reading resource {hash} of a document failed: {error}"), + } } } /// Storage session -> runtime interface. Resources stored in the container are hydrated into the global cache first. async fn build_document_from_storage(gdd: GddV1, byte_store: &ResourcesHandle, document_id: DocumentId) -> Option { - match gdd.resource_hashes().await { - Ok(hashes) => { - for hash in hashes { - match gdd.read_resource(&hash).await { - Ok(holder) => { - byte_store.store(holder.as_slice()); - } - Err(error) => log::error!("Opening document {document_id:?}: failed to read resource {hash}: {error}"), - } - } - } - Err(error) => log::error!("Opening document {document_id:?}: failed to list resources: {error}"), - } + hydrate_resources(&gdd, byte_store).await; let declarations = gdd.declarations(byte_store).await; let (network, node_entries, network_entries) = match gdd.registry().to_runtime_with_full_metadata(&declarations) {