mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-02-01 02:15:32 +00:00
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.
This commit is contained in:
parent
67a6a8f5d4
commit
66bcd2fd68
1 changed files with 11 additions and 2 deletions
|
@ -207,7 +207,9 @@ void MWState::StateManager::saveGame (const std::string& description, const Slot
|
||||||
else
|
else
|
||||||
slot = getCurrentCharacter()->updateSlot (slot, profile);
|
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;
|
ESM::ESMWriter writer;
|
||||||
|
|
||||||
|
@ -262,7 +264,14 @@ void MWState::StateManager::saveGame (const std::string& description, const Slot
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
||||||
if (stream.fail())
|
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",
|
Settings::Manager::setString ("character", "Saves",
|
||||||
slot->mPath.parent_path().filename().string());
|
slot->mPath.parent_path().filename().string());
|
||||||
|
|
Loading…
Reference in a new issue