refactor: move events and service locator into core project

This commit is contained in:
k0t9i
2023-10-17 14:16:58 +04:00
parent 9836ef3a17
commit 03e61a9b9a
55 changed files with 825 additions and 760 deletions

View File

@@ -5,10 +5,10 @@
#include <shared_mutex>
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/AbnormalEffectFactory.h"
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "Domain/Events/AbnormalEffectChangedEvent.h"
#include "Domain/Events/HeroDeletedEvent.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Services/ServiceLocator.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -37,10 +37,10 @@ namespace Interlude
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::AbnormalEffectChangedEvent::name, [this](const Events::Event& evt) {
OnEffectToggled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::HeroDeletedEvent::name, [this](const Events::Event& evt) {
OnHeroDeleted(evt);
});
}
@@ -51,21 +51,21 @@ namespace Interlude
m_Effects.clear();
}
void OnHeroDeleted(const Event& evt)
void OnHeroDeleted(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == HeroDeletedEvent::name)
if (evt.GetName() == Events::HeroDeletedEvent::name)
{
Reset();
}
}
void OnEffectToggled(const Event& evt)
void OnEffectToggled(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == AbnormalEffectChangedEvent::name)
if (evt.GetName() == Events::AbnormalEffectChangedEvent::name)
{
const auto casted = static_cast<const AbnormalEffectChangedEvent&>(evt);
const auto casted = static_cast<const Events::AbnormalEffectChangedEvent&>(evt);
const auto &actualIds = Create(casted.GetSkillInfo());
Delete(actualIds);

View File

@@ -3,8 +3,8 @@
#include <vector>
#include <shared_mutex>
#include "../Factories/ChatMessageFactory.h"
#include "../../../Events/ChatMessageCreatedEvent.h"
#include "../../../Services/ServiceLocator.h"
#include "Domain/Events/ChatMessageCreatedEvent.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -28,7 +28,7 @@ namespace Interlude
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ChatMessageCreatedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::ChatMessageCreatedEvent::name, [this](const Events::Event& evt) {
OnMessageCreated(evt);
});
}
@@ -38,12 +38,12 @@ namespace Interlude
{
}
void OnMessageCreated(const Event& evt)
void OnMessageCreated(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ChatMessageCreatedEvent::name)
if (evt.GetName() == Events::ChatMessageCreatedEvent::name)
{
const auto casted = static_cast<const ChatMessageCreatedEvent&>(evt);
const auto casted = static_cast<const Events::ChatMessageCreatedEvent&>(evt);
const auto message = m_Factory.Create(casted.GetChatMessage());
m_Messages[message->GetId()] = message;

View File

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

View File

@@ -6,13 +6,13 @@
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/ItemFactory.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Events/ItemCreatedEvent.h"
#include "../../../Events/ItemUpdatedEvent.h"
#include "../../../Events/ItemDeletedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/ItemAutousedEvent.h"
#include "../../../Events/OnEndItemListEvent.h"
#include "../../../Services/ServiceLocator.h"
#include "Domain/Events/ItemCreatedEvent.h"
#include "Domain/Events/ItemUpdatedEvent.h"
#include "Domain/Events/ItemDeletedEvent.h"
#include "Domain/Events/HeroDeletedEvent.h"
#include "Domain/Events/ItemAutousedEvent.h"
#include "Domain/Events/OnEndItemListEvent.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -46,10 +46,10 @@ namespace Interlude
{
}
void OnEndItemList(const Event& evt)
void OnEndItemList(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == OnEndItemListEvent::name)
if (evt.GetName() == Events::OnEndItemListEvent::name)
{
for (auto it = m_Items.begin(); it != m_Items.end();)
{
@@ -67,21 +67,21 @@ namespace Interlude
}
}
void OnHeroDeleted(const Event& evt)
void OnHeroDeleted(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == HeroDeletedEvent::name)
if (evt.GetName() == Events::HeroDeletedEvent::name)
{
Reset();
}
}
void OnItemAutoused(const Event& evt)
void OnItemAutoused(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemAutousedEvent::name)
if (evt.GetName() == Events::ItemAutousedEvent::name)
{
const auto casted = static_cast<const ItemAutousedEvent&>(evt);
const auto casted = static_cast<const Events::ItemAutousedEvent&>(evt);
const auto& data = casted.GetAutouseInfo();
const auto itemId = data[0];
@@ -104,10 +104,10 @@ namespace Interlude
}
}
void OnItemCreated(const Event& evt)
void OnItemCreated(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemCreatedEvent::name)
if (evt.GetName() == Events::ItemCreatedEvent::name)
{
if (m_IsNewCycle)
{
@@ -115,7 +115,7 @@ namespace Interlude
m_NewItems.clear();
}
const auto casted = static_cast<const ItemCreatedEvent&>(evt);
const auto casted = static_cast<const Events::ItemCreatedEvent&>(evt);
const auto& data = casted.GetItemData();
@@ -135,12 +135,12 @@ namespace Interlude
}
}
void OnItemUpdated(const Event& evt)
void OnItemUpdated(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemUpdatedEvent::name)
if (evt.GetName() == Events::ItemUpdatedEvent::name)
{
const auto casted = static_cast<const ItemUpdatedEvent&>(evt);
const auto casted = static_cast<const Events::ItemUpdatedEvent&>(evt);
const auto& data = casted.GetItemData();
//todo exception?
@@ -153,13 +153,13 @@ namespace Interlude
}
}
void OnItemDeleted(const Event& evt)
void OnItemDeleted(const Events::Event& evt)
{
//fixme may be a race condition
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemDeletedEvent::name)
if (evt.GetName() == Events::ItemDeletedEvent::name)
{
const auto casted = static_cast<const ItemDeletedEvent&>(evt);
const auto casted = static_cast<const Events::ItemDeletedEvent&>(evt);
m_Items.erase(casted.GetObjectId());
}
@@ -182,22 +182,22 @@ namespace Interlude
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::ItemCreatedEvent::name, [this](const Events::Event& evt) {
OnItemCreated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemUpdatedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::ItemUpdatedEvent::name, [this](const Events::Event& evt) {
OnItemUpdated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemDeletedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::ItemDeletedEvent::name, [this](const Events::Event& evt) {
OnItemDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::HeroDeletedEvent::name, [this](const Events::Event& evt) {
OnHeroDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemAutousedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::ItemAutousedEvent::name, [this](const Events::Event& evt) {
OnItemAutoused(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(OnEndItemListEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::OnEndItemListEvent::name, [this](const Events::Event& evt) {
OnEndItemList(evt);
});
}

View File

@@ -5,10 +5,10 @@
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/NPCFactory.h"
#include "../../../Events/SpoiledEvent.h"
#include "../../../Events/CreatureDiedEvent.h"
#include "Domain/Events/SpoiledEvent.h"
#include "Domain/Events/CreatureDiedEvent.h"
#include "../../GameStructs/FindObjectsTrait.h"
#include "../../../Services/ServiceLocator.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -57,10 +57,10 @@ namespace Interlude
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SpoiledEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::SpoiledEvent::name, [this](const Events::Event& evt) {
OnSpoiled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(CreatureDiedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::CreatureDiedEvent::name, [this](const Events::Event& evt) {
OnCreatureDied(evt);
});
}
@@ -75,12 +75,12 @@ namespace Interlude
NPCRepository() = delete;
virtual ~NPCRepository() = default;
void OnSpoiled(const Event& evt)
void OnSpoiled(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == SpoiledEvent::name)
if (evt.GetName() == Events::SpoiledEvent::name)
{
const auto casted = static_cast<const SpoiledEvent&>(evt);
const auto casted = static_cast<const Events::SpoiledEvent&>(evt);
const auto hero = m_NetworkHandler.GetHero();
if (hero && hero->pawn && hero->pawn->lineagePlayerController)
{
@@ -93,12 +93,12 @@ namespace Interlude
}
}
void OnCreatureDied(const Event& evt)
void OnCreatureDied(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == CreatureDiedEvent::name)
if (evt.GetName() == Events::CreatureDiedEvent::name)
{
const auto casted = static_cast<const CreatureDiedEvent&>(evt);
const auto casted = static_cast<const Events::CreatureDiedEvent&>(evt);
if (m_Spoiled.find(casted.GetCreatureId()) != m_Spoiled.end())
{
const auto isSweepable = casted.GetCreatureInfo()[4] != 0;

View File

@@ -5,15 +5,15 @@
#include <shared_mutex>
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/SkillFactory.h"
#include "../../../Events/SkillCreatedEvent.h"
#include "../../../Events/SkillUsedEvent.h"
#include "../../../Events/SkillCancelledEvent.h"
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/GameEngineTickedEvent.h"
#include "Domain/Events/SkillCreatedEvent.h"
#include "Domain/Events/SkillUsedEvent.h"
#include "Domain/Events/SkillCancelledEvent.h"
#include "Domain/Events/AbnormalEffectChangedEvent.h"
#include "Domain/Events/HeroDeletedEvent.h"
#include "Domain/Events/GameEngineTickedEvent.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Common/TimerMap.h"
#include "../../../Services/ServiceLocator.h"
#include "Domain/Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -53,30 +53,30 @@ namespace Interlude
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillCreatedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::SkillCreatedEvent::name, [this](const Events::Event& evt) {
OnSkillCreated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillUsedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::SkillUsedEvent::name, [this](const Events::Event& evt) {
OnSkillUsed(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillCancelledEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::SkillCancelledEvent::name, [this](const Events::Event& evt) {
OnSkillCancelled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::AbnormalEffectChangedEvent::name, [this](const Events::Event& evt) {
OnSkillToggled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::HeroDeletedEvent::name, [this](const Events::Event& evt) {
OnHeroDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(GameEngineTickedEvent::name, [this](const Event& evt) {
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::GameEngineTickedEvent::name, [this](const Events::Event& evt) {
OnGameEngineTicked(evt);
});
}
void OnGameEngineTicked(const Event& evt)
void OnGameEngineTicked(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == GameEngineTickedEvent::name)
if (evt.GetName() == Events::GameEngineTickedEvent::name)
{
for (auto it = m_Skills.begin(); it != m_Skills.end();)
{
@@ -94,10 +94,10 @@ namespace Interlude
}
}
void OnHeroDeleted(const Event& evt)
void OnHeroDeleted(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == HeroDeletedEvent::name)
if (evt.GetName() == Events::HeroDeletedEvent::name)
{
Reset();
m_CastingTimers.StopAll();
@@ -106,10 +106,10 @@ namespace Interlude
}
//todo need to delete skills if they are not exists in create "queue"
void OnSkillCreated(const Event& evt)
void OnSkillCreated(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == SkillCreatedEvent::name)
if (evt.GetName() == Events::SkillCreatedEvent::name)
{
if (m_IsNewCycle)
{
@@ -117,7 +117,7 @@ namespace Interlude
m_NewSkills.clear();
}
const auto casted = static_cast<const SkillCreatedEvent&>(evt);
const auto casted = static_cast<const Events::SkillCreatedEvent&>(evt);
const auto skillInfo = casted.GetSkillInfo();
const auto skillId = skillInfo[2];
@@ -142,12 +142,12 @@ namespace Interlude
m_NewSkills[skillId] = skillId;
}
}
void OnSkillUsed(const Event& evt)
void OnSkillUsed(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == SkillUsedEvent::name)
if (evt.GetName() == Events::SkillUsedEvent::name)
{
const auto casted = static_cast<const SkillUsedEvent&>(evt);
const auto casted = static_cast<const Events::SkillUsedEvent&>(evt);
const auto skillInfo = casted.GetSkillInfo();
const auto skillId = skillInfo[0];
@@ -174,12 +174,12 @@ namespace Interlude
});
}
}
void OnSkillCancelled(const Event& evt)
void OnSkillCancelled(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == SkillCancelledEvent::name)
if (evt.GetName() == Events::SkillCancelledEvent::name)
{
const auto casted = static_cast<const SkillCancelledEvent&>(evt);
const auto casted = static_cast<const Events::SkillCancelledEvent&>(evt);
const auto hero = m_NetworkHandler.GetHero();
@@ -198,12 +198,12 @@ namespace Interlude
}
}
}
void OnSkillToggled(const Event& evt)
void OnSkillToggled(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == AbnormalEffectChangedEvent::name)
if (evt.GetName() == Events::AbnormalEffectChangedEvent::name)
{
const auto casted = static_cast<const AbnormalEffectChangedEvent&>(evt);
const auto casted = static_cast<const Events::AbnormalEffectChangedEvent&>(evt);
const auto skillInfo = casted.GetSkillInfo();
std::unordered_map<uint32_t, int32_t> ids;