Merge pull request #96 from diamante0018/develop

Added functionalities to Library class (from S1X).
This commit is contained in:
Dss0 2021-07-18 22:01:53 +02:00 committed by GitHub
commit 71271b4ade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 84 additions and 47 deletions

View File

@ -252,7 +252,7 @@ namespace Components
Game::TeleportPlayer(&Game::g_entities[clientNum], pos, orientation); Game::TeleportPlayer(&Game::g_entities[clientNum], pos, orientation);
// Logging that will spam the console and screen if people use cinematics //Logging that will spam the console and screen if people use cinematics
//Logger::Print("Successfully teleported player!\n"); //Logger::Print("Successfully teleported player!\n");
//Toast::Show("cardicon_abduction", "Success", "You have been teleported!", toastDurationShort); //Toast::Show("cardicon_abduction", "Success", "You have been teleported!", toastDurationShort);
}); });

View File

@ -399,36 +399,10 @@ namespace Components
// quit_hard // quit_hard
Command::Add("quit_hard", [](Command::Params*) Command::Add("quit_hard", [](Command::Params*)
{ {
typedef enum _HARDERROR_RESPONSE_OPTION { int data = false;
OptionAbortRetryIgnore, const Utils::Library ntdll("ntdll.dll");
OptionOk, ntdll.invoke_pascal<void>("RtlAdjustPrivilege", 19, true, false, &data);
OptionOkCancel, ntdll.invoke_pascal<void>("NtRaiseHardError", 0xC000007B, 0, nullptr, nullptr, 6, &data);
OptionRetryCancel,
OptionYesNo,
OptionYesNoCancel,
OptionShutdownSystem
} HARDERROR_RESPONSE_OPTION, *PHARDERROR_RESPONSE_OPTION;
typedef enum _HARDERROR_RESPONSE {
ResponseReturnToCaller,
ResponseNotHandled,
ResponseAbort,
ResponseCancel,
ResponseIgnore,
ResponseNo,
ResponseOk,
ResponseRetry,
ResponseYes
} HARDERROR_RESPONSE, *PHARDERROR_RESPONSE;
BOOLEAN hasPerms;
HARDERROR_RESPONSE response;
auto result = ImportFunction<NTSTATUS __stdcall(ULONG, BOOLEAN, BOOLEAN, PBOOLEAN)>("ntdll.dll", "RtlAdjustPrivilege")
(19, true, false, &hasPerms);
result = ImportFunction<NTSTATUS __stdcall(NTSTATUS, ULONG, LPCSTR, PVOID, HARDERROR_RESPONSE_OPTION, PHARDERROR_RESPONSE)>("ntdll.dll", "NtRaiseHardError")
(0xC000007B /*0x0000000A*/, 0, nullptr, nullptr, OptionShutdownSystem, &response);
}); });
// bounce dvar // bounce dvar

View File

@ -149,6 +149,7 @@ template <size_t S> class Sizer { };
#pragma comment(lib, "Advapi32.lib") #pragma comment(lib, "Advapi32.lib")
#pragma comment(lib, "rpcrt4.lib") #pragma comment(lib, "rpcrt4.lib")
#pragma comment(lib, "dbghelp.lib") #pragma comment(lib, "dbghelp.lib")
#pragma comment(lib, "ntdll.lib")
// Enable additional literals // Enable additional literals
using namespace std::literals; using namespace std::literals;

View File

