|
1 | 1 | //! Everything to do with the script lifetime management pipeline
|
2 | 2 |
|
3 |
| -use std::{any::Any, marker::PhantomData, sync::Arc}; |
| 3 | +use std::{any::Any, collections::VecDeque, marker::PhantomData, sync::Arc}; |
4 | 4 |
|
5 | 5 | use bevy_app::{App, Plugin, PostUpdate};
|
6 |
| -use bevy_asset::{Assets, Handle}; |
| 6 | +use bevy_asset::{AssetServer, Assets, Handle, LoadState}; |
7 | 7 | use bevy_ecs::{
|
8 | 8 | event::{Event, EventCursor, EventReader, EventWriter, Events},
|
9 | 9 | resource::Resource,
|
@@ -109,6 +109,59 @@ impl<P: IntoScriptPluginParams> ScriptLoadingPipeline<P> {
|
109 | 109 | }
|
110 | 110 | }
|
111 | 111 |
|
| 112 | +/// A trait describing things containing script handles |
| 113 | +pub trait GetScriptHandle { |
| 114 | + /// Retrieve the contained script handle |
| 115 | + fn get_script_handle(&self) -> Handle<ScriptAsset>; |
| 116 | +} |
| 117 | + |
| 118 | +#[derive(SystemParam)] |
| 119 | +/// A system param which operates over types implementing [`GetScriptHandle`]. |
| 120 | +/// Captures incoming "handle" like types, and waits until their asset is in a final state before proceeding, if that final state |
| 121 | +/// is loaded, will also guarantee a strong handle, otherwise the whole thing is skipped. |
| 122 | +/// |
| 123 | +/// Think of this as a proxy for "baby'ing" asset handles |
| 124 | +pub struct LoadedWithHandles<'w, 's, T: GetScriptHandle + Event + Clone> { |
| 125 | + assets: ResMut<'w, Assets<ScriptAsset>>, |
| 126 | + asset_server: Res<'w, AssetServer>, |
| 127 | + fresh_events: EventReader<'w, 's, T>, |
| 128 | + loaded_with_handles: Local<'s, VecDeque<(T, StrongScriptHandle)>>, |
| 129 | + loading: Local<'s, VecDeque<T>>, |
| 130 | +} |
| 131 | + |
| 132 | +impl<T: GetScriptHandle + Event + Clone> LoadedWithHandles<'_, '_, T> { |
| 133 | + /// Retrieves all of the events of type `T`, which have finished loading and have a strong handle, |
| 134 | + /// the rest will be discarded. |
| 135 | + /// |
| 136 | + /// This uses a [`EventReader<T>`] underneath, meaning if you don't call this method once every frame (or every other frame). |
| 137 | + /// You may miss events. |
| 138 | + pub fn get_loaded(&mut self) -> impl Iterator<Item = (T, StrongScriptHandle)> { |
| 139 | + // first get all of the fresh_events |
| 140 | + self.loading.extend(self.fresh_events.read().cloned()); |
| 141 | + // now process the loading queue |
| 142 | + self.loading.retain(|e| { |
| 143 | + let handle = e.get_script_handle(); |
| 144 | + match self.asset_server.get_load_state(&handle) { |
| 145 | + Some(LoadState::Loaded) => { |
| 146 | + let strong = StrongScriptHandle::from_assets(handle, &mut self.assets); |
| 147 | + if let Some(strong) = strong { |
| 148 | + self.loaded_with_handles.push_front((e.clone(), strong)); |
| 149 | + true |
| 150 | + } else { |
| 151 | + false |
| 152 | + } |
| 153 | + } |
| 154 | + Some(LoadState::Loading) => true, |
| 155 | + Some(_) => false, |
| 156 | + None => false, |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + // now return loaded with handles elements by draining |
| 161 | + self.loaded_with_handles.drain(..) |
| 162 | + } |
| 163 | +} |
| 164 | + |
112 | 165 | // struct StateBuilder<'a, P: IntoScriptPluginParams, M, S> {
|
113 | 166 | // plugin: &'a ScriptLoadingPipeline<P>,
|
114 | 167 | // app: &'a mut App,
|
|
0 commit comments