feat: use unique ptr
This commit is contained in:
parent
7637260d19
commit
c01d1c3549
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#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<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
|
||||
{
|
||||
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<Entities::EntityInterface> 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<Entities::EntityInterface> m_Entity = nullptr;
|
||||
Enums::EntityStateEnum m_State = Enums::EntityStateEnum::none;
|
||||
};
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include "../Serializers/Serializable.h"
|
||||
|
||||
namespace L2Bot::Domain::Entities
|
||||
|
@ -33,7 +33,7 @@ namespace L2Bot::Domain::Serializers
|
||||
result.push_back(
|
||||
{
|
||||
m_ContainerName,
|
||||
std::vector<Serializers::Node>{ { operationName, kvp->GetEntity()->BuildSerializationNodes() } }
|
||||
std::vector<Serializers::Node>{ { operationName, kvp->BuildSerializationNodes() } }
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
#include "Domain/DTO/EntityState.h"
|
||||
|
||||
@ -11,40 +12,40 @@ class EntityHandler
|
||||
{
|
||||
public:
|
||||
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();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#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<Entities::EntityInterface> 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<Entities::Drop>(
|
||||
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:
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#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<Entities::EntityInterface> Create(const User* item) const
|
||||
{
|
||||
const auto playerController = item->pawn ? item->pawn->lineagePlayerController : nullptr;
|
||||
|
||||
return new Entities::Hero{
|
||||
return std::make_unique<Entities::Hero>(
|
||||
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
|
||||
};
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#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<Entities::EntityInterface> Create(const User* item, const Enums::SpoilStateEnum spoiledState) const
|
||||
{
|
||||
return new Entities::NPC{
|
||||
return std::make_unique<Entities::NPC>(
|
||||
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
|
||||
)
|
||||
};
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#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<Entities::EntityInterface> Create(const User* item) const
|
||||
{
|
||||
return new Entities::Player{
|
||||
return std::make_unique<Entities::Player>(
|
||||
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
|
||||
),
|
||||
};
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#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<Entities::EntityInterface> 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<Entities::Skill>(
|
||||
skillId,
|
||||
static_cast<uint8_t>(level),
|
||||
isActive != 1,
|
||||
@ -46,7 +46,7 @@ namespace Interlude
|
||||
false,
|
||||
false,
|
||||
false
|
||||
};
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -27,7 +27,7 @@ namespace Interlude
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
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*>();
|
||||
@ -106,7 +106,8 @@ namespace Interlude
|
||||
skillInfo[0]
|
||||
);
|
||||
|
||||
m_Skills.emplace(skill->GetId(), skill);
|
||||
auto test = static_cast<Entities::Skill*>(skill.get());
|
||||
m_Skills.emplace(skill->GetId(), test);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user