refactor: move event disptacher into service locator

This commit is contained in:
k0t9i 2023-10-16 23:38:01 +04:00
parent b948273172
commit ede55a870e
15 changed files with 160 additions and 94 deletions

View File

@ -9,5 +9,6 @@ namespace L2Bot::Domain::Repositories
public:
virtual const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() = 0;
virtual void Reset() = 0;
virtual void Init() {};
};
}

View File

@ -46,6 +46,15 @@ namespace L2Bot::Domain::Services
void Start()
{
m_DropRepository.Init();
m_HeroRepository.Init();
m_NPCRepository.Init();
m_PlayerRepository.Init();
m_SkillRepository.Init();
m_ItemRepository.Init();
m_AbnormalEffectRepository.Init();
m_ChatMessageRepository.Init();
m_ConnectingThread = std::thread(&WorldHandler::Connect, this);
m_SendingThread = std::thread(&WorldHandler::Send, this);
m_ReceivingThread = std::thread(&WorldHandler::Receive, this);

View File

@ -9,6 +9,8 @@
#include "Serializers/JsonIncomingMessageFactory.h"
#include "Transports/NamedPipeTransport.h"
#include "Versions/VersionAbstractFactory.h"
#include "Services/ServiceLocator.h"
#include "Events/HeroDeletedEvent.h"
using namespace L2Bot::Domain;
@ -33,20 +35,13 @@ public:
m_Transport
)
{
}
Application() = delete;
virtual ~Application() = default;
void Start()
{
HMODULE hEngine = GetModuleHandleA("Engine.dll");
HMODULE hCore = GetModuleHandleA("Core.dll");
m_AbstractFactory.GetNetworkHandler().Init(hEngine);
m_AbstractFactory.GetGameEngine().Init(hEngine);
m_AbstractFactory.GetL2GameData().Init(hEngine);
m_AbstractFactory.GetFName().Init(hCore);
Init();
m_WorldHandler.Start();
}
@ -58,6 +53,20 @@ public:
m_AbstractFactory.GetNetworkHandler().Restore();
}
private:
void Init()
{
ServiceLocator::GetInstance().SetEventDispatcher(std::make_unique<EventDispatcher>());
HMODULE hEngine = GetModuleHandleA("Engine.dll");
HMODULE hCore = GetModuleHandleA("Core.dll");
m_AbstractFactory.GetNetworkHandler().Init(hEngine);
m_AbstractFactory.GetGameEngine().Init(hEngine);
m_AbstractFactory.GetL2GameData().Init(hEngine);
m_AbstractFactory.GetFName().Init(hCore);
}
private:
const VersionAbstractFactory& m_AbstractFactory;
Services::WorldHandler m_WorldHandler;

View File

@ -11,10 +11,8 @@ class EventDispatcher
public:
using Delegate = std::function<void(const Event&)>;
static EventDispatcher& GetInstance() {
static EventDispatcher instance;
return instance;
}
EventDispatcher() = default;
virtual ~EventDispatcher() = default;
void Dispatch(const Event& evt)
{
@ -36,12 +34,6 @@ public:
m_Handlers[eventName].push_back(handler);
}
private:
EventDispatcher() = default;
virtual ~EventDispatcher() = default;
EventDispatcher(const EventDispatcher&) = delete;
EventDispatcher& operator=(const EventDispatcher&) = delete;
private:
std::unordered_map<std::string, std::vector<Delegate>> m_Handlers;
};

View File

@ -186,6 +186,7 @@
<ClInclude Include="Application.h" />
<ClInclude Include="Serializers\JsonIncomingMessageFactory.h" />
<ClInclude Include="Serializers\JsonSerializer.h" />
<ClInclude Include="Services\ServiceLocator.h" />
<ClInclude Include="Versions\Interlude\Services\HeroService.h" />
<ClInclude Include="ThirdParty\json.hpp" />
<ClInclude Include="Versions\GameStructs\FNameInterface.h" />

View File

@ -204,6 +204,9 @@
<ClInclude Include="Versions\Interlude\Services\HeroService.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Services\ServiceLocator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">

View File

