From 25585fc9a445b52d023b6134a252fc7ec2a2e63d Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 17 Sep 2016 00:14:59 +0200 Subject: [PATCH] Introduce library util --- deps/mongoose | 2 +- deps/protobuf | 2 +- .../AssetInterfaces/IMaterialTechniqueSet.cpp | 6 +-- src/STDInclude.hpp | 1 + src/Steam/Proxy.cpp | 45 ++++++------------- src/Steam/Proxy.hpp | 4 +- src/Utils/InfoString.hpp | 2 - src/Utils/Library.cpp | 29 ++++++++++++ src/Utils/Library.hpp | 28 ++++++++++++ 9 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 src/Utils/Library.cpp create mode 100644 src/Utils/Library.hpp diff --git a/deps/mongoose b/deps/mongoose index 806f07db..ffa981d1 160000 --- a/deps/mongoose +++ b/deps/mongoose @@ -1 +1 @@ -Subproject commit 806f07db67b624ceb6638a3bf0713286f75e996d +Subproject commit ffa981d1728163f8b3d0961c127ef924d68ef84c diff --git a/deps/protobuf b/deps/protobuf index e0e54661..3b001ca6 160000 --- a/deps/protobuf +++ b/deps/protobuf @@ -1 +1 @@ -Subproject commit e0e54661f76183684dca66694967a60cbb10f04e +Subproject commit 3b001ca6ba6fb51f5e55b1596fb3ce09ee9981e8 diff --git a/src/Components/Modules/AssetInterfaces/IMaterialTechniqueSet.cpp b/src/Components/Modules/AssetInterfaces/IMaterialTechniqueSet.cpp index 20694b6f..5d725f76 100644 --- a/src/Components/Modules/AssetInterfaces/IMaterialTechniqueSet.cpp +++ b/src/Components/Modules/AssetInterfaces/IMaterialTechniqueSet.cpp @@ -6,7 +6,7 @@ namespace Assets { Game::MaterialTechniqueSet* asset = header.materialTechset; - for (int i = 0; i < ARR_SIZE(Game::MaterialTechniqueSet::techniques); ++i) + for (int i = 0; i < ARRAYSIZE(Game::MaterialTechniqueSet::techniques); ++i) { Game::MaterialTechnique* technique = asset->techniques[i]; @@ -52,9 +52,9 @@ namespace Assets } // Save_MaterialTechniquePtrArray - static_assert(ARR_SIZE(Game::MaterialTechniqueSet::techniques) == 48, "Techniques array invalid!"); + static_assert(ARRAYSIZE(Game::MaterialTechniqueSet::techniques) == 48, "Techniques array invalid!"); - for (int i = 0; i < ARR_SIZE(Game::MaterialTechniqueSet::techniques); ++i) + for (int i = 0; i < ARRAYSIZE(Game::MaterialTechniqueSet::techniques); ++i) { Game::MaterialTechnique* technique = asset->techniques[i]; diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 35936c14..3b50f7d4 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -91,6 +91,7 @@ #include "Utils\Memory.hpp" #include "Utils\String.hpp" #include "Utils\Hooking.hpp" +#include "Utils\Library.hpp" #include "Utils\InfoString.hpp" #include "Utils\Compression.hpp" #include "Utils\Cryptography.hpp" diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index 08c38e86..e46451e4 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -2,8 +2,8 @@ namespace Steam { - HMODULE Proxy::Client = nullptr; - HMODULE Proxy::Overlay = nullptr; + ::Utils::Library Proxy::Client; + ::Utils::Library Proxy::Overlay; bool Proxy::Inititalize() { @@ -12,20 +12,16 @@ namespace Steam SetDllDirectoryA(Proxy::GetSteamDirectory().data()); - Proxy::Client = LoadLibraryA(STEAMCLIENT_LIB); - Proxy::Overlay = LoadLibraryA(GAMEOVERLAY_LIB); + Proxy::Client = ::Utils::Library(STEAMCLIENT_LIB, false); + Proxy::Overlay = ::Utils::Library(GAMEOVERLAY_LIB, false); - return (Proxy::Client && Proxy::Overlay); + return (Proxy::Client.Valid() && Proxy::Overlay.Valid()); } void Proxy::Uninititalize() { - // Freeing libraries causes crashes - //if (Proxy::Client) FreeLibrary(Proxy::Client); - Proxy::Client = nullptr; - - //if (Proxy::Overlay) FreeLibrary(Proxy::Overlay); - Proxy::Overlay = nullptr; + Proxy::Client = ::Utils::Library(); + Proxy::Overlay = ::Utils::Library(); } std::string Proxy::GetSteamDirectory() @@ -46,27 +42,17 @@ namespace Steam void Proxy::SetOverlayNotificationPosition(uint32_t eNotificationPosition) { - if (Proxy::Overlay) + if (Proxy::Overlay.Valid()) { - FARPROC SetNotificationPositionFn = GetProcAddress(Proxy::Overlay, "SetNotificationPosition"); - - if (SetNotificationPositionFn) - { - ::Utils::Hook::Call(SetNotificationPositionFn)(eNotificationPosition); - } + Proxy::Overlay.Get("SetNotificationPosition")(eNotificationPosition); } } bool Proxy::IsOverlayEnabled() { - if (Proxy::Overlay) + if (Proxy::Overlay.Valid()) { - FARPROC IsOverlayEnabledFn = GetProcAddress(Proxy::Overlay, "IsOverlayEnabled"); - - if (IsOverlayEnabledFn) - { - return ::Utils::Hook::Call(IsOverlayEnabledFn)(); - } + return Proxy::Overlay.Get("IsOverlayEnabled")(); } return false; @@ -74,14 +60,9 @@ namespace Steam bool Proxy::BOverlayNeedsPresent() { - if (Proxy::Overlay) + if (Proxy::Overlay.Valid()) { - FARPROC BOverlayNeedsPresentFn = GetProcAddress(Proxy::Overlay, "BOverlayNeedsPresent"); - - if (BOverlayNeedsPresentFn) - { - return ::Utils::Hook::Call(BOverlayNeedsPresentFn)(); - } + return Proxy::Overlay.Get("BOverlayNeedsPresent")(); } return false; diff --git a/src/Steam/Proxy.hpp b/src/Steam/Proxy.hpp index ac3792a0..f2b551c8 100644 --- a/src/Steam/Proxy.hpp +++ b/src/Steam/Proxy.hpp @@ -22,8 +22,8 @@ namespace Steam static bool BOverlayNeedsPresent(); private: - static HMODULE Client; - static HMODULE Overlay; + static ::Utils::Library Client; + static ::Utils::Library Overlay; static std::string GetSteamDirectory(); }; diff --git a/src/Utils/InfoString.hpp b/src/Utils/InfoString.hpp index 78323e92..6be707e0 100644 --- a/src/Utils/InfoString.hpp +++ b/src/Utils/InfoString.hpp @@ -1,5 +1,3 @@ -#define ARR_SIZE(x) (sizeof(x) / sizeof(x[0])) - namespace Utils { class InfoString diff --git a/src/Utils/Library.cpp b/src/Utils/Library.cpp new file mode 100644 index 00000000..9ca1c54a --- /dev/null +++ b/src/Utils/Library.cpp @@ -0,0 +1,29 @@ +#include "STDInclude.hpp" + +namespace Utils +{ + Library::Library(std::string buffer, bool freeOnDestroy) : FreeOnDestroy(freeOnDestroy), Module(nullptr) + { + this->Module = LoadLibraryExA(buffer.data(), NULL, 0); + } + + Library::~Library() + { + if (this->FreeOnDestroy && this->Valid()) + { + FreeLibrary(this->GetModule()); + } + + this->Module = nullptr; + } + + bool Library::Valid() + { + return (this->GetModule() != nullptr); + } + + HMODULE Library::GetModule() + { + return this->Module; + } +} diff --git a/src/Utils/Library.hpp b/src/Utils/Library.hpp new file mode 100644 index 00000000..e4d738e0 --- /dev/null +++ b/src/Utils/Library.hpp @@ -0,0 +1,28 @@ +namespace Utils +{ + class Library + { + public: + Library() : Module(nullptr), FreeOnDestroy(false) {}; + Library(std::string buffer, bool freeOnDestroy = true); + ~Library(); + + bool Valid(); + HMODULE GetModule(); + + template + std::function Get(std::string process) + { + if (this->Valid()) + { + throw new std::runtime_error("Library not loaded!"); + } + + return reinterpret_cast(GetProcAddress(this->GetModule(), process.data())); + } + + private: + HMODULE Module; + bool FreeOnDestroy; + }; +}