Prevent save file corruption while hashing by putting the hashing file in a temporary file instead... Release Build 11201.

pull/65/head
sigonasr2 3 months ago
parent 679dcdf490
commit 85124b8875
  1. 15
      Adventures in Lestoria/SaveFile.cpp
  2. 2
      Adventures in Lestoria/Version.h
  3. BIN
      x64/Release/Adventures in Lestoria.exe

@ -204,13 +204,16 @@ const void SaveFile::SaveGame(){
bool fileHashWrittenSuccessfully{false};
while(!fileNoHashWrittenSuccessfully||!fileHashWrittenSuccessfully){
saveFile["Hash"].SetString("");
fileNoHashWrittenSuccessfully=utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID));
fileNoHashWrittenSuccessfully=utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)+".tmp");
std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID));
std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID)+".tmp");
saveFile["Hash"].SetString(fileHash);
fileHashWrittenSuccessfully=utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); //Once the hash has been computed and added, save the file a second time.
}
std::error_code result;
std::filesystem::remove("save_file_path"_S+std::format("save.{:04}",saveFileID)+".tmp",result);
if(!result)LOG(std::format("WARNING! Could not delete temporary save file. Error: {}",result.message()));
#pragma endregion
//WARNING! DO NOT WRITE ANY CODE BELOW HERE!!!!! THE HASH HAS ALREADY BEEN WRITTEN.
//FILES BECOME CORRUPTED IF THE SAVE FILE IS MODIFIED FROM HERE ONWARDS.
@ -335,7 +338,7 @@ void SaveFile::LoadFile(){
fileNoHashWrittenSuccessfully=false;
fileHashWrittenSuccessfully=false;
loadFile["Hash"].SetString("");
fileNoHashWrittenSuccessfully=utils::datafile::Write(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID));
fileNoHashWrittenSuccessfully=utils::datafile::Write(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)+".tmp");
auto trim = [](std::string& s)
{
@ -343,7 +346,7 @@ void SaveFile::LoadFile(){
s.erase(s.find_last_not_of(" \t\n\r\f\v") + 1);
};
std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID));
std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID)+".tmp");
trim(fileHash); //It's possible the expected file hash has a space at the end/beginning that gets stripped out. We want to trim and match that string.
if(!ADMIN_MODE&&expectedFileHash!=fileHash){
@ -354,6 +357,10 @@ void SaveFile::LoadFile(){
loadFile["Hash"].SetString(expectedFileHash); //Now write the hash back into the file since we tampered with it.
fileHashWrittenSuccessfully=utils::datafile::Write(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID));
}
std::error_code result;
std::filesystem::remove("save_file_path"_S+std::format("save.{:04}",saveFileID)+".tmp",result);
if(!result)LOG(std::format("WARNING! Could not delete temporary save file. Error: {}",result.message()));
}
}
game->ResetGame();

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 1
#define VERSION_MINOR 2
#define VERSION_PATCH 5
#define VERSION_BUILD 11200
#define VERSION_BUILD 11201
#define stringify(a) stringify_(a)
#define stringify_(a) #a

Loading…
Cancel
Save