Write save games to a memory stream first

Two motivations for doing this:

- If the user chooses to overwrite existing save file, and there is an exception during the save process, the existing file will not be lost.
- Many small writes to a file are slow. Very slow. Writing to memory first then writing the completed file to disk appears to be ~500% faster.
openmw-38
scrawl 9 years ago
parent 67a6a8f5d4
commit 66bcd2fd68

@ -207,7 +207,9 @@ void MWState::StateManager::saveGame (const std::string& description, const Slot
else
slot = getCurrentCharacter()->updateSlot (slot, profile);
boost::filesystem::ofstream stream (slot->mPath, std::ios::binary);
// Write to a memory stream first. If there is an exception during the save process, we don't want to trash the
// existing save file we are overwriting.
std::stringstream stream;
ESM::ESMWriter writer;
@ -262,7 +264,14 @@ void MWState::StateManager::saveGame (const std::string& description, const Slot
writer.close();
if (stream.fail())
throw std::runtime_error("Write operation failed");
throw std::runtime_error("Write operation failed (memory stream)");
// All good, write to file
boost::filesystem::ofstream filestream (slot->mPath, std::ios::binary);
filestream << stream.rdbuf();
if (filestream.fail())
throw std::runtime_error("Write operation failed (file stream)");
Settings::Manager::setString ("character", "Saves",
slot->mPath.parent_path().filename().string());

Loading…
Cancel
Save