Component ids can now be serialized and deserialized, deserializer converts between ids
This commit is contained in:
@@ -12,6 +12,8 @@
|
||||
namespace ecs::serial {
|
||||
std::unordered_map<size_t, std::tuple<std::function<void(std::ostream&, ecs::Component*)>, std::function<void(std::istream&, ecs::Component*)>, std::function<ecs::Component*()>>> internal::functions;
|
||||
|
||||
std::unordered_map<size_t, size_t> conversion;
|
||||
|
||||
void serialize(std::ostream& os, Entity* entity) {
|
||||
auto uuid_data = reinterpret_cast<const uint8_t*>(entity->uuid.as_bytes().data());
|
||||
iohelper::write_vector<uint8_t>(os, std::vector<uint8_t>(uuid_data, uuid_data + 16), false);
|
||||
@@ -47,54 +49,55 @@ namespace ecs::serial {
|
||||
void deserialize(std::istream& is, Manager& manager) {
|
||||
auto uuid_vector = iohelper::read_vector<uint8_t>(is, 16);
|
||||
uuids::uuid uuid(uuid_vector.begin(), uuid_vector.end());
|
||||
|
||||
Entity* entity = nullptr;
|
||||
if (manager.has_entity(uuid)) {
|
||||
// Update existing entity
|
||||
Entity* entity = manager.get_entity(uuid);
|
||||
size_t component_count = iohelper::read_length(is);
|
||||
// std::cout << "Updating " << component_count << " components in entity: " << uuid << '\n';
|
||||
for (size_t i = 0; i < component_count; ++i) {
|
||||
size_t new_id = iohelper::read_length(is);
|
||||
bool tagged = iohelper::read<bool>(is);
|
||||
size_t id = new_id;
|
||||
if (tagged) {
|
||||
id = iohelper::read_length(is);
|
||||
}
|
||||
|
||||
// @todo We also need to be able to remove components
|
||||
// Sending a component with length 0 -> remove component
|
||||
// However we might have components that have no data and are just like tags
|
||||
// So maybe instead if we send MAX_INT or something like that
|
||||
// Or we send a flag in front
|
||||
// @todo What if the function does not exist?
|
||||
if (entity->has_components({id})) {
|
||||
// Update the component
|
||||
std::cout << "Updating component: " << id << '\n';
|
||||
Component* component = entity->get_component(id);
|
||||
std::get<1>(internal::functions[new_id])(is, component);
|
||||
} else {
|
||||
// Add new component
|
||||
std::cout << "Adding component: " << id << '\n';
|
||||
Component* component = std::get<2>(internal::functions[new_id])();
|
||||
std::get<1>(internal::functions[new_id])(is, component);
|
||||
entity->add_component(id, component);
|
||||
}
|
||||
}
|
||||
entity = manager.get_entity(uuid);
|
||||
} else {
|
||||
// Create new entity
|
||||
Entity* entity = manager.create_entity(uuid);
|
||||
size_t component_count = iohelper::read_length(is);
|
||||
// std::cout << "Creating entity with " << component_count << " components: " << uuid << '\n';
|
||||
for (size_t i = 0; i < component_count; ++i) {
|
||||
size_t new_id = iohelper::read_length(is);
|
||||
bool tagged = iohelper::read<bool>(is);
|
||||
size_t id = new_id;
|
||||
if (tagged) {
|
||||
id = iohelper::read_length(is);
|
||||
}
|
||||
entity = manager.create_entity(uuid);
|
||||
}
|
||||
|
||||
std::cout << "Adding component: " << id << '\n';
|
||||
Component* component = std::get<2>(internal::functions[new_id])();
|
||||
size_t component_count = iohelper::read_length(is);
|
||||
// std::cout << "Updating " << component_count << " components in entity: " << uuid << '\n';
|
||||
for (size_t i = 0; i < component_count; ++i) {
|
||||
size_t new_id = conversion[iohelper::read_length(is)];
|
||||
bool runtime = iohelper::read<bool>(is);
|
||||
size_t id = new_id;
|
||||
if (runtime) {
|
||||
id = conversion[iohelper::read_length(is)];
|
||||
}
|
||||
|
||||
// @todo We also need to be able to remove components
|
||||
// Sending a component with length 0 -> remove component
|
||||
// However we might have components that have no data and are just like tags
|
||||
// So maybe instead if we send MAX_INT or something like that
|
||||
// Or we send a flag in front
|
||||
// @todo What if the function does not exist?
|
||||
if (entity->has_components({id})) {
|
||||
// Update the component
|
||||
std::cout << "Updating component: " << id << " [" << ComponentID::get_name(id) << "]";
|
||||
if (new_id != id) {
|
||||
std::cout << " (base: " << new_id << " [" << ComponentID::get_name(new_id) << "])";
|
||||
}
|
||||
std::cout << '\n';
|
||||
Component* component = entity->get_component(id);
|
||||
// @note We do not have to check if this exists as the entity already has the component
|
||||
std::get<1>(internal::functions[new_id])(is, component);
|
||||
} else {
|
||||
// Add new component
|
||||
std::cout << "Adding component: " << id << " [" << ComponentID::get_name(id) << "]";
|
||||
if (new_id != id) {
|
||||
std::cout << " (base: " << new_id << " [" << ComponentID::get_name(new_id) << "])";
|
||||
}
|
||||
std::cout << '\n';
|
||||
auto func = internal::functions.find(new_id);
|
||||
if (func == internal::functions.end()) {
|
||||
throw std::runtime_error("Unknown id");
|
||||
}
|
||||
Component* component = std::get<2>(func->second)();
|
||||
std::get<1>(func->second)(is, component);
|
||||
entity->add_component(id, component);
|
||||
}
|
||||
}
|
||||
@@ -109,4 +112,18 @@ namespace ecs::serial {
|
||||
iohelper::write_length(os, id);
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize_ids(std::istream& is) {
|
||||
size_t id_count = iohelper::read_length(is);
|
||||
for (size_t i = 0; i < id_count; ++i) {
|
||||
std::string name = iohelper::read<std::string>(is);
|
||||
size_t id = iohelper::read_length(is);
|
||||
|
||||
conversion[id] = ComponentID::get_id({name})[0];
|
||||
}
|
||||
|
||||
for (auto [remote_id, local_id] : conversion) {
|
||||
std::cout << remote_id << " -> " << local_id << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user