#pragma once namespace Utils { class Library { public: static Library Load(const std::string& name); static Library Load(const std::filesystem::path& path); static Library GetByAddress(void* address); Library() : _module(nullptr), freeOnDestroy(false) {}; Library(const std::string& buffer, bool freeOnDestroy); explicit Library(const std::string& name); explicit Library(HMODULE handle); ~Library(); bool IsValid() const; HMODULE GetModule(); template T GetProc(const std::string& process) const { if (!this->IsValid()) T{}; return reinterpret_cast(GetProcAddress(this->_module, process.data())); } template std::function Get(const std::string& process) const { if (!this->IsValid()) return std::function(); return static_cast(this->GetProc(process)); } template T Invoke(const std::string& process, Args ... args) const { auto method = this->Get(process); if (method) return method(args...); return T(); } template T InvokePascal(const std::string& process, Args ... args) const { auto method = this->Get(process); if (method) return method(args...); return T(); } template T InvokeThis(const std::string& process, void* this_ptr, Args ... args) const { auto method = this->Get(this_ptr, process); if (method) return method(args...); return T(); } void Free(); private: HMODULE _module; bool freeOnDestroy; }; }