From 6add24fed2ba2e87ca8666aa206176914feebeed Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 6 Nov 2024 02:40:29 -0500 Subject: [PATCH] Big revelation is that anytime you're doing file I/O you _must_ use std::filesystem. It simplifies so much about working with files. --- save.cpp | 17 +++++++++-------- save.hpp | 6 ++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/save.cpp b/save.cpp index 036942b..27f4919 100644 --- a/save.cpp +++ b/save.cpp @@ -3,9 +3,10 @@ #include "dbc.hpp" #include #include "config.hpp" +#include using namespace components; - +using namespace fmt; template inline void extract(DinkyECS::World &world, std::map &into) { @@ -15,7 +16,7 @@ inline void extract(DinkyECS::World &world, std::map &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 &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(); diff --git a/save.hpp b/save.hpp index c7ea42b..389616b 100644 --- a/save.hpp +++ b/save.hpp @@ -3,10 +3,12 @@ #include "components.hpp" #include "dinkyecs.hpp" #include "tser.hpp" +#include #include #include 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); }