Big revelation is that anytime you're doing file I/O you _must_ use std::filesystem. It simplifies so much about working with files.

main
Zed A. Shaw 3 weeks ago
parent da63f006c2
commit 6add24fed2
  1. 17
      save.cpp
  2. 6
      save.hpp

@ -3,9 +3,10 @@
#include "dbc.hpp"
#include <fmt/core.h>
#include "config.hpp"
#include <filesystem>
using namespace components;
using namespace fmt;
template<typename CompT>
inline void extract(DinkyECS::World &world, std::map<DinkyECS::Entity, CompT> &into) {
@ -15,7 +16,7 @@ inline void extract(DinkyECS::World &world, std::map<DinkyECS::Entity, CompT> &i
}
}
void save::to_file(std::string path, DinkyECS::World &world) {
void save::to_file(fs::path path, DinkyECS::World &world) {
SaveData save_data;
tser::BinaryArchive archive;
@ -40,22 +41,22 @@ inline void inject(DinkyECS::World &world, std::map<DinkyECS::Entity, CompT> &ou
}
}
void save::from_file(std::string path, DinkyECS::World &world_out) {
void save::from_file(fs::path path, DinkyECS::World &world_out) {
tser::BinaryArchive archive(0);
dbc::check(fs::exists(path), format("save file does not exist {}", path.string()));
auto size = fs::file_size(path);
if(std::ifstream in_file{path, std::ios::binary | std::ios::ate}) {
auto size = in_file.tellg();
if(std::ifstream in_file{path, std::ios::binary}) {
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));
dbc::sentinel(format("wrong size or error reading {}", path.string()));
}
} else {
dbc::sentinel(fmt::format("failed to load file {}", path));
dbc::sentinel(format("failed to load file {}", path.string()));
}
auto save_data = archive.load<SaveData>();

@ -3,10 +3,12 @@
#include "components.hpp"
#include "dinkyecs.hpp"
#include "tser.hpp"
#include <filesystem>
#include <string>
#include <map>
namespace save {
namespace fs = std::filesystem;
struct Facts {
components::Player player;
@ -24,7 +26,7 @@ namespace save {
DEFINE_SERIALIZABLE(SaveData, facts, position, motion, combat);
};
void to_file(std::string path, DinkyECS::World &world);
void from_file(std::string path, DinkyECS::World &world_out);
void to_file(fs::path path, DinkyECS::World &world);
void from_file(fs::path path, DinkyECS::World &world_out);
void load_configs(DinkyECS::World &world);
}

Loading…
Cancel
Save