Lua Wrapper component can now be serialized
This commit is contained in:
@@ -19,13 +19,29 @@ namespace ecs::serial {
|
||||
auto components = entity->get_components();
|
||||
iohelper::write_length(os, components.size());
|
||||
for (auto [id, component] : components) {
|
||||
auto functions = internal::functions.find(id);
|
||||
if (functions == internal::functions.end()) {
|
||||
throw std::runtime_error("Unknown id");
|
||||
}
|
||||
if (!component->_tagged()) {
|
||||
auto functions = internal::functions.find(id);
|
||||
if (functions == internal::functions.end()) {
|
||||
throw std::runtime_error("No known serializer for id");
|
||||
}
|
||||
|
||||
iohelper::write_length(os, id);
|
||||
std::get<0>(internal::functions[id])(os, component);
|
||||
iohelper::write_length(os, id);
|
||||
iohelper::write<bool>(os, false);
|
||||
std::get<0>(internal::functions[id])(os, component);
|
||||
} else {
|
||||
TaggedComponent* tagged_component = (TaggedComponent*)component;
|
||||
auto new_id = ComponentID::get_id({tagged_component->_tag})[0];
|
||||
auto functions = internal::functions.find(new_id);
|
||||
|
||||
if (functions == internal::functions.end()) {
|
||||
throw std::runtime_error("No known serializer for id");
|
||||
}
|
||||
|
||||
iohelper::write_length(os, new_id);
|
||||
iohelper::write<bool>(os, true);
|
||||
iohelper::write_length(os, id);
|
||||
std::get<0>(internal::functions[new_id])(os, component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +54,13 @@ namespace ecs::serial {
|
||||
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 id = iohelper::read_length(is);
|
||||
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
|
||||
@@ -49,12 +71,12 @@ namespace ecs::serial {
|
||||
// Update the component
|
||||
std::cout << "Updating component: " << id << '\n';
|
||||
Component* component = entity->get_component(id);
|
||||
std::get<1>(internal::functions[id])(is, component);
|
||||
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[id])();
|
||||
std::get<1>(internal::functions[id])(is, component);
|
||||
Component* component = std::get<2>(internal::functions[new_id])();
|
||||
std::get<1>(internal::functions[new_id])(is, component);
|
||||
entity->add_component(id, component);
|
||||
}
|
||||
}
|
||||
@@ -64,10 +86,16 @@ namespace ecs::serial {
|
||||
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 id = iohelper::read_length(is);
|
||||
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);
|
||||
}
|
||||
|
||||
std::cout << "Adding component: " << id << '\n';
|
||||
Component* component = std::get<2>(internal::functions[id])();
|
||||
std::get<1>(internal::functions[id])(is, component);
|
||||
Component* component = std::get<2>(internal::functions[new_id])();
|
||||
std::get<1>(internal::functions[new_id])(is, component);
|
||||
entity->add_component(id, component);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user