From d94ffa7fa0c3d2264ce5a6b3fa6dbf624ce9e12b Mon Sep 17 00:00:00 2001 From: OEOTYAN <1124718975@qq.com> Date: Fri, 1 Dec 2023 03:12:02 +0800 Subject: [PATCH] fix: fix IP bugs --- .clang-tidy | 1 - src/ll/api/event/command/ExecuteCommandEvent.h | 9 +++++---- src/ll/api/event/player/PlayerConnectEvent.cpp | 14 ++++++++------ src/ll/api/event/player/PlayerConnectEvent.h | 16 ++++++++++++++-- src/ll/api/event/player/PlayerLeaveEvent.cpp | 3 +-- src/ll/api/service/GlobalService.cpp | 12 ++++-------- src/ll/test/EventTest.cpp | 3 ++- src/mc/network/NetworkIdentifier.cpp | 7 ++++--- 8 files changed, 38 insertions(+), 27 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 0df86776f4..915eee83b0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -63,7 +63,6 @@ cert-str34-c, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-narrowing-conversions, cppcoreguidelines-pro-type-member-init, -cppcoreguidelines-pro-type-static-cast-downcast, cppcoreguidelines-slicing, google-default-arguments, google-explicit-constructor, diff --git a/src/ll/api/event/command/ExecuteCommandEvent.h b/src/ll/api/event/command/ExecuteCommandEvent.h index 898d20bd6b..09d264d1c8 100644 --- a/src/ll/api/event/command/ExecuteCommandEvent.h +++ b/src/ll/api/event/command/ExecuteCommandEvent.h @@ -4,9 +4,10 @@ #include "ll/api/event/Cancellable.h" #include "ll/api/event/Event.h" -#include "mc/codebuilder/MCRESULT.h" #include "mc/server/commands/CommandContext.h" -#include "mc/server/commands/MinecraftCommands.h" + +struct MCRESULT; +class MinecraftCommands; namespace ll::event::command { class ExecutingCommandEvent : public Cancellable { @@ -31,13 +32,13 @@ class ExecutedCommandEvent : public Event { MCRESULT& result; MinecraftCommands& minecraftCommands; CommandContext const& commandContext; - bool suppressOutput; + bool const& suppressOutput; constexpr explicit ExecutedCommandEvent( MCRESULT& result, MinecraftCommands& minecraftCommands, CommandContext& commandContext, - bool suppressOutput + bool const& suppressOutput ) : result(result), minecraftCommands(minecraftCommands), diff --git a/src/ll/api/event/player/PlayerConnectEvent.cpp b/src/ll/api/event/player/PlayerConnectEvent.cpp index 88ab2a602d..73ee3c9450 100644 --- a/src/ll/api/event/player/PlayerConnectEvent.cpp +++ b/src/ll/api/event/player/PlayerConnectEvent.cpp @@ -2,6 +2,9 @@ #include "ll/api/event/Emitter.h" #include "ll/api/memory/Hook.h" +#include "mc/network/ConnectionRequest.h" +#include "mc/network/NetworkIdentifier.h" + #include "mc/network/ServerNetworkHandler.h" @@ -11,14 +14,13 @@ LL_TYPED_INSTANCE_HOOK( PlayerConnectEventHook, HookPriority::Normal, ServerNetworkHandler, - "?sendLoginMessageLocal@ServerNetworkHandler@@QEAAXAEBVNetworkIdentifier@@AEBVConnectionRequest@@AEAVServerPlayer@@" - "@Z", + &ServerNetworkHandler::sendLoginMessageLocal, void, - const NetworkIdentifier& ni, - const ConnectionRequest& cr, - const ServerPlayer& sp + NetworkIdentifier const& ni, + ConnectionRequest const& cr, + ServerPlayer& sp ) { - auto event = PlayerConnectEvent(sp); + auto event = PlayerConnectEvent{ni, cr, sp}; EventBus::getInstance().publish(event); if (event.isCancelled()) { return; diff --git a/src/ll/api/event/player/PlayerConnectEvent.h b/src/ll/api/event/player/PlayerConnectEvent.h index d93b31fe0b..40d6842f21 100644 --- a/src/ll/api/event/player/PlayerConnectEvent.h +++ b/src/ll/api/event/player/PlayerConnectEvent.h @@ -6,12 +6,24 @@ #include "mc/server/ServerPlayer.h" +class ConnectionRequest; +class NetworkIdentifier; + namespace ll::event::player { class PlayerConnectEvent : public Cancellable { public: - const ServerPlayer& player; + NetworkIdentifier const& networkIdentifier; + ConnectionRequest const& connectionRequest; + ServerPlayer& player; - constexpr explicit PlayerConnectEvent(const ServerPlayer& player) : player(player) {} + constexpr explicit PlayerConnectEvent( + NetworkIdentifier const& networkIdentifier, + ConnectionRequest const& connectionRequest, + ServerPlayer& player + ) + : networkIdentifier(networkIdentifier), + connectionRequest(connectionRequest), + player(player) {} LLNDAPI static std::unique_ptr emitterFactory(ListenerBase&); }; diff --git a/src/ll/api/event/player/PlayerLeaveEvent.cpp b/src/ll/api/event/player/PlayerLeaveEvent.cpp index 35d1be0666..33ee2e8ed3 100644 --- a/src/ll/api/event/player/PlayerLeaveEvent.cpp +++ b/src/ll/api/event/player/PlayerLeaveEvent.cpp @@ -7,8 +7,7 @@ namespace ll::event::player { LL_TYPED_INSTANCE_HOOK(PlayerLeaveEventHook, HookPriority::Normal, ServerPlayer, &ServerPlayer::disconnect, void) { - auto event = PlayerLeaveEvent(*this); - EventBus::getInstance().publish(event); + EventBus::getInstance().publish(PlayerLeaveEvent{*this}); origin(); } diff --git a/src/ll/api/service/GlobalService.cpp b/src/ll/api/service/GlobalService.cpp index c7e6495800..abe1dd177b 100644 --- a/src/ll/api/service/GlobalService.cpp +++ b/src/ll/api/service/GlobalService.cpp @@ -159,18 +159,14 @@ LL_AUTO_INSTANCE_HOOK(LevelDestructor, HookPriority::High, "??1Level@@UEAA@XZ", // RakNet::RakPeer -LL_AUTO_STATIC_HOOK( - RakNetRakPeerCreater, - HookPriority::High, - createUniqueRakPeer, - RakNet::RakPeerInterface::InstanceOwner -) { +LL_AUTO_INSTANCE_HOOK(RakNetRakPeerConstructor, HookPriority::High, "??0RakPeer@RakNet@@QEAA@XZ", RakNet::RakPeer*) { auto self = origin(); - ll::Global.init((RakNet::RakPeer*)self.get()); + ll::Global.init(self); + unhook(); return self; } LL_AUTO_INSTANCE_HOOK(RakNetRakPeerDestructor, HookPriority::High, "??1RakPeer@RakNet@@UEAA@XZ", void) { - ll::Global.init(nullptr); + if ((void*)this == (void*)ll::Global.get()) ll::Global.init(nullptr); origin(); } diff --git a/src/ll/test/EventTest.cpp b/src/ll/test/EventTest.cpp index 0f82cc0850..c437082eeb 100644 --- a/src/ll/test/EventTest.cpp +++ b/src/ll/test/EventTest.cpp @@ -12,6 +12,7 @@ #include "ll/api/event/player/PlayerConnectEvent.h" #include "ll/api/event/player/PlayerJoinEvent.h" #include "ll/api/event/player/PlayerLeaveEvent.h" +#include "mc/codebuilder/MCRESULT.h" #include "mc/nbt/CompoundTag.h" #include "ll/api/base/FixedString.h" @@ -125,7 +126,7 @@ LL_AUTO_TYPED_INSTANCE_HOOK( ll::logger.debug("result: {}", ev.result.getFullCode()); }); bus.emplaceListener([](player::PlayerConnectEvent& ev) { - ll::logger.debug("Player connect: {}", ev.player.getRealName()); + ll::logger.debug("Player connect: {} {}", ev.player.getRealName(), ev.player.getIPAndPort()); }); bus.emplaceListener([](player::PlayerJoinEvent& ev) { ll::logger.debug("Player join: {}", ev.player.getRealName()); diff --git a/src/mc/network/NetworkIdentifier.cpp b/src/mc/network/NetworkIdentifier.cpp index 147a7c1555..c1d2c760cf 100644 --- a/src/mc/network/NetworkIdentifier.cpp +++ b/src/mc/network/NetworkIdentifier.cpp @@ -4,7 +4,8 @@ #include "ll/api/service/GlobalService.h" std::string NetworkIdentifier::getIPAndPort() const { - if (!ll::Global) { return ""; } - std::string rv = ll::Global->GetSystemAddressFromGuid(mGuid).ToString(':'); - return rv.substr(0, rv.find('|')); + if (!ll::Global) { + return ""; + } + return ll::Global->GetSystemAddressFromGuid(mGuid).ToString(':'); }