diff --git a/Adventures in Lestoria/SaveFile.cpp b/Adventures in Lestoria/SaveFile.cpp index c091e081..5d271381 100644 --- a/Adventures in Lestoria/SaveFile.cpp +++ b/Adventures in Lestoria/SaveFile.cpp @@ -190,9 +190,6 @@ const void SaveFile::SaveGame(){ saveSystemFile["Window Size"].SetInt(game->GetWindowSize().y,1); saveSystemFile["Fullscreen"].SetBool(game->IsFullscreen()); #pragma endregion - - saveFile["Hash"].SetString(""); - for(auto&[mapName,chunks]:game->minimap.GetChunkData()){ size_t chunkInd=0; @@ -203,25 +200,30 @@ const void SaveFile::SaveGame(){ } #pragma region Save save file and prep File Hash - utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); + bool fileNoHashWrittenSuccessfully{false}; + bool fileHashWrittenSuccessfully{false}; + while(!fileNoHashWrittenSuccessfully||!fileHashWrittenSuccessfully){ + saveFile["Hash"].SetString(""); + fileNoHashWrittenSuccessfully=utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); - std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID)); - saveFile["Hash"].SetString(fileHash); + std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID)); + saveFile["Hash"].SetString(fileHash); - 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. + 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. + } #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. } - utils::datafile::Write(saveSystemFile,"save_file_path"_S+"system.conf"); + while(!utils::datafile::Write(saveSystemFile,"save_file_path"_S+"system.conf")); utils::datafile metadata; if(onlineMode){ if(std::filesystem::exists("save_file_path"_S+"metadata.dat"+"_online")){ - utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"+"_online"); + while(!utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"+"_online")); } }else{ if(std::filesystem::exists("save_file_path"_S+"metadata.dat")){ - utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"); + while(!utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat")); } } metadata.GetProperty(std::format("save{}",saveFileID)).SetReal(game->GetRunTime(),0U); @@ -230,9 +232,9 @@ const void SaveFile::SaveGame(){ metadata.GetProperty(std::format("save{}",saveFileID)).SetString(game->GetPlayer()->GetClassName(),3U); metadata.GetProperty(std::format("save{}",saveFileID)).SetString(std::string(SaveFile::GetSaveFileName()),4U); if(!onlineMode){ - utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat"); + while(!utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat")); }else{ - utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat"+"_online"); + while(!utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat"+"_online")); } utils::datafile::INITIAL_SETUP_COMPLETE=true; @@ -327,25 +329,31 @@ void SaveFile::LoadFile(){ } if(loadFile.HasProperty("Hash")){ std::string expectedFileHash=loadFile["Hash"].GetString(); - loadFile["Hash"].SetString(""); - utils::datafile::Write(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); + bool fileNoHashWrittenSuccessfully{false}; + bool fileHashWrittenSuccessfully{false}; + while(!fileNoHashWrittenSuccessfully||!fileHashWrittenSuccessfully){ + fileNoHashWrittenSuccessfully=false; + fileHashWrittenSuccessfully=false; + loadFile["Hash"].SetString(""); + fileNoHashWrittenSuccessfully=utils::datafile::Write(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); - auto trim = [](std::string& s) - { - s.erase(0, s.find_first_not_of(" \t\n\r\f\v")); - s.erase(s.find_last_not_of(" \t\n\r\f\v") + 1); - }; + auto trim = [](std::string& s) + { + s.erase(0, s.find_first_not_of(" \t\n\r\f\v")); + 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)); - 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. + std::string fileHash=util::GetHash("save_file_path"_S+std::format("save.{:04}",saveFileID)); + 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){ LOG(std::format("WARNING! Filehash for file {} was not identified as proper! Will not load this file!","save_file_path"_S+std::format("save.{:04}",saveFileID))); return; } - loadFile["Hash"].SetString(expectedFileHash); //Now write the hash back into the file since we tampered with it. - utils::datafile::Write(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); + 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)); + } } } game->ResetGame(); diff --git a/Adventures in Lestoria/Version.h b/Adventures in Lestoria/Version.h index 6a56178d..cda3a935 100644 --- a/Adventures in Lestoria/Version.h +++ b/Adventures in Lestoria/Version.h @@ -38,8 +38,8 @@ All rights reserved. #pragma once #define VERSION_MAJOR 1 #define VERSION_MINOR 2 -#define VERSION_PATCH 3 -#define VERSION_BUILD 11130 +#define VERSION_PATCH 5 +#define VERSION_BUILD 11132 #define stringify(a) stringify_(a) #define stringify_(a) #a diff --git a/Adventures in Lestoria/assets/gamepack.pak b/Adventures in Lestoria/assets/gamepack.pak index 26ee6c59..810e5954 100644 Binary files a/Adventures in Lestoria/assets/gamepack.pak and b/Adventures in Lestoria/assets/gamepack.pak differ diff --git a/Adventures in Lestoria/olcUTIL_DataFile.h b/Adventures in Lestoria/olcUTIL_DataFile.h index 9864c4b5..eba60e99 100644 --- a/Adventures in Lestoria/olcUTIL_DataFile.h +++ b/Adventures in Lestoria/olcUTIL_DataFile.h @@ -325,6 +325,7 @@ namespace olc::utils { // Write the file starting form the supplied node write(n, file); + file.close(); return true; } return false; diff --git a/x64/Release/Adventures in Lestoria.exe b/x64/Release/Adventures in Lestoria.exe index 0dad8d8c..cd9ca20d 100644 Binary files a/x64/Release/Adventures in Lestoria.exe and b/x64/Release/Adventures in Lestoria.exe differ