feat: add some info and app logging messages

This commit is contained in:
k0t9i 2023-10-17 23:08:41 +04:00
parent ab85800275
commit a2428bb0d1
12 changed files with 63 additions and 16 deletions

View File

@ -15,7 +15,7 @@ namespace Client.Application.ViewModels
{ {
get get
{ {
return message.Name + ": " + message.Text; return (message.Name != "" ? message.Name + ": " : "") + message.Text;
} }
} }

View File

@ -61,6 +61,10 @@ namespace L2Bot::Domain::Entities
{ {
return "hero"; return "hero";
} }
const ValueObjects::FullName& GetFullName() const
{
return m_FullName;
}
const std::vector<Serializers::Node> BuildSerializationNodes() const override const std::vector<Serializers::Node> BuildSerializationNodes() const override
{ {

View File

@ -11,6 +11,10 @@
#include "Versions/VersionAbstractFactory.h" #include "Versions/VersionAbstractFactory.h"
#include "Domain/Services/ServiceLocator.h" #include "Domain/Services/ServiceLocator.h"
#include "Domain/Events/EventDispatcher.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; using namespace L2Bot::Domain;

View File

@ -28,7 +28,7 @@ protected:
0, 0,
static_cast<uint8_t>(m_ChatChannel), static_cast<uint8_t>(m_ChatChannel),
L"", L"",
GetCurrentDateTime() + logEntry logEntry
} }
} }
); );

View File

@ -14,6 +14,6 @@ public:
protected: protected:
void DoSendToChannel(const std::wstring& logEntry) override void DoSendToChannel(const std::wstring& logEntry) override
{ {
OutputDebugStringW((GetCurrentDateTime() + logEntry).c_str()); OutputDebugStringW(logEntry.c_str());
} }
}; };

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <cstdint> #include <cstdint>
#include "Domain/Services/ServiceLocator.h"
#define BUFFER_SIZE 16384 #define BUFFER_SIZE 16384
@ -37,7 +38,7 @@ public:
if (m_Pipe == INVALID_HANDLE_VALUE) 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; return false;
} }
} }
@ -80,11 +81,13 @@ public:
const auto overlappedResult = GetOverlappedResult(m_Pipe, &m_WritingOverlapped, &ret, false); const auto overlappedResult = GetOverlappedResult(m_Pipe, &m_WritingOverlapped, &ret, false);
if (!overlappedResult) if (!overlappedResult)
{ {
Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot get overlapped result for the pipe ""{}"" when writing", m_PipeName);
m_Connected = false; m_Connected = false;
} }
} }
else else
{ {
Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot write to the pipe ""{}"": {}", m_PipeName, lastError);
m_Connected = false; m_Connected = false;
} }
} }
@ -111,12 +114,14 @@ public:
const auto overlappedResult = GetOverlappedResult(m_Pipe, &m_ReadingOverlapped, &ret, false); const auto overlappedResult = GetOverlappedResult(m_Pipe, &m_ReadingOverlapped, &ret, false);
if (!overlappedResult) if (!overlappedResult)
{ {
Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot get overlapped result for the pipe ""{}"" when reading", m_PipeName);
m_Connected = false; m_Connected = false;
return L""; return L"";
} }
} }
else else
{ {
Services::ServiceLocator::GetInstance().GetLogger()->Error(L"cannot read from the pipe ""{}"": {}", m_PipeName, lastError);
m_Connected = false; m_Connected = false;
return L""; return L"";
} }
@ -147,7 +152,7 @@ private:
const bool connected = ConnectNamedPipe(m_Pipe, &m_ConntectingOverlapped) == 0; const bool connected = ConnectNamedPipe(m_Pipe, &m_ConntectingOverlapped) == 0;
if (!connected) 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()) switch (GetLastError())
@ -155,16 +160,12 @@ private:
// The overlapped connection in progress. // The overlapped connection in progress.
case ERROR_IO_PENDING: case ERROR_IO_PENDING:
break; break;
// Client is already connected, so signal an event. // Client is already connected, so signal an event.
case ERROR_PIPE_CONNECTED: case ERROR_PIPE_CONNECTED:
if (SetEvent(m_ConntectingOverlapped.hEvent)) if (SetEvent(m_ConntectingOverlapped.hEvent))
break; break;
// If an error occurs during the connect operation...
default: 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); overlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (overlapped.hEvent == 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; return;
} }
overlapped.Offset = 0; overlapped.Offset = 0;

View File

