bool GameSaver::SaveToFile(const std::string &filename) { bool success = false; try { if (!FileSystem::rawFileSystem.MakeDirectory(Pi::GetSaveDir())) { throw CouldNotOpenFileException(); } Serializer::Writer wr; m_game->Serialize(wr); const std::string data = wr.GetData(); FILE *f = fopen(filename.c_str(), "wb"); if (!f) throw CouldNotOpenFileException(); size_t nwritten = fwrite(data.data(), data.length(), 1, f); fclose(f); if (nwritten != 1) throw CouldNotWriteToFileException(); success = true; } catch (CouldNotOpenFileException) { Gui::Screen::ShowBadError(Lang::GAME_LOAD_CANNOT_OPEN); } catch (CouldNotWriteToFileException) { Gui::Screen::ShowBadError(Lang::GAME_SAVE_CANNOT_WRITE); } return success; }
void Game::SaveGame(const std::string &filename, Game *game) { assert(game); if (!FileSystem::userFiles.MakeDirectory(Pi::SAVE_DIR_NAME)) { throw CouldNotOpenFileException(); } Serializer::Writer wr; game->Serialize(wr); const std::string data = wr.GetData(); FILE *f = FileSystem::userFiles.OpenWriteStream(FileSystem::JoinPathBelow(Pi::SAVE_DIR_NAME, filename)); if (!f) throw CouldNotOpenFileException(); size_t nwritten = fwrite(data.data(), data.length(), 1, f); fclose(f); if (nwritten != 1) throw CouldNotWriteToFileException(); }