mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-06-16 19:54:23 +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_PhantomCollisionCallbacks = {};
|
||||||
m_IsParentChildDirty = true;
|
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()));
|
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_DefaultPosition = info.pos;
|
||||||
m_DefaultRotation = info.rot;
|
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;
|
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,
|
// 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.
|
// 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) {
|
if (data && data->GetValueType() != eLDFType::LDF_TYPE_UNKNOWN) {
|
||||||
data->WriteToPacket(settingStream);
|
data->WriteToPacket(settingStream);
|
||||||
} else {
|
} else {
|
||||||
@@ -987,16 +987,15 @@ void Entity::WriteBaseReplicaData(RakNet::BitStream& outBitStream, eReplicaPacke
|
|||||||
const auto& syncLDF = GetVar<std::vector<std::u16string>>(u"syncLDF");
|
const auto& syncLDF = GetVar<std::vector<std::u16string>>(u"syncLDF");
|
||||||
|
|
||||||
// Only sync for models.
|
// 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
|
outBitStream.Write1(); // Has ldf data
|
||||||
WriteLDFData(m_Settings, outBitStream);
|
WriteLDFData(m_Settings, outBitStream);
|
||||||
} else if (!syncLDF.empty()) {
|
} else if (!syncLDF.empty()) {
|
||||||
// Find all the ldf data we need to write
|
// Find all the ldf data we need to write
|
||||||
std::vector<LDFBaseData*> ldfData;
|
LwoNameValue ldfData;
|
||||||
ldfData.reserve(m_Settings.size());
|
|
||||||
|
|
||||||
for (const auto& data : syncLDF) {
|
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
|
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 {
|
bool Entity::HasVar(const std::u16string& name) const {
|
||||||
for (auto* data : m_Settings) {
|
return m_Settings.values.contains(name);
|
||||||
if (data->GetKey() == name) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Entity::GetNetworkId() const {
|
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 {
|
LDFBaseData* Entity::GetVarData(const std::u16string& name) const {
|
||||||
for (auto* data : m_Settings) {
|
const auto itr = m_Settings.values.find(name);
|
||||||
if (data == nullptr) {
|
return itr != m_Settings.values.cend() ? itr->second.get() : nullptr;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data->GetKey() != name) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Entity::GetVarAsString(const std::u16string& name) const {
|
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");
|
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();
|
configData.PushDebug<AMFStringValue>(GeneralUtils::UTF16ToWTF8(config->GetKey())) = config->GetValueAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public:
|
|||||||
|
|
||||||
LWOOBJID GetSpawnerID() const { return m_SpawnerID; }
|
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; }
|
const LwoNameValue& GetNetworkSettings() const { return m_NetworkSettings; }
|
||||||
|
|
||||||
@@ -377,12 +377,14 @@ public:
|
|||||||
static Observable<Entity*, const PositionUpdate&> OnPlayerPositionUpdate;
|
static Observable<Entity*, const PositionUpdate&> OnPlayerPositionUpdate;
|
||||||
|
|
||||||
private:
|
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;
|
LWOOBJID m_ObjectID;
|
||||||
|
|
||||||
LOT m_TemplateID;
|
LOT m_TemplateID;
|
||||||
|
|
||||||
std::vector<LDFBaseData*> m_Settings;
|
LwoNameValue m_Settings;
|
||||||
LwoNameValue m_NetworkSettings;
|
LwoNameValue m_NetworkSettings;
|
||||||
|
|
||||||
NiPoint3 m_DefaultPosition;
|
NiPoint3 m_DefaultPosition;
|
||||||
@@ -489,45 +491,34 @@ T Entity::GetVarAs(const std::u16string& name) const {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Entity::SetVar(const std::u16string& name, T value) {
|
void Entity::SetVar(const std::u16string& name, T value) {
|
||||||
auto* data = GetVarData(name);
|
InsertLnvData<T>(m_Settings, name, value);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
LwoNameValue::ValueType::iterator Entity::InsertNetworkVar(const std::u16string& name, T value) {
|
LwoNameValue::ValueType::iterator Entity::InsertLnvData(LwoNameValue& lnv, const std::u16string& key, T value) {
|
||||||
auto itr = m_NetworkSettings.values.find(name);
|
auto itr = lnv.values.find(key);
|
||||||
if (itr != m_NetworkSettings.values.end()) {
|
if (itr != lnv.values.end()) {
|
||||||
auto lnv = dynamic_cast<LDFData<T>*>(itr->second.get());
|
auto lnv = dynamic_cast<LDFData<T>*>(itr->second.get());
|
||||||
if (!lnv) {
|
if (!lnv) {
|
||||||
// Is of different type
|
// Is of different type
|
||||||
itr->second = std::make_unique<LDFData<T>>(name, value);
|
itr->second = std::make_unique<LDFData<T>>(key, value);
|
||||||
} else {
|
} else {
|
||||||
// Is the same type and exists
|
// Is the same type and exists
|
||||||
lnv->SetValue(value);
|
lnv->SetValue(value);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Doesn't exist
|
// 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;
|
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>
|
template<typename T>
|
||||||
void Entity::SetNetworkVar(const std::u16string& name, T value, const SystemAddress& sysAddr) {
|
void Entity::SetNetworkVar(const std::u16string& name, T value, const SystemAddress& sysAddr) {
|
||||||
const auto itr = InsertNetworkVar<T>(name, value);
|
const auto itr = InsertNetworkVar<T>(name, value);
|
||||||
|
|||||||
Reference in New Issue
Block a user