Skip to content

Commit

Permalink
chore: add check to hook pauser
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 19, 2024
1 parent 0cea1f1 commit a58e2d8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/ll/api/memory/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@

namespace ll::memory {

bool shouldHookSuspendThreads() { return getGamingStatus() != GamingStatus::Default; }

int hook(FuncPtr target, FuncPtr detour, FuncPtr* originalFunc, HookPriority priority, bool suspendThreads) {
if (target == nullptr) {
return -1;
}
std::optional<thread::GlobalThreadPauser> pauser;
if (suspendThreads && getGamingStatus() != GamingStatus::Default) {
if (suspendThreads && shouldHookSuspendThreads()) {
pauser.emplace();
}
return pl::hook::pl_hook(target, detour, originalFunc, static_cast<pl::hook::Priority>(priority));
}
bool unhook(FuncPtr target, FuncPtr detour, bool suspendThreads) {
std::optional<thread::GlobalThreadPauser> pauser;
if (suspendThreads && getGamingStatus() != GamingStatus::Default) {
if (suspendThreads && shouldHookSuspendThreads()) {
pauser.emplace();
}
return pl::hook::pl_unhook(target, detour);
Expand Down
12 changes: 10 additions & 2 deletions src/ll/api/memory/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum class HookPriority : int {
Lowest = 400,
};

LLNDAPI bool shouldHookSuspendThreads();

LLAPI int
hook(FuncPtr target, FuncPtr detour, FuncPtr* originalFunc, HookPriority priority, bool suspendThreads = true);

Expand Down Expand Up @@ -96,11 +98,17 @@ template <class... Ts>
class HookRegistrar {
public:
static void hook() {
thread::GlobalThreadPauser pauser;
std::optional<thread::GlobalThreadPauser> pauser;
if (shouldHookSuspendThreads()) {
pauser.emplace();
}
(((++Ts::_AutoHookCount == 1) ? Ts::hook(false) : 0), ...);
}
static void unhook() {
thread::GlobalThreadPauser pauser;
std::optional<thread::GlobalThreadPauser> pauser;
if (shouldHookSuspendThreads()) {
pauser.emplace();
}
(((--Ts::_AutoHookCount == 0) ? Ts::unhook(false) : 0), ...);
}
HookRegistrar() noexcept { hook(); }
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/utils/StacktraceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class Stacktrace;
class StacktraceEntry {
friend ::ll::Stacktrace;
void* address{};
StacktraceEntry(void* address) : address(address) {}

public:
using native_handle_type = void*;

explicit StacktraceEntry(void* address) : address(address) {}

void* native_handle() const { return address; }
};
class Stacktrace {
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/utils/StacktraceUtils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LLNDAPI Stacktrace Stacktrace::current(size_t skip, size_t maxDepth) {
Stacktrace res;
res.entries.reserve(s.size());
for (auto& entry : s) {
res.entries.push_back({entry.native_handle()});
res.entries.emplace_back(entry.native_handle());
}
res.hash = std::hash<std::stacktrace>{}(s);
return res;
Expand Down
12 changes: 7 additions & 5 deletions src/ll/core/mod/ModRegistrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ void ModRegistrar::disableAllMods() noexcept try {
error_utils::printCurrentException(getLogger());
}
void ModRegistrar::releaseAllMods() noexcept try {
std::lock_guard lock(impl->mutex);
if (auto res = ModManagerRegistry::getInstance().releaseManagers(); !res) {
res.error().log(getLogger(), io::LogLevel::Warn);
}
impl->deps.clear();
// TODO: check lifetime
// std::lock_guard lock(impl->mutex);
// if (auto res = ModManagerRegistry::getInstance().releaseManagers(); !res) {
// res.error().log(getLogger(), io::LogLevel::Warn);
// }
// impl->deps.clear();
return;
} catch (...) {
error_utils::printCurrentException(getLogger());
}
Expand Down

0 comments on commit a58e2d8

Please sign in to comment.