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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Contains types required to serialize and deserialize a world via the serde library.

use std::{collections::HashMap, hash::Hash, marker::PhantomData};

use de::{WorldDeserializer, WorldVisitor};
use id::EntitySerializer;
use ser::WorldSerializer;
use serde::{de::DeserializeSeed, Serializer};

use crate::{
    internals::{
        storage::{
            archetype::{ArchetypeIndex, EntityLayout},
            component::{Component, ComponentTypeId},
            UnknownComponentStorage,
        },
        world::World,
    },
    storage::UnknownComponentWriter,
};

pub mod archetypes;
pub mod de;
mod entities;
pub mod id;
pub mod ser;

/// A (de)serializable type which can represent a component type in a serialized world.
///
/// This trait has a blanket impl for all applicable types.
pub trait TypeKey:
    serde::Serialize + for<'de> serde::Deserialize<'de> + Ord + Clone + Hash
{
}

impl<T> TypeKey for T where
    T: serde::Serialize + for<'de> serde::Deserialize<'de> + Ord + Clone + Hash
{
}
/// A [`TypeKey`] which can construct itself for a given type T.
pub trait AutoTypeKey<T: Component>: TypeKey {
    /// Constructs the type key for component type `T`.
    fn new() -> Self;
}

type SerializeFn = fn(*const u8, &mut dyn FnMut(&dyn erased_serde::Serialize));
type SerializeSliceFn =
    fn(&dyn UnknownComponentStorage, ArchetypeIndex, &mut dyn FnMut(&dyn erased_serde::Serialize));
type DeserializeSliceFn = fn(
    UnknownComponentWriter,
    &mut dyn erased_serde::Deserializer,
) -> Result<(), erased_serde::Error>;
type DeserializeSingleBoxedFn =
    fn(&mut dyn erased_serde::Deserializer) -> Result<Box<[u8]>, erased_serde::Error>;

#[derive(Copy, Clone)]
/// An error type describing what to do when a component type is unrecognized.
pub enum UnknownType {
    /// Ignore the component.
    Ignore,
    /// Abort (de)serialization wwith an error.
    Error,
}

/// A world (de)serializer which describes how to (de)serialize the component types in a world.
///
/// The type parameter `T` represents the key used in the serialized output to identify each
/// component type. The type keys used must uniquely identify each component type, and be stable
/// between recompiles.
///
/// See the [legion_typeuuid crate](https://github.com/TomGillen/legion_typeuuid) for an example
/// of a type key which is stable between compiles.
pub struct Registry<T>
where
    T: TypeKey,
{
    _phantom_t: PhantomData<T>,
    missing: UnknownType,
    serialize_fns: HashMap<
        ComponentTypeId,
        (
            T,
            SerializeSliceFn,
            SerializeFn,
            DeserializeSliceFn,
            DeserializeSingleBoxedFn,
        ),
    >,
    constructors: HashMap<T, (ComponentTypeId, fn(&mut EntityLayout))>,
}

