From 85f276c259a712b1d4b11ec981cad13a0f7067b8 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 22 Dec 2021 15:06:16 +0100 Subject: [PATCH] resources: Start using libromfs to embed resources --- .gitmodules | 3 + .../ImGui/include/imgui_imhex_extensions.h | 2 +- .../ImGui/source/imgui_imhex_extensions.cpp | 2 +- external/libromfs | 1 + plugins/libimhex/CMakeLists.txt | 9 +- plugins/libimhex/include/hex/resources.hpp | 18 ---- plugins/libimhex/source/helpers/net.cpp | 6 +- plugins/libimhex/source/resources.cpp | 96 ------------------- source/init/splash_window.cpp | 6 +- source/window/window.cpp | 27 +++++- 10 files changed, 42 insertions(+), 128 deletions(-) create mode 160000 external/libromfs delete mode 100644 plugins/libimhex/include/hex/resources.hpp delete mode 100644 plugins/libimhex/source/resources.cpp diff --git a/.gitmodules b/.gitmodules index 519530f1f..1513ce1a8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -21,3 +21,6 @@ [submodule "external/capstone"] path = external/capstone url = https://github.com/capstone-engine/capstone +[submodule "external/libromfs"] + path = external/libromfs + url = https://github.com/WerWolv/libromfs diff --git a/external/ImGui/include/imgui_imhex_extensions.h b/external/ImGui/include/imgui_imhex_extensions.h index e8a9ffd18..5e006f6c2 100644 --- a/external/ImGui/include/imgui_imhex_extensions.h +++ b/external/ImGui/include/imgui_imhex_extensions.h @@ -71,7 +71,7 @@ namespace ImGui { } Texture LoadImageFromPath(const char *path); - Texture LoadImageFromMemory(ImU8 *buffer, int size); + Texture LoadImageFromMemory(const ImU8 *buffer, int size); void UnloadImage(Texture &texture); void OpenPopupInWindow(const char *window_name, const char *popup_name); diff --git a/external/ImGui/source/imgui_imhex_extensions.cpp b/external/ImGui/source/imgui_imhex_extensions.cpp index cbb5fe5e4..6c728abea 100644 --- a/external/ImGui/source/imgui_imhex_extensions.cpp +++ b/external/ImGui/source/imgui_imhex_extensions.cpp @@ -329,7 +329,7 @@ namespace ImGui { return { reinterpret_cast(static_cast(texture)), imageWidth, imageHeight }; } - Texture LoadImageFromMemory(ImU8 *buffer, int size) { + Texture LoadImageFromMemory(const ImU8 *buffer, int size) { int imageWidth = 0; int imageHeight = 0; diff --git a/external/libromfs b/external/libromfs new file mode 160000 index 000000000..7f134647d --- /dev/null +++ b/external/libromfs @@ -0,0 +1 @@ +Subproject commit 7f134647d92898ad33022ed7dda48d7a4c490f56 diff --git a/plugins/libimhex/CMakeLists.txt b/plugins/libimhex/CMakeLists.txt index 603da8661..a952c53e9 100644 --- a/plugins/libimhex/CMakeLists.txt +++ b/plugins/libimhex/CMakeLists.txt @@ -13,6 +13,10 @@ set_target_properties(microtar PROPERTIES POSITION_INDEPENDENT_CODE ON) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/nativefiledialog ${CMAKE_CURRENT_BINARY_DIR}/external/nativefiledialog EXCLUDE_FROM_ALL) set_target_properties(nfd PROPERTIES POSITION_INDEPENDENT_CODE ON) +set(LIBROMFS_RESOURCE_LOCATION ${CMAKE_SOURCE_DIR}/res/resources) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/libromfs ${CMAKE_CURRENT_BINARY_DIR}/external/libromfs EXCLUDE_FROM_ALL) +set_target_properties(libromfs PROPERTIES POSITION_INDEPENDENT_CODE ON) + set(XDGPP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../external/xdgpp") set(CMAKE_USE_MBEDTLS ON) set(BUILD_CURL_EXE OFF) @@ -131,8 +135,7 @@ set(LIBIMHEX_SOURCES source/views/view.cpp - source/resources.cpp -) + ) if (APPLE) set(OSX_11_0_SDK_PATH /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk) @@ -158,4 +161,4 @@ if (APPLE) target_link_libraries(libimhex PUBLIC ${FOUNDATION}) endif () -target_link_libraries(libimhex PUBLIC imgui nfd magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${LIBCURL_LIBRARIES} ${MBEDTLS_LIBRARIES} ${FMT_LIBRARIES} ${Python_LIBRARIES}) +target_link_libraries(libimhex PUBLIC imgui nfd magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${LIBCURL_LIBRARIES} ${MBEDTLS_LIBRARIES} ${FMT_LIBRARIES} ${Python_LIBRARIES} libromfs) diff --git a/plugins/libimhex/include/hex/resources.hpp b/plugins/libimhex/include/hex/resources.hpp deleted file mode 100644 index bccb657cd..000000000 --- a/plugins/libimhex/include/hex/resources.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#define RESOURCE_EXPORT(name) \ - extern "C" unsigned char name[]; \ - extern "C" int name##_size; - - - -RESOURCE_EXPORT(splash); -RESOURCE_EXPORT(banner_light); -RESOURCE_EXPORT(banner_dark); -RESOURCE_EXPORT(imhex_logo); - -RESOURCE_EXPORT(cacert); - - - -#undef RESOURCE_EXPORT \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/net.cpp b/plugins/libimhex/source/helpers/net.cpp index ef883292c..7b304a466 100644 --- a/plugins/libimhex/source/helpers/net.cpp +++ b/plugins/libimhex/source/helpers/net.cpp @@ -11,7 +11,7 @@ #include #include -#include +#include namespace hex { @@ -54,7 +54,9 @@ namespace hex { static mbedtls_x509_crt crt; mbedtls_x509_crt_init(&crt); - mbedtls_x509_crt_parse(&crt, cacert, cacert_size); + + auto cacert = romfs::get("cacert.pem"); + mbedtls_x509_crt_parse(&crt, reinterpret_cast(cacert.data()), cacert.size()); mbedtls_ssl_conf_ca_chain(cfg, &crt, nullptr); diff --git a/plugins/libimhex/source/resources.cpp b/plugins/libimhex/source/resources.cpp deleted file mode 100644 index 69da7c23d..000000000 --- a/plugins/libimhex/source/resources.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#if defined(OS_WINDOWS) - - #define RESOURCE(name, path) \ - __asm__ ( \ - ".section .rodata\n" \ - ".global " #name "\n" \ - ".global " #name "_size\n" \ - #name ":\n" \ - ".incbin \"" path "\"\n" \ - #name "_size:\n" \ - ".int " #name "_size - " #name "\n" \ - ".align 8\n" \ - ) - - #define RESOURCE_NULL_TERMINATED(name, path) \ - __asm__ ( \ - ".section .rodata\n" \ - ".global " #name "\n" \ - ".global " #name "_size\n" \ - #name ":\n" \ - ".incbin \"" path "\"\n" \ - ".byte 0\n" \ - #name "_size:\n" \ - ".int " #name "_size - " #name "\n" \ - ".align 8\n" \ - ) - -#elif defined(OS_MACOS) - - #define RESOURCE(name, path) \ - __asm__ ( \ - ".global _" #name ";\n" \ - ".global _" #name "_size;\n" \ - "_" #name ":\n" \ - ".incbin \"" path "\";\n" \ - ".align 8;\n" \ - "_" #name "_size:\n" \ - ".int _" #name "_size - _" #name ";\n" \ - ".align 8;\n" \ - ) - - #define RESOURCE_NULL_TERMINATED(name, path) \ - __asm__ ( \ - ".global _" #name ";\n" \ - ".global _" #name "_size;\n" \ - "_" #name ":\n" \ - ".incbin \"" path "\";\n" \ - ".byte 0\n" \ - ".align 8;\n" \ - "_" #name "_size:\n" \ - ".int _" #name "_size - _" #name ";\n" \ - ".align 8;\n" \ - ) - -#elif defined(OS_LINUX) - - #define RESOURCE(name, path) \ - __asm__ ( \ - ".section .rodata\n" \ - ".global " #name "\n" \ - ".global " #name "_size\n" \ - #name ":\n" \ - ".incbin \"" path "\"\n" \ - ".byte 0\n" \ - ".type " #name ", @object\n" \ - ".size " #name "_size, 1\n" \ - #name "_size:\n" \ - ".int " #name "_size - " #name "\n" \ - ".align 8\n" \ - ) - - #define RESOURCE_NULL_TERMINATED(name, path) \ - __asm__ ( \ - ".section .rodata\n" \ - ".global " #name "\n" \ - ".global " #name "_size\n" \ - #name ":\n" \ - ".incbin \"" path "\"\n" \ - ".byte 0\n" \ - ".type " #name ", @object\n" \ - ".size " #name "_size, 1\n" \ - #name "_size:\n" \ - ".int " #name "_size - " #name "\n" \ - ".align 8\n" \ - ) - -#endif - - - -RESOURCE(banner_light, "../../../res/resources/banner_light.png"); -RESOURCE(banner_dark, "../../../res/resources/banner_dark.png"); -RESOURCE(splash, "../../../res/resources/splash.png"); -RESOURCE(imhex_logo, "../../../res/resources/logo.png"); - -RESOURCE_NULL_TERMINATED(cacert, "../../../res/resources/cacert.pem"); \ No newline at end of file diff --git a/source/init/splash_window.cpp b/source/init/splash_window.cpp index b971a19f0..0eca72504 100644 --- a/source/init/splash_window.cpp +++ b/source/init/splash_window.cpp @@ -4,7 +4,8 @@ #include #include #include -#include + +#include #include #define IMGUI_DEFINE_MATH_OPERATORS @@ -70,7 +71,8 @@ namespace hex::init { } bool WindowSplash::loop() { - ImGui::Texture splashTexture = ImGui::LoadImageFromMemory(splash, splash_size); + auto splash = romfs::get("splash.png"); + ImGui::Texture splashTexture = ImGui::LoadImageFromMemory(reinterpret_cast(splash.data()), splash.size()); if (splashTexture == nullptr) { log::fatal("Could not load splash screen image!"); diff --git a/source/window/window.cpp b/source/window/window.cpp index 939a31f24..75c5f4f62 100644 --- a/source/window/window.cpp +++ b/source/window/window.cpp @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -14,6 +13,8 @@ #include #include +#include + #include #define IMGUI_DEFINE_MATH_OPERATORS #include @@ -127,23 +128,38 @@ namespace hex { switch (theme) { default: case 1: /* Dark theme */ + { ImGui::StyleColorsDark(); ImGui::StyleCustomColorsDark(); ImPlot::StyleColorsDark(); - this->m_bannerTexture = ImGui::LoadImageFromMemory(banner_dark, banner_dark_size); + + auto banner = romfs::get("banner_dark.png"); + this->m_bannerTexture = ImGui::LoadImageFromMemory(reinterpret_cast(banner.data()), banner.size()); + break; + } case 2: /* Light theme */ + { ImGui::StyleColorsLight(); ImGui::StyleCustomColorsLight(); ImPlot::StyleColorsLight(); - this->m_bannerTexture = ImGui::LoadImageFromMemory(banner_light, banner_light_size); + + auto banner = romfs::get("banner_light.png"); + this->m_bannerTexture = ImGui::LoadImageFromMemory(reinterpret_cast(banner.data()), banner.size()); + break; + } case 3: /* Classic theme */ + { ImGui::StyleColorsClassic(); ImGui::StyleCustomColorsClassic(); ImPlot::StyleColorsClassic(); - this->m_bannerTexture = ImGui::LoadImageFromMemory(banner_dark, banner_dark_size); + + auto banner = romfs::get("banner_dark.png"); + this->m_bannerTexture = ImGui::LoadImageFromMemory(reinterpret_cast(banner.data()), banner.size()); + break; + } } ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; @@ -255,7 +271,8 @@ namespace hex { std::signal(SIGABRT, signalHandler); std::signal(SIGFPE, signalHandler); - this->m_logoTexture = ImGui::LoadImageFromMemory(imhex_logo, imhex_logo_size); + auto imhexLogo = romfs::get("logo.png"); + this->m_logoTexture = ImGui::LoadImageFromMemory(reinterpret_cast(imhexLogo.data()), imhexLogo.size()); ContentRegistry::Settings::store(); EventManager::post();