feat: add some low level error handling

This commit is contained in:
k0t9i 2023-10-19 10:04:52 +04:00
parent 641e20e82a
commit 2e025fd0cf
9 changed files with 203 additions and 74 deletions

View File

@ -40,7 +40,7 @@ public:
if (m_Pipe == INVALID_HANDLE_VALUE) if (m_Pipe == INVALID_HANDLE_VALUE)
{ {
throw RuntimeException(std::format(L"cannot create the pipe {}: {}", m_PipeName, GetLastError())); throw CriticalRuntimeException(std::format(L"cannot create the pipe {}: {}", m_PipeName, GetLastError()));
} }
} }
else else
@ -83,13 +83,13 @@ public:
if (!overlappedResult) if (!overlappedResult)
{ {
m_Connected = false; m_Connected = false;
throw RuntimeException(std::format(L"cannot get overlapped result for the pipe {} when writing", m_PipeName)); throw CriticalRuntimeException(std::format(L"cannot get overlapped result for the pipe {} when writing", m_PipeName));
} }
} }
else else
{ {
m_Connected = false; m_Connected = false;
throw RuntimeException(std::format(L"cannot write to the pipe {}: {}", m_PipeName, lastError)); throw CriticalRuntimeException(std::format(L"cannot write to the pipe {}: {}", m_PipeName, lastError));
} }
} }
} }
@ -116,13 +116,13 @@ public:
if (!overlappedResult) if (!overlappedResult)
{ {
m_Connected = false; m_Connected = false;
throw RuntimeException(std::format(L"cannot get overlapped result for the pipe {} when reading", m_PipeName)); throw CriticalRuntimeException(std::format(L"cannot get overlapped result for the pipe {} when reading", m_PipeName));
} }
} }
else else
{ {
m_Connected = false; m_Connected = false;
throw RuntimeException(std::format(L"cannot read from the pipe {}: {}", m_PipeName, lastError)); throw CriticalRuntimeException(std::format(L"cannot read from the pipe {}: {}", m_PipeName, lastError));
} }
} }
@ -151,7 +151,7 @@ private:
const bool connected = ConnectNamedPipe(m_Pipe, &m_ConntectingOverlapped) == 0; const bool connected = ConnectNamedPipe(m_Pipe, &m_ConntectingOverlapped) == 0;
if (!connected) if (!connected)
{ {
throw RuntimeException(std::format(L"cannot connect the pipe {}: {}", m_PipeName, GetLastError())); throw CriticalRuntimeException(std::format(L"cannot connect the pipe {}: {}", m_PipeName, GetLastError()));
} }
switch (GetLastError()) switch (GetLastError())
@ -164,7 +164,7 @@ private:
if (SetEvent(m_ConntectingOverlapped.hEvent)) if (SetEvent(m_ConntectingOverlapped.hEvent))
break; break;
default: default:
throw RuntimeException(std::format(L"an error has occurred when connecting to the pipe ""{}"": {}", m_PipeName, GetLastError())); throw CriticalRuntimeException(std::format(L"an error has occurred when connecting to the pipe ""{}"": {}", m_PipeName, GetLastError()));
} }
} }
@ -175,7 +175,7 @@ private:
overlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); overlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (overlapped.hEvent == NULL) if (overlapped.hEvent == NULL)
{ {
throw RuntimeException(std::format(L"cannot create overlapped for the pipe {}: {}", m_PipeName, GetLastError())); throw CriticalRuntimeException(std::format(L"cannot create overlapped for the pipe {}: {}", m_PipeName, GetLastError()));
} }
overlapped.Offset = 0; overlapped.Offset = 0;
overlapped.OffsetHigh = 0; overlapped.OffsetHigh = 0;

View File