impl<T> Registry<T>
where
    T: TypeKey,
{
    /// Constructs a new registry.
    pub fn new() -> Self {
        Self {
            missing: UnknownType::Error,
            serialize_fns: HashMap::new(),
            constructors: HashMap::new(),
            _phantom_t: PhantomData,
        }
    }

    /// Sets the behavior to use when a component type is unknown.
    pub fn on_unknown(&mut self, unknown: UnknownType) {
        self.missing = unknown;
    }

    /// Registers a component type and its key with the registry.
    pub fn register<C: Component + serde::Serialize + for<'de> serde::Deserialize<'de>>(
        &mut self,
        mapped_type_id: T,
    ) {
        let type_id = ComponentTypeId::of::<C>();
        let serialize_slice_fn =
            |storage: &dyn UnknownComponentStorage,
             archetype,
             serialize: &mut dyn FnMut(&dyn erased_serde::Serialize)| unsafe {
                let (ptr, len) = storage.get_raw(archetype).unwrap();
                let slice = std::slice::from_raw_parts(ptr as *const C, len);
                (serialize)(&slice);
            };
        let serialize_fn = |ptr, serialize: &mut dyn FnMut(&dyn erased_serde::Serialize)| {
            let component = unsafe { &*(ptr as *const C) };
            (serialize)(component);
        };
        let deserialize_slice_fn =
            |storage: UnknownComponentWriter, deserializer: &mut dyn erased_serde::Deserializer| {
                // todo avoid temp vec
                ComponentSeq::<C> {
                    storage,
                    _phantom: PhantomData,
                }
                .deserialize(deserializer)?;
                Ok(())
            };
        let deserialize_single_boxed_fn = |deserializer: &mut dyn erased_serde::Deserializer| {
            let component = erased_serde::deserialize::<C>(deserializer)?;
            unsafe {
                let vec = std::slice::from_raw_parts(
                    &component as *const C as *const u8,
                    std::mem::size_of::<C>(),
                )
                .to_vec();
                std::mem::forget(component);
                Ok(vec.into_boxed_slice())
            }
        };
        let constructor_fn = |layout: &mut EntityLayout| layout.register_component::<C>();
        self.serialize_fns.insert(
            type_id,
            (
                mapped_type_id.clone(),
                serialize_slice_fn,
                serialize_fn,
                deserialize_slice_fn,
                deserialize_single_boxed_fn,
            ),
        );
        self.constructors
            .insert(mapped_type_id, (type_id, constructor_fn));
    }

    /// Registers a component type and its key with the registry.
    pub fn register_auto_mapped<
        C: Component + serde::Serialize + for<'de> serde::Deserialize<'de>,
    >(
        &mut self,
    ) where
        T: AutoTypeKey<C>,
    {
        self.register::<C>(<T as AutoTypeKey<C>>::new())
    }

    /// Constructs a serde::DeserializeSeed which will deserialize into an existing world.
    pub fn as_deserialize_into_world<'a, E: EntitySerializer>(
        &'a self,
        world: &'a mut World,
        entity_serializer: &'a E,
    ) -> DeserializeIntoWorld<'a, Self, E> {
        DeserializeIntoWorld {
            world,
            world_deserializer: self,
            entity_serializer,
        }
    }

    /// Constructs a serde::DeserializeSeed which will deserialize into a new world.
    pub fn as_deserialize<'a, E: EntitySerializer>(
        &'a self,
        entity_serializer: &'a E,
    ) -> DeserializeNewWorld<'a, Self, E> {
        DeserializeNewWorld {
            world_deserializer: self,
            entity_serializer,
        }
    }
}

