1
0
mirror of synced 2024-11-28 09:30:51 +01:00

sys: Embed resources into rodata

This commit is contained in:
WerWolv 2021-07-31 17:10:19 +02:00
parent bca7f738a1
commit e8d77f8269
9 changed files with 89 additions and 24 deletions

View File

@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
include("${CMAKE_SOURCE_DIR}/cmake/build_helpers.cmake") include("${CMAKE_SOURCE_DIR}/cmake/build_helpers.cmake")
setDefaultBuiltTypeIfUnset() setDefaultBuiltTypeIfUnset()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
# List plugin names here. Project name must match folder name # List plugin names here. Project name must match folder name
set(PLUGINS set(PLUGINS

View File

@ -22,6 +22,7 @@ namespace ImGui {
} }
std::tuple<ImTextureID, int, int> LoadImageFromPath(const char *path); std::tuple<ImTextureID, int, int> LoadImageFromPath(const char *path);
std::tuple<ImTextureID, int, int> LoadImageFromMemory(ImU8 *buffer, int size);
void UnloadImage(ImTextureID texture); void UnloadImage(ImTextureID texture);
enum ImGuiCustomCol { enum ImGuiCustomCol {

View File

@ -226,7 +226,37 @@ namespace ImGui {
return { reinterpret_cast<ImTextureID>(static_cast<intptr_t>(texture)), imageWidth, imageHeight }; return { reinterpret_cast<ImTextureID>(static_cast<intptr_t>(texture)), imageWidth, imageHeight };
} }
std::tuple<ImTextureID, int, int> 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<ImTextureID>(static_cast<intptr_t>(texture)), imageWidth, imageHeight };
}
void UnloadImage(ImTextureID texture) { void UnloadImage(ImTextureID texture) {
if (texture == nullptr)
return;
auto glTextureId = static_cast<GLuint>(reinterpret_cast<intptr_t>(texture)); auto glTextureId = static_cast<GLuint>(reinterpret_cast<intptr_t>(texture));
glDeleteTextures(1, &glTextureId); glDeleteTextures(1, &glTextureId);
} }

View File

@ -56,6 +56,8 @@ set(LIBIMHEX_SOURCES
source/providers/provider.cpp source/providers/provider.cpp
source/views/view.cpp source/views/view.cpp
source/resources.cpp
) )
if (APPLE) if (APPLE)

View File

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

View File

@ -4,6 +4,11 @@
#include <filesystem> #include <filesystem>
#include <mbedtls/x509.h>
#include <mbedtls/x509_crt.h>
#include <hex/resources.hpp>
namespace hex { namespace hex {
Net::Net() { Net::Net() {
@ -45,10 +50,16 @@ namespace hex {
curl_easy_setopt(ctx, CURLOPT_WRITEFUNCTION, writeToString); curl_easy_setopt(ctx, CURLOPT_WRITEFUNCTION, writeToString);
curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYHOST, 1L); curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYHOST, 1L);
for (const auto &resourceDir : hex::getPath(hex::ImHexPath::Resources)) { curl_easy_setopt(ctx, CURLOPT_CAINFO, nullptr);
if (std::filesystem::exists(resourceDir + "/cacert.pem")) curl_easy_setopt(ctx, CURLOPT_CAPATH, nullptr);
curl_easy_setopt(ctx, CURLOPT_CAPATH, resourceDir.c_str()); curl_easy_setopt(ctx, CURLOPT_SSL_CTX_DATA, [](CURL *ctx, void *sslctx, void *userdata) -> CURLcode {
} auto* mbedtlsCert = static_cast<mbedtls_x509_crt*>(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_WRITEDATA, &response);
curl_easy_setopt(ctx, CURLOPT_TIMEOUT_MS, 2000L); curl_easy_setopt(ctx, CURLOPT_TIMEOUT_MS, 2000L);
curl_easy_setopt(ctx, CURLOPT_CONNECTTIMEOUT_MS, 2000L); curl_easy_setopt(ctx, CURLOPT_CONNECTTIMEOUT_MS, 2000L);

View File

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

View File

@ -2,6 +2,7 @@
#include <hex/helpers/utils.hpp> #include <hex/helpers/utils.hpp>
#include <hex/helpers/shared_data.hpp> #include <hex/helpers/shared_data.hpp>
#include <hex/resources.hpp>
#include <imgui.h> #include <imgui.h>
#include <imgui_internal.h> #include <imgui_internal.h>
@ -68,11 +69,7 @@ namespace hex::init {
ImTextureID splashTexture; ImTextureID splashTexture;
u32 splashWidth, splashHeight; u32 splashWidth, splashHeight;
for (const auto &path : hex::getPath(hex::ImHexPath::Resources)) { std::tie(splashTexture, splashWidth, splashHeight) = ImGui::LoadImageFromMemory(splash, splash_size);
std::tie(splashTexture, splashWidth, splashHeight) = ImGui::LoadImageFromPath((path + "/splash.png").c_str());
if (splashTexture != nullptr)
break;
}
if (splashTexture == nullptr) { if (splashTexture == nullptr) {
log::fatal("Could not load splash screen image!"); log::fatal("Could not load splash screen image!");

View File

@ -2,6 +2,7 @@
#include <hex.hpp> #include <hex.hpp>
#include <hex/api/content_registry.hpp> #include <hex/api/content_registry.hpp>
#include <hex/resources.hpp>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
@ -78,38 +79,28 @@ namespace hex {
ImGui::UnloadImage(this->m_bannerTexture); ImGui::UnloadImage(this->m_bannerTexture);
if (theme.is_number()) { if (theme.is_number()) {
std::string bannerFile;
switch (static_cast<int>(theme)) { switch (static_cast<int>(theme)) {
default: default:
case 0: /* Dark theme */ case 0: /* Dark theme */
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
ImGui::StyleCustomColorsDark(); ImGui::StyleCustomColorsDark();
ImPlot::StyleColorsDark(); 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; break;
case 1: /* Light theme */ case 1: /* Light theme */
ImGui::StyleColorsLight(); ImGui::StyleColorsLight();
ImGui::StyleCustomColorsLight(); ImGui::StyleCustomColorsLight();
ImPlot::StyleColorsLight(); ImPlot::StyleColorsLight();
bannerFile = "/banner_light.png"; std::tie(this->m_bannerTexture, this->m_bannerWidth, this->m_bannerHeight) = ImGui::LoadImageFromMemory(banner_light, banner_light_size); break;
break;
case 2: /* Classic theme */ case 2: /* Classic theme */
ImGui::StyleColorsClassic(); ImGui::StyleColorsClassic();
ImGui::StyleCustomColorsClassic(); ImGui::StyleCustomColorsClassic();
ImPlot::StyleColorsClassic(); ImPlot::StyleColorsClassic();
bannerFile = "/banner_dark.png"; std::tie(this->m_bannerTexture, this->m_bannerWidth, this->m_bannerHeight) = ImGui::LoadImageFromMemory(banner_dark, banner_dark_size); break;
break;
} }
ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; 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) { if (this->m_bannerTexture == nullptr) {
log::fatal("Failed to load banner texture!"); log::fatal("Failed to load banner texture!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -191,8 +182,6 @@ namespace hex {
} }
Window::~Window() { Window::~Window() {
delete SharedData::currentProvider;
this->deinitImGui(); this->deinitImGui();
this->deinitGLFW(); this->deinitGLFW();