fix: spatial and field exports retained after death

This commit is contained in:
Nova
2025-08-04 13:19:05 -07:00
parent fb0f8d4115
commit 07ea966c01
4 changed files with 18 additions and 12 deletions

View File

@@ -25,7 +25,7 @@ use glam::{Vec3, Vec3A, Vec3Swizzles, vec2, vec3, vec3a};
use parking_lot::Mutex; use parking_lot::Mutex;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use stardust_xr::values::Vector3; use stardust_xr::values::Vector3;
use std::sync::{Arc, LazyLock}; use std::sync::{Arc, LazyLock, Weak};
use zbus::interface; 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 // 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<Field> = Registry::new();
stardust_xr_server_codegen::codegen_field_protocol!(); stardust_xr_server_codegen::codegen_field_protocol!();
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref EXPORTED_FIELDS: Mutex<FxHashMap<u64, Arc<Node>>> = Mutex::new(FxHashMap::default()); pub static ref EXPORTED_FIELDS: Mutex<FxHashMap<u64, Weak<Node>>> = Mutex::new(FxHashMap::default());
} }
pub trait FieldTrait: Send + Sync + 'static { pub trait FieldTrait: Send + Sync + 'static {
@@ -270,7 +270,7 @@ impl FieldAspect for Field {
async fn export_field(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<u64> { async fn export_field(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<u64> {
let id = rand::random(); let id = rand::random();
EXPORTED_FIELDS.lock().insert(id, node); EXPORTED_FIELDS.lock().insert(id, Arc::downgrade(&node));
Ok(id) Ok(id)
} }
} }
@@ -371,16 +371,17 @@ impl InterfaceAspect for Interface {
Ok(EXPORTED_FIELDS Ok(EXPORTED_FIELDS
.lock() .lock()
.get(&uid) .get(&uid)
.and_then(|s| s.upgrade())
.map(|s| { .map(|s| {
Alias::create( Alias::create(
s, &s,
&calling_client, &calling_client,
FIELD_REF_ASPECT_ALIAS_INFO.clone(), FIELD_REF_ASPECT_ALIAS_INFO.clone(),
None, None,
) )
.unwrap() .unwrap()
}) })
.ok_or_eyre("Couldn't find spatial with that ID")?) .ok_or_eyre("Couldn't import field with that ID")?)
} }
fn create_field( fn create_field(

View File

@@ -116,7 +116,7 @@ impl Aspect for Zone {
} }
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref EXPORTED_SPATIALS: Mutex<FxHashMap<u64, Arc<Node>>> = Mutex::new(FxHashMap::default()); pub static ref EXPORTED_SPATIALS: Mutex<FxHashMap<u64, Weak<Node>>> = Mutex::new(FxHashMap::default());
} }
static ZONEABLE_REGISTRY: Registry<Spatial> = Registry::new(); static ZONEABLE_REGISTRY: Registry<Spatial> = 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 // legit gotta find a way to remove old ones, this just keeps the node alive
async fn export_spatial(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<u64> { async fn export_spatial(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<u64> {
let id = rand::random(); let id = rand::random();
EXPORTED_SPATIALS.lock().insert(id, node); EXPORTED_SPATIALS.lock().insert(id, Arc::downgrade(&node));
Ok(id) Ok(id)
} }
} }
@@ -521,9 +521,10 @@ impl InterfaceAspect for Interface {
Ok(EXPORTED_SPATIALS Ok(EXPORTED_SPATIALS
.lock() .lock()
.get(&uid) .get(&uid)
.and_then(|s| s.upgrade())
.map(|s| { .map(|s| {
Alias::create( Alias::create(
s, &s,
&calling_client, &calling_client,
SPATIAL_REF_ASPECT_ALIAS_INFO.clone(), SPATIAL_REF_ASPECT_ALIAS_INFO.clone(),
None, None,

View File

@@ -327,7 +327,9 @@ impl MousePointer {
} }
while let Some(Ok(Some((handler, field_ref_id)))) = join_set.join_next().await { while let Some(Ok(Some((handler, field_ref_id)))) = join_set.join_next().await {
let exported_fields = EXPORTED_FIELDS.lock(); 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 :("); println!("didn't find a thing :(");
continue; continue;
}; };

View File

@@ -22,9 +22,9 @@ use std::{
}; };
use zbus::{Connection, interface, object_server::Interface, zvariant::OwnedObjectPath}; use zbus::{Connection, interface, object_server::Interface, zvariant::OwnedObjectPath};
pub mod hmd;
pub mod input; pub mod input;
pub mod play_space; pub mod play_space;
pub mod hmd;
pub struct ObjectHandle<I: Interface>(Connection, OwnedObjectPath, PhantomData<I>); pub struct ObjectHandle<I: Interface>(Connection, OwnedObjectPath, PhantomData<I>);
@@ -49,7 +49,9 @@ impl SpatialRef {
let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false))); let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false)));
let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false); let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
let uid: u64 = rand::random(); 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({ tokio::task::spawn({
let connection = connection.clone(); let connection = connection.clone();
@@ -137,7 +139,7 @@ impl FieldRef {
Spatial::add_to(&node.0, None, Mat4::IDENTITY, false); Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
let field = Field::add_to(&node.0, shape).unwrap(); let field = Field::add_to(&node.0, shape).unwrap();
let uid: u64 = rand::random(); 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({ tokio::task::spawn({
let connection = connection.clone(); let connection = connection.clone();