feat: add attackers handling

This commit is contained in:
k0t9i
2023-11-11 15:44:03 +04:00
parent 129381e13c
commit e42ff2b5e7
10 changed files with 177 additions and 11 deletions

View File

@@ -15,6 +15,7 @@
#include "Domain/Events/ChatMessageCreatedEvent.h"
#include "Domain/Events/OnEndItemListEvent.h"
#include "Domain/Events/CreatureDiedEvent.h"
#include "Domain/Events/AttackedEvent.h"
#include "Domain/DTO/ItemData.h"
#include "Domain/DTO/ChatMessageData.h"
#include "FName.h"
@@ -38,6 +39,7 @@ namespace Interlude
void(__thiscall* GameEngineWrapper::__OnEndItemList)(GameEngine*) = 0;
float(__thiscall* GameEngineWrapper::__GetMaxTickRate)(GameEngine*) = 0;
int(__thiscall* GameEngineWrapper::__OnDie)(GameEngine*, User*, L2ParamStack&) = 0;
int(__thiscall* GameEngineWrapper::__OnAttack)(GameEngine*, User*, User*, int, int, int, int, int, L2::FVector, int) = 0;
void GameEngineWrapper::Init(HMODULE hModule)
@@ -78,6 +80,10 @@ namespace Interlude
(FARPROC&)__OnDie = (FARPROC)splice(
GetProcAddress(hModule, "?OnDie@UGameEngine@@UAEHPAUUser@@AAVL2ParamStack@@@Z"), __OnDie_hook
);
(FARPROC&)__OnAttack = (FARPROC)splice(
GetProcAddress(hModule, "?OnAttack@UGameEngine@@UAEHPAUUser@@0HHHHHVFVector@@H@Z"), __OnAttack_hook
);
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine hooks initialized");
}
@@ -95,6 +101,7 @@ namespace Interlude
restore((void*&)__GetMaxTickRate);
restore((void*&)__OnDie);
restore((void*&)__Tick);
restore((void*&)__OnAttack);
Services::ServiceLocator::GetInstance().GetLogger()->Info(L"UGameEngine hooks restored");
}
@@ -104,10 +111,11 @@ namespace Interlude
(*__OnSkillListPacket)(This, stack);
}
int __fastcall GameEngineWrapper::__OnReceiveMagicSkillUse_hook(GameEngine* This, uint32_t, User* u1, User* u2, L2ParamStack& stack)
int __fastcall GameEngineWrapper::__OnReceiveMagicSkillUse_hook(GameEngine* This, uint32_t, User* attacker, User* target, L2ParamStack& stack)
{
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::SkillUsedEvent{ stack.GetBufferAsVector<int32_t>() });
return (*__OnReceiveMagicSkillUse)(This, u1, u2, stack);
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::AttackedEvent{ attacker->objectId, target->objectId });
return (*__OnReceiveMagicSkillUse)(This, attacker, target, stack);
}
void __fastcall GameEngineWrapper::__OnReceiveMagicSkillCanceled_hook(GameEngine* This, uint32_t, User* user)
@@ -215,7 +223,6 @@ namespace Interlude
(*__OnEndItemList)(This);
}
// TODO ini
// 0 - <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float __fastcall GameEngineWrapper::__GetMaxTickRate_hook(GameEngine* This, int)
{
float fps = (*__GetMaxTickRate)(This);
@@ -228,4 +235,10 @@ namespace Interlude
return (*__OnDie)(This, creature, stack);
}
int __fastcall GameEngineWrapper::__OnAttack_hook(GameEngine* This, int, User* attacker, User* target, int unk0, int unk1, int unk2, int unk3, int unk4, L2::FVector unk5, int unk6)
{
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(Events::AttackedEvent{ attacker->objectId, target->objectId });
return (*__OnAttack)(This, attacker, target, unk0, unk1, unk2, unk3, unk4, unk5, unk6);
}
}

View File

