From a2428bb0d1b430649d55b2cec8171194acc24d01 Mon Sep 17 00:00:00 2001 From: k0t9i Date: Tue, 17 Oct 2023 23:08:41 +0400 Subject: [PATCH] feat: add some info and app logging messages --- .../ViewModels/ChatMessageViewModel.cs | 2 +- L2BotCore/Domain/Entities/Hero.h | 4 +++ L2BotDll/Application.h | 4 +++ L2BotDll/Logger/ChatLogChannel.h | 2 +- L2BotDll/Logger/OutputDebugLogChannel.h | 2 +- L2BotDll/Transports/NamedPipe.h | 17 +++++----- L2BotDll/Transports/NamedPipeTransport.h | 9 +++--- .../GameStructs/GameEngineWrapper.cpp | 1 + .../GameStructs/L2GameDataWrapper.cpp | 4 +++ .../GameStructs/NetworkHandlerWrapper.cpp | 1 + .../Interlude/Repositories/HeroRepository.h | 2 ++ L2BotDll/dllmain.cpp | 31 +++++++++++++++++++ 12 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Client/Application/ViewModels/ChatMessageViewModel.cs b/Client/Application/ViewModels/ChatMessageViewModel.cs index 9f727c2..c4f5937 100644 --- a/Client/Application/ViewModels/ChatMessageViewModel.cs +++ b/Client/Application/ViewModels/ChatMessageViewModel.cs @@ -15,7 +15,7 @@ namespace Client.Application.ViewModels { get { - return message.Name + ": " + message.Text; + return (message.Name != "" ? message.Name + ": " : "") + message.Text; } } diff --git a/L2BotCore/Domain/Entities/Hero.h b/L2BotCore/Domain/Entities/Hero.h index 06552bf..670df66 100644 --- a/L2BotCore/Domain/Entities/Hero.h +++ b/L2BotCore/Domain/Entities/Hero.h @@ -61,6 +61,10 @@ namespace L2Bot::Domain::Entities { return "hero"; } + const ValueObjects::FullName& GetFullName() const + { + return m_FullName; + } const std::vector BuildSerializationNodes() const override { diff --git a/L2BotDll/Application.h b/L2BotDll/Application.h index 49d33a0..f771a19 100644 --- a/L2BotDll/Application.h +++ b/L2BotDll/Application.h @@ -11,6 +11,10 @@ #include "Versions/VersionAbstractFactory.h" #include "Domain/Services/ServiceLocator.h" #include "Domain/Events/EventDispatcher.h" +#include "Domain/Logger/Logger.h" +#include "Logger/ChatLogChannel.h" +#include "Logger/FileLogChannel.h" +#include "Logger/OutputDebugLogChannel.h" using namespace L2Bot::Domain; diff --git a/L2BotDll/Logger/ChatLogChannel.h b/L2BotDll/Logger/ChatLogChannel.h index df4e743..190e94e 100644 --- a/L2BotDll/Logger/ChatLogChannel.h +++ b/L2BotDll/Logger/ChatLogChannel.h @@ -28,7 +28,7 @@ protected: 0, static_cast(m_ChatChannel), L"", - GetCurrentDateTime() + logEntry + logEntry } } ); diff --git a/L2BotDll/Logger/OutputDebugLogChannel.h b/L2BotDll/Logger/OutputDebugLogChannel.h index 27003cb..29d578e 100644 --- a/L2BotDll/Logger/OutputDebugLogChannel.h +++ b/L2BotDll/Logger/OutputDebugLogChannel.h @@ -14,6 +14,6 @@ public: protected: void DoSendToChannel(const std::wstring& logEntry) override { - OutputDebugStringW((GetCurrentDateTime() + logEntry).c_str()); + OutputDebugStringW(logEntry.c_str()); } }; \ No newline at end of file diff --git a/L2BotDll/Transports/NamedPipe.h b/L2BotDll/Transports/NamedPipe.h index 0af2488..936c2b2 100644 --- a/L2BotDll/Transports/NamedPipe.h +++ b/L2BotDll/Transports/NamedPipe.h @@ -4,6 +4,7 @@ #include #include #include +#include "Domain/Services/ServiceLocator.h" #define BUFFER_SIZE 16384 @@ -37,7 +38,7 @@ public: if (m_Pipe == INVALID_HANDLE_VALUE) { - OutputDebugStringA(std::to_string(GetLastError()).c_str()); + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot connect to the pipe ""{}""", m_PipeName); return false; } } @@ -80,11 +81,13 @@ public: const auto overlappedResult = GetOverlappedResult(m_Pipe, &m_WritingOverlapped, &ret, false); if (!overlappedResult) { + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot get overlapped result for the pipe ""{}"" when writing", m_PipeName); m_Connected = false; } } else { + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot write to the pipe ""{}"": {}", m_PipeName, lastError); m_Connected = false; } } @@ -111,12 +114,14 @@ public: const auto overlappedResult = GetOverlappedResult(m_Pipe, &m_ReadingOverlapped, &ret, false); if (!overlappedResult) { + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot get overlapped result for the pipe ""{}"" when reading", m_PipeName); m_Connected = false; return L""; } } else { + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot read from the pipe ""{}"": {}", m_PipeName, lastError); m_Connected = false; return L""; } @@ -147,7 +152,7 @@ private: const bool connected = ConnectNamedPipe(m_Pipe, &m_ConntectingOverlapped) == 0; if (!connected) { - OutputDebugStringA(std::to_string(GetLastError()).c_str()); + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot connect to the pipe ""{}"": {}", m_PipeName, GetLastError()); } switch (GetLastError()) @@ -155,16 +160,12 @@ private: // The overlapped connection in progress. case ERROR_IO_PENDING: break; - // Client is already connected, so signal an event. - case ERROR_PIPE_CONNECTED: if (SetEvent(m_ConntectingOverlapped.hEvent)) break; - - // If an error occurs during the connect operation... default: - OutputDebugStringA(std::to_string(GetLastError()).c_str()); + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"an error has occurred when connecting to the pipe ""{}"": {}", m_PipeName, GetLastError()); } } @@ -175,7 +176,7 @@ private: overlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); if (overlapped.hEvent == NULL) { - OutputDebugStringA(std::to_string(GetLastError()).c_str()); + Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot create overlapped for the pipe ""{}"": {}", m_PipeName, GetLastError()); return; } overlapped.Offset = 0; diff --git a/L2BotDll/Transports/NamedPipeTransport.h b/L2BotDll/Transports/NamedPipeTransport.h index 7066eef..b587137 100644 --- a/L2BotDll/Transports/NamedPipeTransport.h +++ b/L2BotDll/Transports/NamedPipeTransport.h @@ -3,6 +3,7 @@ #include #include "NamedPipe.h" #include "../Common/Common.h" +#include "Domain/Services/ServiceLocator.h" using namespace L2Bot::Domain; @@ -11,26 +12,24 @@ class NamedPipeTransport : public Transports::TransportInterface public: const bool Connect() override { - OutputDebugStringW(m_PipeName.c_str()); if (!m_ConnectionPipe.Connect(m_PipeName)) { return false; } - OutputDebugStringA("Client connected to connection pipe"); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"client connected to the connection pipe ""{}""", m_PipeName); const auto mainPipeName = GenerateUUID(); m_ConnectionPipe.Send(mainPipeName); - OutputDebugStringA("Name of main pipe sended"); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"name ""{}"" of the main pipe sended", mainPipeName); if (!m_Pipe.Connect(mainPipeName)) { - OutputDebugStringA(std::to_string(GetLastError()).c_str()); return false; } - OutputDebugStringA("Client connected to main pipe"); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"client connected to the main pipe ""{}""", mainPipeName); m_Pipe.Send(L"Hello!"); diff --git a/L2BotDll/Versions/Interlude/GameStructs/GameEngineWrapper.cpp b/L2BotDll/Versions/Interlude/GameStructs/GameEngineWrapper.cpp index a47b6a6..33f80d4 100644 --- a/L2BotDll/Versions/Interlude/GameStructs/GameEngineWrapper.cpp +++ b/L2BotDll/Versions/Interlude/GameStructs/GameEngineWrapper.cpp @@ -180,6 +180,7 @@ namespace Interlude if (_target == 0) { _target = This; + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine {:#010x} obtained", (int)_target); } (*__Tick)(This, deltaTime); diff --git a/L2BotDll/Versions/Interlude/GameStructs/L2GameDataWrapper.cpp b/L2BotDll/Versions/Interlude/GameStructs/L2GameDataWrapper.cpp index e3b6be3..3b361f4 100644 --- a/L2BotDll/Versions/Interlude/GameStructs/L2GameDataWrapper.cpp +++ b/L2BotDll/Versions/Interlude/GameStructs/L2GameDataWrapper.cpp @@ -2,6 +2,9 @@ #include "../../../Common/apihook.h" #include "L2GameDataWrapper.h" #include "ProcessManipulation.h" +#include "Domain/Services/ServiceLocator.h" + +using namespace L2Bot::Domain; namespace Interlude { @@ -53,6 +56,7 @@ namespace Interlude restore(originalInitAddress); InjectLibrary::StartCurrentProcess(); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"FL2GameData {:#010x} obtained", (int)_target); return (*__Init)(This, unk, unk1); } diff --git a/L2BotDll/Versions/Interlude/GameStructs/NetworkHandlerWrapper.cpp b/L2BotDll/Versions/Interlude/GameStructs/NetworkHandlerWrapper.cpp index 5d5bbb6..9cb8157 100644 --- a/L2BotDll/Versions/Interlude/GameStructs/NetworkHandlerWrapper.cpp +++ b/L2BotDll/Versions/Interlude/GameStructs/NetworkHandlerWrapper.cpp @@ -166,6 +166,7 @@ namespace Interlude restore(originalInitAddress); InjectLibrary::StartCurrentProcess(); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UNetworkHandler {:#010x} obtained", (int)_target); (*__Init)(This, unk); } } diff --git a/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h b/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h index eb06607..95c097e 100644 --- a/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h +++ b/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h @@ -26,6 +26,7 @@ namespace Interlude if (!m_Hero) { m_Hero = m_Factory.Create(hero); Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::HeroCreatedEvent{}); + Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} enter in the world", m_Hero->GetFullName().GetNickname()); } else { @@ -34,6 +35,7 @@ namespace Interlude result[hero->objectId] = m_Hero; } else if (m_Hero) { + Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} leave the world", m_Hero->GetFullName().GetNickname()); m_Hero = nullptr; Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::HeroDeletedEvent{}); } diff --git a/L2BotDll/dllmain.cpp b/L2BotDll/dllmain.cpp index a13210b..63b3e96 100644 --- a/L2BotDll/dllmain.cpp +++ b/L2BotDll/dllmain.cpp @@ -7,20 +7,50 @@ InjectLibrary::Injector injector("L2BotHookMutex", WH_CALLWNDPROC); Application application(VersionAbstractFactory::Version::interlude); +void ConfigLogger(HMODULE hModule) +{ + wchar_t buf[MAX_PATH]; + GetModuleFileNameW(hModule, buf, MAX_PATH); + const std::wstring libName(buf); + + std::wstring directory; + const size_t lastSlashIndex = libName.rfind(L"\\"); + if (std::string::npos != lastSlashIndex) + { + directory = libName.substr(0, lastSlashIndex); + } + + std::vector > channels; + channels.push_back(std::make_unique(std::vector{})); + channels.push_back(std::make_unique(directory + L"\\error.log", std::vector{ + Logger::LogLevel::error, + Logger::LogLevel::warning + })); + channels.push_back(std::make_unique(Enums::ChatChannelEnum::log, std::vector{ + Logger::LogLevel::app + })); + Services::ServiceLocator::GetInstance().SetLogger(std::make_unique(std::move(channels))); +} + BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { + const int processId = GetCurrentProcessId(); const std::string& processName = InjectLibrary::GetCurrentProcessName(); + switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: injector.SetHook(hModule); if (processName == "l2.exe") { + ConfigLogger(hModule); + InjectLibrary::StopCurrentProcess(); application.Start(); InjectLibrary::StartCurrentProcess(); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"attached to Lineage 2 client {:#010x}", processId); } break; case DLL_PROCESS_DETACH: @@ -28,6 +58,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, InjectLibrary::StopCurrentProcess(); application.Stop(); InjectLibrary::StartCurrentProcess(); + Services::ServiceLocator::GetInstance().GetLogger()->Info(L"detached from Lineage 2 client {:#010x}", processId); } injector.SetHook(); break;