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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
//! Provides `Marker` and `MarkerAllocator` traits use std::collections::HashMap; use std::fmt::Debug; use std::hash::Hash; use join::Join; use shred::Resource; use storage::{DenseVecStorage, ReadStorage, WriteStorage}; use world::{Component, EntitiesRes, Entity, EntityBuilder, EntityResBuilder, LazyBuilder}; use serde::de::DeserializeOwned; use serde::ser::Serialize; /// A common trait for `EntityBuilder` and `LazyBuilder` with a marker function, allowing either to be used. pub trait MarkedBuilder { /// Add a `Marker` to the entity by fetching the associated allocator. /// /// ## Examples /// /// ``` /// use specs::prelude::*; /// use specs::saveload::{MarkedBuilder, U64Marker, U64MarkerAllocator}; /// /// fn MarkEntity<M: Builder + MarkedBuilder>(markable: M) -> Entity { /// markable /// /* .with(Component1) */ /// .marked::<U64Marker>() /// .build() /// } /// /// let mut world = World::new(); /// world.register::<U64Marker>(); /// world.add_resource(U64MarkerAllocator::new()); /// /// MarkEntity(world.create_entity()); /// ``` fn marked<M: Marker>(self) -> Self; } impl<'a> MarkedBuilder for EntityBuilder<'a> { fn marked<M>(self) -> Self where M: Marker, { let mut alloc = self.world.write_resource::<M::Allocator>(); alloc.mark(self.entity, &mut self.world.write_storage::<M>()); self } } impl<'a> MarkedBuilder for LazyBuilder<'a> { /// Add a `Marker` to the entity by fetching the associated allocator. /// /// This will be applied on the next `world.maintain()`. /// /// ## Examples /// /// ```rust /// use specs::prelude::*; /// use specs::saveload::{MarkedBuilder, U64Marker, U64MarkerAllocator}; /// let mut world = World::new(); /// /// world.register::<U64Marker>(); /// world.add_resource(U64MarkerAllocator::new()); /// /// # let lazy = world.read_resource::<LazyUpdate>(); /// # let entities = world.entities(); /// let my_entity = lazy /// .create_entity(&entities) /// /* .with(Component1) */ /// .marked::<U64Marker>() /// .build(); /// ``` /// /// ## Panics /// /// Panics during `world.maintain()` in case there's no allocator /// added to the `World`. fn marked<M>(self) -> Self where M: Marker { let entity = self.entity; self.lazy.exec(move |world| { let mut alloc = world.write_resource::<M::Allocator>(); alloc.mark(entity, &mut world.write_storage::<M>()); }); self } } impl<'a> EntityResBuilder<'a> { /// Add a `Marker` to the entity with the associated allocator, /// and component storage. /// /// ## Examples /// /// ``` /// use specs::prelude::*; /// use specs::saveload::{U64Marker, U64MarkerAllocator}; /// /// let mut world = World::new(); /// world.register::<U64Marker>(); /// world.add_resource(U64MarkerAllocator::new()); /// /// let mut storage = world.write_storage::<U64Marker>(); /// let mut alloc = world.write_resource::<U64MarkerAllocator>(); /// /// let entities = world.entities(); /// entities /// .build_entity() /// /* .with(Component1) */ /// .marked(&mut storage, &mut alloc) /// .build(); /// ``` /// pub fn marked<M>(self, storage: &mut WriteStorage<M>, alloc: &mut M::Allocator) -> Self where M: Marker, { alloc.mark(self.entity, storage); self } } /// This trait should be implemented by a component which is going to be used as marker. /// This marker should be set to entity that should be serialized. /// If serialization strategy needs to set marker to some entity /// then it should use newly allocated marker from `Marker::Allocator`. /// /// ## Example /// /// ```rust /// extern crate specs; /// #[macro_use] extern crate serde; /// use std::collections::HashMap; /// use std::ops::Range; /// /// use specs::prelude::*; /// use specs::world::EntitiesRes; /// use specs::saveload::{MarkedBuilder, Marker, MarkerAllocator}; /// /// // Marker for entities that should be synced over network /// #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] /// struct NetMarker { /// id: u64, /// seq: u64, /// } /// /// impl Component for NetMarker { /// type Storage = DenseVecStorage<Self>; /// } /// /// impl Marker for NetMarker { /// type Identifier = u64; /// type Allocator = NetNode; /// /// fn id(&self) -> u64 { /// self.id /// } /// /// // Updates sequence id. /// // Entities with too old sequence id get deleted. /// fn update(&mut self, update: Self) { /// assert_eq!(self.id, update.id); /// self.seq = update.seq; /// } /// } /// /// // Each client and server has one /// // Contains id range and `NetMarker -> Entity` mapping /// struct NetNode { /// range: Range<u64>, /// mapping: HashMap<u64, Entity>, /// } /// /// impl MarkerAllocator<NetMarker> for NetNode { /// fn allocate(&mut self, entity: Entity, id: Option<u64>) -> NetMarker { /// let id = id.unwrap_or_else(|| { /// self.range.next().expect("Id range must be virtually endless") /// }); /// let marker = NetMarker { /// id: id, /// seq: 0, /// }; /// self.mapping.insert(id, entity); /// marker /// } /// /// fn retrieve_entity_internal(&self, id: u64) -> Option<Entity> { /// self.mapping.get(&id).cloned() /// } /// /// fn maintain(&mut self, entities: &EntitiesRes, storage: &ReadStorage<NetMarker>) { /// self.mapping = (entities, storage) /// .join() /// .map(|(e, m)| (m.id(), e)) /// .collect(); /// } /// } /// /// fn main() { /// let mut world = World::new(); /// world.register::<NetMarker>(); /// /// world.add_resource( /// NetNode { /// range: 0..100, /// mapping: HashMap::new(), /// } /// ); /// /// let entity = world.create_entity().marked::<NetMarker>().build(); /// let storage = &mut world.write_storage::<NetMarker>(); /// let marker = storage.get(entity).unwrap().clone(); /// assert_eq!( /// world.write_resource::<NetNode>().retrieve_entity(marker, storage, &world.entities()), /// entity /// ); /// } /// ``` pub trait Marker: Clone + Component + Debug + Eq + Hash + DeserializeOwned + Serialize { /// Id of the marker type Identifier; /// Allocator for this `Marker` type Allocator: MarkerAllocator<Self>; /// Get this marker internal id. /// The value of this method should be constant. fn id(&self) -> Self::Identifier; /// This gets called when an entity is deserialized by `DeserializeComponents`. /// It can be used to update internal data that is not used for /// identification. /// /// ## Contract /// /// This function may assume that `self.id() == new_revision.id()`. /// However, it must not exhibit undefined behavior in such a case. /// /// ## Panics /// /// May panic if `self.id()` != `new_revision.id()`. /// /// ## Default implementation /// /// The default implementation just sets `self` to `new_revision`. /// /// ## Examples /// /// ```rust,ignore /// #[derive(Clone, Debug, Deserialize, Eq, Hash, Serialize)] /// struct MyMarker { /// id: u64, /// last_modified: String, /// } /// /// impl Marker for MyMarker { /// type Identifier = u64; /// /// fn id(&self) -> u64 { /// self.id /// } /// /// fn update(&self, new: Self) { /// self.last_modified = new.last_modified; /// } /// } /// ``` /// /// Now, the marker always contains the name of the client who updated the /// entity associated with this marker. fn update(&mut self, new_revision: Self) { *self = new_revision; } } /// This allocator is used with the `Marker` trait. /// It provides a method for allocating new `Marker`s. /// It should also provide a `Marker -> Entity` mapping. /// The `maintain` method can be implemented for cleanup and actualization. /// See docs for `Marker` for an example. pub trait MarkerAllocator<M: Marker>: Resource { /// Allocates a new marker for a given entity. /// If you don't pass an id, a new unique id will be created. fn allocate(&mut self, entity: Entity, id: Option<M::Identifier>) -> M; /// Get an `Entity` by a marker identifier. /// This function only accepts an id; it does not update the marker data. /// /// Implementors usually maintain a marker -> entity mapping /// and use that to retrieve the entity. fn retrieve_entity_internal(&self, id: M::Identifier) -> Option<Entity>; /// Tries to retrieve an entity by the id of the marker; /// if no entity has a marker with the same id, a new entity /// will be created and `marker` will be inserted for it. /// /// In case the entity existed, /// this method will update the marker data using `Marker::update`. fn retrieve_entity( &mut self, marker: M, storage: &mut WriteStorage<M>, entities: &EntitiesRes, ) -> Entity { if let Some(entity) = self.retrieve_entity_internal(marker.id()) { if let Some(marker_comp) = storage.get_mut(entity) { marker_comp.update(marker); return entity; } } let entity = entities.create(); let marker = self.allocate(entity, Some(marker.id())); // It's not possible for this to fail, as there's no way a freshly // created entity could be dead this fast. storage.insert(entity, marker).unwrap(); entity } /// Create new unique marker `M` and attach it to entity. /// Or get old marker if this entity is already marked. /// If entity is dead then this will return `None`. fn mark<'m>( &mut self, entity: Entity, storage: &'m mut WriteStorage<M>, ) -> Option<(&'m M, bool)> { if let Ok(entry) = storage.entry(entity) { let mut new = false; let marker = entry.or_insert_with(|| { new = true; self.allocate(entity, None) }); Some((marker, new)) } else { None } } /// Maintain internal data. Cleanup if necessary. fn maintain(&mut self, _entities: &EntitiesRes, _storage: &ReadStorage<M>); } /// Basic marker implementation usable for saving and loading #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct U64Marker(u64); impl Component for U64Marker { type Storage = DenseVecStorage<Self>; } impl Marker for U64Marker { type Allocator = U64MarkerAllocator; type Identifier = u64; fn id(&self) -> u64 { self.0 } } /// Basic marker allocator #[derive(Clone, Debug)] pub struct U64MarkerAllocator { index: u64, mapping: HashMap<u64, Entity>, } impl Default for U64MarkerAllocator { fn default() -> Self { U64MarkerAllocator::new() } } impl U64MarkerAllocator { /// Create new `U64MarkerAllocator` which will yield `U64Marker`s starting with `0` pub fn new() -> Self { U64MarkerAllocator { index: 0, mapping: HashMap::new(), } } } impl MarkerAllocator<U64Marker> for U64MarkerAllocator { fn allocate(&mut self, entity: Entity, id: Option<u64>) -> U64Marker { let marker = if let Some(id) = id { if id >= self.index { self.index = id + 1; } U64Marker(id) } else { self.index += 1; U64Marker(self.index - 1) }; self.mapping.insert(marker.id(), entity); marker } fn retrieve_entity_internal(&self, id: u64) -> Option<Entity> { self.mapping.get(&id).cloned() } fn maintain(&mut self, entities: &EntitiesRes, storage: &ReadStorage<U64Marker>) { // FIXME: may be too slow self.mapping = (entities, storage) .join() .map(|(e, m)| (m.id(), e)) .collect(); } }