use bevy::app::App;
use once_cell::sync::Lazy;
use std::sync::Mutex;
#[doc(hidden)]
pub static COMPONENT_REGISTRY: Lazy<Mutex<Vec<fn(&mut App)>>> =
Lazy::new(|| Mutex::new(Vec::new()));
pub fn apply_registered_persist_types(app: &mut App) {
let fns = COMPONENT_REGISTRY.lock().unwrap();
for f in fns.iter() {
f(app);
}
}
fn collection_name<T: 'static>() -> &'static str {
let full = std::any::type_name::<T>();
match full.rfind("::") {
Some(pos) => &full[pos + 2..],
None => full,
}
}
pub fn register_persist_component<T>(app: &mut App)
where
T: ::bevy::prelude::Component
+ ::serde::Serialize
+ ::serde::de::DeserializeOwned
+ Send
+ Sync
+ 'static,
{
use ::bevy::prelude::IntoScheduleConfigs;
use crate::bevy::plugins::persistence_plugin::{
RegisteredPersistTypes, PersistenceSystemSet, auto_dirty_tracking_entity_system,
};
use crate::core::session::PersistenceSession;
use std::any::TypeId;
let type_id = TypeId::of::<T>();
let is_new = app
.world_mut()
.resource_mut::<RegisteredPersistTypes>()
.types
.insert(type_id);
if is_new {
let name = collection_name::<T>();
app.world_mut()
.resource_mut::<PersistenceSession>()
.register_component_named::<T>(name);
app.add_systems(
::bevy::app::PostUpdate,
auto_dirty_tracking_entity_system::<T>
.in_set(PersistenceSystemSet::TrackChanges),
);
}
}
pub fn register_persist_resource<R>(app: &mut App)
where
R: ::bevy::prelude::Resource
+ ::serde::Serialize
+ ::serde::de::DeserializeOwned
+ Send
+ Sync
+ 'static,
{
use ::bevy::prelude::IntoScheduleConfigs;
use crate::bevy::plugins::persistence_plugin::{
RegisteredPersistTypes, PersistenceSystemSet, auto_dirty_tracking_resource_system,
};
use crate::core::session::PersistenceSession;
use std::any::TypeId;
let type_id = TypeId::of::<R>();
let is_new = app
.world_mut()
.resource_mut::<RegisteredPersistTypes>()
.types
.insert(type_id);
if is_new {
let name = collection_name::<R>();
app.world_mut()
.resource_mut::<PersistenceSession>()
.register_resource_named::<R>(name);
app.add_systems(
::bevy::app::PostUpdate,
auto_dirty_tracking_resource_system::<R>
.in_set(PersistenceSystemSet::TrackChanges),
);
}
}
#[cfg(not(feature = "bevy_many_relationship_edges"))]
pub fn register_persist_bevy_relationship<R>(app: &mut App)
where
R: ::bevy::prelude::Component
+ ::bevy::ecs::relationship::Relationship
+ Send
+ Sync
+ 'static,
{
use ::bevy::prelude::IntoScheduleConfigs;
use crate::bevy::plugins::persistence_plugin::{
RegisteredPersistTypes, PersistenceSystemSet, auto_dirty_tracking_bevy_relationship_system,
};
use crate::core::session::PersistenceSession;
use std::any::TypeId;
let type_id = TypeId::of::<R>();
let is_new = app
.world_mut()
.resource_mut::<RegisteredPersistTypes>()
.types
.insert(type_id);
if is_new {
app.world_mut()
.resource_mut::<PersistenceSession>()
.register_bevy_relationship::<R>(collection_name::<R>());
app.add_systems(
::bevy::app::PostUpdate,
auto_dirty_tracking_bevy_relationship_system::<R>
.in_set(PersistenceSystemSet::TrackChanges),
);
}
}
#[cfg(feature = "bevy_many_relationship_edges")]
pub fn register_persist_many_relationship<R>(app: &mut App)
where
R: ::serde::Serialize + ::serde::de::DeserializeOwned + Send + Sync + 'static,
{
use ::bevy::prelude::IntoScheduleConfigs;
use crate::bevy::plugins::persistence_plugin::{
RegisteredPersistTypes, PersistenceSystemSet, auto_dirty_tracking_relationship_system,
};
use crate::core::session::PersistenceSession;
use std::any::TypeId;
let type_id = TypeId::of::<R>();
let is_new = app
.world_mut()
.resource_mut::<RegisteredPersistTypes>()
.types
.insert(type_id);
if is_new {
app.world_mut()
.resource_mut::<PersistenceSession>()
.register_many_relationship::<R>(collection_name::<R>());
app.add_systems(
::bevy::app::PostUpdate,
auto_dirty_tracking_relationship_system::<R>
.in_set(PersistenceSystemSet::TrackChanges),
);
}
}