impl<T> Default for Registry<T>
where
    T: TypeKey,
{
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> WorldSerializer for Registry<T>
where
    T: TypeKey,
{
    type TypeId = T;

    unsafe fn serialize_component<Ser: Serializer>(
        &self,
        ty: ComponentTypeId,
        ptr: *const u8,
        serializer: Ser,
    ) -> Result<Ser::Ok, Ser::Error> {
        if let Some((_, _, serialize_fn, _, _)) = self.serialize_fns.get(&ty) {
            let mut serializer = Some(serializer);
            let mut result = None;
            let result_ref = &mut result;
            (serialize_fn)(ptr, &mut move |serializable| {
                *result_ref = Some(erased_serde::serialize(
                    serializable,
                    serializer
                        .take()
                        .expect("serialize can only be called once"),
                ));
            });
            result.unwrap()
        } else {
            panic!();
        }
    }

    fn map_id(&self, type_id: ComponentTypeId) -> Result<Self::TypeId, UnknownType> {
        if let Some(type_id) = self
            .serialize_fns
            .get(&type_id)
            .map(|(type_id, _, _, _, _)| type_id.clone())
        {
            Ok(type_id)
        } else {
            Err(self.missing)
        }
    }

    unsafe fn serialize_component_slice<Ser: Serializer>(
        &self,
        ty: ComponentTypeId,
        storage: &dyn UnknownComponentStorage,
        archetype: ArchetypeIndex,
        serializer: Ser,
    ) -> Result<Ser::Ok, Ser::Error> {
        if let Some((_, serialize_fn, _, _, _)) = self.serialize_fns.get(&ty) {
            let mut serializer = Some(serializer);
            let mut result = None;
            let result_ref = &mut result;
            (serialize_fn)(storage, archetype, &mut move |serializable| {
                *result_ref = Some(erased_serde::serialize(
                    serializable,
                    serializer
                        .take()
                        .expect("serialize can only be called once"),
                ));
            });
            result.unwrap()
        } else {
            panic!();
        }
    }
}

impl<T> WorldDeserializer for Registry<T>
where
    T: TypeKey,
{
    type TypeId = T;

    fn unmap_id(&self, type_id: &Self::TypeId) -> Result<ComponentTypeId, UnknownType> {
        if let Some(type_id) = self.constructors.get(type_id).map(|(id, _)| *id) {
            Ok(type_id)
        } else {
            Err(self.missing)
        }
    }

    fn register_component(&self, type_id: Self::TypeId, layout: &mut EntityLayout) {
        if let Some((_, constructor)) = self.constructors.get(&type_id) {
            (constructor)(layout);
        }
    }

    fn deserialize_component_slice<'a, 'de, D: serde::Deserializer<'de>>(
        &self,
        type_id: ComponentTypeId,
        storage: UnknownComponentWriter<'a>,
        deserializer: D,
    ) -> Result<(), D::Error> {
        if let Some((_, _, _, deserialize, _)) = self.serialize_fns.get(&type_id) {
            use serde::de::Error;
            let mut deserializer = erased_serde::Deserializer::erase(deserializer);
            (deserialize)(storage, &mut deserializer).map_err(D::Error::custom)
        } else {
            //Err(D::Error::custom("unrecognized component type"))
            panic!()
        }
    }

    fn deserialize_component<'de, D: serde::Deserializer<'de>>(
        &self,
        type_id: ComponentTypeId,
        deserializer: D,
    ) -> Result<Box<[u8]>, D::Error> {
        if let Some((_, _, _, _, deserialize)) = self.serialize_fns.get(&type_id) {
            use serde::de::Error;
            let mut deserializer = erased_serde::Deserializer::erase(deserializer);
            (deserialize)(&mut deserializer).map_err(D::Error::custom)
        } else {
            //Err(D::Error::custom("unrecognized component type"))
            panic!()
        }
    }
}

struct ComponentSeq<'a, T: Component> {
    storage: UnknownComponentWriter<'a>,
    _phantom: PhantomData<T>,
}

impl<'a, 'de, T: Component + for<'b> serde::de::Deserialize<'b>> serde::de::DeserializeSeed<'de>
    for ComponentSeq<'a, T>
{
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        struct SeqVisitor<'b, C: Component + for<'c> serde::de::Deserialize<'c>> {
            storage: UnknownComponentWriter<'b>,
            _phantom: PhantomData<C>,
        }

        impl<'b, 'de, C: Component + for<'c> serde::de::Deserialize<'c>> serde::de::Visitor<'de>
            for SeqVisitor<'b, C>
        {
            type Value = ();

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("component seq")
            }

            fn visit_seq<V>(mut self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: serde::de::SeqAccess<'de>,
            {
                if let Some(len) = seq.size_hint() {
                    self.storage.ensure_capacity(len);
                }

                while let Some(component) = seq.next_element::<C>()? {
                    unsafe {
                        let ptr = &component as *const C as *const u8;
                        self.storage.extend_memcopy_raw(ptr, 1);
                        std::mem::forget(component)
                    }
                }
                Ok(())
            }
        }

        deserializer.deserialize_seq(SeqVisitor::<T> {
            storage: self.storage,
            _phantom: PhantomData,
        })
    }
}

