|
28 | 28 |
|
29 | 29 | #include <filesystem.hpp>
|
30 | 30 |
|
| 31 | +#include "NetworkComponent.hpp" |
31 | 32 | #include "Registry.hpp"
|
32 | 33 | #include "WorldController.hpp"
|
33 | 34 |
|
@@ -109,6 +110,8 @@ void WorldController::load(const std::string &name) {
|
109 | 110 | chunk.setSent(false);
|
110 | 111 | chunk.setModified(true);
|
111 | 112 | }
|
| 113 | + |
| 114 | + loadEntities(save, world); |
112 | 115 | }
|
113 | 116 | }
|
114 | 117 |
|
@@ -155,9 +158,99 @@ void WorldController::save(const std::string &name) {
|
155 | 158 |
|
156 | 159 | save << chunkCount;
|
157 | 160 | save.append(chunks.getData(), chunks.getDataSize());
|
| 161 | + |
| 162 | + saveEntities(save, world); |
158 | 163 | }
|
159 | 164 |
|
160 | 165 | file.write((const char *)save.getData(), save.getDataSize());
|
161 | 166 |
|
162 | 167 | // gkInfo() << "Saving done.";
|
163 | 168 | }
|
| 169 | + |
| 170 | +#include "AnimationComponent.hpp" |
| 171 | +#include "DrawableDef.hpp" |
| 172 | +#include "PositionComponent.hpp" |
| 173 | +#include "RotationComponent.hpp" |
| 174 | + |
| 175 | +void WorldController::loadEntities(sf::Packet &save, ServerWorld &world) { |
| 176 | + entt::registry ®istry = world.scene().registry(); |
| 177 | + registry.clear(); |
| 178 | + |
| 179 | + u32 componentCount; |
| 180 | + save >> componentCount; |
| 181 | + |
| 182 | + // gkDebug() << "Loading" << componentCount << "components in dimension" << world.dimension().id(); |
| 183 | + |
| 184 | + for (u32 i = 0 ; i < componentCount ; ++i) { |
| 185 | + Network::Command packetID; |
| 186 | + save >> packetID; |
| 187 | + |
| 188 | + entt::entity entityID; |
| 189 | + save >> entityID; |
| 190 | + |
| 191 | + // gkDebug() << "Loading packet" << Network::commandToString(packetID) << "for entity" << std::underlying_type_t<entt::entity>(entityID); |
| 192 | + |
| 193 | + gk::ISerializable *component = nullptr; |
| 194 | + auto it = m_entityMap.find(entityID); |
| 195 | + if (it == m_entityMap.end()) { |
| 196 | + if (packetID == Network::Command::EntitySpawn) { |
| 197 | + entt::entity entity = registry.create(); |
| 198 | + m_entityMap.emplace(entityID, entity); |
| 199 | + registry.emplace<NetworkComponent>(entity, entity); |
| 200 | + } |
| 201 | + else { |
| 202 | + gkWarning() << "World load: Failed to add a component to nonexistent entity:" << std::underlying_type_t<entt::entity>(entityID); |
| 203 | + } |
| 204 | + } |
| 205 | + else { |
| 206 | + if (packetID == Network::Command::EntitySpawn) { |
| 207 | + gkWarning() << "World load: Trying to recreate an entity:" << std::underlying_type_t<entt::entity>(entityID); |
| 208 | + } |
| 209 | + else if (packetID == Network::Command::EntityPosition) { |
| 210 | + component = ®istry.emplace<PositionComponent>(it->second); |
| 211 | + } |
| 212 | + else if (packetID == Network::Command::EntityRotation) { |
| 213 | + component = ®istry.emplace<RotationComponent>(it->second); |
| 214 | + } |
| 215 | + else if (packetID == Network::Command::EntityAnimation) { |
| 216 | + component = ®istry.emplace<AnimationComponent>(it->second); |
| 217 | + } |
| 218 | + else if (packetID == Network::Command::EntityDrawableDef) { |
| 219 | + component = ®istry.emplace<DrawableDef>(it->second); |
| 220 | + } |
| 221 | + |
| 222 | + if (component) |
| 223 | + component->deserialize(save); |
| 224 | + else |
| 225 | + gkWarning() << "Unknown component with packet ID" << std::hex << (int)packetID; |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +void WorldController::saveEntities(sf::Packet &save, ServerWorld &world) { |
| 231 | + entt::registry ®istry = world.scene().registry(); |
| 232 | + |
| 233 | + u32 componentCount = 0; |
| 234 | + Network::Packet entities; |
| 235 | + registry.view<NetworkComponent>().each([&] (auto entity, auto &network) { |
| 236 | + entities << Network::Command::EntitySpawn << network.entityID; |
| 237 | + ++componentCount; |
| 238 | + |
| 239 | + registry.visit(entity, [&] (const auto &component_type) { |
| 240 | + const auto &type = entt::resolve_type(component_type); |
| 241 | + const auto &component = type.func("get"_hs).invoke({}, std::ref(registry), entity); |
| 242 | + Network::Packet packet = type.func("serialize"_hs).invoke({}, entity, component, true).template cast<Network::Packet>(); |
| 243 | + if (packet.getDataSize()) { |
| 244 | + // gkDebug() << "Serializing component" << type.prop("name"_hs).value().template cast<std::string>() << "for entity" << std::underlying_type_t<entt::entity>(entity) << "of size" << packet.getDataSize(); |
| 245 | + entities.append(packet.getData(), packet.getDataSize()); |
| 246 | + ++componentCount; |
| 247 | + } |
| 248 | + }); |
| 249 | + }); |
| 250 | + |
| 251 | + save << componentCount; |
| 252 | + save.append(entities.getData(), entities.getDataSize()); |
| 253 | + |
| 254 | + // gkDebug() << "Saving" << componentCount << "components in dimension" << world.dimension().id(); |
| 255 | +} |
| 256 | + |
0 commit comments