@ -1,17 +1,25 @@
#include "pch.h" #include "pch.h"
#include "FName.h" #include "FName.h"
#include "Domain/Exceptions.h"
using namespace L2Bot::Domain;
namespace Interlude namespace Interlude
{ {
FNameEntry* (__cdecl* FName::__GetEntry)(int) = 0; FNameEntry* (__cdecl* FName::__GetEntry)(int) = 0;
//todo exception(?)
FNameEntry* FName::GetEntry(int index) const FNameEntry* FName::GetEntry(int index) const
{ {
if (__GetEntry) { __try {
return(*__GetEntry)(index); if (__GetEntry) {
return(*__GetEntry)(index);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"FName::GetEntry failed");
} }
return 0;
} }
void FName::Init(HMODULE hModule) void FName::Init(HMODULE hModule)

View File

@ -78,6 +78,7 @@ namespace Interlude
(FARPROC&)__OnDie = (FARPROC)splice( (FARPROC&)__OnDie = (FARPROC)splice(
GetProcAddress(hModule, "?OnDie@UGameEngine@@UAEHPAUUser@@AAVL2ParamStack@@@Z"), __OnDie_hook GetProcAddress(hModule, "?OnDie@UGameEngine@@UAEHPAUUser@@AAVL2ParamStack@@@Z"), __OnDie_hook
); );
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine hooks initialized");
} }
void GameEngineWrapper::Restore() void GameEngineWrapper::Restore()
@ -94,6 +95,7 @@ namespace Interlude
restore((void*&)__GetMaxTickRate); restore((void*&)__GetMaxTickRate);
restore((void*&)__OnDie); restore((void*&)__OnDie);
restore((void*&)__Tick); restore((void*&)__Tick);
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine hooks restored");
} }
void __fastcall GameEngineWrapper::__OnSkillListPacket_hook(GameEngine* This, uint32_t, L2ParamStack& stack) void __fastcall GameEngineWrapper::__OnSkillListPacket_hook(GameEngine* This, uint32_t, L2ParamStack& stack)
@ -181,7 +183,7 @@ namespace Interlude
if (_target == 0) if (_target == 0)
{ {
_target = This; _target = This;
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine {:#010x} obtained", (int)_target); Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine pointer {:#010x} obtained", (int)_target);
} }
(*__Tick)(This, deltaTime); (*__Tick)(This, deltaTime);

View File

@ -3,6 +3,7 @@
#include "L2GameDataWrapper.h" #include "L2GameDataWrapper.h"
#include "ProcessManipulation.h" #include "ProcessManipulation.h"
#include "Domain/Services/ServiceLocator.h" #include "Domain/Services/ServiceLocator.h"
#include "Domain/Exceptions.h"
using namespace L2Bot::Domain; using namespace L2Bot::Domain;
@ -23,40 +24,53 @@ namespace Interlude
(FARPROC&)__GetItemData = GetProcAddress(hModule, "?GetItemData@FL2GameData@@QAEPAVFL2ItemDataBase@@H@Z"); (FARPROC&)__GetItemData = GetProcAddress(hModule, "?GetItemData@FL2GameData@@QAEPAVFL2ItemDataBase@@H@Z");
(FARPROC&)__GetMSData = GetProcAddress(hModule, "?GetMSData@FL2GameData@@QAEPAUFL2MagicSkillData@@HH@Z"); (FARPROC&)__GetMSData = GetProcAddress(hModule, "?GetMSData@FL2GameData@@QAEPAUFL2MagicSkillData@@HH@Z");
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"FL2GameData hooks initialized");
} }
void L2GameDataWrapper::Restore() void L2GameDataWrapper::Restore()
{ {
restore(originalInitAddress); restore(originalInitAddress);
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"FL2GameData hooks restored");
} }
//todo exception(?)
FL2ItemDataBase* L2GameDataWrapper::GetItemData(int itemId) const FL2ItemDataBase* L2GameDataWrapper::GetItemData(int itemId) const
{ {
if (__GetItemData && _target) { __try {
return (*__GetItemData)(_target, itemId); if (__GetItemData && _target) {
return (*__GetItemData)(_target, itemId);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"FL2GameData::GetItemData failed");
} }
return 0;
} }
FL2MagicSkillData* L2GameDataWrapper::GetMSData(int skillId, int level) const FL2MagicSkillData* L2GameDataWrapper::GetMSData(int skillId, int level) const
{ {
if (__GetMSData && _target) { __try {
return (*__GetMSData)(_target, skillId, level); if (__GetMSData && _target) {
return (*__GetMSData)(_target, skillId, level);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"FL2GameData::GetMSData failed");
} }
return 0;
} }
int __fastcall L2GameDataWrapper::__Init_hook(L2GameData* This, int, int unk, int unk1) int __fastcall L2GameDataWrapper::__Init_hook(L2GameData* This, int, int unk, int unk1)
{ {
if (_target == 0) { if (_target == 0) {
_target = This; _target = This;
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"FL2GameData pointer {:#010x} obtained", (int)_target);
InjectLibrary::StopCurrentProcess(); InjectLibrary::StopCurrentProcess();
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

@ -1,5 +1,8 @@
#include "pch.h" #include "pch.h"
#include "L2ParamStack.h" #include "L2ParamStack.h"
#include "Domain/Exceptions.h"
using namespace L2Bot::Domain;
namespace Interlude namespace Interlude
{ {
@ -15,50 +18,94 @@ namespace Interlude
L2ParamStack::L2ParamStack(int size) L2ParamStack::L2ParamStack(int size)
{ {
Init(); Init();
(*__Ctor)(this, size); __try {
(*__Ctor)(this, size);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::constructor failed");
}
} }
L2ParamStack::~L2ParamStack() L2ParamStack::~L2ParamStack()
{ {
Init(); Init();
(*__Dtor)(this); __try {
(*__Dtor)(this);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::desctructor failed");
}
} }
int L2ParamStack::PushBack(void* val) int L2ParamStack::PushBack(void* val)
{ {
Init(); Init();
return (*__PushBack)(this, val); __try {
return (*__PushBack)(this, val);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::PushBack failed");
}
} }
void* L2ParamStack::Top() void* L2ParamStack::Top()
{ {
Init(); Init();
return (*__Top)(this); __try {
return (*__Top)(this);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::Top failed");
}
} }
void** L2ParamStack::GetBuffer() void** L2ParamStack::GetBuffer()
{ {
Init(); Init();
return (__GetBuffer)(this); __try {
return (__GetBuffer)(this);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::GetBuffer failed");
}
} }
int L2ParamStack::GetBufferSize() int L2ParamStack::GetBufferSize()
{ {
Init(); Init();
return (__GetBufferSize)(this); __try {
return (__GetBufferSize)(this);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::GetBufferSize failed");
}
} }
int L2ParamStack::GetTotalBufferSize() int L2ParamStack::GetTotalBufferSize()
{ {
Init(); Init();
return (__GetTotalBufferSize)(this); __try {
return (__GetTotalBufferSize)(this);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"L2ParamStack::GetTotalBufferSize failed");
}
} }
void L2ParamStack::Init() void L2ParamStack::Init()
{ {
// todo exceptions
if (_hModule == 0) { if (_hModule == 0) {
_hModule = GetModuleHandleA("Core.dll"); _hModule = GetModuleHandleA("Core.dll");
if (!_hModule) {
throw CriticalRuntimeException(L"cannot load Core.dll");
}
(FARPROC&)__Ctor = GetProcAddress(_hModule, "??0L2ParamStack@@QAE@H@Z"); (FARPROC&)__Ctor = GetProcAddress(_hModule, "??0L2ParamStack@@QAE@H@Z");
(FARPROC&)__Dtor = GetProcAddress(_hModule, "??1L2ParamStack@@QAE@XZ"); (FARPROC&)__Dtor = GetProcAddress(_hModule, "??1L2ParamStack@@QAE@XZ");
(FARPROC&)__PushBack = GetProcAddress(_hModule, "?PushBack@L2ParamStack@@QAEHPAX@Z"); (FARPROC&)__PushBack = GetProcAddress(_hModule, "?PushBack@L2ParamStack@@QAEHPAX@Z");

View File

@ -4,6 +4,7 @@
#include "Domain/Events/SpoiledEvent.h" #include "Domain/Events/SpoiledEvent.h"
#include "ProcessManipulation.h" #include "ProcessManipulation.h"
#include "Domain/Services/ServiceLocator.h" #include "Domain/Services/ServiceLocator.h"
#include "Domain/Exceptions.h"
using namespace L2Bot::Domain; using namespace L2Bot::Domain;
@ -26,22 +27,32 @@ namespace Interlude
void(__thiscall* NetworkHandlerWrapper::__RequestAutoSoulShot)(NetworkHandler*, L2ParamStack&) = 0; void(__thiscall* NetworkHandlerWrapper::__RequestAutoSoulShot)(NetworkHandler*, L2ParamStack&) = 0;
void(__thiscall* NetworkHandlerWrapper::__ChangeWaitType)(NetworkHandler*, int) = 0; void(__thiscall* NetworkHandlerWrapper::__ChangeWaitType)(NetworkHandler*, int) = 0;
//todo exception
Item* NetworkHandlerWrapper::GetNextItem(float_t radius, int prevId) const Item* NetworkHandlerWrapper::GetNextItem(float_t radius, int prevId) const
{ {
if (__GetNextItem && _target) { __try {
return (*__GetNextItem)(_target, radius, prevId); if (__GetNextItem && _target) {
return (*__GetNextItem)(_target, radius, prevId);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::GetNextItem failed");
} }
return 0;
} }
//todo exception
User* NetworkHandlerWrapper::GetNextCreature(float_t radius, int prevId) const User* NetworkHandlerWrapper::GetNextCreature(float_t radius, int prevId) const
{ {
if (__GetNextCreature && _target) { __try {
return (*__GetNextCreature)(_target, radius, prevId); if (__GetNextCreature && _target) {
return (*__GetNextCreature)(_target, radius, prevId);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::GetNextCreature failed");
} }
return 0;
} }
User* NetworkHandlerWrapper::GetHero() const User* NetworkHandlerWrapper::GetHero() const
@ -63,69 +74,123 @@ namespace Interlude
User* NetworkHandlerWrapper::GetUser(int objectId) const User* NetworkHandlerWrapper::GetUser(int objectId) const
{ {
if (__GetUser && _target) { __try {
return (*__GetUser)(_target, objectId); if (__GetUser && _target) {
return (*__GetUser)(_target, objectId);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::GetUser failed");
} }
return 0;
} }
Item* NetworkHandlerWrapper::GetItem(int objectId) const Item* NetworkHandlerWrapper::GetItem(int objectId) const
{ {
if (__GetItem && _target) { __try {
return (*__GetItem)(_target, objectId); if (__GetItem && _target) {
return (*__GetItem)(_target, objectId);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::GetItem failed");
} }
return 0;
} }
void NetworkHandlerWrapper::MTL(APawn* self, L2::FVector dst, L2::FVector src, void* terrainInfo, int unk1) const void NetworkHandlerWrapper::MTL(APawn* self, L2::FVector dst, L2::FVector src, void* terrainInfo, int unk1) const
{ {
if (__MTL && _target) { __try {
(*__MTL)(_target, self, dst, src, terrainInfo, unk1); if (__MTL && _target) {
(*__MTL)(_target, self, dst, src, terrainInfo, unk1);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::MTL failed");
} }
} }
void NetworkHandlerWrapper::Action(int objectId, L2::FVector objectLocation, int unk) const void NetworkHandlerWrapper::Action(int objectId, L2::FVector objectLocation, int unk) const
{ {
if (__Action && _target) { __try {
(*__Action)(_target, objectId, objectLocation, unk); if (__Action && _target) {
(*__Action)(_target, objectId, objectLocation, unk);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::Action failed");
} }
} }
void NetworkHandlerWrapper::RequestMagicSkillUse(L2ParamStack& stack) const void NetworkHandlerWrapper::RequestMagicSkillUse(L2ParamStack& stack) const
{ {
if (__RequestMagicSkillUse && _target) { __try {
(*__RequestMagicSkillUse)(_target, stack); if (__RequestMagicSkillUse && _target) {
(*__RequestMagicSkillUse)(_target, stack);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::RequestMagicSkillUse failed");
} }
} }
int NetworkHandlerWrapper::RequestUseItem(L2ParamStack& stack) const int NetworkHandlerWrapper::RequestUseItem(L2ParamStack& stack) const
{ {
if (__RequestUseItem && _target) { __try {
return (*__RequestUseItem)(_target, stack); if (__RequestUseItem && _target) {
return (*__RequestUseItem)(_target, stack);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::RequestUseItem failed");
} }
return 0;
} }
void NetworkHandlerWrapper::RequestAutoSoulShot(L2ParamStack& stack) const void NetworkHandlerWrapper::RequestAutoSoulShot(L2ParamStack& stack) const
{ {
if (__RequestAutoSoulShot && _target) { __try {
(*__RequestAutoSoulShot)(_target, stack); if (__RequestAutoSoulShot && _target) {
(*__RequestAutoSoulShot)(_target, stack);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::RequestAutoSoulShot failed");
} }
} }
void NetworkHandlerWrapper::ChangeWaitType(int type) const void NetworkHandlerWrapper::ChangeWaitType(int type) const
{ {
if (__ChangeWaitType && _target) { __try {
(*__ChangeWaitType)(_target, type); if (__ChangeWaitType && _target) {
(*__ChangeWaitType)(_target, type);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::ChangeWaitType failed");
} }
} }
int NetworkHandlerWrapper::RequestItemList() const int NetworkHandlerWrapper::RequestItemList() const
{ {
if (__RequestItemList && _target) { __try {
return (*__RequestItemList)(_target); if (__RequestItemList && _target) {
return (*__RequestItemList)(_target);
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
throw CriticalRuntimeException(L"UNetworkHandler::RequestItemList failed");
} }
return 0;
} }
void NetworkHandlerWrapper::Init(HMODULE hModule) void NetworkHandlerWrapper::Init(HMODULE hModule)
@ -149,24 +214,26 @@ namespace Interlude
(FARPROC&)__AddNetworkQueue = (FARPROC)splice( (FARPROC&)__AddNetworkQueue = (FARPROC)splice(
GetProcAddress(hModule, "?AddNetworkQueue@UNetworkHandler@@UAEHPAUNetworkPacket@@@Z"), __AddNetworkQueue_hook GetProcAddress(hModule, "?AddNetworkQueue@UNetworkHandler@@UAEHPAUNetworkPacket@@@Z"), __AddNetworkQueue_hook
); );
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UNetworkHandler hooks initialized");
} }
void NetworkHandlerWrapper::Restore() void NetworkHandlerWrapper::Restore()
{ {
restore(originalInitAddress); restore(originalInitAddress);
restore((void*&)__AddNetworkQueue); restore((void*&)__AddNetworkQueue);
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UNetworkHandler hooks restored");
} }
void __fastcall NetworkHandlerWrapper::__Init_hook(NetworkHandler* This, int /*edx*/, float unk) void __fastcall NetworkHandlerWrapper::__Init_hook(NetworkHandler* This, int /*edx*/, float unk)
{ {
if (_target == 0) { if (_target == 0) {
_target = This; _target = This;
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UNetworkHandler pointer {:#010x} obtained", (int)_target);
InjectLibrary::StopCurrentProcess(); InjectLibrary::StopCurrentProcess();
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

@ -30,10 +30,7 @@ namespace Interlude
} }
AbnormalEffectRepository() = delete; AbnormalEffectRepository() = delete;
virtual ~AbnormalEffectRepository() virtual ~AbnormalEffectRepository() = default;
{
Reset();
}
void Init() override void Init() override
{ {

View File

@ -166,10 +166,7 @@ namespace Interlude
} }
ItemRepository() = delete; ItemRepository() = delete;
virtual ~ItemRepository() virtual ~ItemRepository() = default;
{
Reset();
}
void Reset() override void Reset() override
{ {

View File

@ -38,14 +38,13 @@ namespace Interlude
} }
SkillRepository() = delete; SkillRepository() = delete;
virtual ~SkillRepository() virtual ~SkillRepository() = default;
{
Reset();
}
void Reset() override void Reset() override
{ {
std::shared_lock<std::shared_timed_mutex>(m_Mutex); std::shared_lock<std::shared_timed_mutex>(m_Mutex);
m_CastingTimers.StopAll();
m_ReloadingTimers.StopAll();
m_Skills.clear(); m_Skills.clear();
m_IsNewCycle = false; m_IsNewCycle = false;
m_NewSkills.clear(); m_NewSkills.clear();
@ -100,8 +99,6 @@ namespace Interlude
if (evt.GetName() == Events::HeroDeletedEvent::name) if (evt.GetName() == Events::HeroDeletedEvent::name)
{ {
Reset(); Reset();
m_CastingTimers.StopAll();
m_ReloadingTimers.StopAll();
} }
} }