Skip to content

Commit 4929a83

Browse files
authored
Add StringName / Lua ElementData optimizations (#4169)
1 parent 9af03b3 commit 4929a83

File tree

13 files changed

+719
-45
lines changed

13 files changed

+719
-45
lines changed

Client/mods/deathmatch/StdInc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
#include "CLatentTransferManager.h"
155155
#include "CDebugHookManager.h"
156156
#include "lua/CLuaShared.h"
157+
#include "CStringName.h"
157158

158159
// Deathmatch includes
159160
#include "ClientCommands.h"

Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,13 +1767,13 @@ int CLuaElementDefs::SetElementData(lua_State* luaVM)
17671767
{
17681768
// bool setElementData ( element theElement, string key, var value, [bool synchronize = true] )
17691769
CClientEntity* pEntity;
1770-
SString strKey;
1770+
CStringName key;
17711771
CLuaArgument value;
17721772
bool bSynchronize;
17731773

17741774
CScriptArgReader argStream(luaVM);
17751775
argStream.ReadUserData(pEntity);
1776-
argStream.ReadString(strKey);
1776+
argStream.ReadStringName(key);
17771777
argStream.ReadLuaArgument(value);
17781778
argStream.ReadBool(bSynchronize, true);
17791779

@@ -1782,15 +1782,16 @@ int CLuaElementDefs::SetElementData(lua_State* luaVM)
17821782
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
17831783
if (pLuaMain)
17841784
{
1785-
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
1785+
if (key->length() > MAX_CUSTOMDATA_NAME_LENGTH)
17861786
{
17871787
// Warn and truncate if key is too long
17881788
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
17891789
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
1790-
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
1790+
1791+
key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH);
17911792
}
17921793

1793-
if (CStaticFunctionDefinitions::SetElementData(*pEntity, strKey, value, bSynchronize))
1794+
if (CStaticFunctionDefinitions::SetElementData(*pEntity, key.ToCString(), value, bSynchronize))
17941795
{
17951796
lua_pushboolean(luaVM, true);
17961797
return 1;
@@ -1809,26 +1810,27 @@ int CLuaElementDefs::RemoveElementData(lua_State* luaVM)
18091810
{
18101811
// bool removeElementData ( element theElement, string key )
18111812
CClientEntity* pEntity;
1812-
SString strKey;
1813+
CStringName key;
18131814

18141815
CScriptArgReader argStream(luaVM);
18151816
argStream.ReadUserData(pEntity);
1816-
argStream.ReadString(strKey);
1817+
argStream.ReadStringName(key);
18171818

18181819
if (!argStream.HasErrors())
18191820
{
18201821
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
18211822
if (pLuaMain)
18221823
{
1823-
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
1824+
if (key->length() > MAX_CUSTOMDATA_NAME_LENGTH)
18241825
{
18251826
// Warn and truncate if key is too long
18261827
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
18271828
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
1828-
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
1829+
1830+
key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH);
18291831
}
18301832

1831-
if (CStaticFunctionDefinitions::RemoveElementData(*pEntity, strKey))
1833+
if (CStaticFunctionDefinitions::RemoveElementData(*pEntity, key.ToCString()))
18321834
{
18331835
lua_pushboolean(luaVM, true);
18341836
return 1;

Server/mods/deathmatch/StdInc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <CSphere.h>
4747
#include <CBox.h>
4848
#include <CMatrix.h>
49+
#include "CStringName.h"
4950
#include <bochs_internal/bochs_crc32.h>
5051
#include <pcrecpp.h>
5152
#include <pthread.h>

Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,14 +1540,14 @@ int CLuaElementDefs::setElementData(lua_State* luaVM)
15401540
{
15411541
// bool setElementData ( element theElement, string key, var value, [var syncMode = true] )
15421542
CElement* pElement;
1543-
SString strKey;
1543+
CStringName key;
15441544
CLuaArgument value;
15451545
ESyncType syncType = ESyncType::BROADCAST;
15461546
std::optional<eCustomDataClientTrust> clientTrust{};
15471547

15481548
CScriptArgReader argStream(luaVM);
15491549
argStream.ReadUserData(pElement);
1550-
argStream.ReadString(strKey);
1550+
argStream.ReadStringName(key);
15511551
argStream.ReadLuaArgument(value);
15521552

15531553
if (argStream.NextIsBool())
@@ -1571,15 +1571,15 @@ int CLuaElementDefs::setElementData(lua_State* luaVM)
15711571
{
15721572
LogWarningIfPlayerHasNotJoinedYet(luaVM, pElement);
15731573

1574-
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
1574+
if (key->length() > MAX_CUSTOMDATA_NAME_LENGTH)
15751575
{
15761576
// Warn and truncate if key is too long
15771577
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
15781578
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
1579-
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
1579+
key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH);
15801580
}
15811581

1582-
if (CStaticFunctionDefinitions::SetElementData(pElement, strKey, value, syncType, clientTrust))
1582+
if (CStaticFunctionDefinitions::SetElementData(pElement, key.ToCString(), value, syncType, clientTrust))
15831583
{
15841584
lua_pushboolean(luaVM, true);
15851585
return 1;
@@ -1596,25 +1596,25 @@ int CLuaElementDefs::removeElementData(lua_State* luaVM)
15961596
{
15971597
// bool removeElementData ( element theElement, string key )
15981598
CElement* pElement;
1599-
SString strKey;
1599+
CStringName key;
16001600

16011601
CScriptArgReader argStream(luaVM);
16021602
argStream.ReadUserData(pElement);
1603-
argStream.ReadString(strKey);
1603+
argStream.ReadStringName(key);
16041604

16051605
if (!argStream.HasErrors())
16061606
{
16071607
LogWarningIfPlayerHasNotJoinedYet(luaVM, pElement);
16081608

1609-
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
1609+
if (key->length() > MAX_CUSTOMDATA_NAME_LENGTH)
16101610
{
16111611
// Warn and truncate if key is too long
16121612
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
16131613
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
1614-
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
1614+
key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH);
16151615
}
16161616

1617-
if (CStaticFunctionDefinitions::RemoveElementData(pElement, strKey))
1617+
if (CStaticFunctionDefinitions::RemoveElementData(pElement, key.ToCString()))
16181618
{
16191619
lua_pushboolean(luaVM, true);
16201620
return 1;
@@ -1631,19 +1631,19 @@ int CLuaElementDefs::addElementDataSubscriber(lua_State* luaVM)
16311631
{
16321632
// bool addElementDataSubscriber ( element theElement, string key, player thePlayer )
16331633
CElement* pElement;
1634-
SString strKey;
1634+
CStringName key;
16351635
CPlayer* pPlayer;
16361636

16371637
CScriptArgReader argStream(luaVM);
16381638
argStream.ReadUserData(pElement);
1639-
argStream.ReadString(strKey);
1639+
argStream.ReadStringName(key);
16401640
argStream.ReadUserData(pPlayer);
16411641

16421642
if (!argStream.HasErrors())
16431643
{
16441644
LogWarningIfPlayerHasNotJoinedYet(luaVM, pElement);
16451645

1646-
if (CStaticFunctionDefinitions::AddElementDataSubscriber(pElement, strKey, pPlayer))
1646+
if (CStaticFunctionDefinitions::AddElementDataSubscriber(pElement, key.ToCString(), pPlayer))
16471647
{
16481648
lua_pushboolean(luaVM, true);
16491649
return 1;
@@ -1660,19 +1660,19 @@ int CLuaElementDefs::removeElementDataSubscriber(lua_State* luaVM)
16601660
{
16611661
// bool removeElementDataSubscriber ( element theElement, string key, player thePlayer )
16621662
CElement* pElement;
1663-
SString strKey;
1663+
CStringName key;
16641664
CPlayer* pPlayer;
16651665

16661666
CScriptArgReader argStream(luaVM);
16671667
argStream.ReadUserData(pElement);
1668-
argStream.ReadString(strKey);
1668+
argStream.ReadStringName(key);
16691669
argStream.ReadUserData(pPlayer);
16701670

16711671
if (!argStream.HasErrors())
16721672
{
16731673
LogWarningIfPlayerHasNotJoinedYet(luaVM, pElement);
16741674

1675-
if (CStaticFunctionDefinitions::RemoveElementDataSubscriber(pElement, strKey, pPlayer))
1675+
if (CStaticFunctionDefinitions::RemoveElementDataSubscriber(pElement, key.ToCString(), pPlayer))
16761676
{
16771677
lua_pushboolean(luaVM, true);
16781678
return 1;
@@ -1689,19 +1689,19 @@ int CLuaElementDefs::hasElementDataSubscriber(lua_State* luaVM)
16891689
{
16901690
// bool hasElementDataSubscriber ( element theElement, string key, player thePlayer )
16911691
CElement* pElement;
1692-
SString strKey;
1692+
CStringName key;
16931693
CPlayer* pPlayer;
16941694

16951695
CScriptArgReader argStream(luaVM);
16961696
argStream.ReadUserData(pElement);
1697-
argStream.ReadString(strKey);
1697+
argStream.ReadStringName(key);
16981698
argStream.ReadUserData(pPlayer);
16991699

17001700
if (!argStream.HasErrors())
17011701
{
17021702
LogWarningIfPlayerHasNotJoinedYet(luaVM, pElement);
17031703

1704-
bool bResult = CStaticFunctionDefinitions::HasElementDataSubscriber(pElement, strKey, pPlayer);
1704+
bool bResult = CStaticFunctionDefinitions::HasElementDataSubscriber(pElement, key.ToCString(), pPlayer);
17051705
lua_pushboolean(luaVM, bResult);
17061706
return 1;
17071707
}

0 commit comments

Comments
 (0)