diff --git a/src/nodes/fields.rs b/src/nodes/fields.rs index 8b1e193..258114e 100644 --- a/src/nodes/fields.rs +++ b/src/nodes/fields.rs @@ -25,7 +25,7 @@ use glam::{Vec3, Vec3A, Vec3Swizzles, vec2, vec3, vec3a}; use parking_lot::Mutex; use rustc_hash::FxHashMap; use stardust_xr::values::Vector3; -use std::sync::{Arc, LazyLock}; +use std::sync::{Arc, LazyLock, Weak}; use zbus::interface; // TODO: get SDFs working properly with non-uniform scale and so on, output distance relative to the spatial it's compared against @@ -135,7 +135,7 @@ static FIELD_REGISTRY_DEBUG_GIZMOS: Registry = Registry::new(); stardust_xr_server_codegen::codegen_field_protocol!(); lazy_static::lazy_static! { - pub static ref EXPORTED_FIELDS: Mutex>> = Mutex::new(FxHashMap::default()); + pub static ref EXPORTED_FIELDS: Mutex>> = Mutex::new(FxHashMap::default()); } pub trait FieldTrait: Send + Sync + 'static { @@ -270,7 +270,7 @@ impl FieldAspect for Field { async fn export_field(node: Arc, _calling_client: Arc) -> Result { let id = rand::random(); - EXPORTED_FIELDS.lock().insert(id, node); + EXPORTED_FIELDS.lock().insert(id, Arc::downgrade(&node)); Ok(id) } } @@ -371,16 +371,17 @@ impl InterfaceAspect for Interface { Ok(EXPORTED_FIELDS .lock() .get(&uid) + .and_then(|s| s.upgrade()) .map(|s| { Alias::create( - s, + &s, &calling_client, FIELD_REF_ASPECT_ALIAS_INFO.clone(), None, ) .unwrap() }) - .ok_or_eyre("Couldn't find spatial with that ID")?) + .ok_or_eyre("Couldn't import field with that ID")?) } fn create_field( diff --git a/src/nodes/spatial/mod.rs b/src/nodes/spatial/mod.rs index 99e2425..d23e6c9 100644 --- a/src/nodes/spatial/mod.rs +++ b/src/nodes/spatial/mod.rs @@ -116,7 +116,7 @@ impl Aspect for Zone { } lazy_static::lazy_static! { - pub static ref EXPORTED_SPATIALS: Mutex>> = Mutex::new(FxHashMap::default()); + pub static ref EXPORTED_SPATIALS: Mutex>> = Mutex::new(FxHashMap::default()); } static ZONEABLE_REGISTRY: Registry = Registry::new(); @@ -374,7 +374,7 @@ impl SpatialAspect for Spatial { // legit gotta find a way to remove old ones, this just keeps the node alive async fn export_spatial(node: Arc, _calling_client: Arc) -> Result { let id = rand::random(); - EXPORTED_SPATIALS.lock().insert(id, node); + EXPORTED_SPATIALS.lock().insert(id, Arc::downgrade(&node)); Ok(id) } } @@ -521,9 +521,10 @@ impl InterfaceAspect for Interface { Ok(EXPORTED_SPATIALS .lock() .get(&uid) + .and_then(|s| s.upgrade()) .map(|s| { Alias::create( - s, + &s, &calling_client, SPATIAL_REF_ASPECT_ALIAS_INFO.clone(), None, diff --git a/src/objects/input/mouse_pointer.rs b/src/objects/input/mouse_pointer.rs index 096a4fe..859bd21 100644 --- a/src/objects/input/mouse_pointer.rs +++ b/src/objects/input/mouse_pointer.rs @@ -327,7 +327,9 @@ impl MousePointer { } while let Some(Ok(Some((handler, field_ref_id)))) = join_set.join_next().await { let exported_fields = EXPORTED_FIELDS.lock(); - let Some(field_ref_node) = exported_fields.get(&field_ref_id) else { + let Some(field_ref_node) = + exported_fields.get(&field_ref_id).and_then(|f| f.upgrade()) + else { println!("didn't find a thing :("); continue; }; diff --git a/src/objects/mod.rs b/src/objects/mod.rs index 98a658a..550e209 100644 --- a/src/objects/mod.rs +++ b/src/objects/mod.rs @@ -22,9 +22,9 @@ use std::{ }; use zbus::{Connection, interface, object_server::Interface, zvariant::OwnedObjectPath}; +pub mod hmd; pub mod input; pub mod play_space; -pub mod hmd; pub struct ObjectHandle(Connection, OwnedObjectPath, PhantomData); @@ -49,7 +49,9 @@ impl SpatialRef { let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false))); let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false); let uid: u64 = rand::random(); - EXPORTED_SPATIALS.lock().insert(uid, node.0.clone()); + EXPORTED_SPATIALS + .lock() + .insert(uid, Arc::downgrade(&node.0)); tokio::task::spawn({ let connection = connection.clone(); @@ -137,7 +139,7 @@ impl FieldRef { Spatial::add_to(&node.0, None, Mat4::IDENTITY, false); let field = Field::add_to(&node.0, shape).unwrap(); let uid: u64 = rand::random(); - EXPORTED_FIELDS.lock().insert(uid, node.0.clone()); + EXPORTED_FIELDS.lock().insert(uid, Arc::downgrade(&node.0)); tokio::task::spawn({ let connection = connection.clone();