2011-08-19 19:06:09 +00:00
|
|
|
#include "windowspath.hpp"
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(__WINDOWS__)
|
|
|
|
|
2023-07-28 22:29:03 +00:00
|
|
|
#include <array>
|
2011-08-19 19:06:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2022-07-01 14:05:17 +00:00
|
|
|
#define FAR
|
|
|
|
#define NEAR
|
|
|
|
|
2012-01-16 22:09:25 +00:00
|
|
|
#include <shlobj.h>
|
2013-06-22 09:33:22 +00:00
|
|
|
#include <shlwapi.h>
|
2020-05-24 14:49:20 +00:00
|
|
|
#include <winreg.h>
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2022-07-01 14:05:17 +00:00
|
|
|
#undef NEAR
|
|
|
|
#undef FAR
|
|
|
|
|
2019-07-02 16:07:38 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
2012-02-19 22:39:37 +00:00
|
|
|
/**
|
|
|
|
* \namespace Files
|
|
|
|
*/
|
2011-08-19 19:06:09 +00:00
|
|
|
namespace Files
|
|
|
|
{
|
|
|
|
|
2012-09-08 16:47:31 +00:00
|
|
|
WindowsPath::WindowsPath(const std::string& application_name)
|
2018-10-09 06:21:12 +00:00
|
|
|
: mName(application_name)
|
2011-08-19 19:06:09 +00:00
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
std::error_code ec;
|
|
|
|
current_path(getLocalPath(), ec);
|
|
|
|
if (ec.value() != 0)
|
|
|
|
Log(Debug::Warning) << "Error " << ec.value() << " when changing current directory";
|
2011-08-19 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 19:08:54 +00:00
|
|
|
std::filesystem::path WindowsPath::getUserConfigPath() const
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2014-01-14 19:08:54 +00:00
|
|
|
std::filesystem::path userPath = std::filesystem::current_path();
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
WCHAR path[MAX_PATH + 1] = {};
|
2013-12-26 00:24:43 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, nullptr, 0, path)))
|
2011-08-19 19:06:09 +00:00
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
userPath = std::filesystem::path(path);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
return userPath / "My Games" / mName;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2018-10-09 06:21:12 +00:00
|
|
|
std::filesystem::path WindowsPath::getUserDataPath() const
|
2011-08-19 19:06:09 +00:00
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
// Have some chaos, windows people!
|
|
|
|
return getUserConfigPath();
|
2011-08-19 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-08 18:34:43 +00:00
|
|
|
std::filesystem::path WindowsPath::getGlobalConfigPath() const
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2012-09-08 18:34:43 +00:00
|
|
|
std::filesystem::path globalPath = std::filesystem::current_path();
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
WCHAR path[MAX_PATH + 1] = {};
|
|
|
|
|
|
|
|
if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PROGRAM_FILES | CSIDL_FLAG_CREATE, nullptr, 0, path)))
|
|
|
|
{
|
|
|
|
globalPath = std::filesystem::path(path);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2019-07-02 16:07:38 +00:00
|
|
|
|
|
|
|
return globalPath / mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::filesystem::path WindowsPath::getLocalPath() const
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2022-12-02 17:01:58 +00:00
|
|
|
std::filesystem::path localPath = std::filesystem::current_path() / "";
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
WCHAR path[MAX_PATH + 1] = {};
|
2012-01-21 16:58:49 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
if (GetModuleFileNameW(nullptr, path, MAX_PATH + 1) > 0)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2022-12-02 17:01:58 +00:00
|
|
|
localPath = std::filesystem::path(path).parent_path() / "";
|
2012-09-02 17:40:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
// lookup exe path
|
|
|
|
return localPath;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2012-01-16 22:09:25 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
std::filesystem::path WindowsPath::getGlobalDataPath() const
|
2012-01-16 22:09:25 +00:00
|
|
|
{
|
|
|
|
return getGlobalConfigPath();
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2012-01-16 22:09:25 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
std::filesystem::path WindowsPath::getCachePath() const
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
return getUserConfigPath() / "cache";
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
std::filesystem::path WindowsPath::getInstallPath() const
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
std::filesystem::path installPath{};
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
if (HKEY hKey; RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Bethesda Softworks\\Morrowind", 0,
|
|
|
|
KEY_READ | KEY_WOW64_32KEY, &hKey)
|
|
|
|
== ERROR_SUCCESS)
|
2012-01-16 22:09:25 +00:00
|
|
|
{
|
|
|
|
// Key existed, let's try to read the install dir
|
2022-06-19 11:28:33 +00:00
|
|
|
std::array<wchar_t, 512> buf{};
|
2024-03-14 23:39:19 +00:00
|
|
|
DWORD len = static_cast<DWORD>(buf.size() * sizeof(wchar_t));
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
if (RegQueryValueExW(hKey, L"Installed Path", nullptr, nullptr, reinterpret_cast<LPBYTE>(buf.data()), &len)
|
|
|
|
== ERROR_SUCCESS)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
installPath = std::filesystem::path(buf.data());
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2015-03-14 19:08:55 +00:00
|
|
|
RegCloseKey(hKey);
|
2012-01-16 22:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return installPath;
|
2012-01-21 16:58:49 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 19:06:09 +00:00
|
|
|
} /* namespace Files */
|
|
|
|
|
|
|
|
#endif /* defined(_WIN32) || defined(__WINDOWS__) */
|