mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-06-13 18:24:20 +00:00
move settings on Entity to managed memory
This commit is contained in:
@@ -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<LDFBaseData>(setting->Copy()));
|
||||
for (const auto* setting : info.networkSettings) m_NetworkSettings.values.insert_or_assign(setting->GetKey(), std::unique_ptr<LDFBaseData>(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<LDFBaseData*>& 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<std::vector<std::u16string>>(u"syncLDF");
|
||||
|
||||
// Only sync for models.
|
||||
if (!m_Settings.empty() && (GetComponent<ModelComponent>() && !GetComponent<PetComponent>())) {
|
||||
if (!m_Settings.values.empty() && (GetComponent<ModelComponent>() && !GetComponent<PetComponent>())) {
|
||||
outBitStream.Write1(); // Has ldf data
|
||||
WriteLDFData(m_Settings, outBitStream);
|
||||
} else if (!syncLDF.empty()) {
|
||||
// Find all the ldf data we need to write
|
||||
std::vector<LDFBaseData*> 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<LDFBaseData>(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<AMFStringValue>(GeneralUtils::UTF16ToWTF8(config->GetKey())) = config->GetValueAsString();
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ public:
|
||||
|
||||
LWOOBJID GetSpawnerID() const { return m_SpawnerID; }
|
||||
|
||||
const std::vector<LDFBaseData*>& 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<Entity*, const PositionUpdate&> OnPlayerPositionUpdate;
|
||||
|
||||
private:
|
||||
void WriteLDFData(const std::vector<LDFBaseData*>& ldf, RakNet::BitStream& outBitStream) const;
|
||||
template<typename T>
|
||||
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<LDFBaseData*> m_Settings;
|
||||
LwoNameValue m_Settings;
|
||||
LwoNameValue m_NetworkSettings;
|
||||
|
||||
NiPoint3 m_DefaultPosition;
|
||||
@@ -489,45 +491,34 @@ T Entity::GetVarAs(const std::u16string& name) const {
|
||||
|
||||
template<typename T>
|
||||
void Entity::SetVar(const std::u16string& name, T value) {
|
||||
auto* data = GetVarData(name);
|
||||
|
||||
if (data == nullptr) {
|
||||
auto* data = new LDFData<T>(name, value);
|
||||
|
||||
m_Settings.push_back(data);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
auto* typed = dynamic_cast<LDFData<T>*>(data);
|
||||
|
||||
if (typed == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
typed->SetValue(value);
|
||||
InsertLnvData<T>(m_Settings, name, value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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<LDFData<T>*>(itr->second.get());
|
||||
if (!lnv) {
|
||||
// Is of different type
|
||||
itr->second = std::make_unique<LDFData<T>>(name, value);
|
||||
itr->second = std::make_unique<LDFData<T>>(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<LDFData<T>>(name, value)).first;
|
||||
itr = lnv.values.insert_or_assign(key, std::make_unique<LDFData<T>>(key, value)).first;
|
||||
}
|
||||
|
||||
|
||||
return itr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
LwoNameValue::ValueType::iterator Entity::InsertNetworkVar(const std::u16string& name, T value) {
|
||||
return InsertLnvData<T>(m_NetworkSettings, name, value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Entity::SetNetworkVar(const std::u16string& name, T value, const SystemAddress& sysAddr) {
|
||||
const auto itr = InsertNetworkVar<T>(name, value);
|
||||
|
||||
Reference in New Issue
Block a user