1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#![deny(missing_docs)]
//! This crate provides [`MirroredStorage`], an implementation of a [`specs`] storage
//! that can track additions, removals and changes to the component it contains.
//!
//! [`MirroredStorage`]: struct.MirroredStorage.html
//! [`specs`]: https://crates.io/crates/specs

extern crate hibitset;
extern crate shrev;
extern crate specs;

use hibitset::BitSetLike;
use shrev::{EventChannel, EventIterator};
use specs::prelude::*;
use specs::storage::{MaskedStorage, TryDefault, UnprotectedStorage};
use specs::world::Index;

use std::any::Any;
use std::ops::{Deref, DerefMut};

/// A [`specs`] storage intended for synchronisation with external libraries.
///
/// [`specs`]: https://crates.io/crates/specs
pub struct MirroredStorage<C: Mirrored, S = DenseVecStorage<C>> {
    chan: EventChannel<C::Event>,
    store: S,
}

/// Components that can be tracked in a [`MirroredStorage`].
///
/// [`MirroredStorage`]: struct.MirroredStorage.html
pub trait Mirrored {
    /// The event type for reporting changes to this component.
    type Event: shrev::Event;

    /// Called when inserting the component.
    /// This method should not be called directly.
    fn insert(&mut self, _chan: &mut EventChannel<Self::Event>, _id: Index) {}

    /// Called when removing the component.
    /// This method should not be called directly.
    fn remove(&mut self, _chan: &mut EventChannel<Self::Event>, _id: Index) {}
}

impl<C: Mirrored, S: UnprotectedStorage<C>> MirroredStorage<C, S> {
    /// Modify the component at the given index.
    unsafe fn modify(&mut self, id: Index) -> (&mut C, &mut EventChannel<C::Event>) {
        (self.store.get_mut(id), &mut self.chan)
    }
}

impl<C: Mirrored, S> Default for MirroredStorage<C, S>
where
    S: TryDefault,
{
    fn default() -> Self {
        MirroredStorage {
            chan: EventChannel::new(),
            store: S::unwrap_default(),
        }
    }
}

impl<C, S> UnprotectedStorage<C> for MirroredStorage<C, S>
where
    C: Mirrored + Component,
    S: UnprotectedStorage<C>,
{
    unsafe fn clean<B>(&mut self, has: B)
    where
        B: BitSetLike,
    {
        self.store.clean(has)
    }

    unsafe fn get(&self, id: Index) -> &C {
        self.store.get(id)
    }

    unsafe fn get_mut(&mut self, id: Index) -> &mut C {
        self.store.get_mut(id)
    }

    unsafe fn insert(&mut self, id: Index, mut comp: C) {
        comp.insert(&mut self.chan, id);
        self.store.insert(id, comp);
    }

    unsafe fn remove(&mut self, id: Index) -> C {
        let mut comp = self.store.remove(id);
        comp.remove(&mut self.chan, id);
        comp
    }
}

/// Extension methods for [`Storage`] to help read events from [`MirroredStorage`].
///
/// [`MirroredStorage`]: struct.MirroredStorage.html
/// [`Storage`]: https://docs.rs/specs/0.11.2/specs/storage/struct.Storage.html
pub trait StorageExt<C: Mirrored> {
    /// Read insertion and removal events from the event channel.
    fn read_events(&self, reader: &mut ReaderId<C::Event>) -> EventIterator<C::Event>;
}

/// Extension methods for [`Storage`] to help read events from [`MirroredStorage`].
///
/// [`MirroredStorage`]: struct.MirroredStorage.html
/// [`Storage`]: https://docs.rs/specs/0.11.2/specs/storage/struct.Storage.html
pub trait StorageMutExt<C: Mirrored>: StorageExt<C> {
    /// Register a new reader of insertion and removal events.
    fn register_reader(&mut self) -> ReaderId<C::Event>;

    /// Get a mutable reference to the component and the event channel for update events.
    fn modify(&mut self, entity: Entity) -> Option<(&mut C, &mut EventChannel<C::Event>)>;

    /// Get a mutable reference to the event channel for update events.
    fn event_channel(&mut self) -> &mut EventChannel<C::Event>;
}

impl<'a, C, S, D> StorageExt<C> for Storage<'a, C, D>
where
    C: Mirrored + Component<Storage = MirroredStorage<C, S>>,
    S: UnprotectedStorage<C> + Any + Send + Sync,
    D: Deref<Target = MaskedStorage<C>>,
{
    fn read_events(&self, reader: &mut ReaderId<C::Event>) -> EventIterator<C::Event> {
        self.unprotected_storage().chan.read(reader)
    }
}

impl<'a, C, S, D> StorageMutExt<C> for Storage<'a, C, D>
where
    C: Mirrored + Component<Storage = MirroredStorage<C, S>>,
    S: UnprotectedStorage<C> + Any + Send + Sync,
    D: DerefMut<Target = MaskedStorage<C>>,
{
    fn register_reader(&mut self) -> ReaderId<C::Event> {
        self.unprotected_storage_mut().chan.register_reader()
    }

    fn modify(&mut self, entity: Entity) -> Option<(&mut C, &mut EventChannel<C::Event>)> {
        if self.contains(entity) {
            Some(unsafe { self.unprotected_storage_mut().modify(entity.id()) })
        } else {
            None
        }
    }

    fn event_channel(&mut self) -> &mut EventChannel<C::Event> {
        &mut self.unprotected_storage_mut().chan
    }
}