From 8ba96904a649361ec7ebc1970e6b638c3bfacd68 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 27 Dec 2020 14:12:53 +0100 Subject: [PATCH] More plugin support work --- CMakeLists.txt | 13 +- external/ImGui/CMakeLists.txt | 8 +- include/helpers/project_file_handler.hpp | 2 +- plugins/example/CMakeLists.txt | 8 +- plugins/libimhex/CMakeLists.txt | 9 +- plugins/libimhex/include/helpers/utils.hpp | 137 ++++++++++++++++++ plugins/libimhex/include/hex.hpp | 1 + .../libimhex/source}/helpers/utils.cpp | 0 8 files changed, 158 insertions(+), 20 deletions(-) create mode 100644 plugins/libimhex/include/helpers/utils.hpp rename {source => plugins/libimhex/source}/helpers/utils.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 292eeb494..0c3894bc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,6 @@ set(Python_FIND_FRAMEWORK NEVER) # Find packages find_package(PkgConfig REQUIRED) -pkg_search_module(GLFW REQUIRED glfw3) -pkg_search_module(GLM REQUIRED glm) pkg_search_module(CRYPTO REQUIRED libcrypto) pkg_search_module(CAPSTONE REQUIRED capstone) find_package(OpenGL REQUIRED) @@ -20,9 +18,9 @@ find_package(nlohmann_json REQUIRED) find_package(Python COMPONENTS Interpreter Development) add_subdirectory(external/llvm/Demangle) -add_subdirectory(external/ImGui) add_subdirectory(plugins/libimhex) + if(Python_VERSION LESS 3) message(STATUS ${PYTHON_VERSION_MAJOR_MINOR}) message(FATAL_ERROR "No valid version of Python 3 was found.") @@ -34,7 +32,7 @@ if(NOT MAGIC_FOUND) endif() # Add include directories -include_directories(include ${GLFW_INCLUDE_DIRS} ${GLM_INCLUDE_DIRS} ${CRYPTO_INCLUDE_DIRS} ${CAPSTONE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS} ${Python_INCLUDE_DIRS}) +include_directories(include ${CRYPTO_INCLUDE_DIRS} ${CAPSTONE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS} ${Python_INCLUDE_DIRS}) # Get Python major and minor string(REPLACE "." ";" PYTHON_VERSION_MAJOR_MINOR ${Python_VERSION}) @@ -88,7 +86,6 @@ add_executable(imhex source/main.cpp source/window.cpp - source/helpers/utils.cpp source/helpers/crypto.cpp source/helpers/patches.cpp source/helpers/math_evaluator.cpp @@ -123,12 +120,12 @@ add_executable(imhex set_target_properties(imhex PROPERTIES CXX_VISIBILITY_PRESET hidden) -target_link_directories(imhex PRIVATE ${GLFW_LIBRARY_DIRS} ${CRYPTO_LIBRARY_DIRS} ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS}) +target_link_directories(imhex PRIVATE ${CRYPTO_LIBRARY_DIRS} ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS}) if (WIN32) - target_link_libraries(imhex libglfw3.a libgcc.a libstdc++.a libmagic.a libgnurx.a libtre.a libintl.a libiconv.a shlwapi.lib libcrypto.a libwinpthread.a libcapstone.a LLVMDemangle imgui libimhex ${Python_LIBRARIES} nlohmann_json::nlohmann_json dl) + target_link_libraries(imhex libgcc.a libstdc++.a libmagic.a libgnurx.a libtre.a libintl.a libiconv.a shlwapi.lib libcrypto.a libwinpthread.a libcapstone.a LLVMDemangle imgui libimhex ${Python_LIBRARIES} nlohmann_json::nlohmann_json dl) elseif (UNIX) - target_link_libraries(imhex glfw magic crypto ${CMAKE_DL_LIBS} capstone LLVMDemangle imgui libimhex ${Python_LIBRARIES} nlohmann_json::nlohmann_json dl) + target_link_libraries(imhex magic crypto ${CMAKE_DL_LIBS} capstone LLVMDemangle imgui libimhex ${Python_LIBRARIES} nlohmann_json::nlohmann_json dl) endif() install(TARGETS imhex DESTINATION bin) diff --git a/external/ImGui/CMakeLists.txt b/external/ImGui/CMakeLists.txt index c7bd42328..896318455 100644 --- a/external/ImGui/CMakeLists.txt +++ b/external/ImGui/CMakeLists.txt @@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD 17) find_package(PkgConfig REQUIRED) find_package(Freetype REQUIRED) +pkg_search_module(GLM REQUIRED glm) pkg_search_module(GLFW REQUIRED glfw3) add_library(imgui @@ -23,13 +24,12 @@ add_compile_definitions(IMGUI_IMPL_OPENGL_LOADER_GLAD) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../glad ${CMAKE_CURRENT_BINARY_DIR}/external/glad) -target_include_directories(imgui PUBLIC include) -target_include_directories(imgui PRIVATE ${FREETYPE_INCLUDE_DIRS} ${GLFW_INCLUDE_DIRS}) +target_include_directories(imgui PUBLIC include ${FREETYPE_INCLUDE_DIRS} ${GLFW_INCLUDE_DIRS}) -target_link_directories(imgui PRIVATE ${GLFW_LIBRARY_DIRS}) +target_link_directories(imgui PUBLIC ${GLM_INCLUDE_DIRS} ${GLFW_LIBRARY_DIRS}) if (WIN32) target_link_libraries(imgui Freetype::Freetype glad libglfw3.a) elseif (UNIX) - target_link_libraries(imgui Freetype::Freetype glad glfw) + target_link_libraries(imgui Freetype::Freetype glad glfw3) endif() diff --git a/include/helpers/project_file_handler.hpp b/include/helpers/project_file_handler.hpp index 88da24116..d90e2d864 100644 --- a/include/helpers/project_file_handler.hpp +++ b/include/helpers/project_file_handler.hpp @@ -5,7 +5,7 @@ #include #include "patches.hpp" -#include "utils.hpp" +#include "helpers/utils.hpp" namespace hex { diff --git a/plugins/example/CMakeLists.txt b/plugins/example/CMakeLists.txt index 47df1ea4c..0c62846b7 100644 --- a/plugins/example/CMakeLists.txt +++ b/plugins/example/CMakeLists.txt @@ -3,8 +3,10 @@ project(example) set(CMAKE_CXX_STANDARD 20) -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/ImGui ${CMAKE_CURRENT_BINARY_DIR}/external/ImGui) -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libimhex ${CMAKE_CURRENT_BINARY_DIR}/plugins/libimhex) +if (TARGET ${CMAKE_PROJECT_NAME}) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/ImGui ${CMAKE_CURRENT_BINARY_DIR}/external/ImGui) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libimhex ${CMAKE_CURRENT_BINARY_DIR}/plugins/libimhex) +endif() set(CMAKE_SHARED_LIBRARY_PREFIX "plugin") @@ -13,4 +15,4 @@ add_library(example SHARED ) target_include_directories(example PUBLIC include) -target_link_libraries(example imgui libimhex) +target_link_libraries(example PRIVATE imgui libimhex) diff --git a/plugins/libimhex/CMakeLists.txt b/plugins/libimhex/CMakeLists.txt index 295071afc..6d112e0bc 100644 --- a/plugins/libimhex/CMakeLists.txt +++ b/plugins/libimhex/CMakeLists.txt @@ -3,17 +3,18 @@ project(libimhex) set(CMAKE_CXX_STANDARD 20) -if (TARGET libimhex) - add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/ImGui ${CMAKE_CURRENT_BINARY_DIR}/external/ImGui) -endif() +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/ImGui ${CMAKE_CURRENT_BINARY_DIR}/external/ImGui) set(CMAKE_STATIC_LIBRARY_PREFIX "") add_library(libimhex STATIC source/helpers/event.cpp + source/helpers/utils.cpp + source/providers/provider.cpp + source/views/view.cpp - ) + ) target_include_directories(libimhex PUBLIC include) target_link_libraries(libimhex PRIVATE imgui) diff --git a/plugins/libimhex/include/helpers/utils.hpp b/plugins/libimhex/include/helpers/utils.hpp new file mode 100644 index 000000000..05a7a1054 --- /dev/null +++ b/plugins/libimhex/include/helpers/utils.hpp @@ -0,0 +1,137 @@ +#pragma once + +#include + +#define TOKEN_CONCAT_IMPL(x, y) x ## y +#define TOKEN_CONCAT(x, y) TOKEN_CONCAT_IMPL(x, y) + +#ifdef __APPLE__ + #define off64_t off_t + #define fopen64 fopen + #define fseeko64 fseek + #define ftello64 ftell +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __MINGW32__ +#include + +#else +#include +#endif + +namespace hex { + + template + inline std::string format(const char *format, Args ... args) { + ssize_t size = snprintf( nullptr, 0, format, args ... ); + + if (size <= 0) + return ""; + + std::vector buffer(size + 1, 0x00); + snprintf(buffer.data(), size + 1, format, args ...); + + return std::string(buffer.data(), buffer.data() + size); + } + + [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) { + std::remove_cvref_t mask = (std::numeric_limits>::max() >> (((sizeof(value) * 8) - 1) - (from - to))) << to; + return (value & mask) >> to; + } + + [[nodiscard]] constexpr inline u64 signExtend(u64 value, u8 currWidth, u8 targetWidth) { + u64 mask = 1LLU << (currWidth - 1); + return (((value ^ mask) - mask) << (64 - targetWidth)) >> (64 - targetWidth); + } + + std::string toByteString(u64 bytes); + std::string makePrintable(char c); + + template + struct always_false : std::false_type {}; + + template + constexpr T changeEndianess(T value, std::endian endian) { + if (endian == std::endian::native) + return value; + + if constexpr (sizeof(T) == 1) + return value; + else if constexpr (sizeof(T) == 2) + return __builtin_bswap16(value); + else if constexpr (sizeof(T) == 4) + return __builtin_bswap32(value); + else if constexpr (sizeof(T) == 8) + return __builtin_bswap64(value); + else + static_assert(always_false::value, "Invalid type provided!"); + } + + template + constexpr T changeEndianess(T value, size_t size, std::endian endian) { + if (endian == std::endian::native) + return value; + + if (size == 1) + return value; + else if (size == 2) + return __builtin_bswap16(value); + else if (size == 4) + return __builtin_bswap32(value); + else if (size == 8) + return __builtin_bswap64(value); + else + throw std::invalid_argument("Invalid value size!"); + } + + template< class T > + constexpr T bit_width(T x) noexcept { + return std::numeric_limits::digits - std::countl_zero(x); + } + + template + constexpr T bit_ceil(T x) noexcept { + if (x <= 1u) + return T(1); + + return T(1) << bit_width(T(x - 1)); + } + + std::vector readFile(std::string_view path); + + #define SCOPE_EXIT(func) ScopeExit TOKEN_CONCAT(scopeGuard, __COUNTER__)([&] { func }) + class ScopeExit { + public: + ScopeExit(std::function func) : m_func(std::move(func)) {} + ~ScopeExit() { if (!this->m_released) this->m_func(); } + + void release() { + this->m_released = true; + } + + private: + bool m_released = false; + std::function m_func; + }; + + struct Region { + u64 address; + size_t size; + }; + + struct Bookmark { + Region region; + + std::vector name; + std::vector comment; + }; +} \ No newline at end of file diff --git a/plugins/libimhex/include/hex.hpp b/plugins/libimhex/include/hex.hpp index 8bd385608..c10d09250 100644 --- a/plugins/libimhex/include/hex.hpp +++ b/plugins/libimhex/include/hex.hpp @@ -20,6 +20,7 @@ extern char **mainArgv; #define IMHEX_PLUGIN namespace hex::plugin::internal { \ void setImGuiContext(ImGuiContext *ctx) { \ + gladLoadGL(); \ ImGui::SetCurrentContext(ctx); \ } \ } \ diff --git a/source/helpers/utils.cpp b/plugins/libimhex/source/helpers/utils.cpp similarity index 100% rename from source/helpers/utils.cpp rename to plugins/libimhex/source/helpers/utils.cpp