2021-09-03 02:33:15 +02:00
|
|
|
#include <hex/helpers/paths.hpp>
|
|
|
|
#include <hex/helpers/paths_mac.h>
|
|
|
|
|
2021-12-03 00:00:25 +01:00
|
|
|
#include <xdg.hpp>
|
|
|
|
|
2021-09-03 02:33:15 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
#include <xdg.hpp>
|
2021-09-10 15:26:19 +02:00
|
|
|
#include <linux/limits.h>
|
2021-09-03 02:33:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-10-31 16:28:10 +01:00
|
|
|
std::string getExecutablePath() {
|
2021-09-03 02:33:15 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
std::string exePath(MAX_PATH, '\0');
|
|
|
|
GetModuleFileName(nullptr, exePath.data(), exePath.length());
|
2021-10-31 16:28:10 +01:00
|
|
|
|
|
|
|
return exePath;
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
std::string exePath(PATH_MAX, '\0');
|
2021-10-31 16:36:45 +01:00
|
|
|
readlink("/proc/self/exe", exePath.data(), PATH_MAX);
|
2021-10-31 16:28:10 +01:00
|
|
|
|
|
|
|
return exePath;
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
return getMacExecutableDirectoryPath();
|
|
|
|
#else
|
|
|
|
return "";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> getPath(ImHexPath path) {
|
2021-12-15 00:21:34 +01:00
|
|
|
std::vector<std::string> result;
|
|
|
|
|
2021-10-31 16:28:10 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
const auto exePath = getExecutablePath();
|
|
|
|
const auto parentDir = std::filesystem::path(exePath).parent_path();
|
2021-09-03 02:33:15 +02:00
|
|
|
|
|
|
|
std::filesystem::path appDataDir;
|
|
|
|
{
|
|
|
|
LPWSTR wAppDataPath = nullptr;
|
|
|
|
if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath)))
|
|
|
|
throw std::runtime_error("Failed to get APPDATA folder path");
|
|
|
|
|
|
|
|
appDataDir = wAppDataPath;
|
|
|
|
CoTaskMemFree(wAppDataPath);
|
|
|
|
}
|
|
|
|
|
2021-09-13 23:55:50 +02:00
|
|
|
std::vector<std::filesystem::path> paths = { parentDir, appDataDir / "imhex" };
|
|
|
|
|
2021-09-03 02:33:15 +02:00
|
|
|
switch (path) {
|
|
|
|
case ImHexPath::Patterns:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "patterns").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::PatternsInclude:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "includes").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Magic:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "magic").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Python:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "python").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Plugins:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "plugins").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Yara:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "yara").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Config:
|
|
|
|
return { (appDataDir / "imhex" / "config").string() };
|
|
|
|
case ImHexPath::Resources:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "resources").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Constants:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-09-13 23:55:50 +02:00
|
|
|
return (path / "constants").string();
|
|
|
|
});
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
default: __builtin_unreachable();
|
|
|
|
}
|
|
|
|
#elif defined(OS_MACOS)
|
2021-10-09 23:07:58 +02:00
|
|
|
// Get path to special directories
|
2021-10-31 16:28:10 +01:00
|
|
|
const auto exePath = getExecutablePath();
|
2021-10-09 23:07:58 +02:00
|
|
|
const std::filesystem::path applicationSupportDir(getMacApplicationSupportDirectoryPath());
|
|
|
|
|
2021-10-31 16:28:10 +01:00
|
|
|
std::vector<std::filesystem::path> paths = { exePath, applicationSupportDir };
|
2021-10-09 23:07:58 +02:00
|
|
|
|
|
|
|
switch (path) {
|
|
|
|
case ImHexPath::Patterns:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "patterns").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::PatternsInclude:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "includes").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::Magic:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "magic").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::Python:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "python").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::Plugins:
|
2021-12-15 00:21:34 +01:00
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
2021-10-09 23:07:58 +02:00
|
|
|
return (path / "plugins").string();
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case ImHexPath::Yara:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "yara").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::Config:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "config").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::Resources:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "resources").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
case ImHexPath::Constants:
|
2021-12-15 00:21:34 +01:00
|
|
|
result.push_back((applicationSupportDir / "constants").string());
|
|
|
|
break;
|
2021-10-09 23:07:58 +02:00
|
|
|
default: __builtin_unreachable();
|
|
|
|
}
|
2021-09-03 02:33:15 +02:00
|
|
|
#else
|
|
|
|
std::vector<std::filesystem::path> configDirs = xdg::ConfigDirs();
|
|
|
|
std::vector<std::filesystem::path> dataDirs = xdg::DataDirs();
|
|
|
|
|
|
|
|
configDirs.insert(configDirs.begin(), xdg::ConfigHomeDir());
|
|
|
|
dataDirs.insert(dataDirs.begin(), xdg::DataHomeDir());
|
|
|
|
|
2021-09-10 15:26:19 +02:00
|
|
|
for (auto &dir : dataDirs)
|
|
|
|
dir = dir / "imhex";
|
|
|
|
|
2021-10-31 16:28:10 +01:00
|
|
|
const auto exePath = getExecutablePath();
|
|
|
|
|
|
|
|
if (!exePath.empty())
|
2021-10-31 16:36:45 +01:00
|
|
|
dataDirs.emplace(dataDirs.begin(), std::filesystem::path(exePath.data()).parent_path());
|
2021-09-10 15:26:19 +02:00
|
|
|
|
2021-09-03 02:33:15 +02:00
|
|
|
switch (path) {
|
|
|
|
case ImHexPath::Patterns:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "patterns").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::PatternsInclude:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "includes").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Magic:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "magic").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Python:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p).string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Plugins:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "plugins").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Yara:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "yara").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Config:
|
|
|
|
std::transform(configDirs.begin(), configDirs.end(), std::back_inserter(result),
|
|
|
|
[](auto p) { return (p / "imhex").string(); });
|
2021-09-10 15:26:19 +02:00
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Resources:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "resources").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
case ImHexPath::Constants:
|
|
|
|
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
2021-09-10 15:26:19 +02:00
|
|
|
[](auto p) { return (p / "constants").string(); });
|
|
|
|
break;
|
2021-09-03 02:33:15 +02:00
|
|
|
default: __builtin_unreachable();
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-15 00:21:34 +01:00
|
|
|
|
|
|
|
result.erase(std::remove_if(result.begin(), result.end(), [](const auto& path){
|
|
|
|
return !std::filesystem::is_directory(path);
|
|
|
|
}), result.end());
|
|
|
|
|
|
|
|
return result;
|
2021-09-03 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|