SDL fallback if Windows::GetPreferencesFolder fails

This commit is contained in:
Iliyas Jorio 2023-09-17 21:26:05 +02:00
parent 9fae17d771
commit 60f1eeaf58
2 changed files with 42 additions and 4 deletions

View File

@ -11,6 +11,7 @@
#include "CompilerSupport/filesystem.h"
#if _WIN32
#include <SDL.h>
#include "Platform/Windows/PommeWindows.h"
#endif
@ -173,9 +174,25 @@ OSErr FindFolder(short vRefNum, OSType folderType, Boolean createFolder, short*
{
#ifdef _WIN32
path = Pomme::Platform::Windows::GetPreferencesFolder();
if (path.empty())
{
const char* fallback = SDL_GetPrefPath("", "PommeFallback");
if (fallback)
{
TODOMINOR2("SDL fallback path is: " << fallback);
path = fallback;
SDL_free((void*) fallback);
}
else
{
TODO2("SDL fallback path failure!");
return fnfErr;
}
}
#elif defined(__APPLE__)
const char *home = getenv("HOME");
if (!home) {
if (!home)
{
return fnfErr;
}
path = fs::path(home) / "Library" / "Preferences";

View File

@ -1,14 +1,35 @@
#undef NOUSER
#include "PommeDebug.h"
#include "Platform/Windows/PommeWindows.h"
#include <shlobj.h>
std::filesystem::path Pomme::Platform::Windows::GetPreferencesFolder()
{
wchar_t* wpath = nullptr;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &wpath);
auto path = std::filesystem::path(wpath);
PWSTR wpath = nullptr;
HRESULT result = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &wpath);
std::filesystem::path path;
if (result != S_OK)
{
TODO2("SHGetKnownFolderPath returned " << result);
path = "";
}
else
{
try
{
path = std::filesystem::path(wpath);
}
catch (std::exception& ex)
{
TODO2("Conversion from wpath failed " << ex.what());
path = "";
}
}
CoTaskMemFree(static_cast<void*>(wpath));
return path;
}