diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 27d408eb..966a674b 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -123,7 +123,7 @@ Entity::Entity(const LWOOBJID& objectID, const EntityInfo& info, User* parentUse m_PhantomCollisionCallbacks = {}; m_IsParentChildDirty = true; - m_Settings = info.settings; + for (const auto* setting : info.settings) m_Settings.values.insert_or_assign(setting->GetKey(), std::unique_ptr(setting->Copy())); for (const auto* setting : info.networkSettings) m_NetworkSettings.values.insert_or_assign(setting->GetKey(), std::unique_ptr(setting->Copy())); m_DefaultPosition = info.pos; m_DefaultRotation = info.rot; @@ -949,13 +949,13 @@ void Entity::SetGMLevel(eGameMasterLevel value) { } } -void Entity::WriteLDFData(const std::vector& ldf, RakNet::BitStream& outBitStream) const { +void Entity::WriteLDFData(const LwoNameValue& ldf, RakNet::BitStream& outBitStream) const { RakNet::BitStream settingStream; - int32_t numberOfValidKeys = ldf.size(); + int32_t numberOfValidKeys = ldf.values.size(); // Writing keys value pairs the client does not expect to receive or interpret will result in undefined behavior, // so we need to filter out any keys that are not valid and fix the number of valid keys to be correct. - for (LDFBaseData* data : ldf) { + for (const auto& data : ldf.values | std::views::values) { if (data && data->GetValueType() != eLDFType::LDF_TYPE_UNKNOWN) { data->WriteToPacket(settingStream); } else { @@ -987,16 +987,15 @@ void Entity::WriteBaseReplicaData(RakNet::BitStream& outBitStream, eReplicaPacke const auto& syncLDF = GetVar>(u"syncLDF"); // Only sync for models. - if (!m_Settings.empty() && (GetComponent() && !GetComponent())) { + if (!m_Settings.values.empty() && (GetComponent() && !GetComponent())) { outBitStream.Write1(); // Has ldf data WriteLDFData(m_Settings, outBitStream); } else if (!syncLDF.empty()) { // Find all the ldf data we need to write - std::vector ldfData; - ldfData.reserve(m_Settings.size()); + LwoNameValue ldfData; for (const auto& data : syncLDF) { - ldfData.push_back(GetVarData(data)); + ldfData.values.insert_or_assign(data, std::unique_ptr(GetVarData(data)->Copy())); } outBitStream.Write1(); // Has ldf data @@ -2045,13 +2044,7 @@ void Entity::SetI64(const std::u16string& name, const int64_t value) { } bool Entity::HasVar(const std::u16string& name) const { - for (auto* data : m_Settings) { - if (data->GetKey() == name) { - return true; - } - } - - return false; + return m_Settings.values.contains(name); } uint16_t Entity::GetNetworkId() const { @@ -2084,19 +2077,8 @@ void Entity::SendNetworkVar(const std::string& data, const SystemAddress& sysAdd } LDFBaseData* Entity::GetVarData(const std::u16string& name) const { - for (auto* data : m_Settings) { - if (data == nullptr) { - continue; - } - - if (data->GetKey() != name) { - continue; - } - - return data; - } - - return nullptr; + const auto itr = m_Settings.values.find(name); + return itr != m_Settings.values.cend() ? itr->second.get() : nullptr; } std::string Entity::GetVarAsString(const std::u16string& name) const { @@ -2276,7 +2258,7 @@ bool Entity::MsgRequestServerObjectInfo(GameMessages::RequestServerObjectInfo& r } auto& configData = objectInfo.PushDebug("Config Data"); - for (const auto config : m_Settings) { + for (const auto& config : m_Settings.values | std::views::values) { configData.PushDebug(GeneralUtils::UTF16ToWTF8(config->GetKey())) = config->GetValueAsString(); } diff --git a/dGame/Entity.h b/dGame/Entity.h index 4feed15c..c3014791 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -97,7 +97,7 @@ public: LWOOBJID GetSpawnerID() const { return m_SpawnerID; } - const std::vector& GetSettings() const { return m_Settings; } + const LwoNameValue& GetSettings() const { return m_Settings; } const LwoNameValue& GetNetworkSettings() const { return m_NetworkSettings; } @@ -377,12 +377,14 @@ public: static Observable OnPlayerPositionUpdate; private: - void WriteLDFData(const std::vector& ldf, RakNet::BitStream& outBitStream) const; + template + LwoNameValue::ValueType::iterator InsertLnvData(LwoNameValue& lnv, const std::u16string& key, T value); + void WriteLDFData(const LwoNameValue& ldf, RakNet::BitStream& outBitStream) const; LWOOBJID m_ObjectID; LOT m_TemplateID; - std::vector m_Settings; + LwoNameValue m_Settings; LwoNameValue m_NetworkSettings; NiPoint3 m_DefaultPosition; @@ -489,45 +491,34 @@ T Entity::GetVarAs(const std::u16string& name) const { template void Entity::SetVar(const std::u16string& name, T value) { - auto* data = GetVarData(name); - - if (data == nullptr) { - auto* data = new LDFData(name, value); - - m_Settings.push_back(data); - - return; - } - - auto* typed = dynamic_cast*>(data); - - if (typed == nullptr) { - return; - } - - typed->SetValue(value); + InsertLnvData(m_Settings, name, value); } template -LwoNameValue::ValueType::iterator Entity::InsertNetworkVar(const std::u16string& name, T value) { - auto itr = m_NetworkSettings.values.find(name); - if (itr != m_NetworkSettings.values.end()) { +LwoNameValue::ValueType::iterator Entity::InsertLnvData(LwoNameValue& lnv, const std::u16string& key, T value) { + auto itr = lnv.values.find(key); + if (itr != lnv.values.end()) { auto lnv = dynamic_cast*>(itr->second.get()); if (!lnv) { // Is of different type - itr->second = std::make_unique>(name, value); + itr->second = std::make_unique>(key, value); } else { // Is the same type and exists lnv->SetValue(value); } } else { // Doesn't exist - itr = m_NetworkSettings.values.insert_or_assign(name, std::make_unique>(name, value)).first; + itr = lnv.values.insert_or_assign(key, std::make_unique>(key, value)).first; } - + return itr; } +template +LwoNameValue::ValueType::iterator Entity::InsertNetworkVar(const std::u16string& name, T value) { + return InsertLnvData(m_NetworkSettings, name, value); +} + template void Entity::SetNetworkVar(const std::u16string& name, T value, const SystemAddress& sysAddr) { const auto itr = InsertNetworkVar(name, value);