feat: use unique ptr

This commit is contained in:
k0t9i 2023-01-23 13:32:10 +04:00
parent 7637260d19
commit c01d1c3549
10 changed files with 60 additions and 40 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <cstdint>
#include <memory>
#include "../Entities/EntityInterface.h" #include "../Entities/EntityInterface.h"
#include "../Enums/EntityStateEnum.h" #include "../Enums/EntityStateEnum.h"
@ -8,10 +10,27 @@ namespace L2Bot::Domain::DTO
class EntityState class EntityState
{ {
public: public:
Entities::EntityInterface* GetEntity() const const uint32_t GetId() const
{ {
return m_Entity; return m_Entity->GetId();
} }
const std::vector<Serializers::Node> BuildSerializationNodes() const
{
return m_Entity->BuildSerializationNodes();
}
void SaveEntityState()
{
m_Entity->SaveState();
}
const bool IsEntityEqual(const Entities::EntityInterface* other) const
{
return m_Entity->IsEqual(other);
}
void UpdateEntity(const Entities::EntityInterface* other)
{
m_Entity->Update(other);
}
const Enums::EntityStateEnum GetState() const const Enums::EntityStateEnum GetState() const
{ {
return m_State; return m_State;
@ -21,23 +40,17 @@ namespace L2Bot::Domain::DTO
m_State = state; m_State = state;
} }
EntityState(Entities::EntityInterface* object, Enums::EntityStateEnum state) : EntityState(std::unique_ptr<Entities::EntityInterface> object, Enums::EntityStateEnum state) :
m_Entity(object), m_Entity(std::move(object)),
m_State(state) m_State(state)
{ {
} }
EntityState() = default; EntityState() = default;
virtual ~EntityState() virtual ~EntityState() = default;
{
if (m_Entity != nullptr)
{
delete m_Entity;
}
}
private: private:
Entities::EntityInterface* m_Entity = nullptr; std::unique_ptr<Entities::EntityInterface> m_Entity = nullptr;
Enums::EntityStateEnum m_State = Enums::EntityStateEnum::none; Enums::EntityStateEnum m_State = Enums::EntityStateEnum::none;
}; };
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <memory>
#include "../Serializers/Serializable.h" #include "../Serializers/Serializable.h"
namespace L2Bot::Domain::Entities namespace L2Bot::Domain::Entities

View File

