You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.5 KiB

  1. use serde::{de::Deserializer, ser::Serializer};
  2. use specs::{
  3. error::NoError,
  4. saveload::{ConvertSaveload, DeserializeComponents, Marker, SerializeComponents, SimpleMarker},
  5. Component, Entities, ReadStorage, World, WorldExt, Write, WriteStorage,
  6. };
  7. use crate::components::{Fleet, Owned, Planet, Player, Position, Ship, Velocity};
  8. /* PersistWorld */
  9. pub struct PersistWorld;
  10. pub struct PersistWorldMarker;
  11. impl Persistence for PersistWorld {
  12. type Marker = SimpleMarker<PersistWorldMarker>;
  13. type Components = (Position, Velocity, Planet, Ship, Owned, Player, Fleet);
  14. }
  15. /* Persistence */
  16. pub trait Persistence {
  17. type Marker: Marker;
  18. type Components: PersistenceComponents<Self::Marker>;
  19. fn setup(world: &mut World)
  20. where
  21. <Self::Marker as Component>::Storage: Default,
  22. <Self::Marker as Marker>::Allocator: Default,
  23. {
  24. world.register::<Self::Marker>();
  25. world.insert(<Self::Marker as Marker>::Allocator::default());
  26. }
  27. fn serialize<S>(&self, world: &World, serializer: S) -> Result<(), S::Error>
  28. where
  29. S: Serializer,
  30. {
  31. Self::Components::serialize(world, serializer)
  32. }
  33. fn deserialize<'de, D>(&self, world: &World, deserializer: D) -> Result<(), D::Error>
  34. where
  35. D: Deserializer<'de>,
  36. {
  37. Self::Components::deserialize(world, deserializer)
  38. }
  39. }
  40. /* PersistenceComponents */
  41. pub trait PersistenceComponents<M>
  42. where
  43. M: Marker,
  44. {
  45. fn serialize<S>(world: &World, serializer: S) -> Result<(), S::Error>
  46. where
  47. S: Serializer;
  48. fn deserialize<'de, D>(world: &World, deserializer: D) -> Result<(), D::Error>
  49. where
  50. D: Deserializer<'de>;
  51. }
  52. macro_rules! define_persistence_components {
  53. ($($T:ident),*) => {
  54. #[allow(non_snake_case)]
  55. impl<M $(,$T)+> PersistenceComponents<M> for ($($T,)+)
  56. where
  57. M: Marker,
  58. M::Allocator: Default,
  59. $($T: Component + ConvertSaveload<M, Error = NoError>,)+
  60. {
  61. fn serialize<S>(world: &World, serializer: S) -> Result<(), S::Error>
  62. where
  63. S: Serializer,
  64. {
  65. let (
  66. entities,
  67. mut marker,
  68. mut allocator,
  69. $($T,)+
  70. ) = world.system_data::<(
  71. Entities,
  72. WriteStorage<M>,
  73. Write<M::Allocator>,
  74. $(ReadStorage<$T>,
  75. )+)>();
  76. SerializeComponents::<NoError, M>::serialize_recursive
  77. (&($($T,)+),
  78. &entities,
  79. &mut marker,
  80. &mut allocator,
  81. serializer,
  82. )?;
  83. Ok(())
  84. }
  85. fn deserialize<'de, D>(world: &World, deserializer: D) -> Result<(), D::Error>
  86. where
  87. D: Deserializer<'de>,
  88. {
  89. let (
  90. entities,
  91. mut marker,
  92. mut allocator,
  93. $($T,)+
  94. ) = world.system_data::<(
  95. Entities,
  96. WriteStorage<M>,
  97. Write<M::Allocator>,
  98. $(WriteStorage<$T>,)+
  99. )>();
  100. DeserializeComponents::<NoError, M>::deserialize(
  101. &mut ($($T,)+),
  102. &entities,
  103. &mut marker,
  104. &mut allocator,
  105. deserializer,
  106. )
  107. }
  108. }
  109. }
  110. }
  111. define_persistence_components!(T1);
  112. define_persistence_components!(T1, T2);
  113. define_persistence_components!(T1, T2, T3);
  114. define_persistence_components!(T1, T2, T3, T4);
  115. define_persistence_components!(T1, T2, T3, T4, T5);
  116. define_persistence_components!(T1, T2, T3, T4, T5, T6);
  117. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7);
  118. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8);
  119. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
  120. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
  121. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
  122. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
  123. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
  124. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
  125. define_persistence_components!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);