diff --git a/Minecraft.World/ConsoleSaveFileOriginal.cpp b/Minecraft.World/ConsoleSaveFileOriginal.cpp index 5173fff2..3093f3a8 100644 --- a/Minecraft.World/ConsoleSaveFileOriginal.cpp +++ b/Minecraft.World/ConsoleSaveFileOriginal.cpp @@ -2,6 +2,8 @@ #include "StringHelpers.h" #include "ConsoleSaveFileOriginal.h" #include "File.h" +#include +#include #include #include "compression.h" #include "../Minecraft.Client/Minecraft.h" @@ -965,35 +967,50 @@ int ConsoleSaveFileOriginal::SaveSaveDataCallback(LPVOID lpParam,bool bRes) app.DebugPrintf("SaveSaveDataCallback: captured save folder '%s'\n", szSaveFolder); } // Try 2: Scan GameHDD for the newest folder -- right after save, it's guaranteed to be ours - if (app.GetCurrentSaveFolderName().empty()) - { - WIN32_FIND_DATAW fd; - HANDLE hFind = FindFirstFileW(L"Windows64\\GameHDD\\*", &fd); - if (hFind != INVALID_HANDLE_VALUE) - { - FILETIME newestTime = {}; - wchar_t newestFolder[MAX_PATH] = {}; - do - { - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && - wcscmp(fd.cFileName, L".") != 0 && wcscmp(fd.cFileName, L"..") != 0) - { - if (CompareFileTime(&fd.ftLastWriteTime, &newestTime) > 0) - { - newestTime = fd.ftLastWriteTime; - wcscpy_s(newestFolder, MAX_PATH, fd.cFileName); - } - } - } while (FindNextFileW(hFind, &fd)); - FindClose(hFind); - if (newestFolder[0] != L'\0') - { - app.SetCurrentSaveFolderName(newestFolder); - app.DebugPrintf("SaveSaveDataCallback: captured save folder via scan '%ls'\n", newestFolder); - } - } - } - } + if (app.GetCurrentSaveFolderName().empty()) + { + std::filesystem::path previousEntry = ""; + if (std::filesystem::exists("Windows64/GameHDD/")) + { + for (const auto &entry : std::filesystem::directory_iterator("Windows64/GameHDD/")) + { + if (previousEntry == "") + { + // The initial previous entry is always an empty string, instead of checking the last write time, we skip to next iteration + // and set previous entry to current path if its not a file + if (std::filesystem::is_directory(entry.path())) + previousEntry = entry.path(); + else + app.DebugPrintf("Not a directory !\n"); + continue; + } + if (!std::filesystem::is_directory(entry.path())) + { + // Go to next iteration if we are looking at a file and not a directory + continue; + } + if (std::filesystem::last_write_time(previousEntry) > std::filesystem::last_write_time(entry.path())) + { + // if the previous file is still newer than the next one we choosed, we skip to next iteration again + continue; + } + else + { + // And now that we are sure that the current iteration's entry is newer than the old entry, we can replace it + previousEntry = entry.path(); + } + } + if (!previousEntry.empty()) + // If the GameHDD directory is empty then we can't set to previous entry or otherwise it would be an empty string. + // The Windows version simply skipped setting it and kept its previous value, hence the empty() check + app.SetCurrentSaveFolderName(previousEntry.filename().wstring()); + } + else + { + app.DebugPrintf("Windows64/GameHDD does not exist !\n"); + } + } + } #endif #if defined(MINECRAFT_SERVER_BUILD) || (defined(_WINDOWS64) || defined(__linux__)) || defined(_DURANGO) || defined(__ORBIS__)