Added codegen to register components, and started reworking lua

This commit is contained in:
2019-06-07 15:52:57 +02:00
parent 53a0b98341
commit 7ff9e21b4d
14 changed files with 381 additions and 110 deletions

View File

@@ -1,6 +1,14 @@
#include "ecs-serial.h"
#include "/home/tim/Projects/cpp/iohelper/iohelper/include/iohelper/read.h"
#include "/home/tim/Projects/cpp/iohelper/iohelper/include/iohelper/write.h"
#include "ecs.h"
#include <ostream>
#include <stdexcept>
// @todo We need to make sure that the id list stays the same between versions
// Maybe store that in the stream as well?
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;
@@ -11,8 +19,12 @@ 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");
}
iohelper::write_length(os, id);
// @todo What if the function does not exist?
std::get<0>(internal::functions[id])(os, component);
}
}
@@ -60,4 +72,14 @@ namespace ecs::serial {
}
}
}
void serialize_ids(std::ostream& os) {
auto& ids = ComponentID::_ids();
iohelper::write_length(os, ids.size());
for (auto& [name, id] : ids) {
iohelper::write<std::string>(os, name);
iohelper::write_length(os, id);
}
}
}