refactor(NativeHooks): Modernized/optimized code

This commit is contained in:
Yimura 2022-05-10 19:40:30 +02:00
parent e3a6de8641
commit 420d20479a
2 changed files with 25 additions and 16 deletions

View File

@ -22,14 +22,14 @@ namespace big
public:
native_hooks()
{
this->add_native_detour(RAGE_JOAAT("carmod_shop"), 0x06843DA7060A026B, carmod_shop::SET_ENTITY_COORDS);
this->add_native_detour(RAGE_JOAAT("carmod_shop"), 0x8E2530AA8ADA980E, carmod_shop::SET_ENTITY_HEADING);
this->add_native_detour(RAGE_JOAAT("carmod_shop"), 0x34E710FF01247C5A, carmod_shop::SET_VEHICLE_LIGHTS);
this->add_native_detour(RAGE_JOAAT("carmod_shop"), 0x767FBC2AC802EF3D, carmod_shop::STAT_GET_INT);
this->add_native_detour(RAGE_JOAAT("freemode"), 0x95914459A87EBA28, freemode::NETWORK_BAIL);
add_native_detour(RAGE_JOAAT("carmod_shop"), 0x06843DA7060A026B, carmod_shop::SET_ENTITY_COORDS);
add_native_detour(RAGE_JOAAT("carmod_shop"), 0x8E2530AA8ADA980E, carmod_shop::SET_ENTITY_HEADING);
add_native_detour(RAGE_JOAAT("carmod_shop"), 0x34E710FF01247C5A, carmod_shop::SET_VEHICLE_LIGHTS);
add_native_detour(RAGE_JOAAT("carmod_shop"), 0x767FBC2AC802EF3D, carmod_shop::STAT_GET_INT);
add_native_detour(RAGE_JOAAT("freemode"), 0x95914459A87EBA28, freemode::NETWORK_BAIL);
for (auto native_detours_for_script : m_native_registrations)
if (GtaThread* thread = gta_util::find_script_thread(native_detours_for_script.first); thread != nullptr && thread->m_context.m_state == rage::eThreadState::running)
for (const auto& native_detours_for_script : m_native_registrations)
if (const GtaThread* thread = gta_util::find_script_thread(native_detours_for_script.first); thread != nullptr && thread->m_context.m_state == rage::eThreadState::running)
this->check_for_thread(thread);
g_native_hooks = this;
@ -39,27 +39,36 @@ namespace big
g_native_hooks = nullptr;
}
native_hooks(const native_hooks&) = delete;
native_hooks(native_hooks&&) noexcept = delete;
native_hooks& operator=(const native_hooks&) = delete;
native_hooks& operator=(native_hooks&&) noexcept = delete;
void add_native_detour(rage::joaat_t script_hash, rage::scrNativeHash hash, rage::scrNativeHandler detour)
{
if (auto it = m_native_registrations.find(script_hash); it != m_native_registrations.end())
return it->second.push_back({ hash, detour });
if (const auto& it = m_native_registrations.find(script_hash); it != m_native_registrations.end())
{
it->second.emplace_back(hash, detour);
return;
}
m_native_registrations.emplace(script_hash, std::vector<native_detour>({ { hash, detour } }));
}
bool check_for_thread(GtaThread* gta_thread)
bool check_for_thread(const GtaThread* gta_thread)
{
std::unordered_map<rage::scrNativeHash, rage::scrNativeHandler> native_replacements;
rage::joaat_t script_hash = gta_thread->m_script_hash;
const auto script_hash = gta_thread->m_script_hash;
std::map<rage::joaat_t, std::vector<native_detour>>::iterator pair = m_native_registrations.find(script_hash);
const auto& pair = m_native_registrations.find(script_hash);
if (pair == m_native_registrations.end())
return false;
for (native_detour native_hook_reg : pair->second)
for (const auto& native_hook_reg : pair->second)
native_replacements.insert(native_hook_reg);
if (native_replacements.size())
if (!native_replacements.empty())
{
if (m_script_hooks.find(gta_thread->m_script_hash) != m_script_hooks.end())
{
@ -79,7 +88,7 @@ namespace big
return false;
}
void do_cleanup_for_thread(GtaThread* gta_thread)
void do_cleanup_for_thread(const GtaThread* gta_thread)
{
if (m_script_hooks.erase(gta_thread->m_script_hash))
{

2
vendor/ImGui vendored

@ -1 +1 @@
Subproject commit afffcd5810d030e24056c1a61f27a6eb632f50ed
Subproject commit 9779cc2fe273c4ea96b9763fcd54f14b8cbae561