@@ -31,9 +31,10 @@ namespace Interlude
static void(__thiscall* __OnEndItemList)(GameEngine*);
static float(__thiscall* __GetMaxTickRate)(GameEngine*);
static int(__thiscall* __OnDie)(GameEngine*, User*, L2ParamStack&);
static int(__thiscall* __OnAttack)(GameEngine*, User*, User*, int, int, int, int, int, L2::FVector, int);
static void __fastcall __OnSkillListPacket_hook(GameEngine* This, uint32_t /*edx*/, L2ParamStack& stack);
static int __fastcall __OnReceiveMagicSkillUse_hook(GameEngine* This, uint32_t /*edx*/, User* u1, User* u2, L2ParamStack& stack);
static int __fastcall __OnReceiveMagicSkillUse_hook(GameEngine* This, uint32_t /*edx*/, User* attacker, User* target, L2ParamStack& stack);
static void __fastcall __OnReceiveMagicSkillCanceled_hook(GameEngine* This, uint32_t /*edx*/, User* user);
static void __fastcall __AddAbnormalStatus_hook(GameEngine* This, uint32_t /*edx*/, L2ParamStack& stack);
static void __fastcall __AddInventoryItem_hook(GameEngine* This, uint32_t /*edx*/, ItemInfo& itemInfo);
@@ -44,7 +45,8 @@ namespace Interlude
static void __fastcall __OnEndItemList_hook(GameEngine* This, uint32_t /*edx*/);
static int __fastcall __OnDie_hook(GameEngine* This, int /*edx*/, User* creature, L2ParamStack& stack);
static float __fastcall __GetMaxTickRate_hook(GameEngine* This, int /*edx*/);
static int __fastcall __OnAttack_hook(GameEngine* This, int /*edx*/, User* attacker, User* target, int unk0, int unk1, int unk2, int unk3, int unk4, L2::FVector unk5, int unk6);
private:
static GameEngine* _target;
};

View File

@@ -6,6 +6,7 @@
#include "Domain/Events/HeroCreatedEvent.h"
#include "Domain/Events/HeroDeletedEvent.h"
#include "Domain/Events/CreatureDiedEvent.h"
#include "Domain/Events/AttackedEvent.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "Domain/Services/ServiceLocator.h"
@@ -32,6 +33,16 @@ namespace Interlude
else
{
m_Factory.Update(m_Hero, hero);
const auto attackers = std::map<uint32_t, uint32_t>(m_Hero->GetAttackerIds());
for (const auto kvp : attackers)
{
const auto attacker = m_NetworkHandler.GetUser(kvp.first);
// try to remove creature out of sight from the attackers
if (attacker == nullptr)
{
m_Hero->RemoveAttacker(kvp.first);
}
}
}
result[hero->objectId] = m_Hero;
}
@@ -53,16 +64,53 @@ namespace Interlude
void Init() override
{
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::CreatureDiedEvent::name, [this](const Events::Event& evt) {
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == Events::CreatureDiedEvent::name)
OnCreatureDied(evt);
});
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::AttackedEvent::name, [this](const Events::Event& evt) {
OnAttacked(evt);
});
}
void OnCreatureDied(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == Events::CreatureDiedEvent::name)
{
const auto casted = static_cast<const Events::CreatureDiedEvent&>(evt);
if (m_Hero)
{
const auto casted = static_cast<const Events::CreatureDiedEvent&>(evt);
if (m_Hero && m_Hero->GetId() == casted.GetCreatureId())
if (m_Hero->GetId() == casted.GetCreatureId())
{
Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} died", m_Hero->GetFullName().GetNickname());
}
else
{
// try to remove dead creature from the attackers
m_Hero->RemoveAttacker(casted.GetCreatureId());
}
}
});
}
}
void OnAttacked(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == Events::AttackedEvent::name)
{
const auto casted = static_cast<const Events::AttackedEvent&>(evt);
if (m_Hero && m_Hero->GetId() != casted.GetAttackerId())
{
if (m_Hero->GetId() == casted.GetTargetId())
{
m_Hero->AddAttacker(casted.GetAttackerId());
}
else
{
// try to remove creature that is attacking another target from the attackers
m_Hero->RemoveAttacker(casted.GetAttackerId());
}
}
}
}
HeroRepository(const NetworkHandlerWrapper& networkHandler, const HeroFactory& factory) :