@ -33,7 +33,7 @@ namespace L2Bot::Domain::Serializers
result.push_back( result.push_back(
{ {
m_ContainerName, m_ContainerName,
std::vector<Serializers::Node>{ { operationName, kvp->GetEntity()->BuildSerializationNodes() } } std::vector<Serializers::Node>{ { operationName, kvp->BuildSerializationNodes() } }
} }
); );
} }

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <functional> #include <functional>
#include <memory>
#include "Domain/Repositories/EntityRepositoryInterface.h" #include "Domain/Repositories/EntityRepositoryInterface.h"
#include "Domain/DTO/EntityState.h" #include "Domain/DTO/EntityState.h"
@ -11,40 +12,40 @@ class EntityHandler
{ {
public: public:
template<typename T> template<typename T>
const std::map<uint32_t, DTO::EntityState*> GetEntities(const std::map<uint32_t, T> items, std::function<Entities::EntityInterface*(T)> callback) const std::map<uint32_t, DTO::EntityState*> GetEntities(const std::map<uint32_t, T> items, std::function<std::unique_ptr<Entities::EntityInterface>(T)> callback)
{ {
RemoveOutdatedStates(); RemoveOutdatedStates();
for (const auto& kvp : items) for (const auto& kvp : items)
{ {
const auto item = kvp.second; const auto item = kvp.second;
const auto newObject = callback(item); auto newObject = callback(item);
if (m_Objects.contains(newObject->GetId())) if (m_Objects.contains(newObject->GetId()))
{ {
if (!m_Objects[kvp.first]->GetEntity()->IsEqual(newObject)) { if (!m_Objects[kvp.first]->IsEntityEqual(newObject.get())) {
m_Objects[kvp.first]->GetEntity()->Update(newObject); m_Objects[kvp.first]->UpdateEntity(newObject.get());
m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::updated); m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::updated);
} }
else else
{ {
m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::none); m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::none);
} }
delete newObject; //delete newObject;
} }
else else
{ {
m_Objects.emplace( m_Objects.emplace(
newObject->GetId(), newObject->GetId(),
new DTO::EntityState{ newObject, Enums::EntityStateEnum::created } new DTO::EntityState{ std::move(newObject), Enums::EntityStateEnum::created }
); );
} }
} }
for (auto& kvp : m_Objects) for (auto& kvp : m_Objects)
{ {
if (!items.contains(kvp.second->GetEntity()->GetId())) if (!items.contains(kvp.second->GetId()))
{ {
m_Objects[kvp.first]->GetEntity()->SaveState(); m_Objects[kvp.first]->SaveEntityState();
kvp.second->UpdateState(Enums::EntityStateEnum::deleted); kvp.second->UpdateState(Enums::EntityStateEnum::deleted);
} }
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include "../GameStructs/L2GameDataWrapper.h" #include "../GameStructs/L2GameDataWrapper.h"
#include "../GameStructs/FName.h" #include "../GameStructs/FName.h"
#include "../GameStructs/GameStructs.h" #include "../GameStructs/GameStructs.h"
@ -20,13 +21,13 @@ namespace Interlude
DropFactory() = delete; DropFactory() = delete;
virtual ~DropFactory() = default; virtual ~DropFactory() = default;
Entities::EntityInterface* Create(const Item* item) const std::unique_ptr<Entities::EntityInterface> 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 new Entities::Drop{ return std::make_unique<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),
@ -42,7 +43,7 @@ namespace Interlude
item->amount, item->amount,
nameEntry ? ConvertFromWideChar(nameEntry->value) : "", nameEntry ? ConvertFromWideChar(nameEntry->value) : "",
iconEntry ? ConvertFromWideChar(iconEntry->value) : "" iconEntry ? ConvertFromWideChar(iconEntry->value) : ""
}; );
} }
private: private:

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include "../GameStructs/GameStructs.h" #include "../GameStructs/GameStructs.h"
#include "../../../Common/Common.h" #include "../../../Common/Common.h"
#include "Domain/Entities/Hero.h" #include "Domain/Entities/Hero.h"
@ -12,11 +13,11 @@ namespace Interlude
HeroFactory() = default; HeroFactory() = default;
virtual ~HeroFactory() = default; virtual ~HeroFactory() = default;
Entities::EntityInterface* Create(const User* item) const std::unique_ptr<Entities::EntityInterface> Create(const User* item) const
{ {
const auto playerController = item->pawn ? item->pawn->lineagePlayerController : nullptr; const auto playerController = item->pawn ? item->pawn->lineagePlayerController : nullptr;
return new Entities::Hero{ return std::make_unique<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),
@ -81,7 +82,7 @@ namespace Interlude
), ),
playerController ? playerController->targetObjectId : 0, playerController ? playerController->targetObjectId : 0,
playerController ? playerController->isStanding == 1 : true playerController ? playerController->isStanding == 1 : true
}; );
} }
}; };
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include "../../../Common/Common.h" #include "../../../Common/Common.h"
#include "Domain/Entities/NPC.h" #include "Domain/Entities/NPC.h"
@ -11,9 +12,9 @@ namespace Interlude
NPCFactory() = default; NPCFactory() = default;
virtual ~NPCFactory() = default; virtual ~NPCFactory() = default;
Entities::EntityInterface* Create(const User* item, const Enums::SpoilStateEnum spoiledState) const std::unique_ptr<Entities::EntityInterface> Create(const User* item, const Enums::SpoilStateEnum spoiledState) const
{ {
return new Entities::NPC{ return std::make_unique<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),
@ -37,7 +38,7 @@ namespace Interlude
item->maxMp, item->mp, item->maxMp, item->mp,
item->maxCp, item->cp item->maxCp, item->cp
) )
}; );
} }
}; };
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include "../../../Common/Common.h" #include "../../../Common/Common.h"
#include "Domain/Entities/Player.h" #include "Domain/Entities/Player.h"
@ -11,9 +12,9 @@ namespace Interlude
PlayerFactory() = default; PlayerFactory() = default;
virtual ~PlayerFactory() = default; virtual ~PlayerFactory() = default;
Entities::EntityInterface* Create(const User* item) const std::unique_ptr<Entities::EntityInterface> Create(const User* item) const
{ {
return new Entities::Player{ return std::make_unique<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),
@ -34,8 +35,8 @@ namespace Interlude
item->gender == L2::Gender::MALE, item->gender == L2::Gender::MALE,
(Enums::ClassEnum)item->classId, (Enums::ClassEnum)item->classId,
(Enums::ClassEnum)item->activeClassId (Enums::ClassEnum)item->activeClassId
), )
}; );
} }
}; };
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include <map> #include <map>
#include <chrono> #include <chrono>
#include "../GameStructs/L2GameDataWrapper.h" #include "../GameStructs/L2GameDataWrapper.h"
@ -23,7 +24,7 @@ namespace Interlude
SkillFactory() = delete; SkillFactory() = delete;
virtual ~SkillFactory() = default; virtual ~SkillFactory() = default;
Entities::Skill* Create(const uint32_t skillId, const uint32_t level, const uint32_t isActive) const std::unique_ptr<Entities::EntityInterface> 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);
@ -33,8 +34,7 @@ namespace Interlude
const auto description = data ? data->description : L""; const auto description = data ? data->description : L"";
const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr; const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr;
return new Entities::Skill return std::make_unique<Entities::Skill>(
{
skillId, skillId,
static_cast<uint8_t>(level), static_cast<uint8_t>(level),
isActive != 1, isActive != 1,
@ -46,7 +46,7 @@ namespace Interlude
false, false,
false, false,
false false
}; );
} }
private: private:

View File

@ -27,7 +27,7 @@ namespace Interlude
std::unique_lock<std::shared_timed_mutex>(m_Mutex); std::unique_lock<std::shared_timed_mutex>(m_Mutex);
const auto objects = m_EntityHandler.GetEntities<Entities::Skill*>(m_Skills, [this](Entities::Skill* item) { const auto objects = m_EntityHandler.GetEntities<Entities::Skill*>(m_Skills, [this](Entities::Skill* item) {
return new Entities::Skill(item); return std::make_unique<Entities::Skill>(item);
}); });
auto result = std::vector<DTO::EntityState*>(); auto result = std::vector<DTO::EntityState*>();
@ -106,7 +106,8 @@ namespace Interlude
skillInfo[0] skillInfo[0]
); );
m_Skills.emplace(skill->GetId(), skill); auto test = static_cast<Entities::Skill*>(skill.get());
m_Skills.emplace(skill->GetId(), test);
} }
else else
{ {