2022-02-21 02:28:07 +00:00
|
|
|
#include "Runtime/ConsoleVariables/FileStoreManager.hpp"
|
2019-09-04 21:22:05 +00:00
|
|
|
|
2022-02-21 02:28:07 +00:00
|
|
|
#include "Runtime/CBasics.hpp"
|
2019-09-04 21:22:05 +00:00
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
#include <SDL.h>
|
2022-02-22 05:53:57 +00:00
|
|
|
#include <logvisor/logvisor.hpp>
|
|
|
|
#if _WIN32
|
|
|
|
#include <nowide/convert.hpp>
|
|
|
|
#endif
|
2019-09-04 21:22:05 +00:00
|
|
|
|
2015-11-14 08:48:31 +00:00
|
|
|
#if _WIN32
|
|
|
|
#include <ShlObj.h>
|
|
|
|
#endif
|
2015-11-13 02:12:09 +00:00
|
|
|
|
2017-12-06 03:22:31 +00:00
|
|
|
#if WINDOWS_STORE
|
|
|
|
using namespace Windows::Storage;
|
|
|
|
#endif
|
|
|
|
|
2022-02-21 02:28:07 +00:00
|
|
|
namespace metaforce {
|
2022-02-28 01:55:47 +00:00
|
|
|
namespace {
|
2016-03-04 23:02:44 +00:00
|
|
|
static logvisor::Module Log("FileStoreManager");
|
2022-02-28 01:55:47 +00:00
|
|
|
FileStoreManager* g_instance = nullptr;
|
|
|
|
}
|
2015-11-13 02:12:09 +00:00
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
FileStoreManager::FileStoreManager(std::string_view org, std::string_view domain) : m_org(org), m_domain(domain) {
|
2022-02-28 02:02:33 +00:00
|
|
|
if (g_instance != nullptr) {
|
2022-02-28 01:55:47 +00:00
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("Attempting to build another FileStoreManager!!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
auto prefPath = SDL_GetPrefPath(org.data(), domain.data());
|
|
|
|
if (prefPath == nullptr) {
|
2015-11-13 02:12:09 +00:00
|
|
|
#if _WIN32
|
2017-12-06 03:22:31 +00:00
|
|
|
#if !WINDOWS_STORE
|
2022-02-28 01:35:13 +00:00
|
|
|
WCHAR home[MAX_PATH];
|
|
|
|
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, home)))
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("unable to locate profile for file store"));
|
2015-11-14 08:48:31 +00:00
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
std::string path = nowide::narrow(home);
|
2017-12-06 03:22:31 +00:00
|
|
|
#else
|
2022-02-28 01:35:13 +00:00
|
|
|
StorageFolder ^ cacheFolder = ApplicationData::Current->LocalCacheFolder;
|
|
|
|
std::string path(cacheFolder->Path->Data());
|
2017-12-06 03:22:31 +00:00
|
|
|
#endif
|
2022-02-28 01:35:13 +00:00
|
|
|
path += "/." + m_org;
|
2015-11-14 08:48:31 +00:00
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
CBasics::MakeDir(path.c_str());
|
|
|
|
path += '/';
|
|
|
|
path += domain.data();
|
2015-11-14 08:48:31 +00:00
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
CBasics::MakeDir(path.c_str());
|
|
|
|
m_storeRoot = path;
|
2015-11-13 02:12:09 +00:00
|
|
|
#else
|
2022-02-28 01:35:13 +00:00
|
|
|
const char* xdg_data_home = getenv("XDG_DATA_HOME");
|
|
|
|
std::string path;
|
|
|
|
if (xdg_data_home) {
|
|
|
|
if (xdg_data_home[0] != '/')
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("invalid $XDG_DATA_HOME for file store (must be absolute)"));
|
|
|
|
path = xdg_data_home;
|
|
|
|
} else {
|
|
|
|
const char* home = getenv("HOME");
|
|
|
|
if (!home)
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("unable to locate $HOME for file store"));
|
|
|
|
path = home;
|
|
|
|
path += "/.local/share";
|
|
|
|
}
|
|
|
|
path += "/" + m_org + "/" + domain.data();
|
|
|
|
if (CBasics::RecursiveMakeDir(path.c_str()) != 0) {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("unable to mkdir at {}"), path);
|
|
|
|
}
|
|
|
|
m_storeRoot = path;
|
|
|
|
#endif
|
2020-02-25 11:29:15 +00:00
|
|
|
} else {
|
2022-02-28 01:35:13 +00:00
|
|
|
m_storeRoot = std::string(prefPath);
|
2020-02-25 11:29:15 +00:00
|
|
|
}
|
2022-02-28 01:55:47 +00:00
|
|
|
g_instance = this;
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 01:55:47 +00:00
|
|
|
FileStoreManager* FileStoreManager::instance() {
|
|
|
|
if (g_instance == nullptr) {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("Requested FileStoreManager instance before it's built!"));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return g_instance;
|
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
} // namespace hecl::Runtime
|