Added codegen to register components, and started reworking lua
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#include <functional>
|
||||
#include <ostream>
|
||||
#pragma once
|
||||
#include "ecs.h"
|
||||
|
||||
@@ -57,6 +59,17 @@ namespace ecs::serial {
|
||||
internal::functions.insert({ComponentID::id<T>, {func1, func2, func3}});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void register_component_custom(std::function<void(std::ostream&, ecs::Component*)> func1, std::function<void(std::istream&, Component*)> func2) {
|
||||
auto func3 = [] () -> Component* {
|
||||
return new T();
|
||||
};
|
||||
|
||||
internal::functions.insert({ComponentID::id<T>, {func1, func2, func3}});
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, Entity* entity);
|
||||
void deserialize(std::istream& is, Manager& manager);
|
||||
|
||||
void serialize_ids(std::ostream& os);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user