From 7de073bd4736900193f6af5c543a3cf62e6f1a73 Mon Sep 17 00:00:00 2001 From: Curve Date: Wed, 3 Jan 2024 12:06:11 +0100 Subject: [PATCH] feat(module): add `load` --- include/lime/module.hpp | 3 ++ src/module.linux.cpp | 10 +++++ src/module.win.cpp | 87 ++++++++++++++++++++++++++--------------- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/include/lime/module.hpp b/include/lime/module.hpp index 1b4d138..26dfb4b 100644 --- a/include/lime/module.hpp +++ b/include/lime/module.hpp @@ -52,6 +52,9 @@ namespace lime public: [[nodiscard]] static std::optional get(const std::string &name); + [[nodiscard]] static std::optional load(const std::string &name); + + public: [[nodiscard]] static std::optional find(const std::string &name); }; } // namespace lime diff --git a/src/module.linux.cpp b/src/module.linux.cpp index 8a9d77d..53f656f 100644 --- a/src/module.linux.cpp +++ b/src/module.linux.cpp @@ -134,6 +134,16 @@ namespace lime return std::move(*module); } + std::optional module::load(const std::string &name) + { + if (dlopen(name.c_str(), RTLD_NOW) == nullptr) + { + return std::nullopt; + } + + return get(name); + } + std::optional module::find(const std::string &name) { auto all = modules(); diff --git a/src/module.win.cpp b/src/module.win.cpp index 8adfbd2..0d22e79 100644 --- a/src/module.win.cpp +++ b/src/module.win.cpp @@ -11,6 +11,9 @@ namespace lime { struct module::impl { + using callback_t = std::function; + + public: HMODULE handle; public: @@ -18,10 +21,11 @@ namespace lime std::string name; public: - static std::string lower(const std::string &string); + void iterate_symbols(const callback_t &) const; public: - void iterate_symbols(const std::function &) const; + static std::optional get(HMODULE module); + static std::string lower(const std::string &string); }; module::module() :m_impl(std::make_unique()) {} @@ -116,27 +120,14 @@ namespace lime for (auto i = 0u; (modules_size / sizeof(HMODULE)) > i; i++) { - CHAR name[MAX_PATH]; + auto module = impl::get(modules[i]); - if (!GetModuleBaseNameA(GetCurrentProcess(), modules[i], name, MAX_PATH)) + if (!module) { continue; } - MODULEINFO info; - - if (!GetModuleInformation(GetCurrentProcess(), modules[i], &info, sizeof(info))) - { - continue; - } - - lime::module item; - - item.m_impl->info = info; - item.m_impl->handle = modules[i]; - item.m_impl->name = impl::lower(name); - - rtn.emplace_back(std::move(item)); + rtn.emplace_back(std::move(module.value())); } return rtn; @@ -144,17 +135,26 @@ namespace lime std::optional module::get(const std::string &name) { - auto all = modules(); - const auto lower = impl::lower(name); + auto *module = GetModuleHandleA(name.c_str()); + + if (!module) + { + return std::nullopt; + } - auto it = std::find_if(all.begin(), all.end(), [&](auto &item) { return item.name() == lower; }); + return impl::get(module); + } - if (it == all.end()) + std::optional module::load(const std::string &name) + { + auto *module = LoadLibraryA(name.c_str()); + + if (!module) { return std::nullopt; } - return std::move(*it); + return impl::get(module); } std::optional module::find(const std::string &name) @@ -173,14 +173,6 @@ namespace lime return std::move(*it); } - std::string module::impl::lower(const std::string &string) - { - auto rtn{string}; - std::transform(rtn.begin(), rtn.end(), rtn.begin(), [](unsigned char c) { return std::tolower(c); }); - - return rtn; - }; - void module::impl::iterate_symbols(const std::function &callback) const { CHAR path[MAX_PATH]; @@ -219,4 +211,37 @@ namespace lime UnMapAndLoad(&image); } + + std::optional module::impl::get(HMODULE module) + { + CHAR name[MAX_PATH]; + + if (!GetModuleBaseNameA(GetCurrentProcess(), module, name, MAX_PATH)) + { + return std::nullopt; + } + + MODULEINFO info; + + if (!GetModuleInformation(GetCurrentProcess(), module, &info, sizeof(info))) + { + return std::nullopt; + } + + lime::module item; + + item.m_impl->info = info; + item.m_impl->handle = module; + item.m_impl->name = lower(name); + + return item; + } + + std::string module::impl::lower(const std::string &string) + { + auto rtn{string}; + std::transform(rtn.begin(), rtn.end(), rtn.begin(), [](unsigned char c) { return std::tolower(c); }); + + return rtn; + }; } // namespace lime