impr: Get rid of cimgui shared library by hooking pinvoke handler

This commit is contained in:
WerWolv
2024-03-12 23:17:49 +01:00
parent 876f091244
commit fdf01dfb50
15 changed files with 135 additions and 57 deletions

View File

@@ -664,6 +664,12 @@ namespace hex {
*/
std::optional<InitialWindowProperties> getInitialWindowProperties();
/**
* @brief Gets the module handle of libimhex
* @return Module handle
*/
void* getLibImHexModuleHandle();
}
/**

View File

@@ -331,4 +331,14 @@ namespace hex {
[[nodiscard]] std::string formatSystemError(i32 error);
/**
* Gets the shared library handle for a given pointer
* @param symbol Pointer to any function or variable in the shared library
* @return The module handle
* @warning Important! Calling this function on functions defined in other modules will return the handle of the current module!
* This is because you're not actually passing a pointer to the function in the other module but rather a pointer to a thunk
* that is defined in the current module.
*/
[[nodiscard]] void* getContainingModule(void* symbol);
}

View File

@@ -599,6 +599,11 @@ namespace hex {
return impl::s_initialWindowProperties;
}
void* getLibImHexModuleHandle() {
return hex::getContainingModule((void*)&getLibImHexModuleHandle);
}
const std::map<std::string, std::string>& getInitArguments() {
return *impl::s_initArguments;
}

View File

@@ -344,5 +344,4 @@ namespace hex {
});
}
}

View File

@@ -16,9 +16,11 @@
#include <wolv/utils/guards.hpp>
#elif defined(OS_LINUX)
#include <unistd.h>
#include <dlfcn.h>
#include <hex/helpers/utils_linux.hpp>
#elif defined(OS_MACOS)
#include <unistd.h>
#include <dlfcn.h>
#include <hex/helpers/utils_macos.hpp>
#elif defined(OS_WEB)
#include "emscripten.h"
@@ -784,4 +786,23 @@ namespace hex {
#endif
}
void* getContainingModule(void* symbol) {
#if defined(OS_WINDOWS)
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery(symbol, &mbi, sizeof(mbi)))
return mbi.AllocationBase;
return nullptr;
#elif !defined(OS_WEB)
Dl_info info;
if (dladdr(symbol, nullptr) == 0)
return nullptr;
return dlopen(info.dli_fname, RTLD_LAZY);
#else
return nullptr;
#endif
}
}

View File

@@ -4,6 +4,7 @@
#include <imgui_internal.h>
#include <implot.h>
#include <implot_internal.h>
#include <cimgui.h>
#include <opengl_support.h>
#undef IMGUI_DEFINE_MATH_OPERATORS