@ -0,0 +1,30 @@
#pragma once
#include <memory>
#include "../Events/EventDispatcher.h"
class ServiceLocator
{
public:
static ServiceLocator& GetInstance() {
static ServiceLocator instance;
return instance;
}
const std::unique_ptr<EventDispatcher>& GetEventDispatcher()
{
return m_EventDispatcher;
}
void SetEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher)
{
m_EventDispatcher = std::move(dispatcher);
}
private:
ServiceLocator() = default;
virtual ~ServiceLocator() = default;
ServiceLocator(const ServiceLocator&) = delete;
ServiceLocator& operator=(const ServiceLocator&) = delete;
private:
std::unique_ptr<EventDispatcher> m_EventDispatcher;
};

View File

@ -7,7 +7,6 @@
#include "../../../Events/SkillUsedEvent.h"
#include "../../../Events/SkillCancelledEvent.h"
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Events/ItemCreatedEvent.h"
#include "../../../Events/ItemUpdatedEvent.h"
#include "../../../Events/ItemDeletedEvent.h"
@ -18,6 +17,7 @@
#include "../../../Events/CreatureDiedEvent.h"
#include "../../../DTO/ItemData.h"
#include "FName.h"
#include "../../../Services/ServiceLocator.h"
namespace Interlude
{
@ -94,31 +94,31 @@ namespace Interlude
void __fastcall GameEngineWrapper::__OnSkillListPacket_hook(GameEngine* This, uint32_t, L2ParamStack& stack)
{
EventDispatcher::GetInstance().Dispatch(SkillCreatedEvent{stack.GetBufferAsVector<int32_t>()});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(SkillCreatedEvent{stack.GetBufferAsVector<int32_t>()});
(*__OnSkillListPacket)(This, stack);
}
int __fastcall GameEngineWrapper::__OnReceiveMagicSkillUse_hook(GameEngine* This, uint32_t, User* u1, User* u2, L2ParamStack& stack)
{
EventDispatcher::GetInstance().Dispatch(SkillUsedEvent{ stack.GetBufferAsVector<int32_t>() });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(SkillUsedEvent{ stack.GetBufferAsVector<int32_t>() });
return (*__OnReceiveMagicSkillUse)(This, u1, u2, stack);
}
void __fastcall GameEngineWrapper::__OnReceiveMagicSkillCanceled_hook(GameEngine* This, uint32_t, User* user)
{
EventDispatcher::GetInstance().Dispatch(SkillCancelledEvent{ user->objectId });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(SkillCancelledEvent{ user->objectId });
(*__OnReceiveMagicSkillCanceled)(This, user);
}
void __fastcall GameEngineWrapper::__AddAbnormalStatus_hook(GameEngine* This, uint32_t, L2ParamStack& stack)
{
EventDispatcher::GetInstance().Dispatch(AbnormalEffectChangedEvent{ stack.GetBufferAsVector<int32_t>(3) });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(AbnormalEffectChangedEvent{ stack.GetBufferAsVector<int32_t>(3) });
(*__AddAbnormalStatus)(This, stack);
}
void __fastcall GameEngineWrapper::__AddInventoryItem_hook(GameEngine* This, uint32_t, ItemInfo& itemInfo)
{
EventDispatcher::GetInstance().Dispatch(
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(
ItemCreatedEvent
{
ItemData
@ -154,13 +154,13 @@ namespace Interlude
switch (actionType)
{
case UpdateItemListActionType::created:
EventDispatcher::GetInstance().Dispatch(ItemCreatedEvent{ itemData });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(ItemCreatedEvent{ itemData });
break;
case UpdateItemListActionType::updated:
EventDispatcher::GetInstance().Dispatch(ItemUpdatedEvent{ itemData });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(ItemUpdatedEvent{ itemData });
break;
case UpdateItemListActionType::deleted:
EventDispatcher::GetInstance().Dispatch(ItemDeletedEvent{ itemInfo.objectId });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(ItemDeletedEvent{ itemInfo.objectId });
break;
}
(*__OnReceiveUpdateItemList)(This, actionType, itemInfo);
@ -168,7 +168,7 @@ namespace Interlude
void __fastcall GameEngineWrapper::__OnExAutoSoulShot_hook(GameEngine* This, uint32_t, L2ParamStack& stack)
{
EventDispatcher::GetInstance().Dispatch(ItemAutousedEvent{ stack.GetBufferAsVector<uint32_t>() });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(ItemAutousedEvent{ stack.GetBufferAsVector<uint32_t>() });
(*__OnExAutoSoulShot)(This, stack);
}
@ -181,13 +181,13 @@ namespace Interlude
(*__Tick)(This, deltaTime);
EventDispatcher::GetInstance().Dispatch(GameEngineTickedEvent{});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(GameEngineTickedEvent{});
}
void __fastcall GameEngineWrapper::__OnSay2_hook(GameEngine* This, uint32_t, L2ParamStack& stack)
{
const auto buffer = stack.GetBufferAsVector<uint32_t>();
EventDispatcher::GetInstance().Dispatch(
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(
ChatMessageCreatedEvent
{
ChatMessage
@ -204,7 +204,7 @@ namespace Interlude
}
void __fastcall GameEngineWrapper::__OnEndItemList_hook(GameEngine* This, uint32_t)
{
EventDispatcher::GetInstance().Dispatch(OnEndItemListEvent());
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(OnEndItemListEvent());
(*__OnEndItemList)(This);
}
// TODO ini
@ -217,7 +217,7 @@ namespace Interlude
int __fastcall GameEngineWrapper::__OnDie_hook(GameEngine* This, int, User* creature, L2ParamStack& stack)
{
EventDispatcher::GetInstance().Dispatch(CreatureDiedEvent{ creature->objectId, stack.GetBufferAsVector<int32_t>() });
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(CreatureDiedEvent{ creature->objectId, stack.GetBufferAsVector<int32_t>() });
return (*__OnDie)(This, creature, stack);
}

View File

@ -2,8 +2,8 @@
#include "../../../Common/apihook.h"
#include "NetworkHandlerWrapper.h"
#include "../../../Events/SpoiledEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "ProcessManipulation.h"
#include "../../../Services/ServiceLocator.h"
namespace Interlude
{
@ -176,7 +176,7 @@ namespace Interlude
p->GetMessageId() == static_cast<int>(L2::SystemMessagePacket::Type::SPOIL_SUCCESS) ||
p->GetMessageId() == static_cast<int>(L2::SystemMessagePacket::Type::ALREADY_SPOILED)
) {
EventDispatcher::GetInstance().Dispatch(SpoiledEvent{});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(SpoiledEvent{});
}
}

View File

@ -7,8 +7,8 @@
#include "../Factories/AbnormalEffectFactory.h"
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@ -27,12 +27,6 @@ namespace Interlude
AbnormalEffectRepository(const AbnormalEffectFactory& factory) :
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnEffectToggled(evt);
});
EventDispatcher::GetInstance().Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
}
AbnormalEffectRepository() = delete;
@ -41,6 +35,16 @@ namespace Interlude
Reset();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnEffectToggled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
}
void Reset() override
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);

View File

@ -4,7 +4,7 @@
#include <shared_mutex>
#include "../Factories/ChatMessageFactory.h"
#include "../../../Events/ChatMessageCreatedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@ -20,18 +20,22 @@ namespace Interlude
return m_Messages;
}
void Reset()
void Reset() override
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
m_Messages.clear();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ChatMessageCreatedEvent::name, [this](const Event& evt) {
OnMessageCreated(evt);
});
}
ChatMessageRepository(const ChatMessageFactory& factory) :
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(ChatMessageCreatedEvent::name, [this](const Event& evt) {
OnMessageCreated(evt);
});
}
void OnMessageCreated(const Event& evt)

