🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

189 lines
5.6 KiB

  1. #include <sstream>
  2. #include <vector>
  3. #include <gtest/gtest.h>
  4. #include <cereal/archives/json.hpp>
  5. #include <entt/core/hashed_string.hpp>
  6. #include <entt/entity/registry.hpp>
  7. #include <entt/entity/snapshot.hpp>
  8. struct position {
  9. float x;
  10. float y;
  11. };
  12. struct timer {
  13. int duration;
  14. int elapsed{0};
  15. };
  16. struct relationship {
  17. entt::entity parent;
  18. };
  19. template<typename Archive>
  20. void serialize(Archive &archive, position &position) {
  21. archive(position.x, position.y);
  22. }
  23. template<typename Archive>
  24. void serialize(Archive &archive, timer &timer) {
  25. archive(timer.duration);
  26. }
  27. template<typename Archive>
  28. void serialize(Archive &archive, relationship &relationship) {
  29. archive(relationship.parent);
  30. }
  31. TEST(Snapshot, Full) {
  32. using namespace entt::literals;
  33. std::stringstream storage;
  34. entt::registry source;
  35. entt::registry destination;
  36. auto e0 = source.create();
  37. source.emplace<position>(e0, 16.f, 16.f);
  38. source.destroy(source.create());
  39. auto e1 = source.create();
  40. source.emplace<position>(e1, .8f, .0f);
  41. source.emplace<relationship>(e1, e0);
  42. auto e2 = source.create();
  43. auto e3 = source.create();
  44. source.emplace<timer>(e3, 1000, 100);
  45. source.emplace<entt::tag<"empty"_hs>>(e3);
  46. source.destroy(e2);
  47. auto v2 = source.current(e2);
  48. {
  49. // output finishes flushing its contents when it goes out of scope
  50. cereal::JSONOutputArchive output{storage};
  51. entt::snapshot{source}.entities(output).component<position, timer, relationship, entt::tag<"empty"_hs>>(output);
  52. }
  53. cereal::JSONInputArchive input{storage};
  54. entt::snapshot_loader{destination}.entities(input).component<position, timer, relationship, entt::tag<"empty"_hs>>(input);
  55. ASSERT_TRUE(destination.valid(e0));
  56. ASSERT_TRUE(destination.all_of<position>(e0));
  57. ASSERT_EQ(destination.get<position>(e0).x, 16.f);
  58. ASSERT_EQ(destination.get<position>(e0).y, 16.f);
  59. ASSERT_TRUE(destination.valid(e1));
  60. ASSERT_TRUE(destination.all_of<position>(e1));
  61. ASSERT_EQ(destination.get<position>(e1).x, .8f);
  62. ASSERT_EQ(destination.get<position>(e1).y, .0f);
  63. ASSERT_TRUE(destination.all_of<relationship>(e1));
  64. ASSERT_EQ(destination.get<relationship>(e1).parent, e0);
  65. ASSERT_FALSE(destination.valid(e2));
  66. ASSERT_EQ(destination.current(e2), v2);
  67. ASSERT_TRUE(destination.valid(e3));
  68. ASSERT_TRUE(destination.all_of<timer>(e3));
  69. ASSERT_TRUE(destination.all_of<entt::tag<"empty"_hs>>(e3));
  70. ASSERT_EQ(destination.get<timer>(e3).duration, 1000);
  71. ASSERT_EQ(destination.get<timer>(e3).elapsed, 0);
  72. }
  73. TEST(Snapshot, Continuous) {
  74. using namespace entt::literals;
  75. std::stringstream storage;
  76. entt::registry source;
  77. entt::registry destination;
  78. std::vector<entt::entity> entities;
  79. for(auto i = 0; i < 10; ++i) {
  80. entities.push_back(source.create());
  81. }
  82. for(auto entity: entities) {
  83. source.destroy(entity);
  84. }
  85. auto e0 = source.create();
  86. source.emplace<position>(e0, 0.f, 0.f);
  87. source.emplace<relationship>(e0, e0);
  88. auto e1 = source.create();
  89. source.emplace<position>(e1, 1.f, 1.f);
  90. source.emplace<relationship>(e1, e0);
  91. auto e2 = source.create();
  92. source.emplace<position>(e2, .2f, .2f);
  93. source.emplace<relationship>(e2, e0);
  94. auto e3 = source.create();
  95. source.emplace<timer>(e3, 1000, 1000);
  96. source.emplace<relationship>(e3, e2);
  97. source.emplace<entt::tag<"empty"_hs>>(e3);
  98. {
  99. // output finishes flushing its contents when it goes out of scope
  100. cereal::JSONOutputArchive output{storage};
  101. entt::snapshot{source}.entities(output).component<position, relationship, timer, entt::tag<"empty"_hs>>(output);
  102. }
  103. cereal::JSONInputArchive input{storage};
  104. entt::continuous_loader loader{destination};
  105. loader.entities(input)
  106. .component<position, relationship>(input, &relationship::parent)
  107. .component<timer, entt::tag<"empty"_hs>>(input);
  108. ASSERT_FALSE(destination.valid(e0));
  109. ASSERT_TRUE(loader.contains(e0));
  110. auto l0 = loader.map(e0);
  111. ASSERT_TRUE(destination.valid(l0));
  112. ASSERT_TRUE(destination.all_of<position>(l0));
  113. ASSERT_EQ(destination.get<position>(l0).x, 0.f);
  114. ASSERT_EQ(destination.get<position>(l0).y, 0.f);
  115. ASSERT_TRUE(destination.all_of<relationship>(l0));
  116. ASSERT_EQ(destination.get<relationship>(l0).parent, l0);
  117. ASSERT_FALSE(destination.valid(e1));
  118. ASSERT_TRUE(loader.contains(e1));
  119. auto l1 = loader.map(e1);
  120. ASSERT_TRUE(destination.valid(l1));
  121. ASSERT_TRUE(destination.all_of<position>(l1));
  122. ASSERT_EQ(destination.get<position>(l1).x, 1.f);
  123. ASSERT_EQ(destination.get<position>(l1).y, 1.f);
  124. ASSERT_TRUE(destination.all_of<relationship>(l1));
  125. ASSERT_EQ(destination.get<relationship>(l1).parent, l0);
  126. ASSERT_FALSE(destination.valid(e2));
  127. ASSERT_TRUE(loader.contains(e2));
  128. auto l2 = loader.map(e2);
  129. ASSERT_TRUE(destination.valid(l2));
  130. ASSERT_TRUE(destination.all_of<position>(l2));
  131. ASSERT_EQ(destination.get<position>(l2).x, .2f);
  132. ASSERT_EQ(destination.get<position>(l2).y, .2f);
  133. ASSERT_TRUE(destination.all_of<relationship>(l2));
  134. ASSERT_EQ(destination.get<relationship>(l2).parent, l0);
  135. ASSERT_FALSE(destination.valid(e3));
  136. ASSERT_TRUE(loader.contains(e3));
  137. auto l3 = loader.map(e3);
  138. ASSERT_TRUE(destination.valid(l3));
  139. ASSERT_TRUE(destination.all_of<timer>(l3));
  140. ASSERT_EQ(destination.get<timer>(l3).duration, 1000);
  141. ASSERT_EQ(destination.get<timer>(l3).elapsed, 0);
  142. ASSERT_TRUE(destination.all_of<relationship>(l3));
  143. ASSERT_EQ(destination.get<relationship>(l3).parent, l2);
  144. ASSERT_TRUE(destination.all_of<entt::tag<"empty"_hs>>(l3));
  145. }