Peoplemon  0.1.0
Peoplemon 3 game source documentation
GameSave.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Util/FileUtil.hpp>
5 #include <Core/Properties.hpp>
6 #include <chrono>
7 
8 namespace core
9 {
10 namespace file
11 {
12 namespace
13 {
14 bool parseSaveName(const std::string& path, std::string& name, long& time) {
15  std::string base = bl::util::FileUtil::getBaseName(path);
16  name = base;
17  time = 0;
18  return true;
19  // TODO - add method to stat file to blib fileutil
20  /*
21  const auto i = base.find_last_of('_');
22  if (i == std::string::npos) return false;
23  name = base.substr(0, i);
24 
25  base = base.substr(i + 1);
26  for (char t : base) {
27  if (!std::isdigit(t)) return false;
28  }
29  time = std::atol(base.c_str());
30  return true;*/
31 }
32 
33 std::string tolower(const std::string& s) {
34  std::string r(s);
35  for (auto& c : r) { c = std::tolower(c); }
36  return r;
37 }
38 } // namespace
39 
40 void GameSave::listSaves(std::vector<GameSave>& result) {
41  const std::vector<std::string> saveFiles = bl::util::FileUtil::listDirectory(
43 
44  result.reserve(saveFiles.size());
45  for (const std::string& file : saveFiles) {
46  std::string name;
47  long time;
48  if (!parseSaveName(file, name, time)) {
49  BL_LOG_ERROR << "Bad save file: " << file;
50  continue;
51  }
52 
53  result.emplace_back();
54  result.back().saveName = std::move(name);
55  result.back().saveTime = time;
56  result.back().sourceFile = file;
57  }
58 
59  std::sort(result.begin(), result.end());
60 }
61 
62 bool GameSave::saveGame(const std::string& name) {
63  GameSave save;
64  save.saveName = name;
65  save.saveTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
66  if (!Properties::InEditor()) {
67  bl::event::Dispatcher::dispatch<event::GameSaveInitializing>({save, true});
68  }
69 
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;
75  return true; // TODO - modify above to return boolean
76 }
77 
78 bool GameSave::loadFromFile(const std::string& file) {
79  GameSave save;
80  save.sourceFile = file;
81  return save.load();
82 }
83 
85  bl::serial::json::Value data = bl::serial::json::Serializer<GameSave>::serialize(*this);
86  bl::serial::json::Group& allData = *data.getAsGroup();
87  bl::serial::json::saveToFile(filename(*player.playerName), allData);
88 }
89 
91  bl::serial::json::Group data;
92  if (!bl::serial::json::loadFromFile(sourceFile, data)) {
93  BL_LOG_ERROR << "Failed to parse game save";
94  return false;
95  }
96  if (!Properties::InEditor()) {
97  bl::event::Dispatcher::dispatch<event::GameSaveInitializing>({*this, false});
98  }
99  if (!bl::serial::json::Serializer<GameSave>::deserialize(*this, {data})) return false;
100 
101  if (!Properties::InEditor()) {
102  event::GameSaveLoaded finish{""};
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;
107  return false;
108  }
109  }
110 
111  return true;
112 }
113 
114 bool GameSave::remove() const { return bl::util::FileUtil::deleteFile(sourceFile); }
115 
116 bool GameSave::operator<(const GameSave& rhs) const {
117  if (saveTime < rhs.saveTime) return true;
118  return !(tolower(saveName) < tolower(rhs.saveName));
119 }
120 
122  localData.emplace();
123  auto& d = localData.value();
124 
125  player.inventory = &d.inventory;
126  player.monei = &d.monei;
127  player.peoplemon = &d.peoplemon;
128  player.playerName = &d.playerName;
129  player.sex = &d.sex;
130  player.whiteoutMap = &d.whiteoutMap;
131  player.whiteoutSpawn = &d.whiteoutSpawn;
132  player.repelSteps = &d.repelSteps;
133  player.storage = &d.storage;
134  player.visitedTowns = &d.visitedTowns;
135  player.seenPeoplemon = &d.seenPeoplemon;
136  player.firstSightingLocations = &d.firstSightingLocations;
137 
138  interaction.convFlags = &d.convFlags;
139  interaction.talkedto = &d.talkedto;
140  scripts.entries = &d.entries;
141 
142  world.currentMap = &d.currentMap;
143  world.playerPos = &d.playerPos;
144  world.prevMap = &d.prevMap;
145  world.prevPlayerPos = &d.prevPlayerPos;
146 
147  clock.time = &d.clockTime;
148 
149  trainers.defeated = &d.defeatedTrainers;
150 }
151 
153  player.inventory = nullptr;
154  player.monei = nullptr;
155  player.peoplemon = nullptr;
156  player.playerName = nullptr;
157  player.sex = nullptr;
158  player.whiteoutMap = nullptr;
159  player.whiteoutSpawn = nullptr;
160  player.repelSteps = nullptr;
161  player.storage = nullptr;
162  player.visitedTowns = nullptr;
163  player.seenPeoplemon = nullptr;
164  player.firstSightingLocations = nullptr;
165 
166  interaction.convFlags = nullptr;
167  interaction.talkedto = nullptr;
168  scripts.entries = nullptr;
169 
170  world.currentMap = nullptr;
171  world.playerPos = nullptr;
172  world.prevMap = nullptr;
173  world.prevPlayerPos = nullptr;
174 
175  clock.time = nullptr;
176 
177  trainers.defeated = nullptr;
178 }
179 
180 std::string GameSave::filename(const std::string& name) {
181  return bl::util::FileUtil::joinPath(Properties::SaveDirectory(), name) + "." +
183 }
184 
185 } // namespace file
186 } // namespace core
std::uint32_t base
Definition: WildIntro.cpp:26
Core classes and functionality for both the editor and game.
Fired when a game save is loaded. Fired after the load is complete.
Definition: GameSave.hpp:46
Represents a game save and provides functionality to load and save.
Definition: GameSave.hpp:24
bool operator<(const GameSave &rhs) const
For sorting.
Definition: GameSave.cpp:116
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.
Definition: GameSave.cpp:78
struct core::file::GameSave::PlayerDataPointers player
GameSave()
Initializes all pointers to the local members.
Definition: GameSave.cpp:152
struct core::file::GameSave::InteractDataPointers interaction
unsigned long long saveTime
Local timestamp that the save was modified on.
Definition: GameSave.hpp:26
bool load()
Loads the save represented by this object and fires an event::GameSaveLoaded event to allow systems t...
Definition: GameSave.cpp:90
static bool saveGame(const std::string &name)
Saves the game. Fires an event::GameSaveInitializing event for systems to add their data to the save ...
Definition: GameSave.cpp:62
static std::string filename(const std::string &saveName)
Returns the filename for the given save name.
Definition: GameSave.cpp:180
void useLocalData()
Instantiates the save data in this object and sets all the pointers to it. Useful for loading a save ...
Definition: GameSave.cpp:121
static void listSaves(std::vector< GameSave > &result)
Lists all saves in the save directory.
Definition: GameSave.cpp:40
bool remove() const
Deletes the game save.
Definition: GameSave.cpp:114
std::string saveName
Same as the player name but always valid.
Definition: GameSave.hpp:29
void editorSave()
Saves the data back to the file it was loaded from.
Definition: GameSave.cpp:84
struct core::file::GameSave::ClockPointers clock
std::unordered_set< std::string > * convFlags
Definition: GameSave.hpp:34
std::unordered_map< std::string, std::unordered_set< std::string > > * talkedto
Definition: GameSave.hpp:33
std::unordered_map< pplmn::Id, std::string > * firstSightingLocations
Definition: GameSave.hpp:50
std::array< std::vector< pplmn::StoredPeoplemon >, player::StorageSystem::BoxCount > * storage
Definition: GameSave.hpp:47
std::unordered_set< std::string > * visitedTowns
Definition: GameSave.hpp:48
std::vector< pplmn::OwnedPeoplemon > * peoplemon
Definition: GameSave.hpp:43
std::unordered_map< pplmn::Id, std::uint32_t > * seenPeoplemon
Definition: GameSave.hpp:49
bl::tmap::Position * prevPlayerPos
Definition: GameSave.hpp:58
std::unordered_map< std::string, bl::script::Value > * entries
Definition: GameSave.hpp:63
system::Clock::Time * time
Definition: GameSave.hpp:68
std::unordered_set< std::string > * defeated
Definition: GameSave.hpp:73
static bool InEditor()
Definition: Properties.cpp:266
static const std::string & SaveExtension()
Definition: Properties.cpp:274
static const std::string & SaveDirectory()
Definition: Properties.cpp:268