Skip to content

Commit

Permalink
feat(hook): support for const member function hook
Browse files Browse the repository at this point in the history
  • Loading branch information
RimuruChan committed Jan 31, 2024
1 parent 98beff7 commit cbfe2f7
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 56 deletions.
17 changes: 11 additions & 6 deletions src/ll/api/memory/Hook.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#include "ll/api/memory/Hook.h"

#include <memory>
#include <string_view>

#include "pl/Hook.h"

#include "ll/api/Logger.h"
#include "ll/api/memory/Memory.h"
#include "ll/api/service/ServerInfo.h"
#include "ll/api/utils/StacktraceUtils.h"
#include "ll/api/thread/GlobalThreadPauser.h"
#include "ll/api/utils/WinUtils.h"
#include "pl/Hook.h"

namespace ll::memory {

int hook(FuncPtr target, FuncPtr detour, FuncPtr* originalFunc, HookPriority priority, bool stopTheWorld) {
int hook(FuncPtr target, FuncPtr detour, FuncPtr* originalFunc, HookPriority priority, bool suspendThreads) {
std::unique_ptr<thread::GlobalThreadPauser> pauser;
if (stopTheWorld && getServerStatus() != ServerStatus::Default) {
if (suspendThreads && getServerStatus() != ServerStatus::Default) {
pauser = std::make_unique<thread::GlobalThreadPauser>();
}
return pl::hook::pl_hook(target, detour, originalFunc, static_cast<pl::hook::Priority>(priority));
}

bool unhook(FuncPtr target, FuncPtr detour, bool stopTheWorld) {
bool unhook(FuncPtr target, FuncPtr detour, bool suspendThreads) {
std::unique_ptr<thread::GlobalThreadPauser> pauser;
if (stopTheWorld && getServerStatus() != ServerStatus::Default) {
if (suspendThreads && getServerStatus() != ServerStatus::Default) {
pauser = std::make_unique<thread::GlobalThreadPauser>();
}
return pl::hook::pl_unhook(target, detour);
Expand Down
113 changes: 67 additions & 46 deletions src/ll/api/memory/Hook.h

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/ll/api/reflection/Reflection.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <string_view>
#include <cstddef>
#include <type_traits>

#include "ll/api/reflection/TypeName.h"
#include <utility>

#include "ll/api/base/Concepts.h"
#include "ll/api/base/Meta.h"
#include "ll/api/reflection/TypeName.h"

#if defined(__clang__) && !defined(BOOST_PFR_CORE_NAME_PARSING)
#define BOOST_PFR_CORE_NAME_PARSING \
Expand All @@ -15,6 +15,7 @@
#endif

#include "boost/pfr.hpp"
#include "boost/pfr/detail/make_integer_sequence.hpp"
#include "boost/pfr/detail/offset_based_getter.hpp"

#include "magic_enum.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/service/ServiceId.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ constexpr ServiceId getServiceId = []() -> ServiceId {
if constexpr (requires { self::ServiceId; } && self::ServiceId != EmptyServiceId) {
return self::ServiceId;
} else {
static_assert(false, "ServiceId not defined for type");
static_assert(ll::concepts::always_false<T>, "ServiceId not defined for type");
}
}();
} // namespace ll::service
Expand Down
21 changes: 21 additions & 0 deletions src/ll/test/HookTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "ll/api/memory/Hook.h"

#include <string>

namespace {
class TestClass {
protected:
int some = 0;

public:
int test(int a, int b) const { return a + b; }
int test(int a, int b) { return a + b; }
std::string test(std::string a, std::string b) { return a + b; }
};

// they will choose non-const version if possible
LL_TYPE_INSTANCE_HOOK(TestHook, HookPriority::Normal, TestClass, &TestClass::test, int, int a, int b) {
some = 1;
return a + b;
}
} // namespace
2 changes: 2 additions & 0 deletions src/mc/world/level/block/utils/StaticVanillaBlocks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "mc/world/level/block/utils/StaticVanillaBlocks.h"

#include "ll/api/memory/Hook.h"

#include "mc/world/level/block/registry/BlockTypeRegistry.h"
#include "mc/world/level/block/utils/BedrockBlockNames.h"
#include "mc/world/level/block/utils/VanillaBlockTypeIds.h"
Expand Down
1 change: 1 addition & 0 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ target("LeviLamina")
)
add_cxflags(
"/EHs",
"-Wno-ignored-qualifiers",
"-Wno-c++2b-extensions",
"-Wno-microsoft-cast",
"-Wno-pragma-system-header-outside-header",
Expand Down

0 comments on commit cbfe2f7

Please sign in to comment.