Files

84 lines
2.1 KiB
C++
Raw Permalink Normal View History

#include "Runtime/ConsoleVariables/FileStoreManager.hpp"
2019-09-04 17:22:05 -04:00
#include "Runtime/CBasics.hpp"
#include "Runtime/Logging.hpp"
2019-09-04 17:22:05 -04:00
2025-04-03 00:14:34 -06:00
#include <SDL3/SDL.h>
#if _WIN32
#include <nowide/convert.hpp>
#endif
2019-09-04 17:22:05 -04:00
2015-11-14 00:48:31 -08:00
#if _WIN32
#include <ShlObj.h>
#endif
2015-11-12 16:12:09 -10:00
2017-12-05 17:22:31 -10:00
#if WINDOWS_STORE
using namespace Windows::Storage;
#endif
namespace metaforce {
namespace {
FileStoreManager* g_instance = nullptr;
}
2015-11-12 16:12:09 -10:00
FileStoreManager::FileStoreManager(std::string_view org, std::string_view domain) : m_org(org), m_domain(domain) {
2022-02-27 18:02:33 -08:00
if (g_instance != nullptr) {
spdlog::fatal("Attempting to build another FileStoreManager!!");
}
auto prefPath = SDL_GetPrefPath(org.data(), domain.data());
if (prefPath == nullptr) {
2015-11-12 16:12:09 -10:00
#if _WIN32
2017-12-05 17:22:31 -10:00
#if !WINDOWS_STORE
WCHAR home[MAX_PATH];
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, home)))
spdlog::fatal("unable to locate profile for file store");
2015-11-14 00:48:31 -08:00
std::string path = nowide::narrow(home);
2017-12-05 17:22:31 -10:00
#else
StorageFolder ^ cacheFolder = ApplicationData::Current->LocalCacheFolder;
std::string path(cacheFolder->Path->Data());
2017-12-05 17:22:31 -10:00
#endif
path += "/." + m_org;
2015-11-14 00:48:31 -08:00
CBasics::MakeDir(path.c_str());
path += '/';
path += domain.data();
2015-11-14 00:48:31 -08:00
CBasics::MakeDir(path.c_str());
m_storeRoot = path;
2015-11-12 16:12:09 -10:00
#else
const char* xdg_data_home = getenv("XDG_DATA_HOME");
std::string path;
if (xdg_data_home) {
if (xdg_data_home[0] != '/')
spdlog::fatal("invalid $XDG_DATA_HOME for file store (must be absolute)");
path = xdg_data_home;
} else {
const char* home = getenv("HOME");
if (!home)
spdlog::fatal("unable to locate $HOME for file store");
path = home;
path += "/.local/share";
}
path += "/" + m_org + "/" + domain.data();
if (CBasics::RecursiveMakeDir(path.c_str()) != 0) {
spdlog::fatal("unable to mkdir at {}", path);
}
m_storeRoot = path;
#endif
} else {
m_storeRoot = std::string(prefPath);
2022-03-08 02:44:46 -05:00
SDL_free(prefPath);
}
g_instance = this;
2015-11-12 16:12:09 -10:00
}
FileStoreManager* FileStoreManager::instance() {
if (g_instance == nullptr) {
spdlog::fatal("Requested FileStoreManager instance before it's built!");
}
return g_instance;
}
} // namespace metaforce