From 6ea34bf00b6f957074413d9577d7682ae1758499 Mon Sep 17 00:00:00 2001 From: Faumaray Date: Mon, 20 Oct 2025 22:04:25 +0300 Subject: [PATCH] Loading mods Signed-off-by: Faumaray --- CMakeLists.txt | 1 - include/Mods/LoadLibrary.hpp | 6 ++ include/Mods/LoadLibraryUnix.hpp | 8 +++ include/Mods/LoadLibraryWin.hpp | 9 +++ include/Mods/Loader.hpp | 2 +- src/Agent.cpp | 5 +- src/Mods/LoadLibraryUnix.cpp | 20 ++++++ src/Mods/LoadLibraryWin.cpp | 114 +++++++++++++++++++++++++++++++ src/Mods/Loader.cpp | 8 ++- src/Mods/Registry.cpp | 6 +- 10 files changed, 169 insertions(+), 10 deletions(-) create mode 100644 include/Mods/LoadLibrary.hpp create mode 100644 include/Mods/LoadLibraryUnix.hpp create mode 100644 include/Mods/LoadLibraryWin.hpp create mode 100644 src/Mods/LoadLibraryUnix.cpp create mode 100644 src/Mods/LoadLibraryWin.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 65a2166..1d2b001 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,6 @@ FetchContent_Declare( GIT_TAG main # release-1.10.0 ) - FetchContent_MakeAvailable(VoidLoaderApi) # ============================================================ diff --git a/include/Mods/LoadLibrary.hpp b/include/Mods/LoadLibrary.hpp new file mode 100644 index 0000000..a009e74 --- /dev/null +++ b/include/Mods/LoadLibrary.hpp @@ -0,0 +1,6 @@ +#pragma once +#if defined(_WIN32) +# include +#else +# include +#endif \ No newline at end of file diff --git a/include/Mods/LoadLibraryUnix.hpp b/include/Mods/LoadLibraryUnix.hpp new file mode 100644 index 0000000..8fc98fd --- /dev/null +++ b/include/Mods/LoadLibraryUnix.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + + +void* LoadLibrary(const std::string& path, bool isGlobal); +bool CloseLibrary(void* handle); +void* LoadSymbol(void* handle, const std::string& name); \ No newline at end of file diff --git a/include/Mods/LoadLibraryWin.hpp b/include/Mods/LoadLibraryWin.hpp new file mode 100644 index 0000000..d74bb72 --- /dev/null +++ b/include/Mods/LoadLibraryWin.hpp @@ -0,0 +1,9 @@ +#pragma once +#if defined(_WIN32) + +#include + +void* LoadLibrary(const std::string& path, bool isGlobal); +bool CloseLibrary(void* handle); +void* LoadSymbol(void* handle, const std::string& name); +#endif \ No newline at end of file diff --git a/include/Mods/Loader.hpp b/include/Mods/Loader.hpp index 3278b53..560bd4d 100644 --- a/include/Mods/Loader.hpp +++ b/include/Mods/Loader.hpp @@ -13,7 +13,7 @@ class ModLoader { public: // Load a module (.so), discover CRTP-registered mod types, instantiate each with Context. // Returns the number of instances created from this module. - std::size_t loadModule(const std::string& path, const Context& ctx); + std::size_t loadModule(const std::string& path, const Context& ctx, bool isGlobal); // Start/stop all currently loaded instances. void startAll(); diff --git a/src/Agent.cpp b/src/Agent.cpp index 668941a..936dc94 100644 --- a/src/Agent.cpp +++ b/src/Agent.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -36,7 +37,7 @@ namespace vl::Agent { return; } - dlopen(mods.at("VoidLoaderApi.vlmod").c_str(), RTLD_NOW | RTLD_GLOBAL); + LoadLibrary(mods.at("VoidLoaderApi.vlmod").c_str(), true); mods.erase("VoidLoaderApi.vlmod"); mods.erase("VoidLoader.vlmod"); @@ -134,7 +135,7 @@ extern "C" JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv* env, jclass claz for (auto& [name, path] : instance.m_mods) { instance.m_logger->trace("Loading mod: {}", name); - loaded += instance.m_modloader->loadModule(path, instance.m_context); + loaded += instance.m_modloader->loadModule(path, instance.m_context, false); } instance.m_logger->info("Mods loaded: {}, Starting", loaded); diff --git a/src/Mods/LoadLibraryUnix.cpp b/src/Mods/LoadLibraryUnix.cpp new file mode 100644 index 0000000..03db8ce --- /dev/null +++ b/src/Mods/LoadLibraryUnix.cpp @@ -0,0 +1,20 @@ +#if defined(unix) + +#include +#include + +void* LoadLibrary(const std::string& path, bool isGlobal) +{ + dlerror(); + return dlopen(path.c_str(), isGlobal ? RTLD_NOW | RTLD_GLOBAL : RTLD_NOW); +} +bool CloseLibrary(void* handle) +{ + return dlclose(handle) == 0; +} +void* LoadSymbol(void* handle, const std::string& name) +{ + dlerror(); + return dlsym(handle, name.c_str()); +} +#endif \ No newline at end of file diff --git a/src/Mods/LoadLibraryWin.cpp b/src/Mods/LoadLibraryWin.cpp new file mode 100644 index 0000000..9248c32 --- /dev/null +++ b/src/Mods/LoadLibraryWin.cpp @@ -0,0 +1,114 @@ +#if defined(_WIN32) + +#include +#include +#include +#include + +static int32_t __wtf8_decode1(const char** input) { + uint32_t code_point; + uint8_t b1; + uint8_t b2; + uint8_t b3; + uint8_t b4; + + b1 = **input; + if (b1 <= 0x7F) + return b1; /* ASCII code point */ + if (b1 < 0xC2) + return -1; /* invalid: continuation byte */ + code_point = b1; + + b2 = *++*input; + if ((b2 & 0xC0) != 0x80) + return -1; /* invalid: not a continuation byte */ + code_point = (code_point << 6) | (b2 & 0x3F); + if (b1 <= 0xDF) + return 0x7FF & code_point; /* two-byte character */ + + b3 = *++*input; + if ((b3 & 0xC0) != 0x80) + return -1; /* invalid: not a continuation byte */ + code_point = (code_point << 6) | (b3 & 0x3F); + if (b1 <= 0xEF) + return 0xFFFF & code_point; /* three-byte character */ + + b4 = *++*input; + if ((b4 & 0xC0) != 0x80) + return -1; /* invalid: not a continuation byte */ + code_point = (code_point << 6) | (b4 & 0x3F); + if (b1 <= 0xF4) { + code_point &= 0x1FFFFF; + if (code_point <= 0x10FFFF) + return code_point; /* four-byte character */ + } + + /* code point too large */ + return -1; +} + +ssize_t wtf8_length_as_utf16(const char* source_ptr) { + size_t w_target_len = 0; + int32_t code_point; + + do { + code_point = __wtf8_decode1(&source_ptr); + if (code_point < 0) + return -1; + if (code_point > 0xFFFF) + w_target_len++; + w_target_len++; + } while (*source_ptr++); + + return w_target_len; +} + +void wtf8_to_utf16(const char* source_ptr, + uint16_t* w_target, + size_t w_target_len) { + int32_t code_point; + + do { + code_point = __wtf8_decode1(&source_ptr); + /* uv_wtf8_length_as_utf16 should have been called and checked first. */ + assert(code_point >= 0); + if (code_point > 0xFFFF) { + assert(code_point < 0x10FFFF); + *w_target++ = (((code_point - 0x10000) >> 10) + 0xD800); + *w_target++ = ((code_point - 0x10000) & 0x3FF) + 0xDC00; + w_target_len -= 2; + } else { + *w_target++ = code_point; + w_target_len -= 1; + } + } while (*source_ptr++); + + (void)w_target_len; + assert(w_target_len == 0); +} + +void* LoadLibrary(const std::string& path, bool isGlobal) +{ + WCHAR filename_w[32768]; + ssize_t r; + void* handle = nullptr; + + r = wtf8_length_as_utf16(path.c_str()); + if (r < 0) + return nullptr; + if ((size_t) r > ARRAY_SIZE(filename_w)) + return nullptr; + _wtf8_to_utf16(path.c_str(), filename_w, r); + + return LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); +} +bool CloseLibrary(void* handle) +{ + return FreeLibrary(handle) != 0; +} +void* LoadSymbol(void* handle, const std::string& name) +{ + return (void*)(uintptr_t) GetProcAddress(handle, name.c_str()); +} + +#endif \ No newline at end of file diff --git a/src/Mods/Loader.cpp b/src/Mods/Loader.cpp index d9cfff4..94936db 100644 --- a/src/Mods/Loader.cpp +++ b/src/Mods/Loader.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include #include #include @@ -6,8 +8,8 @@ namespace vl::mod { -std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx) { - void* handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL); +std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx, bool isGlobal) { + void* handle = LoadLibrary(path, isGlobal); if (!handle) throw std::runtime_error(std::string("dlopen failed: ") + dlerror()); handles_.push_back(handle); @@ -48,7 +50,7 @@ void ModLoader::stopAll() { void ModLoader::unloadAll() { stopAll(); instances_.clear(); - for (auto* h : handles_) if (h) dlclose(h); + for (auto* h : handles_) if (h) CloseLibrary(h); handles_.clear(); } diff --git a/src/Mods/Registry.cpp b/src/Mods/Registry.cpp index a5f0931..312f79e 100644 --- a/src/Mods/Registry.cpp +++ b/src/Mods/Registry.cpp @@ -1,5 +1,5 @@ #include -#include // dlopen/dlsym (ELF) +#include #include #include @@ -21,8 +21,8 @@ std::vector& Registry::mutableAll() { std::size_t Registry::addFromModule(void* soHandle) { #if defined(__ELF__) using BoundPtr = const Registration*; - auto symStart = dlsym(soHandle, "__start_vlmods"); - auto symStop = dlsym(soHandle, "__stop_vlmods"); + auto symStart = LoadSymbol(soHandle, "__start_vlmods"); + auto symStop = LoadSymbol(soHandle, "__stop_vlmods"); if ((symStart == nullptr) || (symStop == nullptr)) { return 0;