refactor: replace unique_ptr to shared_ptr

This commit is contained in:
k0t9i
2023-02-10 18:49:47 +04:00
parent bb4538794b
commit 81e26a7e52
13 changed files with 51 additions and 67 deletions

View File

@@ -10,7 +10,7 @@ namespace L2Bot::Domain::DTO
class EntityState class EntityState
{ {
public: public:
const std::unique_ptr<Entities::EntityInterface>& GetEntity() const const std::shared_ptr<Entities::EntityInterface>& GetEntity() const
{ {
return m_Entity; return m_Entity;
} }
@@ -23,8 +23,8 @@ namespace L2Bot::Domain::DTO
m_State = state; m_State = state;
} }
EntityState(std::unique_ptr<Entities::EntityInterface> object, Enums::EntityStateEnum state) : EntityState(std::shared_ptr<Entities::EntityInterface> object, Enums::EntityStateEnum state) :
m_Entity(std::move(object)), m_Entity(object),
m_State(state) m_State(state)
{ {
@@ -33,7 +33,7 @@ namespace L2Bot::Domain::DTO
EntityState() = default; EntityState() = default;
virtual ~EntityState() = default; virtual ~EntityState() = default;
private: private:
std::unique_ptr<Entities::EntityInterface> m_Entity = nullptr; std::shared_ptr<Entities::EntityInterface> m_Entity = nullptr;
Enums::EntityStateEnum m_State = Enums::EntityStateEnum::none; Enums::EntityStateEnum m_State = Enums::EntityStateEnum::none;
}; };
} }

View File

@@ -98,7 +98,8 @@ namespace L2Bot::Domain::Entities
EtcItem(const EtcItem* other) : EtcItem(const EtcItem* other) :
BaseItem(other), BaseItem(other),
m_Amount(other->m_Amount), m_Amount(other->m_Amount),
m_IsAutoused(other->m_IsAutoused) m_IsAutoused(other->m_IsAutoused),
m_IsQuest(other->m_IsQuest)
{ {
} }

View File

@@ -12,7 +12,7 @@ class EntityFinder
{ {
public: public:
template<typename T> template<typename T>
const std::map<uint32_t, std::shared_ptr<DTO::EntityState>>& FindEntities(const std::map<uint32_t, T> items, std::function<std::unique_ptr<Entities::EntityInterface>(T)> callback) const std::map<uint32_t, std::shared_ptr<DTO::EntityState>>& FindEntities(const std::map<uint32_t, T> items, std::function<std::shared_ptr<Entities::EntityInterface>(T)> callback)
{ {
RemoveOutdatedStates(); RemoveOutdatedStates();
@@ -36,7 +36,7 @@ public:
const auto objectId = newObject->GetId(); const auto objectId = newObject->GetId();
m_Objects.emplace( m_Objects.emplace(
objectId, objectId,
std::make_shared<DTO::EntityState>(std::move(newObject), Enums::EntityStateEnum::created) std::make_shared<DTO::EntityState>(newObject, Enums::EntityStateEnum::created)
); );
} }
} }

View File

