Skip to content

Commit

Permalink
Implement handler removing when no hooks are active
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Jan 19, 2025
1 parent 315bf46 commit dc14d4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion loader/src/loader/HookImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Result<> Hook::Impl::enable() {
Result<> Hook::Impl::disable() {
if (!m_enabled)
return Ok();
GEODE_UNWRAP_INTO(auto handler, LoaderImpl::get()->getHandler(m_address));
GEODE_UNWRAP_INTO(auto handler, LoaderImpl::get()->getOrRemoveHandler(m_address));
tulip::hook::removeHook(handler, m_handle);
m_enabled = false;
log::debug("Disabled {} hook", m_displayName);
Expand Down
20 changes: 17 additions & 3 deletions loader/src/loader/LoaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,15 +948,29 @@ Result<tulip::hook::HandlerHandle> Loader::Impl::getHandler(void* address) {
if (!m_handlerHandles.count(address)) {
return Err("Handler does not exist at address");
}
return Ok(m_handlerHandles[address]);
return Ok(m_handlerHandles[address].first);
}

Result<tulip::hook::HandlerHandle> Loader::Impl::getOrCreateHandler(void* address, tulip::hook::HandlerMetadata const& metadata) {
if (m_handlerHandles.count(address)) {
return Ok(m_handlerHandles[address]);
m_handlerHandles[address].second++;
return Ok(m_handlerHandles[address].first);
}
GEODE_UNWRAP_INTO(auto handle, tulip::hook::createHandler(address, metadata));
m_handlerHandles[address] = handle;
m_handlerHandles[address].first = handle;
m_handlerHandles[address].second = 1;
return Ok(handle);
}

Result<tulip::hook::HandlerHandle> Loader::Impl::getOrRemoveHandler(void* address) {
if (!m_handlerHandles.count(address)) {
return Err("Handler does not exist at address");
}
auto handle = m_handlerHandles[address].first;
if (m_handlerHandles[address].second == 1) {
GEODE_UNWRAP(tulip::hook::removeHandler(handle));
}
m_handlerHandles[address].second--;
return Ok(handle);
}

Expand Down
3 changes: 2 additions & 1 deletion loader/src/loader/LoaderImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ namespace geode {
Mod* takeNextMod();
void releaseNextMod();

std::unordered_map<void*, tulip::hook::HandlerHandle> m_handlerHandles;
std::unordered_map<void*, std::pair<tulip::hook::HandlerHandle, size_t>> m_handlerHandles;

Result<tulip::hook::HandlerHandle> getHandler(void* address);
Result<tulip::hook::HandlerHandle> getOrCreateHandler(void* address, tulip::hook::HandlerMetadata const& metadata);
Result<tulip::hook::HandlerHandle> getOrRemoveHandler(void* address);

bool loadHooks();

Expand Down

0 comments on commit dc14d4c

Please sign in to comment.