Skip to content

Commit

Permalink
feat(module): add load
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Jan 3, 2024
1 parent cb6599d commit 7de073b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
3 changes: 3 additions & 0 deletions include/lime/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace lime

public:
[[nodiscard]] static std::optional<module> get(const std::string &name);
[[nodiscard]] static std::optional<module> load(const std::string &name);

public:
[[nodiscard]] static std::optional<module> find(const std::string &name);
};
} // namespace lime
10 changes: 10 additions & 0 deletions src/module.linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ namespace lime
return std::move(*module);
}

std::optional<module> module::load(const std::string &name)
{
if (dlopen(name.c_str(), RTLD_NOW) == nullptr)
{
return std::nullopt;
}

return get(name);
}

std::optional<module> module::find(const std::string &name)
{
auto all = modules();
Expand Down
87 changes: 56 additions & 31 deletions src/module.win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ namespace lime
{
struct module::impl
{
using callback_t = std::function<bool(const std::string &)>;

public:
HMODULE handle;

public:
MODULEINFO info;
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<bool(const std::string &)> &) const;
static std::optional<module> get(HMODULE module);
static std::string lower(const std::string &string);
};

module::module() :m_impl(std::make_unique<impl>()) {}
Expand Down Expand Up @@ -116,45 +120,41 @@ 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;
}

std::optional<module> 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> 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> module::find(const std::string &name)
Expand All @@ -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<bool(const std::string &)> &callback) const
{
CHAR path[MAX_PATH];
Expand Down Expand Up @@ -219,4 +211,37 @@ namespace lime

UnMapAndLoad(&image);
}

std::optional<module> 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

0 comments on commit 7de073b

Please sign in to comment.