@ -3,6 +3,7 @@
#include <Windows.h> #include <Windows.h>
#include "NamedPipe.h" #include "NamedPipe.h"
#include "../Common/Common.h" #include "../Common/Common.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain; using namespace L2Bot::Domain;
@ -11,26 +12,24 @@ class NamedPipeTransport : public Transports::TransportInterface
public: public:
const bool Connect() override const bool Connect() override
{ {
OutputDebugStringW(m_PipeName.c_str());
if (!m_ConnectionPipe.Connect(m_PipeName)) if (!m_ConnectionPipe.Connect(m_PipeName))
{ {
return false; 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(); const auto mainPipeName = GenerateUUID();
m_ConnectionPipe.Send(mainPipeName); 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)) if (!m_Pipe.Connect(mainPipeName))
{ {
OutputDebugStringA(std::to_string(GetLastError()).c_str());
return false; 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!"); m_Pipe.Send(L"Hello!");

View File

@ -180,6 +180,7 @@ namespace Interlude
if (_target == 0) if (_target == 0)
{ {
_target = This; _target = This;
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine {:#010x} obtained", (int)_target);
} }
(*__Tick)(This, deltaTime); (*__Tick)(This, deltaTime);

View File

@ -2,6 +2,9 @@
#include "../../../Common/apihook.h" #include "../../../Common/apihook.h"
#include "L2GameDataWrapper.h" #include "L2GameDataWrapper.h"
#include "ProcessManipulation.h" #include "ProcessManipulation.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain;
namespace Interlude namespace Interlude
{ {
@ -53,6 +56,7 @@ namespace Interlude
restore(originalInitAddress); restore(originalInitAddress);
InjectLibrary::StartCurrentProcess(); InjectLibrary::StartCurrentProcess();
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"FL2GameData {:#010x} obtained", (int)_target);
return (*__Init)(This, unk, unk1); return (*__Init)(This, unk, unk1);
} }

View File

@ -166,6 +166,7 @@ namespace Interlude
restore(originalInitAddress); restore(originalInitAddress);
InjectLibrary::StartCurrentProcess(); InjectLibrary::StartCurrentProcess();
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UNetworkHandler {:#010x} obtained", (int)_target);
(*__Init)(This, unk); (*__Init)(This, unk);
} }
} }

View File

@ -26,6 +26,7 @@ namespace Interlude
if (!m_Hero) { if (!m_Hero) {
m_Hero = m_Factory.Create(hero); m_Hero = m_Factory.Create(hero);
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::HeroCreatedEvent{}); Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::HeroCreatedEvent{});
Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} enter in the world", m_Hero->GetFullName().GetNickname());
} }
else else
{ {
@ -34,6 +35,7 @@ namespace Interlude
result[hero->objectId] = m_Hero; result[hero->objectId] = m_Hero;
} }
else if (m_Hero) { else if (m_Hero) {
Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} leave the world", m_Hero->GetFullName().GetNickname());
m_Hero = nullptr; m_Hero = nullptr;
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::HeroDeletedEvent{}); Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::HeroDeletedEvent{});
} }

View File

@ -7,20 +7,50 @@
InjectLibrary::Injector injector("L2BotHookMutex", WH_CALLWNDPROC); InjectLibrary::Injector injector("L2BotHookMutex", WH_CALLWNDPROC);
Application application(VersionAbstractFactory::Version::interlude); 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 <std::unique_ptr<Logger::LogChannel>> channels;
channels.push_back(std::make_unique<OutputDebugLogChannel>(std::vector<Logger::LogLevel>{}));
channels.push_back(std::make_unique<FileLogChannel>(directory + L"\\error.log", std::vector<Logger::LogLevel>{
Logger::LogLevel::error,
Logger::LogLevel::warning
}));
channels.push_back(std::make_unique<ChatLogChannel>(Enums::ChatChannelEnum::log, std::vector<Logger::LogLevel>{
Logger::LogLevel::app
}));
Services::ServiceLocator::GetInstance().SetLogger(std::make_unique<Logger::Logger>(std::move(channels)));
}
BOOL APIENTRY DllMain(HMODULE hModule, BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call, DWORD ul_reason_for_call,
LPVOID lpReserved LPVOID lpReserved
) )
{ {
const int processId = GetCurrentProcessId();
const std::string& processName = InjectLibrary::GetCurrentProcessName(); const std::string& processName = InjectLibrary::GetCurrentProcessName();
switch (ul_reason_for_call) switch (ul_reason_for_call)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
injector.SetHook(hModule); injector.SetHook(hModule);
if (processName == "l2.exe") { if (processName == "l2.exe") {
ConfigLogger(hModule);
InjectLibrary::StopCurrentProcess(); InjectLibrary::StopCurrentProcess();
application.Start(); application.Start();
InjectLibrary::StartCurrentProcess(); InjectLibrary::StartCurrentProcess();
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"attached to Lineage 2 client {:#010x}", processId);
} }
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
@ -28,6 +58,7 @@ BOOL APIENTRY DllMain(HMODULE hModule,
InjectLibrary::StopCurrentProcess(); InjectLibrary::StopCurrentProcess();
application.Stop(); application.Stop();
InjectLibrary::StartCurrentProcess(); InjectLibrary::StartCurrentProcess();
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"detached from Lineage 2 client {:#010x}", processId);
} }
injector.SetHook(); injector.SetHook();
break; break;