Skip to content

Commit

Permalink
refactor: add helper function for hook
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 2, 2025
1 parent 8c3a12b commit e3d760e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src-server/ll/api/event/player/PlayerChatEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& identifier,
TextPacket const& packet
) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (auto player = handle->_getServerPlayer(identifier, packet.mClientSubId); player) {
if (auto player = thisFor<NetEventCallback>()->_getServerPlayer(identifier, packet.mClientSubId); player) {
auto event = PlayerChatEvent{*player, const_cast<TextPacket&>(packet).mMessage};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
Expand Down
3 changes: 1 addition & 2 deletions src-server/ll/api/event/player/PlayerJoinEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& identifier,
SetLocalPlayerAsInitializedPacket const& packet
) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (auto player = handle->_getServerPlayer(identifier, packet.mClientSubId); player) {
if (auto player = thisFor<NetEventCallback>()->_getServerPlayer(identifier, packet.mClientSubId); player) {
auto event = PlayerJoinEvent{*player};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& id,
PlayerActionPacket const& packet
) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
auto handle = thisFor<NetEventCallback>();
switch (packet.mAction) {
case PlayerActionType::StartSprinting:
if (auto player = handle->_getServerPlayer(id, packet.mClientSubId); player) {
Expand Down
3 changes: 1 addition & 2 deletions src-server/ll/api/event/player/PlayerSwingEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& id,
AnimatePacket const& packet
) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (packet.mAction == AnimatePacket::Action::Swing) {
if (auto player = handle->_getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = thisFor<NetEventCallback>()->_getServerPlayer(id, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerSwingEvent(*player));
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/ll/api/event/player/PlayerJumpEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ LL_TYPE_INSTANCE_HOOK(
auto mode = settings->getPlayerMovementSettings().mUnk3c7e19.as<ServerAuthMovementMode>();
if (mode == ServerAuthMovementMode::LegacyClientAuthoritativeV1) {
if (packet.mAction == PlayerActionType::StartJump) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (auto player = handle->_getServerPlayer(source, packet.mClientSubId); player) {
if (auto player = thisFor<NetEventCallback>()->_getServerPlayer(source, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerJumpEvent(*player));
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/ll/api/memory/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct LL_EBO Hook {};
inline static _OriginFuncType _OriginalFunc{}; \
\
template <class T> \
static consteval void detector() { \
static consteval void _Detector() { \
if constexpr (requires { ::ll::memory::virtualDetector<T, IDENTIFIER>(); }) { \
if constexpr (::ll::memory::virtualDetector<T, IDENTIFIER>()) { \
static_assert( \
Expand All @@ -172,6 +172,11 @@ struct LL_EBO Hook {};
} \
\
public: \
template <class T> \
requires(::std::is_polymorphic_v<TYPE> && ::std::is_base_of_v<T, TYPE>) \
[[nodiscard]] TYPE* thisFor() { \
return static_cast<decltype(this)>(reinterpret_cast<T*>(this)); \
} \
template <class... Args> \
STATIC RET_TYPE origin(Args&&... params) { \
return CALL(std::forward<Args>(params)...); \
Expand All @@ -180,7 +185,7 @@ struct LL_EBO Hook {};
STATIC RET_TYPE detour(__VA_ARGS__); \
\
static int hook(bool suspendThreads = true) { \
detector<_OriginFuncType>(); \
_Detector<_OriginFuncType>(); \
if (!_HookTarget) _HookTarget = ::ll::memory::resolveIdentifier<_OriginFuncType>(IDENTIFIER); \
return ::ll::memory::hook( \
_HookTarget, \
Expand Down

0 comments on commit e3d760e

Please sign in to comment.