@ -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 ( ) ;