You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
64 lines
1.6 KiB
2 weeks ago
|
#include "save.hpp"
|
||
|
#include <fstream>
|
||
|
#include "dbc.hpp"
|
||
|
#include <fmt/core.h>
|
||
|
|
||
|
using namespace components;
|
||
|
|
||
|
|
||
|
template<typename CompT>
|
||
|
inline void extract(DinkyECS::World &world, std::vector<CompT> &into) {
|
||
|
auto from_world = world.entity_map_for<CompT>();
|
||
|
for(auto [entity, value] : from_world) {
|
||
|
into.push_back(std::any_cast<CompT>(value));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void save::to_file(std::string path, DinkyECS::World &world) {
|
||
|
SaveData save_data;
|
||
|
tser::BinaryArchive archive;
|
||
|
|
||
|
save_data.player = world.get_the<Player>();
|
||
|
extract<Position>(world, save_data.position);
|
||
|
extract<Combat>(world, save_data.combat);
|
||
|
extract<Motion>(world, save_data.motion);
|
||
|
|
||
|
archive.save(save_data);
|
||
|
std::string_view archive_view = archive.get_buffer();
|
||
|
|
||
|
std::ofstream out(path, std::ios::binary);
|
||
|
out << archive_view;
|
||
|
out.flush();
|
||
|
}
|
||
|
|
||
|
|
||
|
void save::from_file(std::string path, DinkyECS::World &world_out) {
|
||
|
tser::BinaryArchive archive(0);
|
||
|
|
||
|
if(std::ifstream in_file{path, std::ios::binary | std::ios::ate}) {
|
||
|
auto size = in_file.tellg();
|
||
|
std::string in_data(size, '\0');
|
||
|
in_file.seekg(0);
|
||
|
|
||
|
if(in_file.read(&in_data[0], size)) {
|
||
|
std::string_view in_view(in_data);
|
||
|
archive.initialize(in_view);
|
||
|
} else {
|
||
|
dbc::sentinel(fmt::format("wrong size or error reading {}", path));
|
||
|
}
|
||
|
} else {
|
||
|
dbc::sentinel(fmt::format("failed to load file {}", path));
|
||
|
}
|
||
|
|
||
|
auto save_data = archive.load<SaveData>();
|
||
|
|
||
|
// BUG: need the entities!
|
||
|
world_out.set_the<Player>(save_data.player);
|
||
|
|
||
|
for(auto position : save_data.position) {
|
||
|
auto entity = world_out.entity();
|
||
|
// BUG: actually do need the entities
|
||
|
world_out.set<Position>(entity, position);
|
||
|
}
|
||
|
}
|