diff --git a/L2BotCore/Domain/DTO/EntityState.h b/L2BotCore/Domain/DTO/EntityState.h index dff5e9b..1ae7b18 100644 --- a/L2BotCore/Domain/DTO/EntityState.h +++ b/L2BotCore/Domain/DTO/EntityState.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include "../Entities/EntityInterface.h" #include "../Enums/EntityStateEnum.h" @@ -8,10 +10,27 @@ namespace L2Bot::Domain::DTO class EntityState { public: - Entities::EntityInterface* GetEntity() const + const uint32_t GetId() const { - return m_Entity; + return m_Entity->GetId(); } + const std::vector 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 { return m_State; @@ -21,23 +40,17 @@ namespace L2Bot::Domain::DTO m_State = state; } - EntityState(Entities::EntityInterface* object, Enums::EntityStateEnum state) : - m_Entity(object), + EntityState(std::unique_ptr object, Enums::EntityStateEnum state) : + m_Entity(std::move(object)), m_State(state) { } EntityState() = default; - virtual ~EntityState() - { - if (m_Entity != nullptr) - { - delete m_Entity; - } - } + virtual ~EntityState() = default; private: - Entities::EntityInterface* m_Entity = nullptr; + std::unique_ptr m_Entity = nullptr; Enums::EntityStateEnum m_State = Enums::EntityStateEnum::none; }; } \ No newline at end of file diff --git a/L2BotCore/Domain/Entities/EntityInterface.h b/L2BotCore/Domain/Entities/EntityInterface.h index c6930e6..34d1b15 100644 --- a/L2BotCore/Domain/Entities/EntityInterface.h +++ b/L2BotCore/Domain/Entities/EntityInterface.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "../Serializers/Serializable.h" namespace L2Bot::Domain::Entities diff --git a/L2BotCore/Domain/Serializers/SerializableStateContainer.h b/L2BotCore/Domain/Serializers/SerializableStateContainer.h index aed4474..0c6fc3f 100644 --- a/L2BotCore/Domain/Serializers/SerializableStateContainer.h +++ b/L2BotCore/Domain/Serializers/SerializableStateContainer.h @@ -33,7 +33,7 @@ namespace L2Bot::Domain::Serializers result.push_back( { m_ContainerName, - std::vector{ { operationName, kvp->GetEntity()->BuildSerializationNodes() } } + std::vector{ { operationName, kvp->BuildSerializationNodes() } } } ); } diff --git a/L2BotDll/Services/EntityHandler.h b/L2BotDll/Services/EntityHandler.h index d7223a1..799c0ce 100644 --- a/L2BotDll/Services/EntityHandler.h +++ b/L2BotDll/Services/EntityHandler.h @@ -2,6 +2,7 @@ #include #include +#include #include "Domain/Repositories/EntityRepositoryInterface.h" #include "Domain/DTO/EntityState.h" @@ -11,40 +12,40 @@ class EntityHandler { public: template - const std::map GetEntities(const std::map items, std::function callback) + const std::map GetEntities(const std::map items, std::function(T)> callback) { RemoveOutdatedStates(); for (const auto& kvp : items) { const auto item = kvp.second; - const auto newObject = callback(item); + auto newObject = callback(item); if (m_Objects.contains(newObject->GetId())) { - if (!m_Objects[kvp.first]->GetEntity()->IsEqual(newObject)) { - m_Objects[kvp.first]->GetEntity()->Update(newObject); + if (!m_Objects[kvp.first]->IsEntityEqual(newObject.get())) { + m_Objects[kvp.first]->UpdateEntity(newObject.get()); m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::updated); } else { m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::none); } - delete newObject; + //delete newObject; } else { m_Objects.emplace( newObject->GetId(), - new DTO::EntityState{ newObject, Enums::EntityStateEnum::created } + new DTO::EntityState{ std::move(newObject), Enums::EntityStateEnum::created } ); } } 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); } } diff --git a/L2BotDll/Versions/Interlude/Factories/DropFactory.h b/L2BotDll/Versions/Interlude/Factories/DropFactory.h index 344ae2b..cc43b2b 100644 --- a/L2BotDll/Versions/Interlude/Factories/DropFactory.h +++ b/L2BotDll/Versions/Interlude/Factories/DropFactory.h @@ -1,5 +1,6 @@ #pragma once +#include #include "../GameStructs/L2GameDataWrapper.h" #include "../GameStructs/FName.h" #include "../GameStructs/GameStructs.h" @@ -20,13 +21,13 @@ namespace Interlude DropFactory() = delete; virtual ~DropFactory() = default; - Entities::EntityInterface* Create(const Item* item) const + std::unique_ptr Create(const Item* item) const { const auto itemData = m_L2GameData.GetItemData(item->itemId); const auto nameEntry = itemData ? m_FName.GetEntry(itemData->nameIndex) : nullptr; const auto iconEntry = itemData ? m_FName.GetEntry(itemData->iconNameIndex) : nullptr; - return new Entities::Drop{ + return std::make_unique( item->objectId, ValueObjects::Transform( ValueObjects::Vector3(item->pawn->Location.x, item->pawn->Location.y, item->pawn->Location.z), @@ -42,7 +43,7 @@ namespace Interlude item->amount, nameEntry ? ConvertFromWideChar(nameEntry->value) : "", iconEntry ? ConvertFromWideChar(iconEntry->value) : "" - }; + ); } private: diff --git a/L2BotDll/Versions/Interlude/Factories/HeroFactory.h b/L2BotDll/Versions/Interlude/Factories/HeroFactory.h index 769f574..2c1d86c 100644 --- a/L2BotDll/Versions/Interlude/Factories/HeroFactory.h +++ b/L2BotDll/Versions/Interlude/Factories/HeroFactory.h @@ -1,5 +1,6 @@ #pragma once +#include #include "../GameStructs/GameStructs.h" #include "../../../Common/Common.h" #include "Domain/Entities/Hero.h" @@ -12,11 +13,11 @@ namespace Interlude HeroFactory() = default; virtual ~HeroFactory() = default; - Entities::EntityInterface* Create(const User* item) const + std::unique_ptr Create(const User* item) const { const auto playerController = item->pawn ? item->pawn->lineagePlayerController : nullptr; - return new Entities::Hero{ + return std::make_unique( item->objectId, ValueObjects::Transform( 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->isStanding == 1 : true - }; + ); } }; } \ No newline at end of file diff --git a/L2BotDll/Versions/Interlude/Factories/NPCFactory.h b/L2BotDll/Versions/Interlude/Factories/NPCFactory.h index 78fd5bd..29bf271 100644 --- a/L2BotDll/Versions/Interlude/Factories/NPCFactory.h +++ b/L2BotDll/Versions/Interlude/Factories/NPCFactory.h @@ -1,5 +1,6 @@ #pragma once +#include #include "../../../Common/Common.h" #include "Domain/Entities/NPC.h" @@ -11,9 +12,9 @@ namespace Interlude NPCFactory() = default; virtual ~NPCFactory() = default; - Entities::EntityInterface* Create(const User* item, const Enums::SpoilStateEnum spoiledState) const + std::unique_ptr Create(const User* item, const Enums::SpoilStateEnum spoiledState) const { - return new Entities::NPC{ + return std::make_unique( item->objectId, ValueObjects::Transform( 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->maxCp, item->cp ) - }; + ); } }; } \ No newline at end of file diff --git a/L2BotDll/Versions/Interlude/Factories/PlayerFactory.h b/L2BotDll/Versions/Interlude/Factories/PlayerFactory.h index 35f6315..17a6e45 100644 --- a/L2BotDll/Versions/Interlude/Factories/PlayerFactory.h +++ b/L2BotDll/Versions/Interlude/Factories/PlayerFactory.h @@ -1,5 +1,6 @@ #pragma once +#include #include "../../../Common/Common.h" #include "Domain/Entities/Player.h" @@ -11,9 +12,9 @@ namespace Interlude PlayerFactory() = default; virtual ~PlayerFactory() = default; - Entities::EntityInterface* Create(const User* item) const + std::unique_ptr Create(const User* item) const { - return new Entities::Player{ + return std::make_unique( item->objectId, ValueObjects::Transform( 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, (Enums::ClassEnum)item->classId, (Enums::ClassEnum)item->activeClassId - ), - }; + ) + ); } }; } \ No newline at end of file diff --git a/L2BotDll/Versions/Interlude/Factories/SkillFactory.h b/L2BotDll/Versions/Interlude/Factories/SkillFactory.h index 2f14e76..3d14ae3 100644 --- a/L2BotDll/Versions/Interlude/Factories/SkillFactory.h +++ b/L2BotDll/Versions/Interlude/Factories/SkillFactory.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include "../GameStructs/L2GameDataWrapper.h" @@ -23,7 +24,7 @@ namespace Interlude SkillFactory() = delete; virtual ~SkillFactory() = default; - Entities::Skill* Create(const uint32_t skillId, const uint32_t level, const uint32_t isActive) const + std::unique_ptr Create(const uint32_t skillId, const uint32_t level, const uint32_t isActive) const { const auto data = m_L2GameData.GetMSData(skillId, level); @@ -33,8 +34,7 @@ namespace Interlude const auto description = data ? data->description : L""; const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr; - return new Entities::Skill - { + return std::make_unique( skillId, static_cast(level), isActive != 1, @@ -46,7 +46,7 @@ namespace Interlude false, false, false - }; + ); } private: diff --git a/L2BotDll/Versions/Interlude/Repositories/SkillRepository.h b/L2BotDll/Versions/Interlude/Repositories/SkillRepository.h index 74fde19..1b0eb12 100644 --- a/L2BotDll/Versions/Interlude/Repositories/SkillRepository.h +++ b/L2BotDll/Versions/Interlude/Repositories/SkillRepository.h @@ -27,7 +27,7 @@ namespace Interlude std::unique_lock(m_Mutex); const auto objects = m_EntityHandler.GetEntities(m_Skills, [this](Entities::Skill* item) { - return new Entities::Skill(item); + return std::make_unique(item); }); auto result = std::vector(); @@ -106,7 +106,8 @@ namespace Interlude skillInfo[0] ); - m_Skills.emplace(skill->GetId(), skill); + auto test = static_cast(skill.get()); + m_Skills.emplace(skill->GetId(), test); } else {