Skip to content

Commit

Permalink
Fixed client setElementData not updating the server when enabling syn…
Browse files Browse the repository at this point in the history
…chronization on an existing key with the same value
  • Loading branch information
ccw808 committed Apr 22, 2018
1 parent 7b5bccb commit 7cf9dc4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
10 changes: 6 additions & 4 deletions Client/mods/deathmatch/logic/CClientEntity.cpp
Expand Up @@ -282,21 +282,23 @@ void CClientEntity::SetID(ElementID ID)
}
}

CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData)
CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced)
{
assert(szName);

// Grab it and return a pointer to the variable
SCustomData* pData = m_pCustomData->Get(szName);
if (pData)
{
if (pbIsSynced)
*pbIsSynced = pData->bSynchronized;
return &pData->Variable;
}

// If none, try returning parent's custom data
if (bInheritData && m_pParent)
{
return m_pParent->GetCustomData(szName, true);
return m_pParent->GetCustomData(szName, true, pbIsSynced);
}

// None available
Expand Down Expand Up @@ -458,7 +460,7 @@ bool CClientEntity::GetCustomDataBool(const char* szName, bool& bOut, bool bInhe
return false;
}

void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variable)
void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
{
assert(szName);
if (strlen(szName) > MAX_CUSTOMDATA_NAME_LENGTH)
Expand All @@ -477,7 +479,7 @@ void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variab
}

// Set the new data
m_pCustomData->Set(szName, Variable);
m_pCustomData->Set(szName, Variable, bSynchronized);

// Trigger the onClientElementDataChange event on us
CLuaArguments Arguments;
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/CClientEntity.h
Expand Up @@ -194,12 +194,12 @@ class CClientEntity : public CClientEntityBase
void SetID(ElementID ID);

CCustomData* GetCustomDataPointer(void) { return m_pCustomData; }
CLuaArgument* GetCustomData(const char* szName, bool bInheritData);
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced = nullptr);
bool GetCustomDataString(const char* szKey, SString& strOut, bool bInheritData);
bool GetCustomDataFloat(const char* szKey, float& fOut, bool bInheritData);
bool GetCustomDataInt(const char* szKey, int& iOut, bool bInheritData);
bool GetCustomDataBool(const char* szKey, bool& bOut, bool bInheritData);
void SetCustomData(const char* szName, const CLuaArgument& Variable);
void SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
void DeleteCustomData(const char* szName);

virtual bool GetMatrix(CMatrix& matrix) const;
Expand Down
4 changes: 3 additions & 1 deletion Client/mods/deathmatch/logic/CCustomData.cpp
Expand Up @@ -32,7 +32,7 @@ SCustomData* CCustomData::Get(const char* szName)
return NULL;
}

void CCustomData::Set(const char* szName, const CLuaArgument& Variable)
void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
{
assert(szName);

Expand All @@ -42,12 +42,14 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable)
{
// Update existing
pData->Variable = Variable;
pData->bSynchronized = bSynchronized;
}
else
{
// Add new
SCustomData newData;
newData.Variable = Variable;
newData.bSynchronized = bSynchronized;
m_Data[szName] = newData;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Client/mods/deathmatch/logic/CCustomData.h
Expand Up @@ -18,6 +18,7 @@
struct SCustomData
{
CLuaArgument Variable;
bool bSynchronized;
};

class CCustomData
Expand All @@ -26,7 +27,7 @@ class CCustomData
void Copy(CCustomData* pCustomData);

SCustomData* Get(const char* szName);
void Set(const char* szName, const CLuaArgument& Variable);
void Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);

bool Delete(const char* szName);

Expand Down
43 changes: 16 additions & 27 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Expand Up @@ -951,39 +951,28 @@ bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, const cha
assert(szName);
assert(strlen(szName) <= MAX_CUSTOMDATA_NAME_LENGTH);

CLuaArgument* pCurrentVariable = Entity.GetCustomData(szName, false);
if (!pCurrentVariable || Variable != *pCurrentVariable)
bool bIsSynced;
CLuaArgument* pCurrentVariable = Entity.GetCustomData(szName, false, &bIsSynced);
if (!pCurrentVariable || Variable != *pCurrentVariable || bIsSynced != bSynchronize)
{
if (bSynchronize && !Entity.IsLocalEntity())
{
// Allocate a bitstream
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
// Write element ID, name length and the name. Also write the variable.
pBitStream->Write(Entity.GetID());
unsigned short usNameLength = static_cast<unsigned short>(strlen(szName));
pBitStream->WriteCompressed(usNameLength);
pBitStream->Write(szName, usNameLength);
Variable.WriteToBitStream(*pBitStream);

// Send the packet and deallocate
g_pNet->SendPacket(PACKET_ID_CUSTOM_DATA, pBitStream, PACKET_PRIORITY_HIGH, PACKET_RELIABILITY_RELIABLE_ORDERED);
g_pNet->DeallocateNetBitStream(pBitStream);

// Set its custom data
Entity.SetCustomData(szName, Variable);

return true;
}
// Write element ID, name length and the name. Also write the variable.
pBitStream->Write(Entity.GetID());
unsigned short usNameLength = static_cast<unsigned short>(strlen(szName));
pBitStream->WriteCompressed(usNameLength);
pBitStream->Write(szName, usNameLength);
Variable.WriteToBitStream(*pBitStream);

// Send the packet and deallocate
g_pNet->SendPacket(PACKET_ID_CUSTOM_DATA, pBitStream, PACKET_PRIORITY_HIGH, PACKET_RELIABILITY_RELIABLE_ORDERED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
else
{
// Set its custom data
Entity.SetCustomData(szName, Variable);

return true;
}
// Set its custom data
Entity.SetCustomData(szName, Variable, bSynchronize);
return true;
}

return false;
Expand Down

0 comments on commit 7cf9dc4

Please sign in to comment.