/// Wraps a [`WorldDeserializer`] and a world and implements `serde::DeserializeSeed`
/// for deserializing into the world.
pub struct DeserializeIntoWorld<'a, W: WorldDeserializer, E: EntitySerializer> {
    /// The [World](../world/struct.World.html) to deserialize into.
    pub world: &'a mut World,
    /// The [WorldDeserializer](trait.WorldDeserializer.html) to use.
    pub world_deserializer: &'a W,
    /// The [EntitySerializer](trait.EntitySerializer.html) to use.
    pub entity_serializer: &'a E,
}

impl<'a, 'de, W: WorldDeserializer, E: EntitySerializer> DeserializeSeed<'de>
    for DeserializeIntoWorld<'a, W, E>
{
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_map(WorldVisitor {
            world: self.world,
            world_deserializer: self.world_deserializer,
            entity_serializer: self.entity_serializer,
        })
    }
}

/// Wraps a [`WorldDeserializer`] and a world and implements `serde::DeserializeSeed`
/// for deserializing into a new world.
pub struct DeserializeNewWorld<'a, W: WorldDeserializer, E: EntitySerializer> {
    /// The [WorldDeserializer](trait.WorldDeserializer.html) to use.
    pub world_deserializer: &'a W,
    /// The [EntitySerializer](trait.EntitySerializer.html) to use.
    pub entity_serializer: &'a E,
}

