use crate::{DataAccesses, MaskType, ReadGuardRef, ResourcesThreadLocal, Sto, entity::EntitiesStorage, operators::{
ChangedDataSend, EntitiesComponentIter, OrderedDataSend,
ReadOnlyOp, ReadOnlyOrderedOp, UnorderedDataSend, UnorderedData, OrderedData, EntitiesData,
}, storage::{FastIndexExt, SubStorages, ReadOnlyStorage}, sync::{IndexGuard, LazyIndexGuard, OrderedIndexGuard, StorageWriteGuard}, utils::IntoEntitiesIterator};
use crate::entity::Entity;
use crate::sync::{Ptr, PtrMut, NodePtr, NodePtrMut};
use crate::component::{self, Component, OneToNComponent, ComponentSend};
#[cfg(feature="debug_parameters")]
use erased_serde::Serializer;
use serde::{Serialize, ser::SerializeSeq};
use crate::storage::{Storage, SliceView, HierarchicalStorage, OneToNStorage, StorageRef};
use crate::entity::{Entities, EntitiesThreadLocal, EntitiesExt};
use crate::Bitmask;
use crate::SystemId;
use std::{any::TypeId, cell::UnsafeCell};
pub struct EntitiesDebug<'a>{
pub(crate) storages: SubStorages<'a>,
}
impl<'a> EntitiesDebug<'a>{
pub fn iter_for<'e, S: UnorderedData<'e> + ReadOnlyOp<'e>>(&'e self) -> <S as UnorderedData<'e>>::Iter{
S::into_iter(self)
}
pub fn iter_for_mut<'e, S: UnorderedDataSend<'e>>(&'e mut self) -> <S as UnorderedData<'e>>::IterMut {
S::into_iter_mut(self)
}
pub fn storage_for<'r, S>(&'r self) -> Sto<'r, S>
where S: UnorderedDataSend<'r> + ReadOnlyOp<'r>
{
Sto::new(
S::storage(self),
Entities{
storages: self.storages.clone()
}
)
}
pub fn storage_for_mut<'r, S>(&'r mut self) -> Sto<'r, S>
where S: UnorderedDataSend<'r>
{
Sto::new(
S::storage(self),
Entities{
storages: self.storages.clone()
}
)
}
pub fn ordered_iter_for<'e, S: OrderedDataSend<'e> + ReadOnlyOrderedOp<'e>>(&'e self) -> <S as OrderedData<'e>>::Iter{
S::into_iter(self)
}
pub fn ordered_iter_for_mut<'e, S: OrderedDataSend<'e>>(&'e mut self) -> <S as OrderedData<'e>>::Iter{
S::into_iter(self)
}
pub fn ordered_storage_for<'e, S: OrderedDataSend<'e> + ReadOnlyOrderedOp<'e>>(&'e self) -> Option<<S as OrderedData<'e>>::Storage> {
S::storage(self)
}
pub fn ordered_storage_for_mut<'e, S: OrderedDataSend<'e>>(&'e mut self) -> Option<<S as OrderedData<'e>>::Storage> {
S::storage(self)
}
pub fn iter_for_entities<'e, U, E>(&'e self, entities: E) -> E::IntoEntitiesIter
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedData<'e>,
U::Storage: ReadOnlyStorage
{
entities.storage_entities_iter(U::storage(self))
}
pub fn iter_for_entities_mut<'e, U, E>(&'e mut self, entities: E) -> E::IntoEntitiesIterMut
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedData<'e>,
{
entities.storage_entities_iter_mut(U::storage(self))
}
pub fn iter_for_entities_opt<'e, U, E>(&'e self, entities: E) -> E::IntoEntitiesOptIter
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedData<'e>,
U::Storage: ReadOnlyStorage
{
entities.storage_entities_opt_iter(U::storage(self))
}
pub fn iter_for_entities_opt_mut<'e, U, E>(&'e mut self, entities: E) -> E::IntoEntitiesOptIterMut
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedData<'e>,
{
entities.storage_entities_opt_iter_mut(U::storage(self))
}
pub fn entity_components<'r, S>(&'r self, entity: &Entity) -> Option<<S as UnorderedData<'r>>::ComponentsRef>
where
S: UnorderedDataSend<'r> + ReadOnlyOp<'r> + 'r,
S::Storage: StorageRef<'r, Component = S::ComponentsRef>
{
let mut storage = S::storage(self)?;
if storage.contains(entity.guid()){
let storage = &mut storage as *mut S::Storage;
unsafe{ Some((*storage).get_unchecked(entity.guid())) }
}else{
None
}
}
pub fn entity_components_mut<'r, S>(&'r mut self, entity: &Entity) -> Option<<S as UnorderedData<'r>>::ComponentsRef>
where
S: UnorderedDataSend<'r> + 'r,
S::Storage: StorageRef<'r, Component = S::ComponentsRef>
{
let mut storage = S::storage(self)?;
if storage.contains(entity.guid()){
let storage = &mut storage as *mut S::Storage;
unsafe{ Some((*storage).get_unchecked(entity.guid())) }
}else{
None
}
}
pub fn component_for<C: Component>(&self, entity: &Entity) -> Option<Ptr<C>>
where for<'s> <C as Component>::Storage: Storage<'s, C>
{
self.to_thread_local().component_for::<C>(entity)
}
pub fn component_for_mut<C: Component>(&self, entity: &Entity) -> Option<PtrMut<C>>
where for<'s> <C as Component>::Storage: Storage<'s, C>
{
self.to_thread_local().component_for_mut::<C>(entity)
}
pub fn has_component<C: 'static>(&self, entity: &Entity) -> bool{
self.to_thread_local().has_component::<C>(entity)
}
pub fn tree_node_for<'r, C: Component>(&'r self, entity: &Entity) -> Option<NodePtr<'r, C>>
where for<'b> <C as Component>::Storage: HierarchicalStorage<'b, C>
{
self.to_thread_local().tree_node_for::<C>(entity)
}
pub fn tree_node_for_mut<'r, C: Component>(&'r self, entity: &Entity) -> Option<NodePtrMut<'r, C>>
where for<'b> <C as Component>::Storage: HierarchicalStorage<'b, C>
{
self.to_thread_local().tree_node_for_mut::<C>(entity)
}
#[cfg(not(feature="debug_parameters"))]
pub fn debug_components_for(&self, entity: Entity) -> impl Iterator<Item = (component::Id, String)> + '_{
self.entity_component_ids(entity)
.map(move |id| (id.clone(), self.storages.debug_components_mask_index(&entity, id)))
}
pub fn entity_component_ids(&self, entity: Entity) -> impl Iterator<Item = &'_ component::Id> + '_{
let entity_mask = self.storages.entities_ref()[entity.guid()].1.clone();
self.storages.components_masks_iter()
.filter_map(move |(id, mask)| {
if self.storages.check(&entity, &entity_mask, &Bitmask::has(mask.clone())){
Some(id)
}else{
None
}
})
}
#[cfg(feature="debug_parameters")]
pub fn debug_components_for<S: Serializer>(&self, entity: Entity, mut serializer: S) -> Result<(), erased_serde::Error>{
for id in self.entity_component_ids(entity){
self.storages.debug_component(&entity, id, &mut serializer)?
}
Ok(())
}
#[cfg(feature="debug_parameters")]
pub fn debug_component_for<S: Serializer>(&self, entity: Entity, id: &component::Id, serializer: S) -> Result<(), erased_serde::Error>{
self.storages.debug_component(&entity, id, serializer)
}
pub fn split<D: DataAccesses>(&mut self) -> (EntitiesDebug, EntitiesDebug) {
let (split, rest) = self.storages.split::<D>();
(
EntitiesDebug{
storages: split,
},
EntitiesDebug{
storages: rest,
}
)
}
fn to_thread_local(&self) -> &EntitiesThreadLocal<'a>{
unsafe{ std::mem::transmute(&self.storages) }
}
fn to_send(&self) -> &Entities<'a>{
unsafe{ std::mem::transmute(&self.storages) }
}
fn to_send_mut(&mut self) -> &mut Entities<'a>{
unsafe{ std::mem::transmute(&mut self.storages) }
}
pub(crate) fn storages(&self) -> &SubStorages<'a> {
&self.storages
}
}
impl<'a> EntitiesExt<'a> for EntitiesDebug<'a>{
fn iter_for<'e, S: UnorderedDataSend<'e> + ReadOnlyOp<'e>>(&'e self) -> <S as UnorderedData<'e>>::Iter {
S::into_iter(self)
}
fn iter_for_mut<'e, S: UnorderedDataSend<'e>>(&'e mut self) -> <S as UnorderedData<'e>>::IterMut {
S::into_iter_mut(self)
}
fn ordered_iter_for<'e, S: OrderedDataSend<'e> + ReadOnlyOrderedOp<'e>>(&'e self) -> <S as OrderedData<'e>>::Iter{
S::into_iter( self )
}
fn ordered_iter_for_mut<'e, S: OrderedDataSend<'e>>(&'e mut self) -> <S as OrderedData<'e>>::Iter{
S::into_iter( self )
}
fn iter_for_entities<'e, U, E>(&'e self, entities: E) -> E::IntoEntitiesIter
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedDataSend<'e>,
U::Storage: ReadOnlyStorage
{
entities.storage_entities_iter(U::storage(self))
}
fn iter_for_entities_mut<'e, U, E>(&'e mut self, entities: E) -> E::IntoEntitiesIterMut
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedDataSend<'e>,
{
entities.storage_entities_iter_mut(U::storage(self))
}
fn iter_for_entities_opt<'e, U, E>(&'e self, entities: E) -> E::IntoEntitiesOptIter
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedDataSend<'e>,
U::Storage: ReadOnlyStorage
{
entities.storage_entities_opt_iter(U::storage(self))
}
fn iter_for_entities_opt_mut<'e, U, E>(&'e mut self, entities: E) -> E::IntoEntitiesOptIterMut
where
E: IntoEntitiesIterator<'e, U::Storage>,
U: UnorderedDataSend<'e>,
{
entities.storage_entities_opt_iter_mut(U::storage(self))
}
fn entity_components<'r, S>(&'r self, entity: &Entity) -> Option<<S as UnorderedData<'r>>::ComponentsRef>
where
S: UnorderedDataSend<'r> + ReadOnlyOp<'r> + 'r,
S::Storage: StorageRef<'r, Component = S::ComponentsRef>
{
self.to_send().entity_components::<S>(entity)
}
fn entity_components_mut<'r, S>(&'r mut self, entity: &Entity) -> Option<<S as UnorderedData<'r>>::ComponentsRef>
where
S: UnorderedDataSend<'r> + 'r,
S::Storage: StorageRef<'r, Component = S::ComponentsRef>
{
self.to_send_mut().entity_components_mut::<S>(entity)
}
fn component_for<C: ComponentSend>(&self, entity: &Entity) -> Option<Ptr<C>>
where for<'s> <C as Component>::Storage: Storage<'s, C>
{
self.to_send().component_for::<C>(entity)
}
fn component_for_mut<C: ComponentSend>(&self, entity: &Entity) -> Option<PtrMut<C>>
where for<'s> <C as Component>::Storage: Storage<'s, C>
{
self.to_send().component_for_mut::<C>(entity)
}
fn has_component<C: 'static>(&self, entity: &Entity) -> bool{
self.to_send().has_component::<C>(entity)
}
fn tree_node_for<C: ComponentSend>(&self, entity: &Entity) -> Option<NodePtr<C>>
where for<'b> <C as Component>::Storage: HierarchicalStorage<'b, C>
{
self.to_send().tree_node_for::<C>(entity)
}
fn tree_node_for_mut<C: ComponentSend>(&self, entity: &Entity) -> Option<NodePtrMut<C>>
where for<'b> <C as Component>::Storage: HierarchicalStorage<'b, C>
{
self.to_send().tree_node_for_mut::<C>(entity)
}
fn changed_iter_for<'r, S>(&'r self) -> EntitiesComponentIter<'r, S::ChangedIter, <S as UnorderedData<'r>>::Storage>
where
S: ChangedDataSend<'r> + EntitiesData + ReadOnlyOp<'r> + 'r,
<S as UnorderedData<'r>>::Storage: StorageRef<'r>
{
let iter = S::changed_iter(self);
S::into_entities_iter(self, iter)
}
fn changed_iter_for_mut<'r, S>(&'r mut self) -> EntitiesComponentIter<'r, S::ChangedIter, <S as UnorderedData<'r>>::Storage>
where
S: ChangedDataSend<'r> + EntitiesData + 'r,
<S as UnorderedData<'r>>::Storage: StorageRef<'r>
{
let iter = S::changed_iter(self);
S::into_entities_iter(self, iter)
}
fn storage_for<'r, S>(&'r self) -> Sto<'r, S>
where S: UnorderedDataSend<'r> + ReadOnlyOp<'r>
{
Sto::new(
S::storage(self),
Entities{
storages: self.storages.clone()
}
)
}
fn storage_for_mut<'r, S>(&'r mut self) -> Sto<'r, S>
where S: UnorderedDataSend<'r>
{
Sto::new(
S::storage(self),
Entities{
storages: self.storages.clone()
}
)
}
}
impl<'a> EntitiesStorage for EntitiesDebug<'a> {
fn storage<C: Component>(&self) -> Option<ReadGuardRef<<C as Component>::Storage>> {
self.storages.storage_thread_local::<C>()
}
fn storage_mut<C: Component>(&self) -> Option<StorageWriteGuard<C>> {
self.storages.storage_thread_local_mut::<C>()
}
fn entities_ref(&self) -> &[(Entity, MaskType)]{
self.storages.entities_ref()
}
fn component_mask<C: 'static>(&self) -> MaskType{
self.storages.component_mask::<C>()
}
fn entities_for_mask<S: FastIndexExt>(&self, query_mask: Bitmask, storage: &S) -> IndexGuard{
self.storages.entities_for_mask(query_mask, storage)
}
fn lazy_entities_for_mask(&self, query_mask: Bitmask) -> LazyIndexGuard {
LazyIndexGuard{
query_mask,
storages: self.storages.clone(),
index_guard: UnsafeCell::new(None),
}
}
fn ordered_entities_for<C: Component, S: FastIndexExt>(&self, query_mask: Bitmask, unordered_storage: &S) -> OrderedIndexGuard
where for<'b> <C as Component>::Storage: HierarchicalStorage<'b,C>
{
self.storages.ordered_entities_for_thread_local::<C,S>(query_mask, unordered_storage)
}
}
pub trait SystemDebug{
fn run(&mut self, entities: EntitiesDebug, resources: ResourcesThreadLocal);
fn name() -> Option<&'static str> where Self: Sized { None }
fn before() -> Vec<SystemId> where Self: Sized { vec![] }
fn after() -> Vec<SystemId> where Self: Sized { vec![] }
fn updates() -> Vec<TypeId> where Self: Sized { vec![] }
fn needs() -> Vec<TypeId> where Self: Sized { vec![] }
fn reads() -> Vec<TypeId> where Self: Sized { vec![] }
fn writes() -> Vec<TypeId> where Self: Sized { vec![] }
fn file_line_info(&self) -> &'static str {""}
}
impl<'a, F: FnMut(EntitiesDebug, ResourcesThreadLocal)> SystemDebug for F{
fn run(&mut self, entities: EntitiesDebug, resources: ResourcesThreadLocal){
self(entities, resources)
}
}
pub trait Inspector {
type InspectSeq;
type InspectTuple;
type InspectTupleStruct;
type InspectTupleVariant;
type InspectMap;
type InspectStruct;
type InspectStructVariant;
fn inspect_bool(self, v: &mut bool, changed: Option<bool>);
fn inspect_i8(self, v: &mut i8, changed: Option<bool>);
fn inspect_i16(self, v: &mut i16, changed: Option<bool>);
fn inspect_i32(self, v: &mut i32, changed: Option<bool>);
fn inspect_i64(self, v: &mut i64, changed: Option<bool>);
fn inspect_u8(self, v: &mut u8, changed: Option<bool>);
fn inspect_u16(self, v: &mut u16, changed: Option<bool>);
fn inspect_u32(self, v: &mut u32, changed: Option<bool>);
fn inspect_u64(self, v: &mut u64, changed: Option<bool>);
fn inspect_f32(self, v: &mut f32, changed: Option<bool>);
fn inspect_f64(self, v: &mut f64, changed: Option<bool>);
fn inspect_char(self, v: &mut char, changed: Option<bool>);
fn inspect_str(self, v: &mut String, changed: Option<bool>);
fn inspect_bytes(self, v: &mut [u8], changed: Option<bool>);
fn inspect_none(self, changed: Option<bool>);
fn inspect_some<T>(self, value: &mut T, changed: Option<bool>);
fn inspect_unit_variant(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
changed: Option<bool>
);
fn inspect_newtype_struct<T>(
self,
name: &'static str,
value: &T,
changed: Option<bool>
);
fn inspect_newtype_variant<T>(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
changed: Option<bool>
);
fn inspect_seq(self, len: Option<usize>, changed: Option<bool>);
fn inspect_tuple(self, len: usize, changed: Option<bool>);
fn inspect_tuple_struct(
self,
name: &'static str,
len: usize,
changed: Option<bool>
);
fn inspect_tuple_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize,
changed: Option<bool>
);
fn inspect_map(self, _len: Option<usize>, changed: Option<bool>);
fn inspect_struct(
self,
name: &'static str,
_len: usize,
changed: Option<bool>
);
fn inspect_struct_variant(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
_len: usize,
changed: Option<bool>
);
}
struct AsSeq<'a, T>(&'a [T]);
impl<'a, T: DebugParameter> Serialize for AsSeq<'a, T>{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
for d in self.0.iter() {
let element = AsSeqElemement{
element: d,
};
seq.serialize_element(&element)?;
}
seq.end()
}
}
struct AsSeqElemement<'a,T>{
element: &'a T,
}
impl<'a, T: DebugParameter> Serialize for AsSeqElemement<'a, T>{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
self.element.debug(serializer)
}
}
pub trait DebugParameter{
fn debug<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>;
}
impl<'a, D: DebugParameter> DebugParameter for &'a D{
fn debug<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
(*self).debug(serializer)
}
}
impl<'a, D> DebugParameter for &'a [D]
where
D: OneToNComponent + DebugParameter + Component,
for<'b> <D as Component>::Storage: OneToNStorage<'b, D>
{
fn debug<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
serializer.serialize_newtype_struct(D::slice_type_name(), &AsSeq(self))
}
}
impl<'a, D> DebugParameter for SliceView<'a,D>
where
D: OneToNComponent + DebugParameter + Component,
for<'b> <D as Component>::Storage: OneToNStorage<'b, D>
{
fn debug<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
serializer.serialize_newtype_struct(D::slice_type_name(), &AsSeq(&**self))
}
}