Skip to content

Commit 02997bd

Browse files
authored
The second iteration of element data optimization (#4492)
1 parent 1f68357 commit 02997bd

30 files changed

+243
-204
lines changed

Client/mods/deathmatch/logic/CClientEntity.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ void CClientEntity::SetID(ElementID ID)
279279
}
280280
}
281281

282-
CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced)
282+
CLuaArgument* CClientEntity::GetCustomData(const CStringName& name, bool bInheritData, bool* pbIsSynced)
283283
{
284-
assert(szName);
284+
assert(name);
285285

286286
// Grab it and return a pointer to the variable
287-
SCustomData* pData = m_pCustomData->Get(szName);
287+
SCustomData* pData = m_pCustomData->Get(name);
288288
if (pData)
289289
{
290290
if (pbIsSynced)
@@ -295,7 +295,7 @@ CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData
295295
// If none, try returning parent's custom data
296296
if (bInheritData && m_pParent)
297297
{
298-
return m_pParent->GetCustomData(szName, true, pbIsSynced);
298+
return m_pParent->GetCustomData(name, true, pbIsSynced);
299299
}
300300

301301
// None available
@@ -315,10 +315,10 @@ CLuaArguments* CClientEntity::GetAllCustomData(CLuaArguments* table)
315315
return table;
316316
}
317317

318-
bool CClientEntity::GetCustomDataString(const char* szName, SString& strOut, bool bInheritData)
318+
bool CClientEntity::GetCustomDataString(const CStringName& name, SString& strOut, bool bInheritData)
319319
{
320320
// Grab the custom data variable
321-
CLuaArgument* pData = GetCustomData(szName, bInheritData);
321+
CLuaArgument* pData = GetCustomData(name, bInheritData);
322322
if (pData)
323323
{
324324
// Write the content depending on what type it is
@@ -350,10 +350,10 @@ bool CClientEntity::GetCustomDataString(const char* szName, SString& strOut, boo
350350
return false;
351351
}
352352

353-
bool CClientEntity::GetCustomDataInt(const char* szName, int& iOut, bool bInheritData)
353+
bool CClientEntity::GetCustomDataInt(const CStringName& name, int& iOut, bool bInheritData)
354354
{
355355
// Grab the custom data variable
356-
CLuaArgument* pData = GetCustomData(szName, bInheritData);
356+
CLuaArgument* pData = GetCustomData(name, bInheritData);
357357
if (pData)
358358
{
359359
// Write the content depending on what type it is
@@ -388,10 +388,10 @@ bool CClientEntity::GetCustomDataInt(const char* szName, int& iOut, bool bInheri
388388
return false;
389389
}
390390

391-
bool CClientEntity::GetCustomDataFloat(const char* szName, float& fOut, bool bInheritData)
391+
bool CClientEntity::GetCustomDataFloat(const CStringName& name, float& fOut, bool bInheritData)
392392
{
393393
// Grab the custom data variable
394-
CLuaArgument* pData = GetCustomData(szName, bInheritData);
394+
CLuaArgument* pData = GetCustomData(name, bInheritData);
395395
if (pData)
396396
{
397397
// Write the content depending on what type it is
@@ -415,10 +415,10 @@ bool CClientEntity::GetCustomDataFloat(const char* szName, float& fOut, bool bIn
415415
return false;
416416
}
417417

418-
bool CClientEntity::GetCustomDataBool(const char* szName, bool& bOut, bool bInheritData)
418+
bool CClientEntity::GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData)
419419
{
420420
// Grab the custom data variable
421-
CLuaArgument* pData = GetCustomData(szName, bInheritData);
421+
CLuaArgument* pData = GetCustomData(name, bInheritData);
422422
if (pData)
423423
{
424424
// Write the content depending on what type it is
@@ -470,50 +470,50 @@ bool CClientEntity::GetCustomDataBool(const char* szName, bool& bOut, bool bInhe
470470
return false;
471471
}
472472

473-
void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
473+
void CClientEntity::SetCustomData(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized)
474474
{
475-
assert(szName);
476-
if (strlen(szName) > MAX_CUSTOMDATA_NAME_LENGTH)
475+
assert(name);
476+
if (name->length() > MAX_CUSTOMDATA_NAME_LENGTH)
477477
{
478478
// Don't allow it to be set if the name is too long
479-
CLogger::ErrorPrintf("Custom data name too long (%s)", *SStringX(szName).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1));
479+
CLogger::ErrorPrintf("Custom data name too long (%s)", *SStringX(name.ToCString()).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1));
480480
return;
481481
}
482482

483483
// Grab the old variable
484484
CLuaArgument oldVariable;
485-
SCustomData* pData = m_pCustomData->Get(szName);
485+
SCustomData* pData = m_pCustomData->Get(name);
486486
if (pData)
487487
{
488488
oldVariable = pData->Variable;
489489
}
490490

491491
// Set the new data
492-
m_pCustomData->Set(szName, Variable, bSynchronized);
492+
m_pCustomData->Set(name, Variable, bSynchronized);
493493

494494
// Trigger the onClientElementDataChange event on us
495495
CLuaArguments Arguments;
496-
Arguments.PushString(szName);
496+
Arguments.PushString(name);
497497
Arguments.PushArgument(oldVariable);
498498
Arguments.PushArgument(Variable);
499499
CallEvent("onClientElementDataChange", Arguments, true);
500500
}
501501

502-
void CClientEntity::DeleteCustomData(const char* szName)
502+
void CClientEntity::DeleteCustomData(const CStringName& name)
503503
{
504504
// Grab the old variable
505-
SCustomData* pData = m_pCustomData->Get(szName);
505+
SCustomData* pData = m_pCustomData->Get(name);
506506
if (pData)
507507
{
508508
CLuaArgument oldVariable;
509509
oldVariable = pData->Variable;
510510

511511
// Delete the custom data
512-
m_pCustomData->Delete(szName);
512+
m_pCustomData->Delete(name);
513513

514514
// Trigger the onClientElementDataChange event on us
515515
CLuaArguments Arguments;
516-
Arguments.PushString(szName);
516+
Arguments.PushString(name);
517517
Arguments.PushArgument(oldVariable);
518518
Arguments.PushArgument(CLuaArgument()); // Use nil as the new value to indicate the data has been removed
519519
CallEvent("onClientElementDataChange", Arguments, true);

Client/mods/deathmatch/logic/CClientEntity.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CClientEntity;
1616
#include "CClientCommon.h"
1717
#include <core/CClientEntityBase.h>
1818
#include "logic/CClientEntityRefManager.h"
19+
#include "CStringName.h"
1920
class CLuaFunctionRef;
2021

2122
// Used to check fast version of getElementsByType
@@ -201,14 +202,14 @@ class CClientEntity : public CClientEntityBase
201202
void SetID(ElementID ID);
202203

203204
CCustomData* GetCustomDataPointer() { return m_pCustomData; }
204-
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced = nullptr);
205+
CLuaArgument* GetCustomData(const CStringName& name, bool bInheritData, bool* pbIsSynced = nullptr);
205206
CLuaArguments* GetAllCustomData(CLuaArguments* table);
206-
bool GetCustomDataString(const char* szKey, SString& strOut, bool bInheritData);
207-
bool GetCustomDataFloat(const char* szKey, float& fOut, bool bInheritData);
208-
bool GetCustomDataInt(const char* szKey, int& iOut, bool bInheritData);
209-
bool GetCustomDataBool(const char* szKey, bool& bOut, bool bInheritData);
210-
void SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
211-
void DeleteCustomData(const char* szName);
207+
bool GetCustomDataString(const CStringName& name, SString& strOut, bool bInheritData);
208+
bool GetCustomDataFloat(const CStringName& name, float& fOut, bool bInheritData);
209+
bool GetCustomDataInt(const CStringName& name, int& iOut, bool bInheritData);
210+
bool GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData);
211+
void SetCustomData(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized = true);
212+
void DeleteCustomData(const CStringName& name);
212213

213214
virtual bool GetMatrix(CMatrix& matrix) const;
214215
virtual bool SetMatrix(const CMatrix& matrix);

Client/mods/deathmatch/logic/CCustomData.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@
1414

1515
void CCustomData::Copy(CCustomData* pCustomData)
1616
{
17-
std::map<std::string, SCustomData>::const_iterator iter = pCustomData->IterBegin();
17+
auto iter = pCustomData->IterBegin();
1818
for (; iter != pCustomData->IterEnd(); iter++)
1919
{
20-
Set(iter->first.c_str(), iter->second.Variable);
20+
Set(iter->first, iter->second.Variable);
2121
}
2222
}
2323

24-
SCustomData* CCustomData::Get(const char* szName)
24+
SCustomData* CCustomData::Get(const CStringName& name)
2525
{
26-
assert(szName);
26+
assert(name);
2727

28-
std::map<std::string, SCustomData>::iterator it = m_Data.find(szName);
28+
auto it = m_Data.find(name);
2929
if (it != m_Data.end())
3030
return &it->second;
3131

3232
return NULL;
3333
}
3434

35-
void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
35+
void CCustomData::Set(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized)
3636
{
37-
assert(szName);
37+
assert(name);
3838

3939
// Grab the item with the given name
40-
SCustomData* pData = Get(szName);
40+
SCustomData* pData = Get(name);
4141
if (pData)
4242
{
4343
// Update existing
@@ -50,14 +50,14 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSy
5050
SCustomData newData;
5151
newData.Variable = Variable;
5252
newData.bSynchronized = bSynchronized;
53-
m_Data[szName] = newData;
53+
m_Data[name] = newData;
5454
}
5555
}
5656

57-
bool CCustomData::Delete(const char* szName)
57+
bool CCustomData::Delete(const CStringName& name)
5858
{
5959
// Find the item and delete it
60-
std::map<std::string, SCustomData>::iterator it = m_Data.find(szName);
60+
auto it = m_Data.find(name);
6161
if (it != m_Data.end())
6262
{
6363
m_Data.erase(it);

Client/mods/deathmatch/logic/CCustomData.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include "lua/CLuaArgument.h"
14+
#include "CStringName.h"
1415

1516
#define MAX_CUSTOMDATA_NAME_LENGTH 128
1617

@@ -25,14 +26,14 @@ class CCustomData
2526
public:
2627
void Copy(CCustomData* pCustomData);
2728

28-
SCustomData* Get(const char* szName);
29-
void Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
29+
SCustomData* Get(const CStringName& name);
30+
void Set(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized = true);
3031

31-
bool Delete(const char* szName);
32+
bool Delete(const CStringName& name);
3233

33-
std::map<std::string, SCustomData>::const_iterator IterBegin() { return m_Data.begin(); }
34-
std::map<std::string, SCustomData>::const_iterator IterEnd() { return m_Data.end(); }
34+
std::unordered_map<CStringName, SCustomData>::const_iterator IterBegin() { return m_Data.begin(); }
35+
std::unordered_map<CStringName, SCustomData>::const_iterator IterEnd() { return m_Data.end(); }
3536

3637
private:
37-
std::map<std::string, SCustomData> m_Data;
38+
std::unordered_map<CStringName, SCustomData> m_Data;
3839
};

Client/mods/deathmatch/logic/CPacketHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2885,7 +2885,7 @@ void CPacketHandler::Packet_EntityAdd(NetBitStreamInterface& bitStream)
28852885
CLuaArgument Argument;
28862886
Argument.ReadFromBitStream(bitStream);
28872887

2888-
pCustomData->Set(strName, Argument);
2888+
pCustomData->Set(CStringName{strName}, Argument);
28892889
}
28902890
else
28912891
{

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,23 +1026,23 @@ bool CStaticFunctionDefinitions::SetElementID(CClientEntity& Entity, const char*
10261026
return false;
10271027
}
10281028

1029-
bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, const char* szName, CLuaArgument& Variable, bool bSynchronize)
1029+
bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, CStringName name, CLuaArgument& Variable, bool bSynchronize)
10301030
{
1031-
assert(szName);
1032-
assert(strlen(szName) <= MAX_CUSTOMDATA_NAME_LENGTH);
1031+
assert(name);
1032+
assert(name->length() <= MAX_CUSTOMDATA_NAME_LENGTH);
10331033

10341034
bool bIsSynced;
1035-
CLuaArgument* pCurrentVariable = Entity.GetCustomData(szName, false, &bIsSynced);
1035+
CLuaArgument* pCurrentVariable = Entity.GetCustomData(name, false, &bIsSynced);
10361036
if (!pCurrentVariable || Variable != *pCurrentVariable || bIsSynced != bSynchronize)
10371037
{
10381038
if (bSynchronize && !Entity.IsLocalEntity())
10391039
{
10401040
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
10411041
// Write element ID, name length and the name. Also write the variable.
10421042
pBitStream->Write(Entity.GetID());
1043-
unsigned short usNameLength = static_cast<unsigned short>(strlen(szName));
1043+
unsigned short usNameLength = static_cast<unsigned short>(name->length());
10441044
pBitStream->WriteCompressed(usNameLength);
1045-
pBitStream->Write(szName, usNameLength);
1045+
pBitStream->Write(name.ToCString(), usNameLength);
10461046
Variable.WriteToBitStream(*pBitStream);
10471047

10481048
// Send the packet and deallocate
@@ -1051,14 +1051,14 @@ bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, const cha
10511051
}
10521052

10531053
// Set its custom data
1054-
Entity.SetCustomData(szName, Variable, bSynchronize);
1054+
Entity.SetCustomData(name, Variable, bSynchronize);
10551055
return true;
10561056
}
10571057

10581058
return false;
10591059
}
10601060

1061-
bool CStaticFunctionDefinitions::RemoveElementData(CClientEntity& Entity, const char* szName)
1061+
bool CStaticFunctionDefinitions::RemoveElementData(CClientEntity& Entity, CStringName name)
10621062
{
10631063
// TODO
10641064
return false;

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class CStaticFunctionDefinitions
8585
static CClientDummy* CreateElement(CResource& Resource, const char* szTypeName, const char* szID);
8686
static bool DestroyElement(CClientEntity& Entity);
8787
static bool SetElementID(CClientEntity& Entity, const char* szID);
88-
static bool SetElementData(CClientEntity& Entity, const char* szName, CLuaArgument& Variable, bool bSynchronize);
89-
static bool RemoveElementData(CClientEntity& Entity, const char* szName);
88+
static bool SetElementData(CClientEntity& Entity, CStringName name, CLuaArgument& Variable, bool bSynchronize);
89+
static bool RemoveElementData(CClientEntity& Entity, CStringName name);
9090
static bool SetElementMatrix(CClientEntity& Entity, const CMatrix& matrix);
9191
static bool SetElementPosition(CClientEntity& Entity, const CVector& vecPosition, bool bWarp = true);
9292
static bool SetElementRotation(CClientEntity& Entity, const CVector& vecRotation, eEulerRotationOrder rotationOrder, bool bNewWay);

Client/mods/deathmatch/logic/lua/CLuaArgument.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ void CLuaArgument::ReadString(const std::string_view& string)
334334
m_strString = std::string{string};
335335
}
336336

337+
void CLuaArgument::ReadString(const CStringName& string)
338+
{
339+
m_iType = LUA_TSTRING;
340+
DeleteTableData();
341+
m_strString = string.ToString();
342+
}
343+
337344
void CLuaArgument::ReadString(const char* string)
338345
{
339346
m_iType = LUA_TSTRING;

Client/mods/deathmatch/logic/lua/CLuaArgument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern "C"
1717
#include <net/bitstream.h>
1818
#include <string>
1919
#include "json.h"
20+
#include "CStringName.h"
2021

2122
class CClientEntity;
2223
class CLuaArguments;
@@ -42,6 +43,7 @@ class CLuaArgument
4243
void ReadNumber(double dNumber);
4344
void ReadString(const std::string& string);
4445
void ReadString(const std::string_view& string);
46+
void ReadString(const CStringName& string);
4547
void ReadString(const char* string);
4648
void ReadElement(CClientEntity* pElement);
4749
void ReadScriptID(uint uiScriptID);

Client/mods/deathmatch/logic/lua/CLuaArguments.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ CLuaArgument* CLuaArguments::PushString(const std::string_view& string)
351351
return arg;
352352
}
353353

354+
CLuaArgument* CLuaArguments::PushString(const CStringName& string)
355+
{
356+
CLuaArgument* arg = new CLuaArgument();
357+
arg->ReadString(string);
358+
m_Arguments.push_back(arg);
359+
return arg;
360+
}
361+
354362
CLuaArgument* CLuaArguments::PushString(const char* string)
355363
{
356364
CLuaArgument* arg = new CLuaArgument();

0 commit comments

Comments
 (0)