From be253e8cb73718874404fe96acfa0e956296bc9c Mon Sep 17 00:00:00 2001 From: copygirl Date: Mon, 5 Jan 2026 11:15:01 +0100 Subject: [PATCH] Add change detection to `Manifest` --- common/src/assets/manifest.rs | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/common/src/assets/manifest.rs b/common/src/assets/manifest.rs index ced7506..7ba9671 100644 --- a/common/src/assets/manifest.rs +++ b/common/src/assets/manifest.rs @@ -76,6 +76,11 @@ pub struct Manifest<'w, T: ManifestEntry> { } impl Manifest<'_, T> { + /// System condition which returns `true` if this manifest was added or changed. + pub fn changed(res: Option>>) -> bool { + resource_exists_and_changed(res) + } + /// Looks up an [`Asset`] of type `T` from its associated manifest. /// /// Returns `None` if an asset with the given identifier is missing from @@ -90,6 +95,31 @@ impl Manifest<'_, T> { let entry = self.entry_assets.get(entry_handle).unwrap(); Some(entry) } + + pub fn iter(&self) -> impl Iterator { + let Some(manifest_handle) = self.resource.as_deref().map(|res| &res.handle) else { + return ManifestIter(None); + }; + // // SAFETY: Manifest loads this as a dependency, so it should exist. + let manifest = self.manifest_assets.get(manifest_handle).unwrap(); + let values = manifest.map.values(); + // // SAFETY: Entry loads this as a dependency, so they should exist. + let iter = values.map(|h| self.entry_assets.get(h).unwrap()); + ManifestIter(Some(iter)) + } +} + +struct ManifestIter(Option); + +impl<'a, T, I> Iterator for ManifestIter +where + T: ManifestEntry, + I: Iterator, +{ + type Item = &'a T; + fn next(&mut self) -> Option { + self.0.as_mut().and_then(|i| i.next()) + } } pub trait InitManifest { @@ -105,5 +135,19 @@ impl InitManifest for App { // Insert a `Resource` that holds a handle to the `ManifestAsset` once it's loaded. let handle = self.world_mut().load_asset(path); self.load_resource_with(ManifestResource:: { handle }); + + self.add_systems(PostUpdate, trigger_change_on_loaded::); + } +} + +fn trigger_change_on_loaded( + mut events: MessageReader>>, + mut resource: Option>>, +) { + for event in events.read() { + if let AssetEvent::LoadedWithDependencies { id: _ } = event { + // Trigger change detection on the `ManifestResource`. + resource.as_deref_mut(); + } } }