sys: Move imhex path resolution away from utils
This commit is contained in:
parent
19c367b540
commit
68d72eac16
@ -46,6 +46,7 @@ set(LIBIMHEX_SOURCES
|
||||
source/data_processor/node.cpp
|
||||
|
||||
source/helpers/utils.cpp
|
||||
source/helpers/paths.cpp
|
||||
source/helpers/shared_data.cpp
|
||||
source/helpers/crypto.cpp
|
||||
source/helpers/lang.cpp
|
||||
@ -74,7 +75,7 @@ if (APPLE)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES} source/helpers/utils_mac.mm)
|
||||
set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES} source/helpers/paths_mac.mm)
|
||||
endif ()
|
||||
|
||||
add_library(libimhex SHARED ${LIBIMHEX_SOURCES})
|
||||
|
22
plugins/libimhex/include/hex/helpers/paths.hpp
Normal file
22
plugins/libimhex/include/hex/helpers/paths.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace hex {
|
||||
|
||||
enum class ImHexPath {
|
||||
Patterns,
|
||||
PatternsInclude,
|
||||
Magic,
|
||||
Python,
|
||||
Plugins,
|
||||
Yara,
|
||||
Config,
|
||||
Resources,
|
||||
Constants
|
||||
};
|
||||
|
||||
std::vector<std::string> getPath(ImHexPath path);
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(OS_MACOS)
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
|
||||
namespace hex {
|
||||
std::string getPathForMac(ImHexPath path);
|
@ -157,20 +157,6 @@ namespace hex {
|
||||
trimRight(s);
|
||||
}
|
||||
|
||||
enum class ImHexPath {
|
||||
Patterns,
|
||||
PatternsInclude,
|
||||
Magic,
|
||||
Python,
|
||||
Plugins,
|
||||
Yara,
|
||||
Config,
|
||||
Resources,
|
||||
Constants
|
||||
};
|
||||
|
||||
std::vector<std::string> getPath(ImHexPath path);
|
||||
|
||||
enum class DialogMode {
|
||||
Open,
|
||||
Save,
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <hex/api/content_registry.hpp>
|
||||
|
||||
#include <hex/helpers/shared_data.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
108
plugins/libimhex/source/helpers/paths.cpp
Normal file
108
plugins/libimhex/source/helpers/paths.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/paths_mac.h>
|
||||
|
||||
#if defined(OS_WINDOWS)
|
||||
#include <windows.h>
|
||||
#include <shlobj.h>
|
||||
#elif defined(OS_LINUX)
|
||||
#include <xdg.hpp>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace hex {
|
||||
|
||||
std::vector<std::string> getPath(ImHexPath path) {
|
||||
#if defined(OS_WINDOWS)
|
||||
std::string exePath(MAX_PATH, '\0');
|
||||
GetModuleFileName(nullptr, exePath.data(), exePath.length());
|
||||
auto parentDir = std::filesystem::path(exePath).parent_path();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
switch (path) {
|
||||
case ImHexPath::Patterns:
|
||||
return { (parentDir / "patterns").string() };
|
||||
case ImHexPath::PatternsInclude:
|
||||
return { (parentDir / "includes").string() };
|
||||
case ImHexPath::Magic:
|
||||
return { (parentDir / "magic").string() };
|
||||
case ImHexPath::Python:
|
||||
return { parentDir.string() };
|
||||
case ImHexPath::Plugins:
|
||||
return { (parentDir / "plugins").string() };
|
||||
case ImHexPath::Yara:
|
||||
return { (parentDir / "yara").string() };
|
||||
case ImHexPath::Config:
|
||||
return { (appDataDir / "imhex" / "config").string() };
|
||||
case ImHexPath::Resources:
|
||||
return { (parentDir / "resources").string() };
|
||||
case ImHexPath::Constants:
|
||||
return { (parentDir / "constants").string() };
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#elif defined(OS_MACOS)
|
||||
return { getPathForMac(path) };
|
||||
#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());
|
||||
|
||||
std::vector<std::string> result;
|
||||
|
||||
switch (path) {
|
||||
case ImHexPath::Patterns:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "patterns").string(); });
|
||||
return result;
|
||||
case ImHexPath::PatternsInclude:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "includes").string(); });
|
||||
return result;
|
||||
case ImHexPath::Magic:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "magic").string(); });
|
||||
return result;
|
||||
case ImHexPath::Python:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex").string(); });
|
||||
return result;
|
||||
case ImHexPath::Plugins:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "plugins").string(); });
|
||||
return result;
|
||||
case ImHexPath::Yara:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "yara").string(); });
|
||||
return result;
|
||||
case ImHexPath::Config:
|
||||
std::transform(configDirs.begin(), configDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex").string(); });
|
||||
return result;
|
||||
case ImHexPath::Resources:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "resources").string(); });
|
||||
return result;
|
||||
case ImHexPath::Constants:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "constants").string(); });
|
||||
return result;
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
@ -7,15 +7,6 @@
|
||||
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
|
||||
#if defined(OS_WINDOWS)
|
||||
#include <windows.h>
|
||||
#include <shlobj.h>
|
||||
#elif defined(OS_MACOS)
|
||||
#include <hex/helpers/utils_mac.h>
|
||||
#elif defined(OS_LINUX)
|
||||
#include <xdg.hpp>
|
||||
#endif
|
||||
|
||||
namespace hex {
|
||||
|
||||
std::string to_string(u128 value) {
|
||||
@ -209,96 +200,6 @@ namespace hex {
|
||||
|
||||
}
|
||||
|
||||
std::vector<std::string> getPath(ImHexPath path) {
|
||||
#if defined(OS_WINDOWS)
|
||||
std::string exePath(MAX_PATH, '\0');
|
||||
GetModuleFileName(nullptr, exePath.data(), exePath.length());
|
||||
auto parentDir = std::filesystem::path(exePath).parent_path();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
switch (path) {
|
||||
case ImHexPath::Patterns:
|
||||
return { (parentDir / "patterns").string() };
|
||||
case ImHexPath::PatternsInclude:
|
||||
return { (parentDir / "includes").string() };
|
||||
case ImHexPath::Magic:
|
||||
return { (parentDir / "magic").string() };
|
||||
case ImHexPath::Python:
|
||||
return { parentDir.string() };
|
||||
case ImHexPath::Plugins:
|
||||
return { (parentDir / "plugins").string() };
|
||||
case ImHexPath::Yara:
|
||||
return { (parentDir / "yara").string() };
|
||||
case ImHexPath::Config:
|
||||
return { (appDataDir / "imhex" / "config").string() };
|
||||
case ImHexPath::Resources:
|
||||
return { (parentDir / "resources").string() };
|
||||
case ImHexPath::Constants:
|
||||
return { (parentDir / "constants").string() };
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#elif defined(OS_MACOS)
|
||||
return { getPathForMac(path) };
|
||||
#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());
|
||||
|
||||
std::vector<std::string> result;
|
||||
|
||||
switch (path) {
|
||||
case ImHexPath::Patterns:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "patterns").string(); });
|
||||
return result;
|
||||
case ImHexPath::PatternsInclude:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "includes").string(); });
|
||||
return result;
|
||||
case ImHexPath::Magic:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "magic").string(); });
|
||||
return result;
|
||||
case ImHexPath::Python:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex").string(); });
|
||||
return result;
|
||||
case ImHexPath::Plugins:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "plugins").string(); });
|
||||
return result;
|
||||
case ImHexPath::Yara:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "yara").string(); });
|
||||
return result;
|
||||
case ImHexPath::Config:
|
||||
std::transform(configDirs.begin(), configDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex").string(); });
|
||||
return result;
|
||||
case ImHexPath::Resources:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "resources").string(); });
|
||||
return result;
|
||||
case ImHexPath::Constants:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "imhex" / "constants").string(); });
|
||||
return result;
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void openFileBrowser(std::string_view title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback) {
|
||||
NFD::Init();
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <hex/lang/preprocessor.hpp>
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "helpers/loader_script_handler.hpp"
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/views/view.hpp>
|
||||
#include <hex/providers/provider.hpp>
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "views/view_constants.hpp"
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/logger.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "views/view_help.hpp"
|
||||
#include <hex/helpers/paths.hpp>
|
||||
|
||||
#include <imgui_imhex_extensions.h>
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "views/view_information.hpp"
|
||||
|
||||
#include <hex/providers/provider.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
|
||||
#include <cstring>
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "helpers/project_file_handler.hpp"
|
||||
#include <hex/lang/preprocessor.hpp>
|
||||
#include <hex/lang/pattern_data.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
|
||||
#include <magic.h>
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include <hex/resources.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/logger.hpp>
|
||||
|
||||
#include <chrono>
|
||||
|
Loading…
Reference in New Issue
Block a user