View File

@ -3,10 +3,10 @@
#include <shared_mutex>
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/HeroFactory.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Events/HeroCreatedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@ -25,7 +25,7 @@ namespace Interlude
if (hero) {
if (!m_Hero) {
m_Hero = m_Factory.Create(hero);
EventDispatcher::GetInstance().Dispatch(HeroCreatedEvent{});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(HeroCreatedEvent{});
}
else
{
@ -35,7 +35,7 @@ namespace Interlude
}
else if (m_Hero) {
m_Hero = nullptr;
EventDispatcher::GetInstance().Dispatch(HeroDeletedEvent{});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(HeroDeletedEvent{});
}
return result;

View File

@ -12,7 +12,7 @@
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/ItemAutousedEvent.h"
#include "../../../Events/OnEndItemListEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@ -44,24 +44,6 @@ namespace Interlude
m_NetworkHandler(networkHandler),
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
OnItemCreated(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemUpdatedEvent::name, [this](const Event& evt) {
OnItemUpdated(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemDeletedEvent::name, [this](const Event& evt) {
OnItemDeleted(evt);
});
EventDispatcher::GetInstance().Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemAutousedEvent::name, [this](const Event& evt) {
OnItemAutoused(evt);
});
EventDispatcher::GetInstance().Subscribe(OnEndItemListEvent::name, [this](const Event& evt) {
OnEndItemList(evt);
});
}
void OnEndItemList(const Event& evt)
@ -198,6 +180,28 @@ namespace Interlude
m_NetworkHandler.RequestItemList();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
OnItemCreated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemUpdatedEvent::name, [this](const Event& evt) {
OnItemUpdated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemDeletedEvent::name, [this](const Event& evt) {
OnItemDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemAutousedEvent::name, [this](const Event& evt) {
OnItemAutoused(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(OnEndItemListEvent::name, [this](const Event& evt) {
OnEndItemList(evt);
});
}
private:
const ItemFactory& m_Factory;
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::BaseItem>> m_Items;

View File

@ -5,10 +5,10 @@
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/NPCFactory.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Events/SpoiledEvent.h"
#include "../../../Events/CreatureDiedEvent.h"
#include "../../GameStructs/FindObjectsTrait.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@ -55,17 +55,21 @@ namespace Interlude
m_Npcs.clear();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SpoiledEvent::name, [this](const Event& evt) {
OnSpoiled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(CreatureDiedEvent::name, [this](const Event& evt) {
OnCreatureDied(evt);
});
}
NPCRepository(const NetworkHandlerWrapper& networkHandler, const NPCFactory& factory, const uint16_t radius) :
m_NetworkHandler(networkHandler),
m_Factory(factory),
m_Radius(radius)
{
EventDispatcher::GetInstance().Subscribe(SpoiledEvent::name, [this](const Event& evt) {
OnSpoiled(evt);
});
EventDispatcher::GetInstance().Subscribe(CreatureDiedEvent::name, [this](const Event& evt) {
OnCreatureDied(evt);
});
}
NPCRepository() = delete;

View File

@ -11,9 +11,9 @@
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/GameEngineTickedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Common/TimerMap.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@ -34,24 +34,7 @@ namespace Interlude
m_NetworkHandler(networkHandler),
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(SkillCreatedEvent::name, [this](const Event& evt) {
OnSkillCreated(evt);
});
EventDispatcher::GetInstance().Subscribe(SkillUsedEvent::name, [this](const Event& evt) {
OnSkillUsed(evt);
});
EventDispatcher::GetInstance().Subscribe(SkillCancelledEvent::name, [this](const Event& evt) {
OnSkillCancelled(evt);
});
EventDispatcher::GetInstance().Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnSkillToggled(evt);
});
EventDispatcher::GetInstance().Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
EventDispatcher::GetInstance().Subscribe(GameEngineTickedEvent::name, [this](const Event& evt) {
OnGameEngineTicked(evt);
});
}
SkillRepository() = delete;
@ -68,6 +51,28 @@ namespace Interlude
m_NewSkills.clear();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillCreatedEvent::name, [this](const Event& evt) {
OnSkillCreated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillUsedEvent::name, [this](const Event& evt) {
OnSkillUsed(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillCancelledEvent::name, [this](const Event& evt) {
OnSkillCancelled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnSkillToggled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(GameEngineTickedEvent::name, [this](const Event& evt) {
OnGameEngineTicked(evt);
});
}
void OnGameEngineTicked(const Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);