This commit is contained in:
Danny McGee
2023-11-14 22:15:23 -05:00
parent 0d712c9627
commit 2f37dad5ce
2 changed files with 50 additions and 46 deletions
+46 -46
View File
@@ -18,59 +18,59 @@ pub fn save_scene() {
} }
pub(super) fn save_scene_system(world: &mut World) -> Result<(), String> { pub(super) fn save_scene_system(world: &mut World) -> Result<(), String> {
wasm::read_buffer!(SCENE_REQUEST, |_| { if wasm::read_buffer!(SCENE_REQUEST).is_none() {
let mut scene = DynamicScene::from_world(world); return Ok(());
let type_registry = world.resource::<AppTypeRegistry>(); }
#[allow(clippy::borrowed_box)] let mut scene = DynamicScene::from_world(world);
let is_serializable = |reflect: &Box<dyn Reflect>| -> bool { let type_registry = world.resource::<AppTypeRegistry>();
TypedReflectSerializer::new(&**reflect, &type_registry.read())
.serialize(&wasm::SERIALIZER)
.is_ok()
};
// FIXME: This includes some stuff we don't want, including (most #[allow(clippy::borrowed_box)]
// importantly) the _window_. We probably want to grab just the let is_serializable = |reflect: &Box<dyn Reflect>| -> bool {
// main camera, the skybox, the glTF scene instance and its TypedReflectSerializer::new(&**reflect, &type_registry.read())
// children. .serialize(&wasm::SERIALIZER)
scene.resources = scene .is_ok()
.resources };
.into_iter()
.filter(is_serializable)
.collect();
scene.entities = scene // FIXME: This includes some stuff we don't want, including (most
.entities // importantly) the _window_. We probably want to grab just the
.into_iter() // main camera, the skybox, the glTF scene instance and its
.filter_map(|mut ent| { // children.
ent.components = ent.components.into_iter().filter(is_serializable).collect(); scene.resources = scene
if ent.components.is_empty() { .resources
None .into_iter()
} else { .filter(is_serializable)
Some(ent) .collect();
}
})
.collect();
let serialized = scene scene.entities = scene
.serialize_ron(type_registry) .entities
.map_err(|err| format!("{err}"))?; .into_iter()
.filter_map(|mut ent| {
ent.components = ent.components.into_iter().filter(is_serializable).collect();
if ent.components.is_empty() {
None
} else {
Some(ent)
}
})
.collect();
let window = world let serialized = scene
.iter_entities() .serialize_ron(type_registry)
.find_map(|ent| { .map_err(|err| format!("{err}"))?;
if ent.contains::<PrimaryWindow>() {
ent.get::<Window>()
} else {
None
}
})
.ok_or_else(|| "Failed to get Window from &mut World".to_string())?;
wasm::dispatch_to_js(window, "scene-download-ready", &serialized)?; let window = world
}); .iter_entities()
.find_map(|ent| {
if ent.contains::<PrimaryWindow>() {
ent.get::<Window>()
} else {
None
}
})
.ok_or_else(|| "Failed to get Window from &mut World".to_string())?;
Ok(()) wasm::dispatch_to_js(window, "scene-download-ready", &serialized)
} }
#[wasm_bindgen(typescript_custom_section)] #[wasm_bindgen(typescript_custom_section)]
+4
View File
@@ -91,6 +91,10 @@ macro_rules! read_buffer {
} }
} }
}}; }};
($buffer:ident) => {{
let buffer = crate::wasm::get_buffer!($buffer);
buffer.try_lock().and_then(|lock| lock.borrow_mut().take())
}};
} }
pub(crate) use get_buffer; pub(crate) use get_buffer;