impl<'a, 'de, W: WorldDeserializer, E: EntitySerializer> DeserializeSeed<'de>
    for DeserializeNewWorld<'a, W, E>
{
    type Value = World;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let mut world = World::default();
        DeserializeIntoWorld {
            world: &mut world,
            world_deserializer: self.world_deserializer,
            entity_serializer: self.entity_serializer,
        }
        .deserialize(deserializer)?;
        Ok(world)
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
enum WorldField {
    Packed,
    Entities,
}

#[cfg(test)]
mod test {
    use super::Registry;
    use crate::internals::{
        entity::Entity,
        query::filter::filter_fns::any,
        serialize::id::{set_entity_serializer, Canon},
        world::{EntityStore, World},
    };

    #[test]
    fn serialize_json() {
        let mut world = World::default();

        let entity = world.extend(vec![
            (1usize, false, 1isize),
            (2usize, false, 2isize),
            (3usize, false, 3isize),
            (4usize, false, 4isize),
        ])[0];

        #[derive(serde::Serialize, serde::Deserialize)]
        struct EntityRef(Entity);

        let with_ref = world.extend(vec![
            (5usize, 5isize, EntityRef(entity)),
            (6usize, 6isize, EntityRef(entity)),
            (7usize, 7isize, EntityRef(entity)),
            (8usize, 8isize, EntityRef(entity)),
        ])[0];

        let entity_serializer = Canon::default();
        let mut registry = Registry::<String>::default();
        registry.register::<usize>("usize".to_string());
        registry.register::<bool>("bool".to_string());
        registry.register::<isize>("isize".to_string());
        registry.register::<EntityRef>("entity_ref".to_string());

        let json =
            serde_json::to_value(&world.as_serializable(any(), &registry, &entity_serializer))
                .unwrap();
        println!("{:#}", json);

        use serde::de::DeserializeSeed;
        let world: World = registry
            .as_deserialize(&entity_serializer)
            .deserialize(json)
            .unwrap();
        let entry = world.entry_ref(entity).unwrap();
        assert_eq!(entry.get_component::<usize>().unwrap(), &1usize);
        assert_eq!(entry.get_component::<bool>().unwrap(), &false);
        assert_eq!(entry.get_component::<isize>().unwrap(), &1isize);
        assert_eq!(
            world
                .entry_ref(with_ref)
                .unwrap()
                .get_component::<EntityRef>()
                .unwrap()
                .0,
            entity
        );

        assert_eq!(8, world.len());
    }

    #[test]
    fn serialize_bincode() {
        let mut world = World::default();

        let entity = world.extend(vec![
            (1usize, false, 1isize),
            (2usize, false, 2isize),
            (3usize, false, 3isize),
            (4usize, false, 4isize),
        ])[0];

        world.extend(vec![
            (5usize, 5isize),
            (6usize, 6isize),
            (7usize, 7isize),
            (8usize, 8isize),
        ]);

        let entity_serializer = Canon::default();
        let mut registry = Registry::<i32>::default();
        registry.register::<usize>(1);
        registry.register::<bool>(2);
        registry.register::<isize>(3);

        let encoded =
            bincode::serialize(&world.as_serializable(any(), &registry, &entity_serializer))
                .unwrap();

        use bincode::config::Options;
        use serde::de::DeserializeSeed;
        let mut deserializer = bincode::de::Deserializer::from_slice(
            &encoded[..],
            bincode::config::DefaultOptions::new()
                .with_fixint_encoding()
                .allow_trailing_bytes(),
        );
        let world: World = registry
            .as_deserialize(&entity_serializer)
            .deserialize(&mut deserializer)
            .unwrap();
        let entity = world.entry_ref(entity).unwrap();
        assert_eq!(entity.get_component::<usize>().unwrap(), &1usize);
        assert_eq!(entity.get_component::<bool>().unwrap(), &false);
        assert_eq!(entity.get_component::<isize>().unwrap(), &1isize);

        assert_eq!(8, world.len());
    }

    #[test]
    fn run_as_context_panic() {
        std::panic::catch_unwind(|| {
            let entity_serializer = Canon::default();
            set_entity_serializer(&entity_serializer, || panic!());
        })
        .unwrap_err();

        // run the serialize_bincode test again
        serialize_bincode();
    }

    #[test]
    fn serialize_json_external_canon() {
        let mut world1 = World::default();

        let entity = world1.extend(vec![
            (1usize, false, 1isize),
            (2usize, false, 2isize),
            (3usize, false, 3isize),
            (4usize, false, 4isize),
        ])[0];

        #[derive(serde::Serialize, serde::Deserialize)]
        struct EntityRef(Entity);

        let with_ref = world1.extend(vec![
            (5usize, 5isize, EntityRef(entity)),
            (6usize, 6isize, EntityRef(entity)),
            (7usize, 7isize, EntityRef(entity)),
            (8usize, 8isize, EntityRef(entity)),
        ])[0];

        let entity_serializer = Canon::default();
        let mut registry = Registry::<String>::new();
        registry.register::<usize>("usize".to_string());
        registry.register::<bool>("bool".to_string());
        registry.register::<isize>("isize".to_string());
        registry.register::<EntityRef>("entity_ref".to_string());

        let json =
            serde_json::to_value(&world1.as_serializable(any(), &registry, &entity_serializer))
                .unwrap();
        println!("{:#}", json);

        let entityname = entity_serializer.get_name(entity).unwrap();
        assert_eq!(entity_serializer.get_id(&entityname).unwrap(), entity);

        use serde::de::DeserializeSeed;
        let world2: World = registry
            .as_deserialize(&entity_serializer)
            .deserialize(json)
            .unwrap();
        let entry = world2.entry_ref(entity).unwrap();
        assert_eq!(entry.get_component::<usize>().unwrap(), &1usize);
        assert_eq!(entry.get_component::<bool>().unwrap(), &false);
        assert_eq!(entry.get_component::<isize>().unwrap(), &1isize);
        assert_eq!(
            world2
                .entry_ref(with_ref)
                .unwrap()
                .get_component::<EntityRef>()
                .unwrap()
                .0,
            entity
        );

        assert_eq!(8, world2.len());
    }
}