3 #include <BLIB/Util/FileUtil.hpp>
14 bool parseSaveName(
const std::string& path, std::string& name,
long& time) {
15 std::string
base = bl::util::FileUtil::getBaseName(path);
33 std::string tolower(
const std::string& s) {
35 for (
auto& c : r) { c = std::tolower(c); }
41 const std::vector<std::string> saveFiles = bl::util::FileUtil::listDirectory(
44 result.reserve(saveFiles.size());
45 for (
const std::string& file : saveFiles) {
48 if (!parseSaveName(file, name, time)) {
49 BL_LOG_ERROR <<
"Bad save file: " << file;
53 result.emplace_back();
54 result.back().saveName = std::move(name);
55 result.back().saveTime = time;
56 result.back().sourceFile = file;
59 std::sort(result.begin(), result.end());
65 save.
saveTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
67 bl::event::Dispatcher::dispatch<event::GameSaveInitializing>({save,
true});
70 const std::string file =
filename(name);
71 bl::serial::json::Value data = bl::serial::json::Serializer<GameSave>::serialize(save);
72 bl::serial::json::Group& allData = *data.getAsGroup();
73 bl::serial::json::saveToFile(file, allData);
74 BL_LOG_INFO <<
"Saved game to: " << file;
80 save.sourceFile = file;
85 bl::serial::json::Value data = bl::serial::json::Serializer<GameSave>::serialize(*
this);
86 bl::serial::json::Group& allData = *data.getAsGroup();
91 bl::serial::json::Group data;
92 if (!bl::serial::json::loadFromFile(sourceFile, data)) {
93 BL_LOG_ERROR <<
"Failed to parse game save";
97 bl::event::Dispatcher::dispatch<event::GameSaveInitializing>({*
this,
false});
99 if (!bl::serial::json::Serializer<GameSave>::deserialize(*
this, {data}))
return false;
103 bl::event::Dispatcher::dispatch<event::GameSaveLoaded>(finish);
104 if (!finish.failMessage.empty()) {
105 BL_LOG_ERROR <<
"Failed to load save file'" << sourceFile
106 <<
"': " << finish.failMessage;
123 auto& d = localData.value();
Core classes and functionality for both the editor and game.
Fired when a game save is loaded. Fired after the load is complete.
Represents a game save and provides functionality to load and save.
bool operator<(const GameSave &rhs) const
For sorting.
struct core::file::GameSave::ScriptDataPointers scripts
struct core::file::GameSave::TrainerPointers trainers
struct core::file::GameSave::WorldDataPointers world
static bool loadFromFile(const std::string &sourceFile)
Loads the game from the given save file.
struct core::file::GameSave::PlayerDataPointers player
GameSave()
Initializes all pointers to the local members.
struct core::file::GameSave::InteractDataPointers interaction
unsigned long long saveTime
Local timestamp that the save was modified on.
bool load()
Loads the save represented by this object and fires an event::GameSaveLoaded event to allow systems t...
static bool saveGame(const std::string &name)
Saves the game. Fires an event::GameSaveInitializing event for systems to add their data to the save ...
static std::string filename(const std::string &saveName)
Returns the filename for the given save name.
void useLocalData()
Instantiates the save data in this object and sets all the pointers to it. Useful for loading a save ...
static void listSaves(std::vector< GameSave > &result)
Lists all saves in the save directory.
bool remove() const
Deletes the game save.
std::string saveName
Same as the player name but always valid.
void editorSave()
Saves the data back to the file it was loaded from.
struct core::file::GameSave::ClockPointers clock
std::unordered_set< std::string > * convFlags
std::unordered_map< std::string, std::unordered_set< std::string > > * talkedto
std::unordered_map< pplmn::Id, std::string > * firstSightingLocations
std::array< std::vector< pplmn::StoredPeoplemon >, player::StorageSystem::BoxCount > * storage
std::string * whiteoutMap
unsigned int * whiteoutSpawn
unsigned int * repelSteps
std::unordered_set< std::string > * visitedTowns
std::vector< pplmn::OwnedPeoplemon > * peoplemon
std::unordered_map< pplmn::Id, std::uint32_t > * seenPeoplemon
bl::tmap::Position * prevPlayerPos
bl::tmap::Position * playerPos
std::unordered_map< std::string, bl::script::Value > * entries
system::Clock::Time * time
std::unordered_set< std::string > * defeated
static const std::string & SaveExtension()
static const std::string & SaveDirectory()