1use super::{
2 component::{Component, ComponentKey},
3 entity::{
4 ContainEntity, Entity, EntityId, EntityIndex, EntityKey, EntityKeyKind, EntityKeyRef,
5 EntityName, EntityTag,
6 },
7};
8use crate::{
9 ds::{
10 BorrowResult, Borrowed, DescribeGroup, Getter, GetterMut, GroupDesc, GroupMap,
11 SimpleHolder, TypeInfo,
12 },
13 ecs::EcsError,
14 util::{macros::debug_format, With},
15};
16use std::{
17 any::TypeId,
18 collections::HashMap,
19 fmt,
20 hash::BuildHasher,
21 marker::PhantomData,
22 ops::{Deref, DerefMut},
23 ptr::NonNull,
24 sync::Arc,
25};
26
27pub trait AsEntityReg {
29 fn entity_descriptor() -> EntityReg;
30}
31
32#[derive(Debug)]
55pub(crate) struct EntityStorage<S> {
56 map: GroupMap<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo, S>,
60
61 name_to_index: HashMap<EntityName, EntityIndex, S>,
63
64 ent_gens: Vec<u64>,
67
68 generation: u64,
71}
72
73impl<S> EntityStorage<S>
74where
75 S: Default,
76{
77 pub(crate) fn new() -> Self {
78 Self {
79 map: GroupMap::new(),
80 name_to_index: HashMap::default(),
81 ent_gens: Vec::new(),
82 generation: 1,
83 }
84 }
85}
86
87impl<S> EntityStorage<S>
88where
89 S: BuildHasher + Default,
90{
91 pub(crate) fn convert_entity_key<'r, K>(&self, key: K, to: EntityKeyKind) -> Option<EntityKey>
93 where
94 K: Into<EntityKeyRef<'r>>,
95 {
96 return inner(self, key.into(), to);
97
98 fn inner<S>(
101 this: &EntityStorage<S>,
102 key: EntityKeyRef<'_>,
103 to: EntityKeyKind,
104 ) -> Option<EntityKey>
105 where
106 S: BuildHasher + Default,
107 {
108 let ei = this.entity_index(key)?;
109 let res = match to {
110 EntityKeyKind::Name => {
111 let (cont, _) = unsafe { this.map.get_group(ei).unwrap_unchecked() };
113 let name = cont.get_tag().get_name()?.clone();
114 EntityKey::Name(name)
115 }
116 EntityKeyKind::Index => {
117 let ei = EntityIndex::new(With::new(
118 ei,
119 unsafe { *this.ent_gens.get_unchecked(ei) },
121 ));
122 EntityKey::Index(ei)
123 }
124 EntityKeyKind::Ckeys => {
125 let ckeys = unsafe { this.map.get_group_key(ei).unwrap_unchecked() };
127 EntityKey::Ckeys(Arc::clone(ckeys))
128 }
129 };
130 Some(res)
131 }
132 }
133
134 pub(crate) fn get_component_keys<'r, K>(&self, key: K) -> Option<&Arc<[ComponentKey]>>
135 where
136 K: Into<EntityKeyRef<'r>>,
137 {
138 return inner(self, key.into());
139
140 fn inner<'this, S>(
143 this: &'this EntityStorage<S>,
144 key: EntityKeyRef<'_>,
145 ) -> Option<&'this Arc<[ComponentKey]>>
146 where
147 S: BuildHasher + Default,
148 {
149 let ei = this.entity_index(key)?;
150 let ckeys = unsafe { this.map.get_group_key(ei).unwrap_unchecked() };
152 Some(ckeys)
153 }
154 }
155
156 pub(crate) fn get_entity_container<'r, K>(&self, key: K) -> Option<&EntityContainer>
157 where
158 K: Into<EntityKeyRef<'r>>,
159 {
160 return inner(self, key.into());
161
162 fn inner<'this, S>(
165 this: &'this EntityStorage<S>,
166 key: EntityKeyRef<'_>,
167 ) -> Option<&'this EntityContainer>
168 where
169 S: BuildHasher + Default,
170 {
171 let ei = this.entity_index(key)?;
172 let cont = unsafe { this.map.get_group(ei).unwrap_unchecked().0 };
174 Some(cont)
175 }
176 }
177
178 pub(crate) fn get_entity_container_mut<'r, K>(&mut self, key: K) -> Option<&mut EntityContainer>
179 where
180 K: Into<EntityKeyRef<'r>>,
181 {
182 return inner(self, key.into());
183
184 fn inner<'this, S>(
187 this: &'this mut EntityStorage<S>,
188 key: EntityKeyRef<'_>,
189 ) -> Option<&'this mut EntityContainer>
190 where
191 S: BuildHasher + Default,
192 {
193 let ei = this.entity_index(key)?;
194 let cont = unsafe { this.map.get_group_mut(ei).unwrap_unchecked().0 };
196 Some(cont)
197 }
198 }
199
200 pub(crate) fn iter_entity_container(
201 &self,
202 ) -> impl Iterator<Item = (&Arc<[ComponentKey]>, EntityIndex, &EntityContainer)> {
203 self.map.iter_group().map(|(ckeys, index, cont)| {
204 (
205 ckeys,
206 EntityIndex::new(With::new(index, self.ent_gens[index])),
207 cont,
208 )
209 })
210 }
211
212 pub(crate) fn register(&mut self, mut desc: EntityReg) -> Result<EntityIndex, EcsError> {
217 if desc.is_empty() {
218 let reason = debug_format!(
219 "failed to register an entity: `{:?}` has no components",
220 desc.get_name()
221 );
222 return Err(EcsError::InvalidEntity(reason, ()));
223 }
224
225 let gkey = desc
226 .get_key_info_pairs()
227 .iter()
228 .map(|(ckey, _)| *ckey)
229 .collect::<Vec<_>>();
230 let index = self.map.next_index(&*gkey);
231 let ei = EntityIndex::new(With::new(index, self.generation));
232 desc.set_index(ei);
233
234 let ename = desc.get_name().cloned();
235 match self.map.add_group(desc) {
236 Ok(i) => {
237 debug_assert_eq!(i, index);
238 self.generation += 1;
239
240 if let Some(name) = ename {
242 self.name_to_index.insert(name, ei);
243 }
244
245 while self.ent_gens.len() <= index {
247 self.ent_gens.push(0);
248 }
249 self.ent_gens[index] = ei.generation();
250
251 Ok(ei)
252 }
253 Err(desc) => {
254 let (_cont, _) = self.map.get_group2(&desc.group_key).unwrap();
255 let reason = debug_format!(
256 "failed to register an entity: two entities `{:?}` and `{:?}` are the same",
257 ename,
258 _cont.get_tag().get_name()
259 );
260 Err(EcsError::InvalidEntity(reason, ()))
261 }
262 }
263 }
264
265 pub(crate) fn unregister<'r, K>(
268 &mut self,
269 key: K,
270 ) -> Option<(Arc<[ComponentKey]>, EntityContainer)>
271 where
272 K: Into<EntityKeyRef<'r>>,
273 {
274 return inner(self, key.into());
275
276 fn inner<S>(
279 this: &mut EntityStorage<S>,
280 key: EntityKeyRef<'_>,
281 ) -> Option<(Arc<[ComponentKey]>, EntityContainer)>
282 where
283 S: BuildHasher + Default,
284 {
285 let index = this.entity_index(key)?;
286 let ckeys_cont = this.map.remove_group(index);
287 debug_assert!(ckeys_cont.is_some());
288
289 if let Some((_ckeys, cont)) = ckeys_cont.as_ref() {
291 if let Some(name) = cont.get_tag().get_name() {
292 this.name_to_index.remove(name);
293 }
294 }
295
296 ckeys_cont
300 }
301 }
302
303 pub(crate) fn entity_index<'r, K>(&self, key: K) -> Option<usize>
304 where
305 K: Into<EntityKeyRef<'r>>,
306 {
307 return inner(self, key.into());
308
309 fn inner<S>(this: &EntityStorage<S>, key: EntityKeyRef<'_>) -> Option<usize>
312 where
313 S: BuildHasher + Default,
314 {
315 match key {
316 EntityKeyRef::Ckeys(ckeys) => this.map.get_group_index(ckeys),
317 EntityKeyRef::Index(ei) => this.is_valid_index(ei).then_some(ei.index()),
318 EntityKeyRef::Name(name) => {
319 let ei = this.name_to_index.get(name)?;
320 this.is_valid_index(ei).then_some(ei.index())
321 }
322 }
323 }
324 }
325
326 #[allow(dead_code)]
332 pub(crate) unsafe fn get_ptr(&self, ei: &EntityIndex) -> Option<NonNull<dyn ContainEntity>> {
333 let ekey = EntityKeyRef::from(ei);
334 let cont = self.get_entity_container(ekey)?;
335 let ptr = unsafe { cont.cont_ptr.get_unchecked() };
336 Some(*ptr)
337 }
338
339 fn is_valid_index(&self, ei: &EntityIndex) -> bool {
340 self.map.contains_group(ei.index()) && ei.generation() == self.ent_gens[ei.index()]
341 }
342}
343
344impl<S> Default for EntityStorage<S>
345where
346 S: Default,
347{
348 fn default() -> Self {
349 Self::new()
350 }
351}
352
353pub struct EntityReg {
355 name: Option<EntityName>,
356 index: Option<EntityIndex>,
357 cont: Box<dyn ContainEntity>,
358 key_info_pairs: Vec<(ComponentKey, TypeInfo)>,
359}
360
361impl fmt::Debug for EntityReg {
362 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363 f.debug_struct("EntityReg")
364 .field("name", &self.get_name())
365 .field("index", &self.get_index())
366 .field("key_info_pairs", &self.get_key_info_pairs())
367 .finish_non_exhaustive()
368 }
369}
370
371impl EntityReg {
372 pub fn new(name: Option<EntityName>, mut cont: Box<dyn ContainEntity>) -> Self {
373 for ci in (0..cont.num_columns()).rev() {
375 cont.remove_column(ci);
376 }
377
378 Self {
379 name,
380 index: None,
381 cont,
382 key_info_pairs: Vec::new(),
383 }
384 }
385
386 pub(crate) const fn get_name(&self) -> Option<&EntityName> {
387 self.name.as_ref()
388 }
389
390 pub(crate) const fn get_index(&self) -> Option<&EntityIndex> {
391 self.index.as_ref()
392 }
393
394 pub(crate) const fn get_key_info_pairs(&self) -> &Vec<(ComponentKey, TypeInfo)> {
395 &self.key_info_pairs
396 }
397
398 pub fn add_component_of<C: Component>(&mut self) {
399 self.add_component(C::type_info());
400 }
401
402 pub fn add_component(&mut self, tinfo: TypeInfo) {
403 let pair = (ComponentKey::from(&tinfo), tinfo);
404 self.key_info_pairs.push(pair);
405 }
406
407 fn is_empty(&self) -> bool {
408 self.key_info_pairs.is_empty()
409 }
410
411 fn set_index(&mut self, index: EntityIndex) {
412 self.index = Some(index);
413 }
414
415 fn finish(self) -> GroupDesc<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> {
416 let Self {
417 name,
418 index,
419 mut cont,
420 mut key_info_pairs,
421 } = self;
422 let index = index.unwrap();
423
424 let old_len = key_info_pairs.len();
426 key_info_pairs.sort_unstable_by_key(|(key, _)| *key);
427 key_info_pairs.dedup_by_key(|(key, _)| *key);
428 assert_eq!(
429 key_info_pairs.len(),
430 old_len,
431 "entity cannot have duplicated components"
432 );
433
434 let mut ckeys = Vec::new();
436 let mut cnames = Vec::new();
437 for (ckey, tinfo) in key_info_pairs.iter() {
438 cnames.push(tinfo.name);
439 ckeys.push(*ckey);
440 cont.add_column(*tinfo);
441 }
442 let ckeys: Arc<[ComponentKey]> = ckeys.into();
443 let cnames: Box<[&'static str]> = cnames.into();
444
445 let tag = EntityTag::new(index, name, Arc::clone(&ckeys), cnames);
447 let tag = Arc::new(tag);
448 let cont = EntityContainer::new(tag, cont);
449
450 GroupDesc {
451 group_key: ckeys,
452 group_value: cont,
453 items: key_info_pairs,
454 }
455 }
456}
457
458impl DescribeGroup<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> for EntityReg {
459 fn into_group_and_items(
460 self,
461 ) -> GroupDesc<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> {
462 self.finish()
463 }
464}
465
466pub(crate) struct EntityContainer {
471 tag: Arc<EntityTag>,
472
473 cont: Box<dyn ContainEntity>,
475
476 cont_ptr: SimpleHolder<NonNull<dyn ContainEntity>>,
478}
479
480impl fmt::Debug for EntityContainer {
481 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
482 f.debug_struct("EntityContainer")
483 .field("tag", &self.get_tag())
484 .field("cont_ptr", &self.cont_ptr)
485 .finish_non_exhaustive()
486 }
487}
488
489impl EntityContainer {
490 pub(crate) fn new(tag: Arc<EntityTag>, mut cont: Box<dyn ContainEntity>) -> Self {
491 let cont_ptr = unsafe { NonNull::new_unchecked(&mut *cont as *mut _) };
493 let cont_ptr = SimpleHolder::new(cont_ptr);
494
495 Self {
496 tag,
497 cont,
498 cont_ptr,
499 }
500 }
501
502 pub(crate) fn into_inner(self) -> Box<dyn ContainEntity> {
503 self.cont
504 }
505
506 pub(crate) const fn get_tag(&self) -> &Arc<EntityTag> {
507 &self.tag
508 }
509
510 pub(crate) fn borrow(&self) -> BorrowResult<NonNull<dyn ContainEntity>> {
511 self.cont_ptr.borrow()
512 }
513}
514
515impl Deref for EntityContainer {
516 type Target = dyn ContainEntity;
517
518 fn deref(&self) -> &Self::Target {
519 &*self.cont
520 }
521}
522
523impl DerefMut for EntityContainer {
524 fn deref_mut(&mut self) -> &mut Self::Target {
525 &mut *self.cont
526 }
527}
528
529pub struct EntityContainerRef<'buf, T> {
530 etag: &'buf EntityTag,
531 cont: &'buf mut dyn ContainEntity,
532 _marker: PhantomData<&'buf mut T>,
533}
534
535impl<T> fmt::Debug for EntityContainerRef<'_, T> {
536 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
537 f.debug_struct("EntityContainerRef")
538 .field("etag", &self.get_entity_tag())
539 .finish_non_exhaustive()
540 }
541}
542
543impl<'buf, T> EntityContainerRef<'buf, T> {
544 pub(crate) fn new(etag: &'buf EntityTag, cont: &'buf mut dyn ContainEntity) -> Self {
545 Self {
546 etag,
547 cont,
548 _marker: PhantomData,
549 }
550 }
551
552 pub const fn get_entity_tag(&self) -> &EntityTag {
553 self.etag
554 }
555
556 pub fn len(&self) -> usize {
558 self.cont.len()
559 }
560
561 pub fn is_empty(&self) -> bool {
563 self.len() == 0
564 }
565
566 pub fn capacity(&self) -> usize {
570 self.cont.capacity()
571 }
572
573 pub fn num_columns(&self) -> usize {
575 self.cont.num_columns()
576 }
577
578 pub fn is_default(&self) -> bool {
580 (0..self.num_columns()).all(|ci| {
581 let tinfo = unsafe { self.cont.get_column_info(ci).unwrap_unchecked() };
583 tinfo.is_default
584 })
585 }
586
587 pub fn is_clone(&self) -> bool {
589 (0..self.num_columns()).all(|ci| {
590 let tinfo = unsafe { self.cont.get_column_info(ci).unwrap_unchecked() };
592 tinfo.is_clone
593 })
594 }
595
596 pub fn reserve(&mut self, additional: usize) {
599 self.cont.reserve(additional);
600 }
601
602 pub fn shrink_to_fit(&mut self) {
603 self.cont.shrink_to_fit();
604 }
605
606 pub fn get_column_of<C: Component>(&self) -> Option<Borrowed<Getter<'_, C>>> {
611 let ci = self.cont.get_column_index(&TypeId::of::<C>())?;
612 self.cont.borrow_column(ci).ok().map(|col| {
613 col.map(|raw_getter| unsafe { Getter::from_raw(raw_getter) })
615 })
616 }
617
618 pub fn get_column_mut_of<C: Component>(&mut self) -> Option<Borrowed<GetterMut<'_, C>>> {
623 let ci = self.cont.get_column_index(&TypeId::of::<C>())?;
624 self.cont.borrow_column_mut(ci).ok().map(|col| {
625 col.map(|raw_getter| unsafe { GetterMut::from_raw(raw_getter) })
627 })
628 }
629
630 pub fn remove(&mut self, eid: &EntityId) {
632 if eid.container_index() == self.get_entity_tag().index() {
633 if let Some(vi) = self.cont.to_value_index(eid.row_index()) {
634 self.remove_by_value_index(vi);
635 }
636 }
637 }
638
639 pub fn remove_by_value_index(&mut self, vi: usize) {
645 self.cont.remove_row_by_value_index(vi);
646 }
647}
648
649impl<T> EntityContainerRef<'_, T>
650where
651 T: Entity,
652{
653 pub fn get(&self, eid: &EntityId) -> Option<T::Ref<'_>> {
679 if eid.container_index() == self.get_entity_tag().index() {
680 let vi = self.cont.to_value_index(eid.row_index())?;
681 Some(self.get_by_value_index(vi))
682 } else {
683 None
684 }
685 }
686
687 pub fn get_mut(&mut self, eid: &EntityId) -> Option<T::Mut<'_>> {
715 if eid.container_index() == self.get_entity_tag().index() {
716 let vi = self.cont.to_value_index(eid.row_index())?;
717 Some(self.get_mut_by_value_index(vi))
718 } else {
719 None
720 }
721 }
722
723 pub fn get_by_value_index(&self, vi: usize) -> T::Ref<'_> {
755 T::get_ref_from(self.cont, vi)
757 }
758
759 pub fn get_mut_by_value_index(&mut self, vi: usize) -> T::Mut<'_> {
793 T::get_mut_from(self.cont, vi)
795 }
796
797 pub fn add(&mut self, value: T) -> EntityId {
805 let ei = self.get_entity_tag().index();
806 let ri = value.move_to(self.cont);
807 EntityId::new(ei, ri)
808 }
809
810 pub fn take(&mut self, eid: &EntityId) -> Option<T> {
813 if eid.container_index() == self.get_entity_tag().index() {
814 let vi = self.cont.to_value_index(eid.row_index())?;
815 Some(self.take_by_value_index(vi))
816 } else {
817 None
818 }
819 }
820
821 pub fn take_by_value_index(&mut self, vi: usize) -> T {
828 T::take_from(self.cont, vi)
829 }
830
831 pub fn resize(&mut self, new_len: usize, value: T) {
840 for ci in 0..T::num_components() {
841 unsafe {
843 self.cont
844 .resize_column(ci, new_len, value.component_ptr(ci));
845 }
846 }
847 }
848}