2022-02-20 18:28:07 -08:00
|
|
|
#include "Runtime/ConsoleVariables/FileStoreManager.hpp"
|
2019-09-04 17:22:05 -04:00
|
|
|
|
2022-02-20 18:28:07 -08:00
|
|
|
#include "Runtime/CBasics.hpp"
|
2025-04-03 21:07:07 -06:00
|
|
|
#include "Runtime/Logging.hpp"
|
2019-09-04 17:22:05 -04:00
|
|
|
|
2025-04-03 00:14:34 -06:00
|
|
|
#include <SDL3/SDL.h>
|
2022-02-22 00:53:57 -05:00
|
|
|
#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
|
|
|
|
|
|
2022-02-20 18:28:07 -08:00
|
|
|
namespace metaforce {
|
2022-02-27 17:55:47 -08:00
|
|
|
namespace {
|
|
|
|
|
FileStoreManager* g_instance = nullptr;
|
|
|
|
|
}
|
2015-11-12 16:12:09 -10:00
|
|
|
|
2022-02-27 17:35:13 -08: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) {
|
2025-04-03 21:07:07 -06:00
|
|
|
spdlog::fatal("Attempting to build another FileStoreManager!!");
|
2022-02-27 17:55:47 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-27 17:35:13 -08:00
|
|
|
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
|
2022-02-27 17:35:13 -08:00
|
|
|
WCHAR home[MAX_PATH];
|
|
|
|
|
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, home)))
|
2025-04-03 21:07:07 -06:00
|
|
|
spdlog::fatal("unable to locate profile for file store");
|
2015-11-14 00:48:31 -08:00
|
|
|
|
2022-02-27 17:35:13 -08:00
|
|
|
std::string path = nowide::narrow(home);
|
2017-12-05 17:22:31 -10:00
|
|
|
#else
|
2022-02-27 17:35:13 -08:00
|
|
|
StorageFolder ^ cacheFolder = ApplicationData::Current->LocalCacheFolder;
|
|
|
|
|
std::string path(cacheFolder->Path->Data());
|
2017-12-05 17:22:31 -10:00
|
|
|
#endif
|
2022-02-27 17:35:13 -08:00
|
|
|
path += "/." + m_org;
|
2015-11-14 00:48:31 -08:00
|
|
|
|
2022-02-27 17:35:13 -08:00
|
|
|
CBasics::MakeDir(path.c_str());
|
|
|
|
|
path += '/';
|
|
|
|
|
path += domain.data();
|
2015-11-14 00:48:31 -08:00
|
|
|
|
2022-02-27 17:35:13 -08:00
|
|
|
CBasics::MakeDir(path.c_str());
|
|
|
|
|
m_storeRoot = path;
|
2015-11-12 16:12:09 -10:00
|
|
|
#else
|
2022-02-27 17:35:13 -08:00
|
|
|
const char* xdg_data_home = getenv("XDG_DATA_HOME");
|
|
|
|
|
std::string path;
|
|
|
|
|
if (xdg_data_home) {
|
|
|
|
|
if (xdg_data_home[0] != '/')
|
2025-04-03 21:07:07 -06:00
|
|
|
spdlog::fatal("invalid $XDG_DATA_HOME for file store (must be absolute)");
|
2022-02-27 17:35:13 -08:00
|
|
|
path = xdg_data_home;
|
|
|
|
|
} else {
|
|
|
|
|
const char* home = getenv("HOME");
|
|
|
|
|
if (!home)
|
2025-04-03 21:07:07 -06:00
|
|
|
spdlog::fatal("unable to locate $HOME for file store");
|
2022-02-27 17:35:13 -08:00
|
|
|
path = home;
|
|
|
|
|
path += "/.local/share";
|
|
|
|
|
}
|
|
|
|
|
path += "/" + m_org + "/" + domain.data();
|
|
|
|
|
if (CBasics::RecursiveMakeDir(path.c_str()) != 0) {
|
2025-04-03 21:07:07 -06:00
|
|
|
spdlog::fatal("unable to mkdir at {}", path);
|
2022-02-27 17:35:13 -08:00
|
|
|
}
|
|
|
|
|
m_storeRoot = path;
|
|
|
|
|
#endif
|
2020-02-25 12:29:15 +01:00
|
|
|
} else {
|
2022-02-27 17:35:13 -08:00
|
|
|
m_storeRoot = std::string(prefPath);
|
2022-03-08 02:44:46 -05:00
|
|
|
SDL_free(prefPath);
|
2020-02-25 12:29:15 +01:00
|
|
|
}
|
2022-02-27 17:55:47 -08:00
|
|
|
g_instance = this;
|
2015-11-12 16:12:09 -10:00
|
|
|
}
|
|
|
|
|
|
2022-02-27 17:55:47 -08:00
|
|
|
FileStoreManager* FileStoreManager::instance() {
|
|
|
|
|
if (g_instance == nullptr) {
|
2025-04-03 21:07:07 -06:00
|
|
|
spdlog::fatal("Requested FileStoreManager instance before it's built!");
|
2022-02-27 17:55:47 -08:00
|
|
|
}
|
|
|
|
|
return g_instance;
|
|
|
|
|
}
|
2025-04-03 21:07:07 -06:00
|
|
|
} // namespace metaforce
|