1
0
mirror of synced 2024-11-25 00:00:27 +01:00

sys: Move imhex path resolution away from utils

This commit is contained in:
WerWolv 2021-09-03 02:33:15 +02:00
parent 19c367b540
commit 68d72eac16
15 changed files with 141 additions and 120 deletions

View File

@ -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})

View 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);
}

View File

@ -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);

View File

@ -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,

View File

@ -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>

View 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
}
}

View File

@ -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();

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -1,4 +1,5 @@
#include "views/view_help.hpp"
#include <hex/helpers/paths.hpp>
#include <imgui_imhex_extensions.h>

View File

@ -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>

View File

@ -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>

View File

@ -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>