Moved majority over to flint2 and updated sol2
This commit is contained in:
parent
748b9c76ef
commit
3b49c00df0
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -31,6 +31,7 @@ tags
|
|||
|
||||
# flint
|
||||
.flint
|
||||
.build
|
||||
|
||||
# Custom
|
||||
.clangd/
|
||||
|
|
|
@ -86,7 +86,8 @@ namespace ecs::lua {
|
|||
},
|
||||
"__newindex", [] (Wrapper* thiz, std::string key, sol::object value) {
|
||||
thiz->table[key] = value;
|
||||
}
|
||||
},
|
||||
sol::base_classes, sol::bases<ecs::Component>()
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](std::string _name, sol::table _table) {
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
#pragma once
|
||||
#include "ecs.h"
|
||||
|
||||
#include "iohelper/write.h"
|
||||
#include "iohelper/read.h"
|
||||
#include "io/write.h"
|
||||
#include "io/read.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
@ -20,7 +20,7 @@ namespace ecs::serial {
|
|||
|
||||
template <typename T, typename M, typename... Args>
|
||||
void serialize_member(std::ostream& os, T* t, M T::*m, Args... args) {
|
||||
iohelper::write<M>(os, t->*m);
|
||||
io::write<M>(os, t->*m);
|
||||
|
||||
serialize_member(os, t, args...);
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ namespace ecs::serial {
|
|||
|
||||
template <typename T, typename M, typename... Args>
|
||||
void deserialize_member(std::istream& is, T* t, M T::*m, Args... args) {
|
||||
t->*m = iohelper::read<M>(is);
|
||||
t->*m = io::read<M>(is);
|
||||
|
||||
deserialize_member(is, t, args...);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
#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>
|
||||
|
||||
|
@ -16,10 +14,10 @@ namespace ecs::serial {
|
|||
|
||||
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);
|
||||
io::write<std::vector<uint8_t>>(os, std::vector<uint8_t>(uuid_data, uuid_data + 16), false);
|
||||
|
||||
auto components = entity->get_components();
|
||||
iohelper::write_length(os, components.size());
|
||||
io::write<size_t>(os, components.size());
|
||||
for (auto [id, component] : components) {
|
||||
if (!component->_runtime) {
|
||||
auto functions = internal::functions.find(id);
|
||||
|
@ -27,8 +25,8 @@ namespace ecs::serial {
|
|||
throw std::runtime_error("No known serializer for id");
|
||||
}
|
||||
|
||||
iohelper::write_length(os, id);
|
||||
iohelper::write<bool>(os, false);
|
||||
io::write<size_t>(os, id);
|
||||
io::write<bool>(os, false);
|
||||
std::get<0>(internal::functions[id])(os, component);
|
||||
} else {
|
||||
auto new_id = component->_id;
|
||||
|
@ -38,17 +36,17 @@ namespace ecs::serial {
|
|||
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);
|
||||
io::write<size_t>(os, new_id);
|
||||
io::write<bool>(os, true);
|
||||
io::write<size_t>(os, id);
|
||||
std::get<0>(internal::functions[new_id])(os, component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
auto uuid_array = io::read<std::array<uint8_t, 16>>(is);
|
||||
uuids::uuid uuid(uuid_array.begin(), uuid_array.end());
|
||||
|
||||
Entity* entity = nullptr;
|
||||
if (manager.has_entity(uuid)) {
|
||||
|
@ -59,14 +57,14 @@ namespace ecs::serial {
|
|||
entity = manager.create_entity(uuid);
|
||||
}
|
||||
|
||||
size_t component_count = iohelper::read_length(is);
|
||||
size_t component_count = io::read<size_t>(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 new_id = conversion[io::read<size_t>(is)];
|
||||
bool runtime = io::read<bool>(is);
|
||||
size_t id = new_id;
|
||||
if (runtime) {
|
||||
id = conversion[iohelper::read_length(is)];
|
||||
id = conversion[io::read<size_t>(is)];
|
||||
}
|
||||
|
||||
// @todo We also need to be able to remove components
|
||||
|
@ -106,18 +104,18 @@ namespace ecs::serial {
|
|||
void serialize_ids(std::ostream& os) {
|
||||
auto& ids = ComponentID::_ids();
|
||||
|
||||
iohelper::write_length(os, ids.size());
|
||||
io::write<size_t>(os, ids.size());
|
||||
for (auto& [name, id] : ids) {
|
||||
iohelper::write<std::string>(os, name);
|
||||
iohelper::write_length(os, id);
|
||||
io::write<std::string>(os, name);
|
||||
io::write<size_t>(os, id);
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize_ids(std::istream& is) {
|
||||
size_t id_count = iohelper::read_length(is);
|
||||
size_t id_count = io::read<size_t>(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);
|
||||
std::string name = io::read<std::string>(is);
|
||||
size_t id = io::read<size_t>(is);
|
||||
|
||||
conversion[id] = ComponentID::get_id({name})[0];
|
||||
}
|
||||
|
|
63
flint.yaml
Normal file
63
flint.yaml
Normal file
|
@ -0,0 +1,63 @@
|
|||
git:
|
||||
sol2:
|
||||
url: https://github.com/ThePhD/sol2
|
||||
revision: v3.2.0
|
||||
|
||||
stduuid:
|
||||
url: https://github.com/mariusbancila/stduuid
|
||||
|
||||
io:
|
||||
url: https://git.mtgames.nl/Dreaded_X/io
|
||||
|
||||
include:
|
||||
- .build/git/io/flint.yaml
|
||||
|
||||
targets:
|
||||
sol2:
|
||||
type: lib
|
||||
git: sol2
|
||||
auto: false
|
||||
include:
|
||||
- single/include/sol
|
||||
|
||||
link:
|
||||
- lua
|
||||
|
||||
stduuid:
|
||||
type: lib
|
||||
git: stduuid
|
||||
platform:
|
||||
linux:
|
||||
link:
|
||||
- uuid
|
||||
|
||||
ecs:
|
||||
type: lib
|
||||
path: ecs
|
||||
dependency:
|
||||
- stduuid
|
||||
|
||||
ecs-lua:
|
||||
type: lib
|
||||
path: ecs-lua
|
||||
dependency:
|
||||
- ecs
|
||||
- sol2
|
||||
|
||||
ecs-serial:
|
||||
type: lib
|
||||
path: ecs-serial
|
||||
dependency:
|
||||
- ecs
|
||||
- io
|
||||
|
||||
test:
|
||||
type: exe
|
||||
path: test
|
||||
include:
|
||||
- generated
|
||||
dependency:
|
||||
- ecs
|
||||
- ecs-lua
|
||||
- ecs-serial
|
||||
|
|
@ -11,7 +11,8 @@ namespace generated {
|
|||
preload["ecs.${c.name}"] = [&lua] {
|
||||
sol::table component = lua.create_table();
|
||||
component.new_usertype<${c.name}>("${c.name}",
|
||||
${', '.join("\"{}\", &{}::{}".format(v.name, c.name, v.name) for v in c.variables if not "hidden" in v.annotations)}
|
||||
${', '.join("\"{}\", &{}::{}".format(v.name, c.name, v.name) for v in c.variables if not "hidden" in v.annotations)},
|
||||
sol::base_classes, sol::bases<ecs::Component>()
|
||||
);
|
||||
|
||||
% for con in c.constructors:
|
||||
|
|
82
test/generated/ecs_components.h
Normal file
82
test/generated/ecs_components.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include "components.h"
|
||||
|
||||
#if __has_include("ecs-lua.h")
|
||||
#include "sol.hpp"
|
||||
|
||||
namespace generated {
|
||||
void init(sol::state& lua) {
|
||||
sol::table preload = lua["package"]["preload"];
|
||||
|
||||
preload["ecs.Position"] = [&lua] {
|
||||
sol::table component = lua.create_table();
|
||||
component.new_usertype<Position>("Position",
|
||||
"x", &Position::x, "y", &Position::y,
|
||||
sol::base_classes, sol::bases<ecs::Component>()
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](float _x, float _y) {
|
||||
return std::make_pair(new Position(_x, _y), "Position");
|
||||
}));
|
||||
|
||||
lua.set_function("_internal_to_Position", [] (ecs::Component* component) {
|
||||
return (Position*)component;
|
||||
});
|
||||
|
||||
return component;
|
||||
};
|
||||
|
||||
preload["ecs.Velocity"] = [&lua] {
|
||||
sol::table component = lua.create_table();
|
||||
component.new_usertype<Velocity>("Velocity",
|
||||
"x", &Velocity::x, "y", &Velocity::y,
|
||||
sol::base_classes, sol::bases<ecs::Component>()
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](float _x, float _y) {
|
||||
return std::make_pair(new Velocity(_x, _y), "Velocity");
|
||||
}));
|
||||
|
||||
lua.set_function("_internal_to_Velocity", [] (ecs::Component* component) {
|
||||
return (Velocity*)component;
|
||||
});
|
||||
|
||||
return component;
|
||||
};
|
||||
|
||||
preload["ecs.Meta"] = [&lua] {
|
||||
sol::table component = lua.create_table();
|
||||
component.new_usertype<Meta>("Meta",
|
||||
"name", &Meta::name,
|
||||
sol::base_classes, sol::bases<ecs::Component>()
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](std::string _name) {
|
||||
return std::make_pair(new Meta(_name), "Meta");
|
||||
}));
|
||||
|
||||
lua.set_function("_internal_to_Meta", [] (ecs::Component* component) {
|
||||
return (Meta*)component;
|
||||
});
|
||||
|
||||
return component;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if __has_include("ecs-serial.h")
|
||||
namespace generated {
|
||||
void init() {
|
||||
ecs::serial::register_component<Position>(
|
||||
&Position::x, &Position::y
|
||||
);
|
||||
ecs::serial::register_component<Velocity>(
|
||||
&Velocity::x, &Velocity::y
|
||||
);
|
||||
ecs::serial::register_component<Meta>(
|
||||
&Meta::name
|
||||
);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
#include "/home/tim/Projects/cpp/iohelper/iohelper/include/iohelper/read.h"
|
||||
#include "/home/tim/Projects/cpp/iohelper/iohelper/include/iohelper/write.h"
|
||||
#include "components.h"
|
||||
#include "ecs-lua.h"
|
||||
#include "ecs-serial.h"
|
||||
|
@ -60,29 +58,29 @@ int main(int argc, const char** argv) {
|
|||
for (auto [a, b] : wrapper->table) {
|
||||
size++;
|
||||
}
|
||||
iohelper::write_length(os, size);
|
||||
io::write<size_t>(os, size);
|
||||
|
||||
for (auto [a, b] : wrapper->table) {
|
||||
iohelper::write<std::string>(os, a.as<std::string>());
|
||||
io::write<std::string>(os, a.as<std::string>());
|
||||
|
||||
sol::type type = b.get_type();
|
||||
iohelper::write<uint8_t>(os, (uint8_t)type);
|
||||
io::write<uint8_t>(os, (uint8_t)type);
|
||||
switch (type) {
|
||||
case sol::type::none:
|
||||
case sol::type::nil:
|
||||
break;
|
||||
|
||||
case sol::type::string:
|
||||
iohelper::write<std::string>(os, b.as<std::string>());
|
||||
io::write<std::string>(os, b.as<std::string>());
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
// @todo These should be doubles instead of floats
|
||||
iohelper::write<float>(os, b.as<float>());
|
||||
io::write<float>(os, b.as<float>());
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
iohelper::write<bool>(os, b.as<bool>());
|
||||
io::write<bool>(os, b.as<bool>());
|
||||
break;
|
||||
|
||||
case sol::type::table:
|
||||
|
@ -101,14 +99,14 @@ int main(int argc, const char** argv) {
|
|||
// @todo Only do this if table is not created yet
|
||||
wrapper->table = lua.create_table();
|
||||
|
||||
size_t size = iohelper::read_length(is);
|
||||
size_t size = io::read<size_t>(is);
|
||||
std::cout << "Size: " << size << '\n';
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
std::string name = iohelper::read<std::string>(is);
|
||||
std::string name = io::read<std::string>(is);
|
||||
|
||||
std::cout << "Name: " << name << '\n';
|
||||
|
||||
sol::type type = (sol::type)iohelper::read<uint8_t>(is);
|
||||
sol::type type = (sol::type)io::read<uint8_t>(is);
|
||||
switch (type) {
|
||||
case sol::type::none:
|
||||
break;
|
||||
|
@ -117,16 +115,16 @@ int main(int argc, const char** argv) {
|
|||
break;
|
||||
|
||||
case sol::type::string:
|
||||
wrapper->table[name] = iohelper::read<std::string>(is);
|
||||
wrapper->table[name] = io::read<std::string>(is);
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
wrapper->table[name] = iohelper::read<float>(is);
|
||||
wrapper->table[name] = io::read<float>(is);
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
{
|
||||
wrapper->table[name] = iohelper::read<bool>(is);
|
||||
wrapper->table[name] = io::read<bool>(is);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -199,7 +197,7 @@ int main(int argc, const char** argv) {
|
|||
std::ofstream file("entities", std::ios::out | std::ios::trunc);
|
||||
ecs::serial::serialize_ids(file);
|
||||
|
||||
iohelper::write_length(file, manager.view<>().size());
|
||||
io::write<size_t>(file, manager.view<>().size());
|
||||
for (auto [uuid, entity] : manager.view<>()) {
|
||||
ecs::serial::serialize(file, entity);
|
||||
}
|
||||
|
@ -221,7 +219,7 @@ int main(int argc, const char** argv) {
|
|||
|
||||
ecs::serial::deserialize_ids(file);
|
||||
|
||||
size_t entity_count = iohelper::read_length(file);
|
||||
size_t entity_count = io::read<size_t>(file);
|
||||
size_t pos = file.tellg();
|
||||
for (size_t i = 0; i < entity_count; ++i) {
|
||||
ecs::serial::deserialize(file, manager);
|
||||
|
|
|
@ -7,8 +7,8 @@ local Wrapper = require 'ecs.Wrapper'
|
|||
|
||||
manager = get_manager()
|
||||
ent = manager:create_entity()
|
||||
ent:add_component(Position.new(1.9, 9.7))
|
||||
ent:add_component(Velocity.new(0.2, 0.3))
|
||||
ent:add_component(Position.new(1.9, 9.7))
|
||||
ent:add_component(Meta.new("Soldier"))
|
||||
|
||||
data = {
|
||||
|
|
Loading…
Reference in New Issue
Block a user