@ -384,11 +384,11 @@ namespace Steam
Proxy::LaunchWatchGuard(); Proxy::LaunchWatchGuard();
Proxy::Overlay = ::Utils::Library(GAMEOVERLAY_LIB, false); Proxy::Overlay = ::Utils::Library(GAMEOVERLAY_LIB, false);
if (!Proxy::Overlay.valid()) return false; if (!Proxy::Overlay.is_valid()) return false;
} }
Proxy::Client = ::Utils::Library(STEAMCLIENT_LIB, false); Proxy::Client = ::Utils::Library(STEAMCLIENT_LIB, false);
if (!Proxy::Client.valid()) return false; if (!Proxy::Client.is_valid()) return false;
Proxy::SteamClient = Proxy::Client.get<ISteamClient008*(const char*, int*)>("CreateInterface")("SteamClient008", nullptr); Proxy::SteamClient = Proxy::Client.get<ISteamClient008*(const char*, int*)>("CreateInterface")("SteamClient008", nullptr);
if(!Proxy::SteamClient) return false; if(!Proxy::SteamClient) return false;
@ -526,7 +526,7 @@ namespace Steam
void Proxy::SetOverlayNotificationPosition(uint32_t eNotificationPosition) void Proxy::SetOverlayNotificationPosition(uint32_t eNotificationPosition)
{ {
if (Proxy::Overlay.valid()) if (Proxy::Overlay.is_valid())
{ {
Proxy::Overlay.get<void(uint32_t)>("SetNotificationPosition")(eNotificationPosition); Proxy::Overlay.get<void(uint32_t)>("SetNotificationPosition")(eNotificationPosition);
} }
@ -534,7 +534,7 @@ namespace Steam
bool Proxy::IsOverlayEnabled() bool Proxy::IsOverlayEnabled()
{ {
if (Proxy::Overlay.valid()) if (Proxy::Overlay.is_valid())
{ {
return Proxy::Overlay.get<bool()>("IsOverlayEnabled")(); return Proxy::Overlay.get<bool()>("IsOverlayEnabled")();
} }
@ -544,7 +544,7 @@ namespace Steam
bool Proxy::BOverlayNeedsPresent() bool Proxy::BOverlayNeedsPresent()
{ {
if (Proxy::Overlay.valid()) if (Proxy::Overlay.is_valid())
{ {
return Proxy::Overlay.get<bool()>("BOverlayNeedsPresent")(); return Proxy::Overlay.get<bool()>("BOverlayNeedsPresent")();
} }

View File

@ -2,11 +2,40 @@
namespace Utils namespace Utils
{ {
Library Library::load(const std::string& name)
{
return Library(LoadLibraryA(name.data()));
}
Library Library::load(const std::filesystem::path& path)
{
return Library::load(path.generic_string());
}
Library Library::get_by_address(void* address)
{
HMODULE handle = nullptr;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, static_cast<LPCSTR>(address), &handle);
return Library(handle);
}
Library::Library(const std::string& buffer, bool _freeOnDestroy) : _module(nullptr), freeOnDestroy(_freeOnDestroy) Library::Library(const std::string& buffer, bool _freeOnDestroy) : _module(nullptr), freeOnDestroy(_freeOnDestroy)
{ {
this->_module = LoadLibraryExA(buffer.data(), nullptr, 0); this->_module = LoadLibraryExA(buffer.data(), nullptr, 0);
} }
Library::Library(const std::string& buffer)
{
this->_module = GetModuleHandleA(buffer.data());
this->freeOnDestroy = true;
}
Library::Library(const HMODULE handle)
{
this->_module = handle;
this->freeOnDestroy = true;
}
Library::~Library() Library::~Library()
{ {
if (this->freeOnDestroy) if (this->freeOnDestroy)
@ -15,9 +44,9 @@ namespace Utils
} }
} }
bool Library::valid() bool Library::is_valid() const
{ {
return (this->getModule() != nullptr); return this->_module != nullptr;
} }
HMODULE Library::getModule() HMODULE Library::getModule()
@ -27,7 +56,7 @@ namespace Utils
void Library::free() void Library::free()
{ {
if (this->valid()) if (this->is_valid())
{ {
FreeLibrary(this->getModule()); FreeLibrary(this->getModule());
} }

View File

@ -5,22 +5,55 @@ namespace Utils
class Library class Library
{ {
public: public:
static Library load(const std::string& name);
static Library load(const std::filesystem::path& path);
static Library get_by_address(void* address);
Library() : _module(nullptr), freeOnDestroy(false) {}; Library() : _module(nullptr), freeOnDestroy(false) {};
Library(const std::string& buffer, bool freeOnDestroy = true); Library(const std::string& buffer, bool freeOnDestroy);
explicit Library(const std::string& name);
explicit Library(HMODULE handle);
~Library(); ~Library();
bool valid(); bool is_valid() const;
HMODULE getModule(); HMODULE getModule();
template <typename T> template <typename T>
std::function<T> get(const std::string& process) T get_proc(const std::string& process) const
{ {
if (!this->valid()) if (!this->is_valid()) T{};
{ return reinterpret_cast<T>(GetProcAddress(this->_module, process.data()));
throw std::runtime_error("Library not loaded!"); }
}
return reinterpret_cast<T*>(GetProcAddress(this->getModule(), process.data())); template <typename T>
std::function<T> get(const std::string& process) const
{
if (!this->is_valid()) return std::function<T>();
return static_cast<T*>(this->get_proc<void*>(process));
}
template <typename T, typename... Args>
T invoke(const std::string& process, Args ... args) const
{
auto method = this->get<T(__cdecl)(Args ...)>(process);
if (method) return method(args...);
return T();
}
template <typename T, typename... Args>
T invoke_pascal(const std::string& process, Args ... args) const
{
auto method = this->get<T(__stdcall)(Args ...)>(process);
if (method) return method(args...);
return T();
}
template <typename T, typename... Args>
T invoke_this(const std::string& process, void* this_ptr, Args ... args) const
{
auto method = this->get<T(__thiscall)(void*, Args ...)>(this_ptr, process);
if (method) return method(args...);
return T();
} }
void free(); void free();