From 84bfd10416a9e45e1875b62196ae45123dab3bbc Mon Sep 17 00:00:00 2001 From: Nik Date: Fri, 22 Dec 2023 23:39:38 +0100 Subject: [PATCH] build: Restructured entire custom plugin system (#1469) --- CMakeLists.txt | 1 + cmake/build_helpers.cmake | 36 ++++-- cmake/modules/ImHexPlugin.cmake | 7 +- cmake/sdk/CMakeLists.txt | 43 +++++++ dist/rpm/imhex.spec | 1 + lib/libimhex/CMakeLists.txt | 42 ++++--- .../include/hex/api/content_registry.hpp | 13 +-- lib/libimhex/include/hex/api/task_manager.hpp | 5 - lib/libimhex/include/hex/helpers/tar.hpp | 24 ++-- lib/libimhex/source/api/content_registry.cpp | 14 ++- lib/libimhex/source/api/task_manager.cpp | 105 +++++++++--------- lib/libimhex/source/helpers/tar.cpp | 74 ++++++------ lib/third_party/imgui/CMakeLists.txt | 2 + .../imgui/ColorTextEditor/CMakeLists.txt | 2 + lib/third_party/imgui/custom/CMakeLists.txt | 1 + lib/third_party/imgui/fonts/CMakeLists.txt | 4 +- lib/third_party/imgui/imgui/CMakeLists.txt | 1 + lib/third_party/imgui/imnodes/CMakeLists.txt | 1 + lib/third_party/imgui/implot/CMakeLists.txt | 2 + main/gui/CMakeLists.txt | 2 +- main/gui/source/init/tasks.cpp | 1 - plugins/builtin/CMakeLists.txt | 11 ++ .../include/content}/helpers/disassembler.hpp | 0 .../content/views/view_disassembler.hpp | 2 +- plugins/windows/CMakeLists.txt | 2 + tests/common/CMakeLists.txt | 2 +- .../common/include/hex/test/test_provider.hpp | 13 ++- 27 files changed, 258 insertions(+), 153 deletions(-) create mode 100644 cmake/sdk/CMakeLists.txt rename {lib/libimhex/include/hex => plugins/builtin/include/content}/helpers/disassembler.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0f84b43f..50eb1b9af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,3 +67,4 @@ add_subdirectory(tests EXCLUDE_FROM_ALL) # Configure packaging createPackage() generatePDBs() +generateSDKDirectory() \ No newline at end of file diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index 213d9fef8..896478fb5 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -256,8 +256,8 @@ macro(createPackage) # Enforce DragNDrop packaging. set(CPACK_GENERATOR "DragNDrop") - set (CPACK_BUNDLE_ICON "${CMAKE_SOURCE_DIR}/resources/dist/macos/AppIcon.icns" ) - set (CPACK_BUNDLE_PLIST "${CMAKE_BINARY_DIR}/imhex.app/Contents/Info.plist") + set(CPACK_BUNDLE_ICON "${CMAKE_SOURCE_DIR}/resources/dist/macos/AppIcon.icns" ) + set(CPACK_BUNDLE_PLIST "${CMAKE_BINARY_DIR}/imhex.app/Contents/Info.plist") else() install(TARGETS main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) if(WIN32) # Forwarder is only needed on Windows @@ -679,10 +679,9 @@ function(generatePDBs) WORKING_DIRECTORY ${cv2pdb_SOURCE_DIR} COMMAND ( - ${CMAKE_COMMAND} -E copy_if_different $ ${CMAKE_BINARY_DIR}/${GENERATED_PDB}.bak && ${CMAKE_COMMAND} -E remove -f ${CMAKE_BINARY_DIR}/${GENERATED_PDB}.pdb && - ${cv2pdb_SOURCE_DIR}/cv2pdb64.exe ${CMAKE_BINARY_DIR}/${GENERATED_PDB}.bak && - ${CMAKE_COMMAND} -E remove -f ${CMAKE_BINARY_DIR}/${GENERATED_PDB}.bak + ${cv2pdb_SOURCE_DIR}/cv2pdb64.exe + $ ) || (exit 0) DEPENDS $ COMMAND_EXPAND_LISTS) @@ -694,10 +693,33 @@ function(generatePDBs) endfunction() function(generateSDKDirectory) - set(SDK_PATH "./sdk") + if (WIN32) + set(SDK_PATH "./sdk") + elseif (APPLE) + set(SDK_PATH "imhex.app/Contents/Resources/sdk") + else() + set(SDK_PATH "share/imhex/sdk") + endif() + + install(DIRECTORY ${CMAKE_SOURCE_DIR}/lib/libimhex DESTINATION "${SDK_PATH}/lib") + install(DIRECTORY ${CMAKE_SOURCE_DIR}/lib/external DESTINATION "${SDK_PATH}/lib") + install(DIRECTORY ${CMAKE_SOURCE_DIR}/lib/third_party/imgui DESTINATION "${SDK_PATH}/lib/third_party") + if (NOT USE_SYSTEM_FMT) + install(DIRECTORY ${CMAKE_SOURCE_DIR}/lib/third_party/fmt DESTINATION "${SDK_PATH}/lib/third_party") + endif() + if (NOT USE_SYSTEM_NLOHMANN_JSON) + install(DIRECTORY ${CMAKE_SOURCE_DIR}/lib/third_party/nlohmann_json DESTINATION "${SDK_PATH}/lib/third_party") + endif() - install(DIRECTORY ${CMAKE_SOURCE_DIR}/lib/libimhex/include DESTINATION "${SDK_PATH}") install(FILES ${CMAKE_SOURCE_DIR}/cmake/modules/ImHexPlugin.cmake DESTINATION "${SDK_PATH}/cmake/modules") + install(FILES ${CMAKE_SOURCE_DIR}/cmake/build_helpers.cmake DESTINATION "${SDK_PATH}/cmake") + install(FILES ${CMAKE_SOURCE_DIR}/cmake/sdk/CMakeLists.txt DESTINATION "${SDK_PATH}") install(TARGETS libimhex ARCHIVE DESTINATION "${SDK_PATH}/lib") install(TARGETS libimhex RUNTIME DESTINATION "${SDK_PATH}/lib") + install(TARGETS libimhex LIBRARY DESTINATION "${SDK_PATH}/lib") +endfunction() + +function(addIncludesFromLibrary target library) + get_target_property(library_include_dirs ${library} INTERFACE_INCLUDE_DIRECTORIES) + target_include_directories(${target} PRIVATE ${library_include_dirs}) endfunction() \ No newline at end of file diff --git a/cmake/modules/ImHexPlugin.cmake b/cmake/modules/ImHexPlugin.cmake index d141ae0b0..63e6f8e9f 100644 --- a/cmake/modules/ImHexPlugin.cmake +++ b/cmake/modules/ImHexPlugin.cmake @@ -24,7 +24,8 @@ macro(add_imhex_plugin) # Add include directories and link libraries target_include_directories(${IMHEX_PLUGIN_NAME} PUBLIC ${IMHEX_PLUGIN_INCLUDES}) - target_link_libraries(${IMHEX_PLUGIN_NAME} PRIVATE libimhex ${FMT_LIBRARIES} ${IMHEX_PLUGIN_LIBRARIES}) + target_link_libraries(${IMHEX_PLUGIN_NAME} PRIVATE libimhex ${IMHEX_PLUGIN_LIBRARIES} fmt::fmt imgui_all_includes libwolv) + addIncludesFromLibrary(${IMHEX_PLUGIN_NAME} libpl) # Add IMHEX_PROJECT_NAME and IMHEX_VERSION define target_compile_definitions(${IMHEX_PLUGIN_NAME} PRIVATE IMHEX_PROJECT_NAME="${IMHEX_PLUGIN_NAME}") @@ -53,7 +54,9 @@ macro(add_imhex_plugin) target_link_libraries(${IMHEX_PLUGIN_NAME} PRIVATE ${LIBROMFS_LIBRARY}) # Add the new plugin to the main dependency list so it gets built by default - add_dependencies(imhex_all ${IMHEX_PLUGIN_NAME}) + if (TARGET imhex_all) + add_dependencies(imhex_all ${IMHEX_PLUGIN_NAME}) + endif() endmacro() macro(add_romfs_resource input output) diff --git a/cmake/sdk/CMakeLists.txt b/cmake/sdk/CMakeLists.txt new file mode 100644 index 000000000..2f6b5b2b3 --- /dev/null +++ b/cmake/sdk/CMakeLists.txt @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 3.20) +project(ImHexSDK) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/build_helpers.cmake") + +set(IMHEX_BASE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE) +include(ImHexPlugin) + +function(add_subdirectory_if_exists folder) + if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${folder}/CMakeLists.txt") + add_subdirectory("${folder}") + endif() +endfunction() + +set(IMHEX_EXTERNAL_PLUGIN_BUILD ON) +add_custom_target(imhex_all) + +add_subdirectory(lib/third_party/imgui) +add_subdirectory_if_exists(lib/third_party/fmt) +add_subdirectory_if_exists(lib/third_party/nlohmann_json) + +add_subdirectory(lib/external/libwolv) + +set(LIBPL_BUILD_CLI_AS_EXECUTABLE OFF) +add_subdirectory(lib/external/pattern_language) + +add_subdirectory(lib/libimhex) + +if (WIN32) + set_target_properties(libimhex PROPERTIES + IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/lib/libimhex.dll" + IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/liblibimhex.dll.a" + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/lib/libimhex/include") +elseif (APPLE) + set_target_properties(libimhex PROPERTIES + IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/lib/libimhex.dylib" + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/lib/libimhex/include") +else() + set_target_properties(libimhex PROPERTIES + IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/lib/libimhex.so" + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/lib/libimhex/include") +endif() diff --git a/dist/rpm/imhex.spec b/dist/rpm/imhex.spec index 4dd08cbf3..73ed94b9a 100644 --- a/dist/rpm/imhex.spec +++ b/dist/rpm/imhex.spec @@ -125,6 +125,7 @@ cp -a lib/third_party/xdgpp/LICENSE %{buildroot %{_libdir}/libimhex.so* %{_libdir}/%{name}/ %{_metainfodir}/net.werwolv.%{name}.metainfo.xml +%{_datadir}/%{name}/sdk/ %changelog diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index f9de79686..bcb2a63cf 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -65,10 +65,17 @@ endif () add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}") -if (IMHEX_STATIC_LINK_PLUGINS) - add_library(libimhex STATIC ${LIBIMHEX_SOURCES}) +if (IMHEX_EXTERNAL_PLUGIN_BUILD) + add_library(libimhex IMPORTED SHARED GLOBAL) + set(LIBIMHEX_LIBRARY_TYPE INTERFACE) else() - add_library(libimhex SHARED ${LIBIMHEX_SOURCES}) + if (IMHEX_STATIC_LINK_PLUGINS) + add_library(libimhex STATIC ${LIBIMHEX_SOURCES}) + else() + add_library(libimhex SHARED ${LIBIMHEX_SOURCES}) + endif() + + set(LIBIMHEX_LIBRARY_TYPE PUBLIC) endif() set_target_properties(libimhex PROPERTIES POSITION_INDEPENDENT_CODE ON) @@ -78,26 +85,29 @@ setupCompilerFlags(libimhex) include(GenerateExportHeader) generate_export_header(libimhex) -target_include_directories(libimhex PUBLIC include ${XDGPP_INCLUDE_DIRS} ${MBEDTLS_INCLUDE_DIR} ${CAPSTONE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS} ${LLVM_INCLUDE_DIRS} ${FMT_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ${YARA_INCLUDE_DIRS} ${LIBBACKTRACE_INCLUDE_DIRS}) -target_link_directories(libimhex PUBLIC ${MBEDTLS_LIBRARY_DIR} ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS}) +target_include_directories(libimhex ${LIBIMHEX_LIBRARY_TYPE} include ${XDGPP_INCLUDE_DIRS} ${MBEDTLS_INCLUDE_DIR} ${MAGIC_INCLUDE_DIRS} ${LLVM_INCLUDE_DIRS} ${FMT_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ${LIBBACKTRACE_INCLUDE_DIRS}) +target_link_directories(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${MBEDTLS_LIBRARY_DIR} ${MAGIC_LIBRARY_DIRS}) if (NOT EMSCRIPTEN) # curl is only used in non-emscripten builds - target_include_directories(libimhex PUBLIC ${CURL_INCLUDE_DIRS}) - target_link_libraries(libimhex PUBLIC ${LIBCURL_LIBRARIES}) + target_include_directories(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${CURL_INCLUDE_DIRS}) + target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${LIBCURL_LIBRARIES}) endif() -if (WIN32) - set_target_properties(libimhex PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE) - target_link_options(libimhex PRIVATE -Wl,--export-all-symbols) -elseif (APPLE) - find_library(FOUNDATION NAMES Foundation) - target_link_libraries(libimhex PUBLIC ${FOUNDATION}) -endif () +if (NOT IMHEX_EXTERNAL_PLUGIN_BUILD) + if (WIN32) + set_target_properties(libimhex PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE) + target_link_options(libimhex PRIVATE -Wl,--export-all-symbols) + elseif (APPLE) + find_library(FOUNDATION NAMES Foundation) + target_link_libraries(libimhex PUBLIC ${FOUNDATION}) + endif () -target_link_libraries(libimhex PRIVATE ${FMT_LIBRARIES}) -target_link_libraries(libimhex PUBLIC dl ${IMGUI_LIBRARIES} ${NFD_LIBRARIES} magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} plcli libpl libpl-gen ${MINIAUDIO_LIBRARIES} ${JTHREAD_LIBRARIES} wolv::utils wolv::io wolv::hash wolv::net wolv::containers wolv::math_eval) + target_link_libraries(libimhex PRIVATE microtar libpl plcli libpl-gen libwolv ${FMT_LIBRARIES} ${NFD_LIBRARIES} magic dl ${IMGUI_LIBRARIES} ${NLOHMANN_JSON_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} ${JTHREAD_LIBRARIES}) +endif() + +target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${NLOHMANN_JSON_LIBRARIES} ${IMGUI_LIBRARIES} LLVMDemangle) set_property(TARGET libimhex PROPERTY INTERPROCEDURAL_OPTIMIZATION FALSE) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index d1b1838c9..04338d2e2 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -4,19 +4,16 @@ #include #include -#include - #include #include #include #include -#include #include #include -#include - +#include #include +#include using ImGuiDataType = int; using ImGuiInputTextFlags = int; @@ -972,12 +969,6 @@ namespace hex { namespace impl { using Callback = std::function; - struct Service { - std::string name; - std::jthread thread; - }; - - std::vector &getServices(); void stopServices(); } diff --git a/lib/libimhex/include/hex/api/task_manager.hpp b/lib/libimhex/include/hex/api/task_manager.hpp index 7364573f8..1c7868be1 100644 --- a/lib/libimhex/include/hex/api/task_manager.hpp +++ b/lib/libimhex/include/hex/api/task_manager.hpp @@ -4,15 +4,12 @@ #include #include -#include #include #include #include #include #include -#include - namespace hex { class TaskHolder; @@ -166,8 +163,6 @@ namespace hex { static void runDeferredCalls(); private: - static void runner(const std::stop_token &stopToken); - static TaskHolder createTask(std::string name, u64 maxValue, bool background, std::function function); }; diff --git a/lib/libimhex/include/hex/helpers/tar.hpp b/lib/libimhex/include/hex/helpers/tar.hpp index f5cb7ed60..efbbc6d1d 100644 --- a/lib/libimhex/include/hex/helpers/tar.hpp +++ b/lib/libimhex/include/hex/helpers/tar.hpp @@ -4,7 +4,9 @@ #include -#include +#include + +struct mtar_t; namespace hex { @@ -32,28 +34,28 @@ namespace hex { */ std::string getOpenErrorString() const; - [[nodiscard]] std::vector readVector(const std::fs::path &path); - [[nodiscard]] std::string readString(const std::fs::path &path); + [[nodiscard]] std::vector readVector(const std::fs::path &path) const; + [[nodiscard]] std::string readString(const std::fs::path &path) const; - void writeVector(const std::fs::path &path, const std::vector &data); - void writeString(const std::fs::path &path, const std::string &data); + void writeVector(const std::fs::path &path, const std::vector &data) const; + void writeString(const std::fs::path &path, const std::string &data) const; - [[nodiscard]] std::vector listEntries(const std::fs::path &basePath = "/"); - [[nodiscard]] bool contains(const std::fs::path &path); + [[nodiscard]] std::vector listEntries(const std::fs::path &basePath = "/") const; + [[nodiscard]] bool contains(const std::fs::path &path) const; - void extract(const std::fs::path &path, const std::fs::path &outputPath); - void extractAll(const std::fs::path &outputPath); + void extract(const std::fs::path &path, const std::fs::path &outputPath) const; + void extractAll(const std::fs::path &outputPath) const; [[nodiscard]] bool isValid() const { return m_valid; } private: - mtar_t m_ctx = { }; + std::unique_ptr m_ctx; std::fs::path m_path; bool m_valid = false; // These will be updated when the constructor is called - int m_tarOpenErrno = MTAR_ESUCCESS; + int m_tarOpenErrno = 0; int m_fileOpenErrno = 0; }; diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 858413072..04263cc14 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #if defined(OS_WEB) @@ -1000,6 +999,11 @@ namespace hex { namespace impl { + struct Service { + std::string name; + std::jthread thread; + }; + std::vector &getServices() { static std::vector services; @@ -1007,14 +1011,18 @@ namespace hex { } void stopServices() { - for (auto &service : getServices()) { + auto &services = getServices(); + + for (auto &service : services) { service.thread.request_stop(); } - for (auto &service : getServices()) { + for (auto &service : services) { if (service.thread.joinable()) service.thread.join(); } + + services.clear(); } } diff --git a/lib/libimhex/source/api/task_manager.cpp b/lib/libimhex/source/api/task_manager.cpp index 8169c6e42..7bc2245e5 100644 --- a/lib/libimhex/source/api/task_manager.cpp +++ b/lib/libimhex/source/api/task_manager.cpp @@ -5,6 +5,8 @@ #include +#include + #if defined(OS_WINDOWS) #include #include @@ -227,8 +229,57 @@ namespace hex { log::debug("Initializing task manager thread pool with {} workers.", threadCount); // Create worker threads - for (u32 i = 0; i < threadCount; i++) - s_workers.emplace_back(TaskManager::runner); + for (u32 i = 0; i < threadCount; i++) { + s_workers.emplace_back([](const std::stop_token &stopToken) { + while (true) { + std::shared_ptr task; + + // Set the thread name to "Idle Task" while waiting for a task + setThreadName("Idle Task"); + + { + // Wait for a task to be added to the queue + std::unique_lock lock(s_queueMutex); + s_jobCondVar.wait(lock, [&] { + return !s_taskQueue.empty() || stopToken.stop_requested(); + }); + + // Check if the thread should exit + if (stopToken.stop_requested()) + break; + + // Grab the next task from the queue + task = std::move(s_taskQueue.front()); + s_taskQueue.pop_front(); + } + + try { + // Set the thread name to the name of the task + setThreadName(Lang(task->m_unlocalizedName)); + + // Execute the task + task->m_function(*task); + + log::debug("Task '{}' finished", task->m_unlocalizedName.get()); + } catch (const Task::TaskInterruptor &) { + // Handle the task being interrupted by user request + task->interruption(); + } catch (const std::exception &e) { + log::error("Exception in task '{}': {}", task->m_unlocalizedName.get(), e.what()); + + // Handle the task throwing an uncaught exception + task->exception(e.what()); + } catch (...) { + log::error("Exception in task '{}'", task->m_unlocalizedName.get()); + + // Handle the task throwing an uncaught exception of unknown type + task->exception("Unknown Exception"); + } + + task->finish(); + } + }); + } } void TaskManager::exit() { @@ -251,56 +302,6 @@ namespace hex { s_taskQueue.clear(); } - void TaskManager::runner(const std::stop_token &stopToken) { - while (true) { - std::shared_ptr task; - - // Set the thread name to "Idle Task" while waiting for a task - setThreadName("Idle Task"); - - { - // Wait for a task to be added to the queue - std::unique_lock lock(s_queueMutex); - s_jobCondVar.wait(lock, [&] { - return !s_taskQueue.empty() || stopToken.stop_requested(); - }); - - // Check if the thread should exit - if (stopToken.stop_requested()) - break; - - // Grab the next task from the queue - task = std::move(s_taskQueue.front()); - s_taskQueue.pop_front(); - } - - try { - // Set the thread name to the name of the task - setThreadName(Lang(task->m_unlocalizedName)); - - // Execute the task - task->m_function(*task); - - log::debug("Task '{}' finished", task->m_unlocalizedName.get()); - } catch (const Task::TaskInterruptor &) { - // Handle the task being interrupted by user request - task->interruption(); - } catch (const std::exception &e) { - log::error("Exception in task '{}': {}", task->m_unlocalizedName.get(), e.what()); - - // Handle the task throwing an uncaught exception - task->exception(e.what()); - } catch (...) { - log::error("Exception in task '{}'", task->m_unlocalizedName.get()); - - // Handle the task throwing an uncaught exception of unknown type - task->exception("Unknown Exception"); - } - - task->finish(); - } - } - TaskHolder TaskManager::createTask(std::string name, u64 maxValue, bool background, std::function function) { std::scoped_lock lock(s_queueMutex); diff --git a/lib/libimhex/source/helpers/tar.cpp b/lib/libimhex/source/helpers/tar.cpp index 9b32304c4..d7dee42f6 100644 --- a/lib/libimhex/source/helpers/tar.cpp +++ b/lib/libimhex/source/helpers/tar.cpp @@ -5,12 +5,14 @@ #include +#include + namespace hex { using namespace hex::literals; Tar::Tar(const std::fs::path &path, Mode mode) { - int tar_error = MTAR_ESUCCESS; + int tarError = MTAR_ESUCCESS; // Explicitly create file so a short path gets generated if (mode == Mode::Create) { @@ -18,21 +20,23 @@ namespace hex { file.flush(); } + m_ctx = std::make_unique(); + auto shortPath = wolv::io::fs::toShortPath(path); if (mode == Tar::Mode::Read) - tar_error = mtar_open(&m_ctx, shortPath.string().c_str(), "r"); + tarError = mtar_open(m_ctx.get(), shortPath.string().c_str(), "r"); else if (mode == Tar::Mode::Write) - tar_error = mtar_open(&m_ctx, shortPath.string().c_str(), "a"); + tarError = mtar_open(m_ctx.get(), shortPath.string().c_str(), "a"); else if (mode == Tar::Mode::Create) - tar_error = mtar_open(&m_ctx, shortPath.string().c_str(), "w"); + tarError = mtar_open(m_ctx.get(), shortPath.string().c_str(), "w"); else - tar_error = MTAR_EFAILURE; + tarError = MTAR_EFAILURE; m_path = path; - m_valid = (tar_error == MTAR_ESUCCESS); + m_valid = (tarError == MTAR_ESUCCESS); if (!m_valid) { - m_tarOpenErrno = tar_error; + m_tarOpenErrno = tarError; // Hopefully this errno corresponds to the file open call in mtar_open m_fileOpenErrno = errno; @@ -44,7 +48,7 @@ namespace hex { } Tar::Tar(hex::Tar &&other) noexcept { - m_ctx = other.m_ctx; + m_ctx = std::move(other.m_ctx); m_path = other.m_path; m_valid = other.m_valid; m_tarOpenErrno = other.m_tarOpenErrno; @@ -55,10 +59,8 @@ namespace hex { } Tar &Tar::operator=(Tar &&other) noexcept { - m_ctx = other.m_ctx; - other.m_ctx = { }; - - m_path = other.m_path; + m_ctx = std::move(other.m_ctx); + m_path = std::move(other.m_path); m_valid = other.m_valid; other.m_valid = false; @@ -68,24 +70,24 @@ namespace hex { return *this; } - std::vector Tar::listEntries(const std::fs::path &basePath) { + std::vector Tar::listEntries(const std::fs::path &basePath) const { std::vector result; const std::string PaxHeaderName = "@PaxHeader"; mtar_header_t header; - while (mtar_read_header(&m_ctx, &header) != MTAR_ENULLRECORD) { + while (mtar_read_header(m_ctx.get(), &header) != MTAR_ENULLRECORD) { std::fs::path path = header.name; if (header.name != PaxHeaderName && wolv::io::fs::isSubPath(basePath, path)) { result.emplace_back(header.name); } - mtar_next(&m_ctx); + mtar_next(m_ctx.get()); } return result; } - bool Tar::contains(const std::fs::path &path) { + bool Tar::contains(const std::fs::path &path) const { mtar_header_t header; auto fixedPath = path.string(); @@ -93,7 +95,7 @@ namespace hex { std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/'); #endif - return mtar_find(&m_ctx, fixedPath.c_str(), &header) == MTAR_ESUCCESS; + return mtar_find(m_ctx.get(), fixedPath.c_str(), &header) == MTAR_ESUCCESS; } std::string Tar::getOpenErrorString() const { @@ -102,22 +104,22 @@ namespace hex { void Tar::close() { if (m_valid) { - mtar_finalize(&m_ctx); - mtar_close(&m_ctx); + mtar_finalize(m_ctx.get()); + mtar_close(m_ctx.get()); } - m_ctx = { }; + m_ctx.reset(); m_valid = false; } - std::vector Tar::readVector(const std::fs::path &path) { + std::vector Tar::readVector(const std::fs::path &path) const { mtar_header_t header; auto fixedPath = path.string(); #if defined(OS_WINDOWS) std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/'); #endif - int ret = mtar_find(&m_ctx, fixedPath.c_str(), &header); + int ret = mtar_find(m_ctx.get(), fixedPath.c_str(), &header); if (ret != MTAR_ESUCCESS){ log::debug("Failed to read vector from path {} in tarred file {}: {}", path.string(), m_path.string(), mtar_strerror(ret)); @@ -125,17 +127,17 @@ namespace hex { } std::vector result(header.size, 0x00); - mtar_read_data(&m_ctx, result.data(), result.size()); + mtar_read_data(m_ctx.get(), result.data(), result.size()); return result; } - std::string Tar::readString(const std::fs::path &path) { + std::string Tar::readString(const std::fs::path &path) const { auto result = this->readVector(path); return { result.begin(), result.end() }; } - void Tar::writeVector(const std::fs::path &path, const std::vector &data) { + void Tar::writeVector(const std::fs::path &path, const std::vector &data) const { if (path.has_parent_path()) { std::fs::path pathPart; for (const auto &part : path.parent_path()) { @@ -145,7 +147,7 @@ namespace hex { #if defined(OS_WINDOWS) std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/'); #endif - mtar_write_dir_header(&m_ctx, fixedPath.c_str()); + mtar_write_dir_header(m_ctx.get(), fixedPath.c_str()); } } @@ -153,11 +155,11 @@ namespace hex { #if defined(OS_WINDOWS) std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/'); #endif - mtar_write_file_header(&m_ctx, fixedPath.c_str(), data.size()); - mtar_write_data(&m_ctx, data.data(), data.size()); + mtar_write_file_header(m_ctx.get(), fixedPath.c_str(), data.size()); + mtar_write_data(m_ctx.get(), data.data(), data.size()); } - void Tar::writeString(const std::fs::path &path, const std::string &data) { + void Tar::writeString(const std::fs::path &path, const std::string &data) const { this->writeVector(path, { data.begin(), data.end() }); } @@ -175,26 +177,26 @@ namespace hex { } } - void Tar::extract(const std::fs::path &path, const std::fs::path &outputPath) { + void Tar::extract(const std::fs::path &path, const std::fs::path &outputPath) const { mtar_header_t header; - mtar_find(&m_ctx, path.string().c_str(), &header); + mtar_find(m_ctx.get(), path.string().c_str(), &header); - writeFile(&m_ctx, &header, outputPath); + writeFile(m_ctx.get(), &header, outputPath); } - void Tar::extractAll(const std::fs::path &outputPath) { + void Tar::extractAll(const std::fs::path &outputPath) const { mtar_header_t header; - while (mtar_read_header(&m_ctx, &header) != MTAR_ENULLRECORD) { + while (mtar_read_header(m_ctx.get(), &header) != MTAR_ENULLRECORD) { const auto filePath = std::fs::absolute(outputPath / std::fs::path(header.name)); if (filePath.filename() != "@PaxHeader") { std::fs::create_directories(filePath.parent_path()); - writeFile(&m_ctx, &header, filePath); + writeFile(m_ctx.get(), &header, filePath); } - mtar_next(&m_ctx); + mtar_next(m_ctx.get()); } } diff --git a/lib/third_party/imgui/CMakeLists.txt b/lib/third_party/imgui/CMakeLists.txt index b9b646877..8323aff11 100644 --- a/lib/third_party/imgui/CMakeLists.txt +++ b/lib/third_party/imgui/CMakeLists.txt @@ -3,6 +3,8 @@ project(imgui) set(CMAKE_CXX_STANDARD 17) +add_library(imgui_all_includes INTERFACE) + add_subdirectory(imgui) add_subdirectory(implot) add_subdirectory(imnodes) diff --git a/lib/third_party/imgui/ColorTextEditor/CMakeLists.txt b/lib/third_party/imgui/ColorTextEditor/CMakeLists.txt index 437e3fcf8..906aa5ac3 100644 --- a/lib/third_party/imgui/ColorTextEditor/CMakeLists.txt +++ b/lib/third_party/imgui/ColorTextEditor/CMakeLists.txt @@ -12,5 +12,7 @@ target_include_directories(imgui_color_text_editor PUBLIC include ) +target_include_directories(imgui_all_includes INTERFACE include) + target_link_libraries(imgui_color_text_editor PRIVATE imgui_includes) set_property(TARGET imgui_color_text_editor PROPERTY POSITION_INDEPENDENT_CODE ON) \ No newline at end of file diff --git a/lib/third_party/imgui/custom/CMakeLists.txt b/lib/third_party/imgui/custom/CMakeLists.txt index a2e60c94d..615f90906 100644 --- a/lib/third_party/imgui/custom/CMakeLists.txt +++ b/lib/third_party/imgui/custom/CMakeLists.txt @@ -15,6 +15,7 @@ target_include_directories(imgui_custom PUBLIC target_link_libraries(imgui_custom PRIVATE imgui_includes) set_property(TARGET imgui_custom PROPERTY POSITION_INDEPENDENT_CODE ON) +target_include_directories(imgui_all_includes INTERFACE include) find_package(PkgConfig REQUIRED) find_package(OpenGL REQUIRED) diff --git a/lib/third_party/imgui/fonts/CMakeLists.txt b/lib/third_party/imgui/fonts/CMakeLists.txt index 803f361d8..6058eb46b 100644 --- a/lib/third_party/imgui/fonts/CMakeLists.txt +++ b/lib/third_party/imgui/fonts/CMakeLists.txt @@ -9,4 +9,6 @@ add_library(imgui_fonts INTERFACE) target_include_directories(imgui_fonts INTERFACE include -) \ No newline at end of file +) + +target_include_directories(imgui_all_includes INTERFACE include) diff --git a/lib/third_party/imgui/imgui/CMakeLists.txt b/lib/third_party/imgui/imgui/CMakeLists.txt index 4e4380193..068b5e129 100644 --- a/lib/third_party/imgui/imgui/CMakeLists.txt +++ b/lib/third_party/imgui/imgui/CMakeLists.txt @@ -21,6 +21,7 @@ target_include_directories(imgui_imgui PUBLIC add_library(imgui_includes INTERFACE) target_include_directories(imgui_includes INTERFACE include) +target_include_directories(imgui_all_includes INTERFACE include include/misc/freetype) target_compile_options(imgui_imgui PRIVATE -Wno-unknown-warning-option) set_property(TARGET imgui_imgui PROPERTY POSITION_INDEPENDENT_CODE ON) diff --git a/lib/third_party/imgui/imnodes/CMakeLists.txt b/lib/third_party/imgui/imnodes/CMakeLists.txt index baf5cae24..e7689919a 100644 --- a/lib/third_party/imgui/imnodes/CMakeLists.txt +++ b/lib/third_party/imgui/imnodes/CMakeLists.txt @@ -13,4 +13,5 @@ target_include_directories(imgui_imnodes PUBLIC ) target_link_libraries(imgui_imnodes PRIVATE imgui_includes) +target_include_directories(imgui_all_includes INTERFACE include) set_property(TARGET imgui_imnodes PROPERTY POSITION_INDEPENDENT_CODE ON) diff --git a/lib/third_party/imgui/implot/CMakeLists.txt b/lib/third_party/imgui/implot/CMakeLists.txt index 2dfa882b5..a2903e0aa 100644 --- a/lib/third_party/imgui/implot/CMakeLists.txt +++ b/lib/third_party/imgui/implot/CMakeLists.txt @@ -15,4 +15,6 @@ target_include_directories(imgui_implot PUBLIC ) target_link_libraries(imgui_implot PRIVATE imgui_includes) +target_include_directories(imgui_all_includes INTERFACE include) + set_property(TARGET imgui_implot PROPERTY POSITION_INDEPENDENT_CODE ON) diff --git a/main/gui/CMakeLists.txt b/main/gui/CMakeLists.txt index 7e50a5fa5..14e5b91dd 100644 --- a/main/gui/CMakeLists.txt +++ b/main/gui/CMakeLists.txt @@ -52,7 +52,7 @@ set_target_properties(main PROPERTIES add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}") -target_link_libraries(main PRIVATE libromfs-imhex libimhex ${FMT_LIBRARIES}) +target_link_libraries(main PRIVATE libromfs-imhex libimhex ${GLFW_LIBRARIES} libwolv libpl plcli libpl-gen ${FMT_LIBRARIES}) if (WIN32) target_link_libraries(main PRIVATE usp10 wsock32 ws2_32 Dwmapi.lib) else () diff --git a/main/gui/source/init/tasks.cpp b/main/gui/source/init/tasks.cpp index e46e94c1a..4fb622188 100644 --- a/main/gui/source/init/tasks.cpp +++ b/main/gui/source/init/tasks.cpp @@ -125,7 +125,6 @@ namespace hex::init { ContentRegistry::HexEditor::impl::getVisualizers().clear(); ContentRegistry::BackgroundServices::impl::stopServices(); - ContentRegistry::BackgroundServices::impl::getServices().clear(); ContentRegistry::CommunicationInterface::impl::getNetworkEndpoints().clear(); diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index 23678db08..eed18ceaf 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -129,6 +129,17 @@ add_imhex_plugin( INCLUDES include + ${CAPSTONE_INCLUDE_DIRS} + ${YARA_INCLUDE_DIRS} + ${MINIAUDIO_INCLUDE_DIRS} + + LIBRARIES + ${CAPSTONE_LIBRARIES} + ${YARA_LIBRARIES} + ${MINIAUDIO_LIBRARIES} + ${JTHREAD_LIBRARIES} + ${GLFW_LIBRARIES} + plcli libpl-gen ) if (WIN32) diff --git a/lib/libimhex/include/hex/helpers/disassembler.hpp b/plugins/builtin/include/content/helpers/disassembler.hpp similarity index 100% rename from lib/libimhex/include/hex/helpers/disassembler.hpp rename to plugins/builtin/include/content/helpers/disassembler.hpp diff --git a/plugins/builtin/include/content/views/view_disassembler.hpp b/plugins/builtin/include/content/views/view_disassembler.hpp index 4a4d93476..4689b6eb6 100644 --- a/plugins/builtin/include/content/views/view_disassembler.hpp +++ b/plugins/builtin/include/content/views/view_disassembler.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include diff --git a/plugins/windows/CMakeLists.txt b/plugins/windows/CMakeLists.txt index 01bcaecfb..dfb91c23f 100644 --- a/plugins/windows/CMakeLists.txt +++ b/plugins/windows/CMakeLists.txt @@ -15,6 +15,8 @@ if (WIN32) source/content/settings_entries.cpp INCLUDES include + LIBRARIES + ${JTHREAD_LIBRARIES} ) endif () diff --git a/tests/common/CMakeLists.txt b/tests/common/CMakeLists.txt index 67d5b1435..5fadc6ab4 100644 --- a/tests/common/CMakeLists.txt +++ b/tests/common/CMakeLists.txt @@ -6,4 +6,4 @@ add_library(tests_common STATIC source/main.cpp ) target_include_directories(tests_common PUBLIC include) -target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES}) +target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES} libwolv) diff --git a/tests/common/include/hex/test/test_provider.hpp b/tests/common/include/hex/test/test_provider.hpp index 2d8ec7026..358047ca6 100644 --- a/tests/common/include/hex/test/test_provider.hpp +++ b/tests/common/include/hex/test/test_provider.hpp @@ -1,14 +1,14 @@ -#include +#pragma once -#include -#include +#include +#include namespace hex::test { using namespace hex::prv; class TestProvider : public prv::Provider { public: - explicit TestProvider(std::vector *data) : Provider() { + explicit TestProvider(std::vector *data) { this->setData(data); } ~TestProvider() override = default; @@ -47,11 +47,14 @@ namespace hex::test { return m_data->size(); } - [[nodiscard]] virtual std::string getTypeName() const override { return "hex.test.provider.test"; } + [[nodiscard]] std::string getTypeName() const override { return "hex.test.provider.test"; } bool open() override { return true; } void close() override { } + nlohmann::json storeSettings(nlohmann::json) const override { return {}; } + void loadSettings(const nlohmann::json &) override {}; + private: std::vector *m_data = nullptr; };