Update to actually work with component list

This commit is contained in:
David Markowitz 2023-06-10 00:14:20 -07:00
parent 5714ac558e
commit cebe3c732a
3 changed files with 30 additions and 29 deletions

View File

@ -170,13 +170,13 @@ Entity::~Entity() {
if (m_ParentEntity) m_ParentEntity->RemoveChild(this); if (m_ParentEntity) m_ParentEntity->RemoveChild(this);
} }
void Entity::ApplyComponentWhitelist(std::vector<eReplicaComponentType>& components) { void Entity::ApplyComponentWhitelist(TemplateComponents& components) {
const auto whitelistIndex = GetVar<int32_t>(u"componentWhitelist"); const auto whitelistIndex = GetVar<int32_t>(u"componentWhitelist");
if (whitelistIndex < 0 || whitelistIndex >= m_ComponentWhitelists.size()) return; if (whitelistIndex < 0 || whitelistIndex >= m_ComponentWhitelists.size()) return;
const auto& whitelist = m_ComponentWhitelists.at(whitelistIndex); const auto& whitelist = m_ComponentWhitelists.at(whitelistIndex);
const auto endRange = std::remove_if(components.begin(), components.end(), [&whitelist](const eReplicaComponentType& componentCandidate) { const auto endRange = std::remove_if(components.begin(), components.end(), [&whitelist](const auto& componentCandidate) {
return std::find(whitelist.begin(), whitelist.end(), componentCandidate) == whitelist.end(); return std::find(whitelist.begin(), whitelist.end(), componentCandidate.first) == whitelist.end();
}); });
components.erase(endRange, components.end()); components.erase(endRange, components.end());
} }
@ -204,8 +204,8 @@ void Entity::Initialize() {
} }
auto* componentsRegistry = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>(); auto* componentsRegistry = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
auto components = componentsRegistry->GetTemplateComponents(m_TemplateID); TemplateComponents components = componentsRegistry->GetTemplateComponents(m_TemplateID);
// Apply the whitelist here based on the corresponding ldf key. Removes components that are not whitelisted. List is determined based on the clients whitelist data. ApplyComponentWhitelist(components);
for (const auto& [componentTemplate, componentId] : components) { for (const auto& [componentTemplate, componentId] : components) {
switch (componentTemplate) { switch (componentTemplate) {
case eReplicaComponentType::CONTROLLABLE_PHYSICS: case eReplicaComponentType::CONTROLLABLE_PHYSICS:

View File

@ -50,13 +50,14 @@ namespace CppScripts {
using ComponentPtr = std::unique_ptr<Component>; using ComponentPtr = std::unique_ptr<Component>;
using ComponentWhitelist = std::vector<eReplicaComponentType>; using ComponentWhitelist = std::vector<eReplicaComponentType>;
using TemplateComponents = std::vector<std::pair<eReplicaComponentType, uint32_t>>;
class Entity { class Entity {
public: public:
explicit Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity = nullptr); explicit Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity = nullptr);
virtual ~Entity(); virtual ~Entity();
void ApplyComponentWhitelist(std::vector<eReplicaComponentType>& components); void ApplyComponentWhitelist(TemplateComponents& components);
static const std::vector<ComponentWhitelist>& GetComponentWhitelists() { return m_ComponentWhitelists; } static const std::vector<ComponentWhitelist>& GetComponentWhitelists() { return m_ComponentWhitelists; }
virtual void Initialize(); virtual void Initialize();

View File

@ -21,40 +21,40 @@ protected:
this->TearDownDependencies(); this->TearDownDependencies();
} }
void RunWhitelistTest(const int32_t whitelistIndex, std::vector<eReplicaComponentType> componentList) { void RunWhitelistTest(const int32_t whitelistIndex, TemplateComponents componentList) {
Game::logger->Log("EntityTests", "whitelist test %i", whitelistIndex); Game::logger->Log("EntityTests", "whitelist test %i", whitelistIndex);
entity->SetVar<int32_t>(u"componentWhitelist", whitelistIndex); entity->SetVar<int32_t>(u"componentWhitelist", whitelistIndex);
entity->ApplyComponentWhitelist(componentList); entity->ApplyComponentWhitelist(componentList);
const auto whitelist = Entity::GetComponentWhitelists().at(whitelistIndex); const auto whitelist = Entity::GetComponentWhitelists().at(whitelistIndex);
std::for_each(whitelist.begin(), whitelist.end(), [&componentList](const eReplicaComponentType& keptComponent) { std::for_each(whitelist.begin(), whitelist.end(), [&componentList](const eReplicaComponentType& keptComponent) {
EXPECT_EQ(std::count(componentList.begin(), componentList.end(), keptComponent), 2); EXPECT_EQ(std::count(componentList.begin(), componentList.end(), std::pair(keptComponent, 0U)), 2);
}); });
} }
}; };
TEST_F(EntityTests, WhitelistTest) { TEST_F(EntityTests, WhitelistTest) {
const auto whitelists = Entity::GetComponentWhitelists(); const auto whitelists = Entity::GetComponentWhitelists();
std::vector<eReplicaComponentType> components = { TemplateComponents components = {
eReplicaComponentType::CONTROLLABLE_PHYSICS, { eReplicaComponentType::CONTROLLABLE_PHYSICS, 0},
eReplicaComponentType::SIMPLE_PHYSICS, { eReplicaComponentType::SIMPLE_PHYSICS, 0},
eReplicaComponentType::MODEL_BEHAVIOR, { eReplicaComponentType::MODEL_BEHAVIOR, 0},
eReplicaComponentType::SPAWN, { eReplicaComponentType::SPAWN, 0},
eReplicaComponentType::RENDER, { eReplicaComponentType::RENDER, 0},
eReplicaComponentType::ITEM, { eReplicaComponentType::ITEM, 0},
eReplicaComponentType::BLUEPRINT, { eReplicaComponentType::BLUEPRINT, 0},
eReplicaComponentType::PET, { eReplicaComponentType::PET, 0},
eReplicaComponentType::SKILL, { eReplicaComponentType::SKILL, 0},
eReplicaComponentType::DESTROYABLE, { eReplicaComponentType::DESTROYABLE, 0},
eReplicaComponentType::CONTROLLABLE_PHYSICS, { eReplicaComponentType::CONTROLLABLE_PHYSICS, 0},
eReplicaComponentType::SIMPLE_PHYSICS, { eReplicaComponentType::SIMPLE_PHYSICS, 0},
eReplicaComponentType::MODEL_BEHAVIOR, { eReplicaComponentType::MODEL_BEHAVIOR, 0},
eReplicaComponentType::SPAWN, { eReplicaComponentType::SPAWN, 0},
eReplicaComponentType::RENDER, { eReplicaComponentType::RENDER, 0},
eReplicaComponentType::ITEM, { eReplicaComponentType::ITEM, 0},
eReplicaComponentType::BLUEPRINT, { eReplicaComponentType::BLUEPRINT, 0},
eReplicaComponentType::PET, { eReplicaComponentType::PET, 0},
eReplicaComponentType::SKILL, { eReplicaComponentType::SKILL, 0},
eReplicaComponentType::DESTROYABLE, { eReplicaComponentType::DESTROYABLE, 0},
}; };
RunWhitelistTest(0, components); RunWhitelistTest(0, components);
RunWhitelistTest(1, components); RunWhitelistTest(1, components);