@@ -24,7 +24,7 @@ namespace Interlude
AbnormalEffectFactory() = delete; AbnormalEffectFactory() = delete;
virtual ~AbnormalEffectFactory() = default; virtual ~AbnormalEffectFactory() = default;
std::unique_ptr<Entities::AbnormalEffect> Create(const uint32_t skillId, const uint32_t level) const std::shared_ptr<Entities::AbnormalEffect> Create(const uint32_t skillId, const uint32_t level) const
{ {
const auto data = m_L2GameData.GetMSData(skillId, level); const auto data = m_L2GameData.GetMSData(skillId, level);
@@ -32,7 +32,7 @@ namespace Interlude
const auto description = data && data->description ? data->description : L""; const auto description = data && data->description ? data->description : L"";
const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr; const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr;
return std::make_unique<Entities::AbnormalEffect>( return std::make_shared<Entities::AbnormalEffect>(
skillId, skillId,
static_cast<uint8_t>(level), static_cast<uint8_t>(level),
std::wstring(name), std::wstring(name),

View File

@@ -21,13 +21,13 @@ namespace Interlude
DropFactory() = delete; DropFactory() = delete;
virtual ~DropFactory() = default; virtual ~DropFactory() = default;
std::unique_ptr<Entities::Drop> Create(const Item* item) const std::shared_ptr<Entities::Drop> Create(const Item* item) const
{ {
const auto itemData = m_L2GameData.GetItemData(item->itemId); const auto itemData = m_L2GameData.GetItemData(item->itemId);
const auto nameEntry = itemData ? m_FName.GetEntry(itemData->nameIndex) : nullptr; const auto nameEntry = itemData ? m_FName.GetEntry(itemData->nameIndex) : nullptr;
const auto iconEntry = itemData ? m_FName.GetEntry(itemData->iconNameIndex) : nullptr; const auto iconEntry = itemData ? m_FName.GetEntry(itemData->iconNameIndex) : nullptr;
return std::make_unique<Entities::Drop>( return std::make_shared<Entities::Drop>(
item->objectId, item->objectId,
ValueObjects::Transform( ValueObjects::Transform(
ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z), ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z),

View File

@@ -13,11 +13,11 @@ namespace Interlude
HeroFactory() = default; HeroFactory() = default;
virtual ~HeroFactory() = default; virtual ~HeroFactory() = default;
std::unique_ptr<Entities::Hero> Create(const User* item) const std::shared_ptr<Entities::Hero> Create(const User* item) const
{ {
const auto playerController = item->pawn ? item->pawn->lineagePlayerController : nullptr; const auto playerController = item->pawn ? item->pawn->lineagePlayerController : nullptr;
return std::make_unique<Entities::Hero>( return std::make_shared<Entities::Hero>(
item->objectId, item->objectId,
ValueObjects::Transform( ValueObjects::Transform(
ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z), ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z),

View File

@@ -30,7 +30,7 @@ namespace Interlude
ItemFactory() = delete; ItemFactory() = delete;
virtual ~ItemFactory() = default; virtual ~ItemFactory() = default;
std::unique_ptr<Entities::BaseItem> Create(const ItemData& itemInfo) const std::shared_ptr<Entities::BaseItem> Create(const ItemData& itemInfo) const
{ {
//FIXME during first start data may be undefined //FIXME during first start data may be undefined
const auto data = m_L2GameData.GetItemData(itemInfo.itemId); const auto data = m_L2GameData.GetItemData(itemInfo.itemId);
@@ -55,42 +55,43 @@ namespace Interlude
return CreateEtc(itemInfo, data, name, icon, description); return CreateEtc(itemInfo, data, name, icon, description);
} }
std::unique_ptr<Entities::BaseItem> CreateFromPointer(const Entities::BaseItem* other) const std::shared_ptr<Entities::BaseItem> Copy(std::shared_ptr<Entities::BaseItem> other) const
{ {
auto otherPtr = other.get();
{ {
const auto object = dynamic_cast<const Entities::EtcItem*>(other); const auto object = dynamic_cast<const Entities::EtcItem*>(otherPtr);
if (object) if (object)
{ {
return std::make_unique<Entities::EtcItem>(object); return std::make_shared<Entities::EtcItem>(object);
} }
} }
{ {
const auto object = dynamic_cast<const Entities::ArmorItem*>(other); const auto object = dynamic_cast<const Entities::ArmorItem*>(otherPtr);
if (object) if (object)
{ {
return std::make_unique<Entities::ArmorItem>(object); return std::make_shared<Entities::ArmorItem>(object);
} }
} }
{ {
const auto object = dynamic_cast<const Entities::WeaponItem*>(other); const auto object = dynamic_cast<const Entities::WeaponItem*>(otherPtr);
if (object) if (object)
{ {
return std::make_unique<Entities::WeaponItem>(object); return std::make_shared<Entities::WeaponItem>(object);
} }
} }
{ {
const auto object = dynamic_cast<const Entities::ShieldItem*>(other); const auto object = dynamic_cast<const Entities::ShieldItem*>(otherPtr);
if (object) if (object)
{ {
return std::make_unique<Entities::ShieldItem>(object); return std::make_shared<Entities::ShieldItem>(object);
} }
} }
return std::make_unique<Entities::BaseItem>(other); return std::make_shared<Entities::BaseItem>(otherPtr);
} }
private: private:
std::unique_ptr<Entities::BaseItem> CreateEtc( std::shared_ptr<Entities::BaseItem> CreateEtc(
const ItemData& itemInfo, const ItemData& itemInfo,
const FL2ItemDataBase* itemData, const FL2ItemDataBase* itemData,
const std::wstring& name, const std::wstring& name,
@@ -98,7 +99,7 @@ namespace Interlude
const std::wstring& description const std::wstring& description
) const ) const
{ {
return std::make_unique<Entities::EtcItem>( return std::make_shared<Entities::EtcItem>(
itemInfo.objectId, itemInfo.objectId,
itemInfo.itemId, itemInfo.itemId,
itemInfo.mana, itemInfo.mana,
@@ -111,7 +112,7 @@ namespace Interlude
); );
} }
std::unique_ptr<Entities::BaseItem> CreateArmor( std::shared_ptr<Entities::BaseItem> CreateArmor(
const ItemData& itemInfo, const ItemData& itemInfo,
const FL2ItemDataBase* itemData, const FL2ItemDataBase* itemData,
const std::wstring& name, const std::wstring& name,
@@ -125,7 +126,7 @@ namespace Interlude
const auto addSetEffect = casted && casted->setEffect ? std::wstring(casted->setEffect) : L""; const auto addSetEffect = casted && casted->setEffect ? std::wstring(casted->setEffect) : L"";
const auto enchantEffect = casted && casted->enchantEffect ? std::wstring(casted->enchantEffect) : L""; const auto enchantEffect = casted && casted->enchantEffect ? std::wstring(casted->enchantEffect) : L"";
return std::make_unique<Entities::ArmorItem>( return std::make_shared<Entities::ArmorItem>(
itemInfo.objectId, itemInfo.objectId,
itemInfo.itemId, itemInfo.itemId,
itemInfo.mana, itemInfo.mana,
@@ -145,7 +146,7 @@ namespace Interlude
); );
} }
std::unique_ptr<Entities::BaseItem> CreateWeaponOrShield( std::shared_ptr<Entities::BaseItem> CreateWeaponOrShield(
const ItemData& itemInfo, const ItemData& itemInfo,
const FL2ItemDataBase* itemData, const FL2ItemDataBase* itemData,
const std::wstring& name, const std::wstring& name,
@@ -157,7 +158,7 @@ namespace Interlude
if (casted->weaponType != L2::WeaponType::SHIELD) if (casted->weaponType != L2::WeaponType::SHIELD)
{ {
return std::make_unique<Entities::WeaponItem>( return std::make_shared<Entities::WeaponItem>(
itemInfo.objectId, itemInfo.objectId,
itemInfo.itemId, itemInfo.itemId,
itemInfo.mana, itemInfo.mana,
@@ -181,7 +182,7 @@ namespace Interlude
); );
} }
return std::make_unique<Entities::ShieldItem>( return std::make_shared<Entities::ShieldItem>(
itemInfo.objectId, itemInfo.objectId,
itemInfo.itemId, itemInfo.itemId,
itemInfo.mana, itemInfo.mana,

View File

@@ -12,9 +12,9 @@ namespace Interlude
NPCFactory() = default; NPCFactory() = default;
virtual ~NPCFactory() = default; virtual ~NPCFactory() = default;
std::unique_ptr<Entities::NPC> Create(const User* item, const Enums::SpoilStateEnum spoiledState) const std::shared_ptr<Entities::NPC> Create(const User* item, const Enums::SpoilStateEnum spoiledState) const
{ {
return std::make_unique<Entities::NPC>( return std::make_shared<Entities::NPC>(
item->objectId, item->objectId,
ValueObjects::Transform( ValueObjects::Transform(
ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z), ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z),

View File

@@ -12,9 +12,9 @@ namespace Interlude
PlayerFactory() = default; PlayerFactory() = default;
virtual ~PlayerFactory() = default; virtual ~PlayerFactory() = default;
std::unique_ptr<Entities::Player> Create(const User* item) const std::shared_ptr<Entities::Player> Create(const User* item) const
{ {
return std::make_unique<Entities::Player>( return std::make_shared<Entities::Player>(
item->objectId, item->objectId,
ValueObjects::Transform( ValueObjects::Transform(
ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z), ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z),

View File

@@ -24,7 +24,7 @@ namespace Interlude
SkillFactory() = delete; SkillFactory() = delete;
virtual ~SkillFactory() = default; virtual ~SkillFactory() = default;
std::unique_ptr<Entities::Skill> Create(const uint32_t skillId, const uint32_t level, const uint32_t isActive) const std::shared_ptr<Entities::Skill> Create(const uint32_t skillId, const uint32_t level, const uint32_t isActive) const
{ {
const auto data = m_L2GameData.GetMSData(skillId, level); const auto data = m_L2GameData.GetMSData(skillId, level);
@@ -34,7 +34,7 @@ namespace Interlude
const auto description = data && data->description ? data->description : L""; const auto description = data && data->description ? data->description : L"";
const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr; const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr;
return std::make_unique<Entities::Skill>( return std::make_shared<Entities::Skill>(
skillId, skillId,
static_cast<uint8_t>(level), static_cast<uint8_t>(level),
isActive != 1, isActive != 1,

View File

@@ -22,14 +22,8 @@ namespace Interlude
{ {
std::unique_lock<std::shared_timed_mutex>(m_Mutex); std::unique_lock<std::shared_timed_mutex>(m_Mutex);
std::map<uint32_t, Entities::AbnormalEffect*> skillPtrs; const auto objects = m_EntityFinder.FindEntities<std::shared_ptr<Entities::AbnormalEffect>>(m_Effects, [this](std::shared_ptr<Entities::AbnormalEffect> item) {
for (const auto& kvp : m_Effects) return std::make_shared<Entities::AbnormalEffect>(item.get());
{
skillPtrs[kvp.first] = kvp.second.get();
}
const auto objects = m_EntityFinder.FindEntities<Entities::AbnormalEffect*>(skillPtrs, [this](Entities::AbnormalEffect* item) {
return std::make_unique<Entities::AbnormalEffect>(item);
}); });
auto result = std::vector<std::shared_ptr<DTO::EntityState>>(); auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
@@ -90,7 +84,7 @@ namespace Interlude
const auto level = skillInfo[i + 1]; const auto level = skillInfo[i + 1];
auto effect = m_Factory.Create(effectId, level); auto effect = m_Factory.Create(effectId, level);
m_Effects.emplace(effect->GetId(), std::move(effect)); m_Effects.emplace(effect->GetId(), effect);
ids[effectId] = effectId; ids[effectId] = effectId;
} }
@@ -113,7 +107,7 @@ namespace Interlude
private: private:
const AbnormalEffectFactory& m_Factory; const AbnormalEffectFactory& m_Factory;
std::map<uint32_t, std::unique_ptr<Entities::AbnormalEffect>> m_Effects; std::map<uint32_t, std::shared_ptr<Entities::AbnormalEffect>> m_Effects;
std::shared_timed_mutex m_Mutex; std::shared_timed_mutex m_Mutex;
EntityFinder& m_EntityFinder; EntityFinder& m_EntityFinder;
}; };

View File

@@ -26,14 +26,8 @@ namespace Interlude
{ {
std::unique_lock<std::shared_timed_mutex>(m_Mutex); std::unique_lock<std::shared_timed_mutex>(m_Mutex);
std::map<uint32_t, Entities::BaseItem*> itemPtrs; const auto objects = m_EntityFinder.FindEntities<std::shared_ptr<Entities::BaseItem>>(m_Items, [this](std::shared_ptr<Entities::BaseItem> item) {
for (const auto& kvp : m_Items) return m_Factory.Copy(item);
{
itemPtrs[kvp.first] = kvp.second.get();
}
const auto objects = m_EntityFinder.FindEntities<Entities::BaseItem*>(itemPtrs, [this](Entities::BaseItem* item) {
return m_Factory.CreateFromPointer(item);
}); });
auto result = std::vector<std::shared_ptr<DTO::EntityState>>(); auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
@@ -152,7 +146,7 @@ namespace Interlude
auto item = m_Factory.Create(data); auto item = m_Factory.Create(data);
if (m_Items.find(data.objectId) == m_Items.end()) if (m_Items.find(data.objectId) == m_Items.end())
{ {
m_Items.emplace(data.objectId, std::move(item)); m_Items.emplace(data.objectId, item);
} }
else else
{ {
@@ -211,7 +205,7 @@ namespace Interlude
private: private:
const ItemFactory& m_Factory; const ItemFactory& m_Factory;
std::map<uint32_t, std::unique_ptr<Entities::BaseItem>> m_Items; std::map<uint32_t, std::shared_ptr<Entities::BaseItem>> m_Items;
std::map<uint32_t, uint32_t> m_NewItems; std::map<uint32_t, uint32_t> m_NewItems;
bool m_IsNewCycle = true; bool m_IsNewCycle = true;
uint32_t m_UsedSkillId = 0; uint32_t m_UsedSkillId = 0;

View File

@@ -27,14 +27,8 @@ namespace Interlude
{ {
std::unique_lock<std::shared_timed_mutex>(m_Mutex); std::unique_lock<std::shared_timed_mutex>(m_Mutex);
std::map<uint32_t, Entities::Skill*> skillPtrs; const auto objects = m_EntityFinder.FindEntities<std::shared_ptr<Entities::Skill>>(m_Skills, [this](std::shared_ptr<Entities::Skill> item) {
for (const auto& kvp : m_Skills) return std::make_shared<Entities::Skill>(item.get());
{
skillPtrs[kvp.first] = kvp.second.get();
}
const auto objects = m_EntityFinder.FindEntities<Entities::Skill*>(skillPtrs, [this](Entities::Skill* item) {
return std::make_unique<Entities::Skill>(item);
}); });
auto result = std::vector<std::shared_ptr<DTO::EntityState>>(); auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
@@ -141,7 +135,7 @@ namespace Interlude
); );
if (m_Skills.find(skillId) == m_Skills.end()) if (m_Skills.find(skillId) == m_Skills.end())
{ {
m_Skills.emplace(skill->GetId(), std::move(skill)); m_Skills.emplace(skill->GetId(), skill);
} }
else else
{ {
@@ -245,7 +239,7 @@ namespace Interlude
private: private:
const SkillFactory& m_Factory; const SkillFactory& m_Factory;
std::map<uint32_t, std::unique_ptr<Entities::Skill>> m_Skills; std::map<uint32_t, std::shared_ptr<Entities::Skill>> m_Skills;
std::map<uint32_t, uint32_t> m_NewSkills; std::map<uint32_t, uint32_t> m_NewSkills;
bool m_IsNewCycle = true; bool m_IsNewCycle = true;
uint32_t m_UsedSkillId = 0; uint32_t m_UsedSkillId = 0;