From e8d77f8269cc98e9aa66f0465056d2f2357a42bc Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 31 Jul 2021 17:10:19 +0200 Subject: [PATCH] sys: Embed resources into rodata --- CMakeLists.txt | 1 + .../ImGui/include/imgui_imhex_extensions.h | 1 + .../ImGui/source/imgui_imhex_extensions.cpp | 30 +++++++++++++++++++ plugins/libimhex/CMakeLists.txt | 2 ++ plugins/libimhex/include/hex/resources.hpp | 16 ++++++++++ plugins/libimhex/source/helpers/net.cpp | 19 +++++++++--- plugins/libimhex/source/resources.cpp | 18 +++++++++++ source/init/splash_window.cpp | 7 ++--- source/window.cpp | 19 +++--------- 9 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 plugins/libimhex/include/hex/resources.hpp create mode 100644 plugins/libimhex/source/resources.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fc266f0c..b6ef1354e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") include("${CMAKE_SOURCE_DIR}/cmake/build_helpers.cmake") setDefaultBuiltTypeIfUnset() +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") # List plugin names here. Project name must match folder name set(PLUGINS diff --git a/external/ImGui/include/imgui_imhex_extensions.h b/external/ImGui/include/imgui_imhex_extensions.h index 63473c128..56ba9edc0 100644 --- a/external/ImGui/include/imgui_imhex_extensions.h +++ b/external/ImGui/include/imgui_imhex_extensions.h @@ -22,6 +22,7 @@ namespace ImGui { } std::tuple LoadImageFromPath(const char *path); + std::tuple LoadImageFromMemory(ImU8 *buffer, int size); void UnloadImage(ImTextureID texture); enum ImGuiCustomCol { diff --git a/external/ImGui/source/imgui_imhex_extensions.cpp b/external/ImGui/source/imgui_imhex_extensions.cpp index 5f90472e7..5a59ca770 100644 --- a/external/ImGui/source/imgui_imhex_extensions.cpp +++ b/external/ImGui/source/imgui_imhex_extensions.cpp @@ -226,7 +226,37 @@ namespace ImGui { return { reinterpret_cast(static_cast(texture)), imageWidth, imageHeight }; } + std::tuple LoadImageFromMemory(ImU8 *buffer, int size) { + int imageWidth = 0; + int imageHeight = 0; + + + unsigned char* imageData = stbi_load_from_memory(buffer, size, &imageWidth, &imageHeight, nullptr, 4); + if (imageData == nullptr) + return { nullptr, -1, -1 }; + + GLuint texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + #if defined(GL_UNPACK_ROW_LENGTH) + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + #endif + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); + stbi_image_free(imageData); + + return { reinterpret_cast(static_cast(texture)), imageWidth, imageHeight }; + } + void UnloadImage(ImTextureID texture) { + if (texture == nullptr) + return; + auto glTextureId = static_cast(reinterpret_cast(texture)); glDeleteTextures(1, &glTextureId); } diff --git a/plugins/libimhex/CMakeLists.txt b/plugins/libimhex/CMakeLists.txt index fe8a4ce65..ce03a1172 100644 --- a/plugins/libimhex/CMakeLists.txt +++ b/plugins/libimhex/CMakeLists.txt @@ -56,6 +56,8 @@ set(LIBIMHEX_SOURCES source/providers/provider.cpp source/views/view.cpp + + source/resources.cpp ) if (APPLE) diff --git a/plugins/libimhex/include/hex/resources.hpp b/plugins/libimhex/include/hex/resources.hpp new file mode 100644 index 000000000..0d556caeb --- /dev/null +++ b/plugins/libimhex/include/hex/resources.hpp @@ -0,0 +1,16 @@ +#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(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 818e0dd37..7150c5153 100644 --- a/plugins/libimhex/source/helpers/net.cpp +++ b/plugins/libimhex/source/helpers/net.cpp @@ -4,6 +4,11 @@ #include +#include +#include + +#include + namespace hex { Net::Net() { @@ -45,10 +50,16 @@ namespace hex { curl_easy_setopt(ctx, CURLOPT_WRITEFUNCTION, writeToString); curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYHOST, 1L); - for (const auto &resourceDir : hex::getPath(hex::ImHexPath::Resources)) { - if (std::filesystem::exists(resourceDir + "/cacert.pem")) - curl_easy_setopt(ctx, CURLOPT_CAPATH, resourceDir.c_str()); - } + curl_easy_setopt(ctx, CURLOPT_CAINFO, nullptr); + curl_easy_setopt(ctx, CURLOPT_CAPATH, nullptr); + curl_easy_setopt(ctx, CURLOPT_SSL_CTX_DATA, [](CURL *ctx, void *sslctx, void *userdata) -> CURLcode { + auto* mbedtlsCert = static_cast(sslctx); + mbedtls_x509_crt_init(mbedtlsCert); + + mbedtls_x509_crt_parse(mbedtlsCert, cacert, cacert_size); + + return CURLE_OK; + }); curl_easy_setopt(ctx, CURLOPT_WRITEDATA, &response); curl_easy_setopt(ctx, CURLOPT_TIMEOUT_MS, 2000L); curl_easy_setopt(ctx, CURLOPT_CONNECTTIMEOUT_MS, 2000L); diff --git a/plugins/libimhex/source/resources.cpp b/plugins/libimhex/source/resources.cpp new file mode 100644 index 000000000..fd45ca72f --- /dev/null +++ b/plugins/libimhex/source/resources.cpp @@ -0,0 +1,18 @@ +#define RESOURCE(name, path) __asm__ ( \ +".section .rodata\n" \ +".global " #name "\n" \ +".global " #name "_size\n" \ + #name ":\n" \ + ".incbin \"" path "\"\n" \ + ".type " #name ", @object\n" \ + ".size " #name "_size, 1\n" \ + #name "_size:\n" \ + ".int " #name "_size - " #name "\n" \ + ".align 8\n" \ + ) + +RESOURCE(banner_light, "../../../res/resources/banner_light.png"); +RESOURCE(banner_dark, "../../../res/resources/banner_dark.png"); +RESOURCE(splash, "../../../res/resources/splash.png"); + +RESOURCE(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 e3062a169..2f862aa2c 100644 --- a/source/init/splash_window.cpp +++ b/source/init/splash_window.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -68,11 +69,7 @@ namespace hex::init { ImTextureID splashTexture; u32 splashWidth, splashHeight; - for (const auto &path : hex::getPath(hex::ImHexPath::Resources)) { - std::tie(splashTexture, splashWidth, splashHeight) = ImGui::LoadImageFromPath((path + "/splash.png").c_str()); - if (splashTexture != nullptr) - break; - } + std::tie(splashTexture, splashWidth, splashHeight) = ImGui::LoadImageFromMemory(splash, splash_size); if (splashTexture == nullptr) { log::fatal("Could not load splash screen image!"); diff --git a/source/window.cpp b/source/window.cpp index b06284c5b..c066d3c98 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -78,38 +79,28 @@ namespace hex { ImGui::UnloadImage(this->m_bannerTexture); if (theme.is_number()) { - std::string bannerFile; - switch (static_cast(theme)) { default: case 0: /* Dark theme */ ImGui::StyleColorsDark(); ImGui::StyleCustomColorsDark(); ImPlot::StyleColorsDark(); - bannerFile = "/banner_dark.png"; + std::tie(this->m_bannerTexture, this->m_bannerWidth, this->m_bannerHeight) = ImGui::LoadImageFromMemory(banner_dark, banner_dark_size); break; case 1: /* Light theme */ ImGui::StyleColorsLight(); ImGui::StyleCustomColorsLight(); ImPlot::StyleColorsLight(); - bannerFile = "/banner_light.png"; - break; + std::tie(this->m_bannerTexture, this->m_bannerWidth, this->m_bannerHeight) = ImGui::LoadImageFromMemory(banner_light, banner_light_size); break; case 2: /* Classic theme */ ImGui::StyleColorsClassic(); ImGui::StyleCustomColorsClassic(); ImPlot::StyleColorsClassic(); - bannerFile = "/banner_dark.png"; - break; + std::tie(this->m_bannerTexture, this->m_bannerWidth, this->m_bannerHeight) = ImGui::LoadImageFromMemory(banner_dark, banner_dark_size); break; } ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; - for (const auto &path : hex::getPath(hex::ImHexPath::Resources)) { - std::tie(this->m_bannerTexture, this->m_bannerWidth, this->m_bannerHeight) = ImGui::LoadImageFromPath((path + bannerFile).c_str()); - if (this->m_bannerTexture != nullptr) - break; - } - if (this->m_bannerTexture == nullptr) { log::fatal("Failed to load banner texture!"); exit(EXIT_FAILURE); @@ -191,8 +182,6 @@ namespace hex { } Window::~Window() { - delete SharedData::currentProvider; - this->deinitImGui(); this->deinitGLFW();