L2Bot2.0/L2BotDll/dllmain.cpp
2024-08-15 20:14:20 +02:00

73 lines
2.1 KiB
C++

#include "pch.h"
#include "Common/apihook.h"
#include "Application.h"
#include "ProcessManipulation.h"
#include "Injector.h"
InjectLibrary::Injector injector("L2BotHookMutex", WH_GETMESSAGE);
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;
#ifdef _DEBUG
channels.push_back(std::make_unique<OutputDebugLogChannel>(std::vector<Logger::LogLevel>{}));
#endif
channels.push_back(std::make_unique<FileLogChannel>(directory + L"\\app.log", std::vector<Logger::LogLevel>{
#ifndef _DEBUG
Logger::LogLevel::error,
Logger::LogLevel::warning
#endif
}));
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,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
const int processId = GetCurrentProcessId();
const std::string& processName = InjectLibrary::GetCurrentProcessName();
const bool isLineageProcess = processName == "l2.exe" || processName == "l2.bin";
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
injector.SetHook(hModule);
if (isLineageProcess) {
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:
if (isLineageProcess) {
InjectLibrary::StopCurrentProcess();
application.Stop();
InjectLibrary::StartCurrentProcess();
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"detached from Lineage 2 client {:#010x}", processId);
}
injector.SetHook();
break;
}
return TRUE;
}