diff --git a/SilentPatch/Patterns.cpp b/SilentPatch/Patterns.cpp index 431667c..66aa517 100644 --- a/SilentPatch/Patterns.cpp +++ b/SilentPatch/Patterns.cpp @@ -37,8 +37,8 @@ struct basic_fnv_1 } }; -const std::uint64_t fnv_prime = 1099511628211u; -const std::uint64_t fnv_offset_basis = 14695981039346656037u; +static constexpr std::uint64_t fnv_prime = 1099511628211u; +static constexpr std::uint64_t fnv_offset_basis = 14695981039346656037u; typedef basic_fnv_1 fnv_1; @@ -56,7 +56,11 @@ namespace hook #if PATTERNS_USE_HINTS -static std::multimap g_hints; +static auto& getHints() +{ + static std::multimap hints; + return hints; +} #endif static void TransformPattern(std::string_view pattern, std::string& data, std::string& mask) @@ -148,13 +152,13 @@ void pattern::Initialize(const char* pattern, size_t length) // if there's hints, try those first if (m_module == GetModuleHandle(nullptr)) { - auto range = g_hints.equal_range(m_hash); + auto range = getHints().equal_range(m_hash); if (range.first != range.second) { std::for_each(range.first, range.second, [&] (const std::pair& hint) { - ConsiderMatch(hint.second); + ConsiderHint(hint.second); }); // if the hints succeeded, we don't need to do anything more @@ -181,7 +185,7 @@ void pattern::EnsureMatches(uint32_t maxCount) auto matchSuccess = [&] (uintptr_t address) { #if PATTERNS_USE_HINTS - g_hints.emplace(m_hash, address); + getHints().emplace(m_hash, address); #else (void)address; #endif @@ -229,13 +233,14 @@ void pattern::EnsureMatches(uint32_t maxCount) m_matched = true; } -bool pattern::ConsiderMatch(uintptr_t offset) +bool pattern::ConsiderHint(uintptr_t offset) { + char* ptr = reinterpret_cast(offset); + +#if PATTERNS_CAN_SERIALIZE_HINTS const char* pattern = m_bytes.c_str(); const char* mask = m_mask.c_str(); - char* ptr = reinterpret_cast(offset); - for (size_t i = 0, j = m_mask.size(); i < j; i++) { if (mask[i] == '?') @@ -248,18 +253,21 @@ bool pattern::ConsiderMatch(uintptr_t offset) return false; } } +#endif m_matches.emplace_back(ptr); return true; } -#if PATTERNS_USE_HINTS +#if PATTERNS_USE_HINTS && PATTERNS_CAN_SERIALIZE_HINTS void pattern::hint(uint64_t hash, uintptr_t address) { - auto range = g_hints.equal_range(hash); + auto& hints = getHints(); - for (auto it = range.first; it != range.second; it++) + auto range = hints.equal_range(hash); + + for (auto it = range.first; it != range.second; ++it) { if (it->second == address) { @@ -267,7 +275,7 @@ void pattern::hint(uint64_t hash, uintptr_t address) } } - g_hints.emplace(hash, address); + hints.emplace(hash, address); } #endif } \ No newline at end of file diff --git a/SilentPatch/Patterns.h b/SilentPatch/Patterns.h index c17bf5f..d517da1 100644 --- a/SilentPatch/Patterns.h +++ b/SilentPatch/Patterns.h @@ -13,8 +13,6 @@ #pragma warning(push) #pragma warning(disable:4201) -#define PATTERNS_USE_HINTS 0 - namespace hook { extern ptrdiff_t baseAddressDifference; @@ -103,7 +101,7 @@ namespace hook void Initialize(const char* pattern, size_t length); private: - bool ConsiderMatch(uintptr_t offset); + bool ConsiderHint(uintptr_t offset); void EnsureMatches(uint32_t maxCount); @@ -179,7 +177,7 @@ namespace hook } public: -#if PATTERNS_USE_HINTS +#if PATTERNS_USE_HINTS && PATTERNS_CAN_SERIALIZE_HINTS // define a hint static void hint(uint64_t hash, uintptr_t address); #endif diff --git a/SilentPatchIII/SilentPatchIII.vcxproj b/SilentPatchIII/SilentPatchIII.vcxproj index 115f685..5f1afef 100644 --- a/SilentPatchIII/SilentPatchIII.vcxproj +++ b/SilentPatchIII/SilentPatchIII.vcxproj @@ -99,7 +99,7 @@ Level4 Disabled MultiThreadedDebug - _HAS_EXCEPTIONS=0;_GTA_III;%(PreprocessorDefinitions) + _HAS_EXCEPTIONS=0;PATTERNS_USE_HINTS=1;_GTA_III;%(PreprocessorDefinitions) ..\SilentPatch Use StdAfx.h @@ -134,7 +134,7 @@ AnySuitable Speed true - _HAS_EXCEPTIONS=0;_GTA_III;%(PreprocessorDefinitions) + _HAS_EXCEPTIONS=0;PATTERNS_USE_HINTS=1;_GTA_III;%(PreprocessorDefinitions) ..\SilentPatch Use StdAfx.h @@ -171,7 +171,7 @@ AnySuitable Speed true - _HAS_EXCEPTIONS=0;_GTA_III;NDEBUG;%(PreprocessorDefinitions) + _HAS_EXCEPTIONS=0;PATTERNS_USE_HINTS=1;_GTA_III;NDEBUG;%(PreprocessorDefinitions) ..\SilentPatch Use StdAfx.h diff --git a/SilentPatchVC/SilentPatchVC.vcxproj b/SilentPatchVC/SilentPatchVC.vcxproj index 1ddf939..3d84576 100644 --- a/SilentPatchVC/SilentPatchVC.vcxproj +++ b/SilentPatchVC/SilentPatchVC.vcxproj @@ -74,7 +74,7 @@ Level4 Disabled - _HAS_EXCEPTIONS=0;_GTA_VC;%(PreprocessorDefinitions) + _HAS_EXCEPTIONS=0;PATTERNS_USE_HINTS=1;_GTA_VC;%(PreprocessorDefinitions) MultiThreadedDebug ..\SilentPatch Use @@ -103,7 +103,7 @@ MaxSpeed true true - _HAS_EXCEPTIONS=0;_GTA_VC;%(PreprocessorDefinitions) + _HAS_EXCEPTIONS=0;PATTERNS_USE_HINTS=1;_GTA_VC;%(PreprocessorDefinitions) true MultiThreaded AnySuitable @@ -138,7 +138,7 @@ MaxSpeed true true - _HAS_EXCEPTIONS=0;_GTA_VC;NDEBUG;%(PreprocessorDefinitions) + _HAS_EXCEPTIONS=0;PATTERNS_USE_HINTS=1;_GTA_VC;NDEBUG;%(PreprocessorDefinitions) true MultiThreaded AnySuitable