iw4x-client/src/Utils/Library.hpp

66 lines
1.6 KiB
C++
Raw Normal View History

2017-01-20 08:36:52 -05:00
#pragma once
2017-01-19 16:23:59 -05:00
namespace Utils
{
class Library
{
public:
2021-07-21 12:44:58 -04:00
static Library Load(const std::string& name);
static Library Load(const std::filesystem::path& path);
static Library GetByAddress(void* address);
2021-07-18 12:11:20 -04:00
2020-12-09 14:13:34 -05:00
Library() : _module(nullptr), freeOnDestroy(false) {};
2021-07-18 12:11:20 -04:00
Library(const std::string& buffer, bool freeOnDestroy);
explicit Library(const std::string& name);
explicit Library(HMODULE handle);
2017-01-19 16:23:59 -05:00
~Library();
2021-07-21 12:44:58 -04:00
bool IsValid() const;
HMODULE GetModule();
2017-01-19 16:23:59 -05:00
template <typename T>
2021-07-21 12:44:58 -04:00
T GetProc(const std::string& process) const
2017-01-19 16:23:59 -05:00
{
2021-07-21 12:44:58 -04:00
if (!this->IsValid()) T{};
2021-07-18 11:36:01 -04:00
return reinterpret_cast<T>(GetProcAddress(this->_module, process.data()));
}
template <typename T>
2021-07-21 12:44:58 -04:00
std::function<T> Get(const std::string& process) const
2021-07-18 11:36:01 -04:00
{
2021-07-21 12:44:58 -04:00
if (!this->IsValid()) return std::function<T>();
return static_cast<T*>(this->GetProc<void*>(process));
2021-07-18 11:36:01 -04:00
}
template <typename T, typename... Args>
2021-07-21 12:44:58 -04:00
T Invoke(const std::string& process, Args ... args) const
2021-07-18 11:36:01 -04:00
{
2021-07-21 12:44:58 -04:00
auto method = this->Get<T(__cdecl)(Args ...)>(process);
2021-07-18 11:36:01 -04:00
if (method) return method(args...);
return T();
}
template <typename T, typename... Args>
2021-07-21 12:44:58 -04:00
T InvokePascal(const std::string& process, Args ... args) const
2021-07-18 11:36:01 -04:00
{
2021-07-21 12:44:58 -04:00
auto method = this->Get<T(__stdcall)(Args ...)>(process);
2021-07-18 11:36:01 -04:00
if (method) return method(args...);
return T();
}
2017-01-19 16:23:59 -05:00
2021-07-18 11:36:01 -04:00
template <typename T, typename... Args>
2021-07-21 12:44:58 -04:00
T InvokeThis(const std::string& process, void* this_ptr, Args ... args) const
2021-07-18 11:36:01 -04:00
{
2021-07-21 12:44:58 -04:00
auto method = this->Get<T(__thiscall)(void*, Args ...)>(this_ptr, process);
2021-07-18 11:36:01 -04:00
if (method) return method(args...);
return T();
2017-01-19 16:23:59 -05:00
}
2021-07-21 12:44:58 -04:00
void Free();
2017-03-26 12:00:06 -04:00
2017-01-19 16:23:59 -05:00
private:
2020-12-09 14:13:34 -05:00
HMODULE _module;
2017-01-19 16:23:59 -05:00
bool freeOnDestroy;
};
}