diff --git a/Client/mods/deathmatch/logic/CClientEntity.cpp b/Client/mods/deathmatch/logic/CClientEntity.cpp index cc4ea3d7057..4585bf2779c 100644 --- a/Client/mods/deathmatch/logic/CClientEntity.cpp +++ b/Client/mods/deathmatch/logic/CClientEntity.cpp @@ -279,12 +279,12 @@ void CClientEntity::SetID(ElementID ID) } } -CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced) +CLuaArgument* CClientEntity::GetCustomData(const CStringName& name, bool bInheritData, bool* pbIsSynced) { - assert(szName); + assert(name); // Grab it and return a pointer to the variable - SCustomData* pData = m_pCustomData->Get(szName); + SCustomData* pData = m_pCustomData->Get(name); if (pData) { if (pbIsSynced) @@ -295,7 +295,7 @@ CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData // If none, try returning parent's custom data if (bInheritData && m_pParent) { - return m_pParent->GetCustomData(szName, true, pbIsSynced); + return m_pParent->GetCustomData(name, true, pbIsSynced); } // None available @@ -315,10 +315,10 @@ CLuaArguments* CClientEntity::GetAllCustomData(CLuaArguments* table) return table; } -bool CClientEntity::GetCustomDataString(const char* szName, SString& strOut, bool bInheritData) +bool CClientEntity::GetCustomDataString(const CStringName& name, SString& strOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -350,10 +350,10 @@ bool CClientEntity::GetCustomDataString(const char* szName, SString& strOut, boo return false; } -bool CClientEntity::GetCustomDataInt(const char* szName, int& iOut, bool bInheritData) +bool CClientEntity::GetCustomDataInt(const CStringName& name, int& iOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -388,10 +388,10 @@ bool CClientEntity::GetCustomDataInt(const char* szName, int& iOut, bool bInheri return false; } -bool CClientEntity::GetCustomDataFloat(const char* szName, float& fOut, bool bInheritData) +bool CClientEntity::GetCustomDataFloat(const CStringName& name, float& fOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -415,10 +415,10 @@ bool CClientEntity::GetCustomDataFloat(const char* szName, float& fOut, bool bIn return false; } -bool CClientEntity::GetCustomDataBool(const char* szName, bool& bOut, bool bInheritData) +bool CClientEntity::GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -470,50 +470,50 @@ bool CClientEntity::GetCustomDataBool(const char* szName, bool& bOut, bool bInhe return false; } -void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized) +void CClientEntity::SetCustomData(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized) { - assert(szName); - if (strlen(szName) > MAX_CUSTOMDATA_NAME_LENGTH) + assert(name); + if (name->length() > MAX_CUSTOMDATA_NAME_LENGTH) { // Don't allow it to be set if the name is too long - CLogger::ErrorPrintf("Custom data name too long (%s)", *SStringX(szName).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1)); + CLogger::ErrorPrintf("Custom data name too long (%s)", *SStringX(name.ToCString()).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1)); return; } // Grab the old variable CLuaArgument oldVariable; - SCustomData* pData = m_pCustomData->Get(szName); + SCustomData* pData = m_pCustomData->Get(name); if (pData) { oldVariable = pData->Variable; } // Set the new data - m_pCustomData->Set(szName, Variable, bSynchronized); + m_pCustomData->Set(name, Variable, bSynchronized); // Trigger the onClientElementDataChange event on us CLuaArguments Arguments; - Arguments.PushString(szName); + Arguments.PushString(name); Arguments.PushArgument(oldVariable); Arguments.PushArgument(Variable); CallEvent("onClientElementDataChange", Arguments, true); } -void CClientEntity::DeleteCustomData(const char* szName) +void CClientEntity::DeleteCustomData(const CStringName& name) { // Grab the old variable - SCustomData* pData = m_pCustomData->Get(szName); + SCustomData* pData = m_pCustomData->Get(name); if (pData) { CLuaArgument oldVariable; oldVariable = pData->Variable; // Delete the custom data - m_pCustomData->Delete(szName); + m_pCustomData->Delete(name); // Trigger the onClientElementDataChange event on us CLuaArguments Arguments; - Arguments.PushString(szName); + Arguments.PushString(name); Arguments.PushArgument(oldVariable); Arguments.PushArgument(CLuaArgument()); // Use nil as the new value to indicate the data has been removed CallEvent("onClientElementDataChange", Arguments, true); diff --git a/Client/mods/deathmatch/logic/CClientEntity.h b/Client/mods/deathmatch/logic/CClientEntity.h index 031c1418b8f..3d345a9fd28 100644 --- a/Client/mods/deathmatch/logic/CClientEntity.h +++ b/Client/mods/deathmatch/logic/CClientEntity.h @@ -16,6 +16,7 @@ class CClientEntity; #include "CClientCommon.h" #include #include "logic/CClientEntityRefManager.h" +#include "CStringName.h" class CLuaFunctionRef; // Used to check fast version of getElementsByType @@ -201,14 +202,14 @@ class CClientEntity : public CClientEntityBase void SetID(ElementID ID); CCustomData* GetCustomDataPointer() { return m_pCustomData; } - CLuaArgument* GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced = nullptr); + CLuaArgument* GetCustomData(const CStringName& name, bool bInheritData, bool* pbIsSynced = nullptr); CLuaArguments* GetAllCustomData(CLuaArguments* table); - 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, bool bSynchronized = true); - void DeleteCustomData(const char* szName); + bool GetCustomDataString(const CStringName& name, SString& strOut, bool bInheritData); + bool GetCustomDataFloat(const CStringName& name, float& fOut, bool bInheritData); + bool GetCustomDataInt(const CStringName& name, int& iOut, bool bInheritData); + bool GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData); + void SetCustomData(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized = true); + void DeleteCustomData(const CStringName& name); virtual bool GetMatrix(CMatrix& matrix) const; virtual bool SetMatrix(const CMatrix& matrix); diff --git a/Client/mods/deathmatch/logic/CCustomData.cpp b/Client/mods/deathmatch/logic/CCustomData.cpp index 8f441030783..355ab5f0973 100644 --- a/Client/mods/deathmatch/logic/CCustomData.cpp +++ b/Client/mods/deathmatch/logic/CCustomData.cpp @@ -14,30 +14,30 @@ void CCustomData::Copy(CCustomData* pCustomData) { - std::map::const_iterator iter = pCustomData->IterBegin(); + auto iter = pCustomData->IterBegin(); for (; iter != pCustomData->IterEnd(); iter++) { - Set(iter->first.c_str(), iter->second.Variable); + Set(iter->first, iter->second.Variable); } } -SCustomData* CCustomData::Get(const char* szName) +SCustomData* CCustomData::Get(const CStringName& name) { - assert(szName); + assert(name); - std::map::iterator it = m_Data.find(szName); + auto it = m_Data.find(name); if (it != m_Data.end()) return &it->second; return NULL; } -void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized) +void CCustomData::Set(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized) { - assert(szName); + assert(name); // Grab the item with the given name - SCustomData* pData = Get(szName); + SCustomData* pData = Get(name); if (pData) { // Update existing @@ -50,14 +50,14 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSy SCustomData newData; newData.Variable = Variable; newData.bSynchronized = bSynchronized; - m_Data[szName] = newData; + m_Data[name] = newData; } } -bool CCustomData::Delete(const char* szName) +bool CCustomData::Delete(const CStringName& name) { // Find the item and delete it - std::map::iterator it = m_Data.find(szName); + auto it = m_Data.find(name); if (it != m_Data.end()) { m_Data.erase(it); diff --git a/Client/mods/deathmatch/logic/CCustomData.h b/Client/mods/deathmatch/logic/CCustomData.h index 01e5eb81cdb..6219750266b 100644 --- a/Client/mods/deathmatch/logic/CCustomData.h +++ b/Client/mods/deathmatch/logic/CCustomData.h @@ -11,6 +11,7 @@ #pragma once #include "lua/CLuaArgument.h" +#include "CStringName.h" #define MAX_CUSTOMDATA_NAME_LENGTH 128 @@ -25,14 +26,14 @@ class CCustomData public: void Copy(CCustomData* pCustomData); - SCustomData* Get(const char* szName); - void Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true); + SCustomData* Get(const CStringName& name); + void Set(const CStringName& name, const CLuaArgument& Variable, bool bSynchronized = true); - bool Delete(const char* szName); + bool Delete(const CStringName& name); - std::map::const_iterator IterBegin() { return m_Data.begin(); } - std::map::const_iterator IterEnd() { return m_Data.end(); } + std::unordered_map::const_iterator IterBegin() { return m_Data.begin(); } + std::unordered_map::const_iterator IterEnd() { return m_Data.end(); } private: - std::map m_Data; + std::unordered_map m_Data; }; diff --git a/Client/mods/deathmatch/logic/CPacketHandler.cpp b/Client/mods/deathmatch/logic/CPacketHandler.cpp index 0e7268ca350..25d3c3e79c7 100644 --- a/Client/mods/deathmatch/logic/CPacketHandler.cpp +++ b/Client/mods/deathmatch/logic/CPacketHandler.cpp @@ -2885,7 +2885,7 @@ void CPacketHandler::Packet_EntityAdd(NetBitStreamInterface& bitStream) CLuaArgument Argument; Argument.ReadFromBitStream(bitStream); - pCustomData->Set(strName, Argument); + pCustomData->Set(CStringName{strName}, Argument); } else { diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 6992f04e26e..59216442a64 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1026,13 +1026,13 @@ bool CStaticFunctionDefinitions::SetElementID(CClientEntity& Entity, const char* return false; } -bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, const char* szName, CLuaArgument& Variable, bool bSynchronize) +bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, CStringName name, CLuaArgument& Variable, bool bSynchronize) { - assert(szName); - assert(strlen(szName) <= MAX_CUSTOMDATA_NAME_LENGTH); + assert(name); + assert(name->length() <= MAX_CUSTOMDATA_NAME_LENGTH); bool bIsSynced; - CLuaArgument* pCurrentVariable = Entity.GetCustomData(szName, false, &bIsSynced); + CLuaArgument* pCurrentVariable = Entity.GetCustomData(name, false, &bIsSynced); if (!pCurrentVariable || Variable != *pCurrentVariable || bIsSynced != bSynchronize) { if (bSynchronize && !Entity.IsLocalEntity()) @@ -1040,9 +1040,9 @@ bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, const cha NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream(); // Write element ID, name length and the name. Also write the variable. pBitStream->Write(Entity.GetID()); - unsigned short usNameLength = static_cast(strlen(szName)); + unsigned short usNameLength = static_cast(name->length()); pBitStream->WriteCompressed(usNameLength); - pBitStream->Write(szName, usNameLength); + pBitStream->Write(name.ToCString(), usNameLength); Variable.WriteToBitStream(*pBitStream); // Send the packet and deallocate @@ -1051,14 +1051,14 @@ bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, const cha } // Set its custom data - Entity.SetCustomData(szName, Variable, bSynchronize); + Entity.SetCustomData(name, Variable, bSynchronize); return true; } return false; } -bool CStaticFunctionDefinitions::RemoveElementData(CClientEntity& Entity, const char* szName) +bool CStaticFunctionDefinitions::RemoveElementData(CClientEntity& Entity, CStringName name) { // TODO return false; diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h index 3a9b4ea5ffc..e1f05f1d108 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h @@ -85,8 +85,8 @@ class CStaticFunctionDefinitions static CClientDummy* CreateElement(CResource& Resource, const char* szTypeName, const char* szID); static bool DestroyElement(CClientEntity& Entity); static bool SetElementID(CClientEntity& Entity, const char* szID); - static bool SetElementData(CClientEntity& Entity, const char* szName, CLuaArgument& Variable, bool bSynchronize); - static bool RemoveElementData(CClientEntity& Entity, const char* szName); + static bool SetElementData(CClientEntity& Entity, CStringName name, CLuaArgument& Variable, bool bSynchronize); + static bool RemoveElementData(CClientEntity& Entity, CStringName name); static bool SetElementMatrix(CClientEntity& Entity, const CMatrix& matrix); static bool SetElementPosition(CClientEntity& Entity, const CVector& vecPosition, bool bWarp = true); static bool SetElementRotation(CClientEntity& Entity, const CVector& vecRotation, eEulerRotationOrder rotationOrder, bool bNewWay); diff --git a/Client/mods/deathmatch/logic/lua/CLuaArgument.cpp b/Client/mods/deathmatch/logic/lua/CLuaArgument.cpp index 7ed7b463c38..e4a577bcea1 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaArgument.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaArgument.cpp @@ -334,6 +334,13 @@ void CLuaArgument::ReadString(const std::string_view& string) m_strString = std::string{string}; } +void CLuaArgument::ReadString(const CStringName& string) +{ + m_iType = LUA_TSTRING; + DeleteTableData(); + m_strString = string.ToString(); +} + void CLuaArgument::ReadString(const char* string) { m_iType = LUA_TSTRING; diff --git a/Client/mods/deathmatch/logic/lua/CLuaArgument.h b/Client/mods/deathmatch/logic/lua/CLuaArgument.h index 3cb5b451904..b47aba8bde0 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaArgument.h +++ b/Client/mods/deathmatch/logic/lua/CLuaArgument.h @@ -17,6 +17,7 @@ extern "C" #include #include #include "json.h" +#include "CStringName.h" class CClientEntity; class CLuaArguments; @@ -42,6 +43,7 @@ class CLuaArgument void ReadNumber(double dNumber); void ReadString(const std::string& string); void ReadString(const std::string_view& string); + void ReadString(const CStringName& string); void ReadString(const char* string); void ReadElement(CClientEntity* pElement); void ReadScriptID(uint uiScriptID); diff --git a/Client/mods/deathmatch/logic/lua/CLuaArguments.cpp b/Client/mods/deathmatch/logic/lua/CLuaArguments.cpp index e30a707a3ef..740e9f5f707 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaArguments.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaArguments.cpp @@ -351,6 +351,14 @@ CLuaArgument* CLuaArguments::PushString(const std::string_view& string) return arg; } +CLuaArgument* CLuaArguments::PushString(const CStringName& string) +{ + CLuaArgument* arg = new CLuaArgument(); + arg->ReadString(string); + m_Arguments.push_back(arg); + return arg; +} + CLuaArgument* CLuaArguments::PushString(const char* string) { CLuaArgument* arg = new CLuaArgument(); diff --git a/Client/mods/deathmatch/logic/lua/CLuaArguments.h b/Client/mods/deathmatch/logic/lua/CLuaArguments.h index 45ec796cbba..508fc426564 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaArguments.h +++ b/Client/mods/deathmatch/logic/lua/CLuaArguments.h @@ -57,6 +57,7 @@ class CLuaArguments CLuaArgument* PushNumber(double dNumber); CLuaArgument* PushString(const std::string& string); CLuaArgument* PushString(const std::string_view& string); + CLuaArgument* PushString(const CStringName& string); CLuaArgument* PushString(const char* string); CLuaArgument* PushElement(CClientEntity* pElement); CLuaArgument* PushArgument(const CLuaArgument& argument); diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp index c5459e917aa..c763e1d3f4a 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp @@ -1789,7 +1789,7 @@ int CLuaElementDefs::SetElementData(lua_State* luaVM) key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH); } - if (CStaticFunctionDefinitions::SetElementData(*pEntity, key.ToCString(), value, bSynchronize)) + if (CStaticFunctionDefinitions::SetElementData(*pEntity, key, value, bSynchronize)) { lua_pushboolean(luaVM, true); return 1; @@ -1828,7 +1828,7 @@ int CLuaElementDefs::RemoveElementData(lua_State* luaVM) key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH); } - if (CStaticFunctionDefinitions::RemoveElementData(*pEntity, key.ToCString())) + if (CStaticFunctionDefinitions::RemoveElementData(*pEntity, key)) { lua_pushboolean(luaVM, true); return 1; diff --git a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp index 40738952991..756c0e10adc 100644 --- a/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp +++ b/Client/mods/deathmatch/logic/rpc/CElementRPCs.cpp @@ -96,7 +96,7 @@ void CElementRPCs::SetElementData(CClientEntity* pSource, NetBitStreamInterface& CLuaArgument Argument; if (bitStream.ReadStringCharacters(strName, usNameLength) && Argument.ReadFromBitStream(bitStream)) { - pSource->SetCustomData(strName, Argument); + pSource->SetCustomData(CStringName{strName}, Argument); } } } @@ -114,7 +114,7 @@ void CElementRPCs::RemoveElementData(CClientEntity* pSource, NetBitStreamInterfa if (bitStream.ReadStringCharacters(strName, usNameLength) && bitStream.ReadBit(bRecursive)) { // Remove that name - pSource->DeleteCustomData(strName); + pSource->DeleteCustomData(CStringName{strName}); } } } diff --git a/Server/mods/deathmatch/logic/CCustomData.cpp b/Server/mods/deathmatch/logic/CCustomData.cpp index 8ccd96a1685..0b3a317cd96 100644 --- a/Server/mods/deathmatch/logic/CCustomData.cpp +++ b/Server/mods/deathmatch/logic/CCustomData.cpp @@ -14,39 +14,39 @@ void CCustomData::Copy(CCustomData* pCustomData) { - std::map::const_iterator iter = pCustomData->IterBegin(); + auto iter = pCustomData->IterBegin(); for (; iter != pCustomData->IterEnd(); iter++) { - Set(iter->first.c_str(), iter->second.Variable); + Set(iter->first, iter->second.Variable); } } -SCustomData* CCustomData::Get(const char* szName) const +SCustomData* CCustomData::Get(const CStringName& name) const { - assert(szName); + assert(name); - std::map::const_iterator it = m_Data.find(szName); + auto it = m_Data.find(name); if (it != m_Data.end()) return (SCustomData*)&it->second; return NULL; } -SCustomData* CCustomData::GetSynced(const char* szName) +SCustomData* CCustomData::GetSynced(const CStringName& name) { - assert(szName); + assert(name); - std::map::const_iterator it = m_SyncedData.find(szName); + auto it = m_SyncedData.find(name); if (it != m_SyncedData.end()) return (SCustomData*)&it->second; return NULL; } -bool CCustomData::DeleteSynced(const char* szName) +bool CCustomData::DeleteSynced(const CStringName& name) { // Find the item and delete it - std::map::iterator iter = m_SyncedData.find(szName); + auto iter = m_SyncedData.find(name); if (iter != m_SyncedData.end()) { m_SyncedData.erase(iter); @@ -57,11 +57,11 @@ bool CCustomData::DeleteSynced(const char* szName) return false; } -void CCustomData::UpdateSynced(const char* szName, const CLuaArgument& Variable, ESyncType syncType) +void CCustomData::UpdateSynced(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType) { if (syncType == ESyncType::BROADCAST) { - SCustomData* pDataSynced = GetSynced(szName); + SCustomData* pDataSynced = GetSynced(name); if (pDataSynced) { pDataSynced->Variable = Variable; @@ -72,27 +72,27 @@ void CCustomData::UpdateSynced(const char* szName, const CLuaArgument& Variable, SCustomData newData; newData.Variable = Variable; newData.syncType = syncType; - m_SyncedData[szName] = newData; + m_SyncedData[name] = newData; } } else { - DeleteSynced(szName); + DeleteSynced(name); } } -void CCustomData::Set(const char* szName, const CLuaArgument& Variable, ESyncType syncType) +void CCustomData::Set(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType) { - assert(szName); + assert(name); // Grab the item with the given name - SCustomData* pData = Get(szName); + SCustomData* pData = Get(name); if (pData) { // Update existing pData->Variable = Variable; pData->syncType = syncType; - UpdateSynced(szName, Variable, syncType); + UpdateSynced(name, Variable, syncType); } else { @@ -101,18 +101,18 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, ESyncTyp newData.Variable = Variable; newData.syncType = syncType; newData.clientChangesMode = eCustomDataClientTrust::UNSET; - m_Data[szName] = newData; - UpdateSynced(szName, Variable, syncType); + m_Data[name] = newData; + UpdateSynced(name, Variable, syncType); } } -bool CCustomData::Delete(const char* szName) +bool CCustomData::Delete(const CStringName& name) { // Find the item and delete it - std::map::iterator it = m_Data.find(szName); + auto it = m_Data.find(name); if (it != m_Data.end()) { - DeleteSynced(szName); + DeleteSynced(name); m_Data.erase(it); return true; } @@ -121,15 +121,15 @@ bool CCustomData::Delete(const char* szName) return false; } -void CCustomData::SetClientChangesMode(const char* szName, eCustomDataClientTrust mode) +void CCustomData::SetClientChangesMode(const CStringName& name, eCustomDataClientTrust mode) { - SCustomData& pData = m_Data[szName]; + SCustomData& pData = m_Data[name]; pData.clientChangesMode = mode; } CXMLNode* CCustomData::OutputToXML(CXMLNode* pNode) { - std::map::const_iterator iter = m_Data.begin(); + auto iter = m_Data.begin(); for (; iter != m_Data.end(); iter++) { CLuaArgument* arg = (CLuaArgument*)&iter->second.Variable; @@ -138,19 +138,19 @@ CXMLNode* CCustomData::OutputToXML(CXMLNode* pNode) { case LUA_TSTRING: { - CXMLAttribute* attr = pNode->GetAttributes().Create(iter->first.c_str()); + CXMLAttribute* attr = pNode->GetAttributes().Create(iter->first.ToCString()); attr->SetValue(arg->GetString().c_str()); break; } case LUA_TNUMBER: { - CXMLAttribute* attr = pNode->GetAttributes().Create(iter->first.c_str()); + CXMLAttribute* attr = pNode->GetAttributes().Create(iter->first.ToCString()); attr->SetValue((float)arg->GetNumber()); break; } case LUA_TBOOLEAN: { - CXMLAttribute* attr = pNode->GetAttributes().Create(iter->first.c_str()); + CXMLAttribute* attr = pNode->GetAttributes().Create(iter->first.ToCString()); attr->SetValue(arg->GetBoolean()); break; } diff --git a/Server/mods/deathmatch/logic/CCustomData.h b/Server/mods/deathmatch/logic/CCustomData.h index 1fa5686780d..da52ba63253 100644 --- a/Server/mods/deathmatch/logic/CCustomData.h +++ b/Server/mods/deathmatch/logic/CCustomData.h @@ -44,28 +44,28 @@ class CCustomData public: void Copy(CCustomData* pCustomData); - SCustomData* Get(const char* szName) const; - SCustomData* GetSynced(const char* szName); - void Set(const char* szName, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST); + SCustomData* Get(const CStringName& name) const; + SCustomData* GetSynced(const CStringName& name); + void Set(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST); - bool Delete(const char* szName); + bool Delete(const CStringName& name); - void SetClientChangesMode(const char* szName, eCustomDataClientTrust mode); + void SetClientChangesMode(const CStringName& name, eCustomDataClientTrust mode); unsigned short CountOnlySynchronized(); CXMLNode* OutputToXML(CXMLNode* pNode); - std::map::const_iterator IterBegin() { return m_Data.begin(); } - std::map::const_iterator IterEnd() { return m_Data.end(); } + std::unordered_map::const_iterator IterBegin() { return m_Data.begin(); } + std::unordered_map::const_iterator IterEnd() { return m_Data.end(); } - std::map::const_iterator SyncedIterBegin() { return m_SyncedData.begin(); } - std::map::const_iterator SyncedIterEnd() { return m_SyncedData.end(); } + std::unordered_map::const_iterator SyncedIterBegin() { return m_SyncedData.begin(); } + std::unordered_map::const_iterator SyncedIterEnd() { return m_SyncedData.end(); } private: - bool DeleteSynced(const char* szName); - void UpdateSynced(const char* szName, const CLuaArgument& Variable, ESyncType syncType); + bool DeleteSynced(const CStringName& name); + void UpdateSynced(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType); - std::map m_Data; - std::map m_SyncedData; + std::unordered_map m_Data; + std::unordered_map m_SyncedData; }; diff --git a/Server/mods/deathmatch/logic/CElement.cpp b/Server/mods/deathmatch/logic/CElement.cpp index 08571d68332..6e190abe9f1 100644 --- a/Server/mods/deathmatch/logic/CElement.cpp +++ b/Server/mods/deathmatch/logic/CElement.cpp @@ -508,12 +508,12 @@ void CElement::ReadCustomData(CEvents* pEvents, CXMLNode& Node) } } -CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType, eCustomDataClientTrust* clientChangesMode) +CLuaArgument* CElement::GetCustomData(const CStringName& name, bool bInheritData, ESyncType* pSyncType, eCustomDataClientTrust* clientChangesMode) { - assert(szName); + assert(name); // Grab it and return a pointer to the variable - SCustomData* pData = m_CustomData.Get(szName); + SCustomData* pData = m_CustomData.Get(name); if (pData) { if (pSyncType) @@ -528,7 +528,7 @@ CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESy // If none, try returning parent's custom data if (bInheritData && m_pParent) { - return m_pParent->GetCustomData(szName, true, pSyncType, clientChangesMode); + return m_pParent->GetCustomData(name, true, pSyncType, clientChangesMode); } // None available @@ -540,20 +540,20 @@ CLuaArguments* CElement::GetAllCustomData(CLuaArguments* table) assert(table); // Grab it and return a pointer to the variable - map::const_iterator iter = m_CustomData.IterBegin(); + auto iter = m_CustomData.IterBegin(); for (; iter != m_CustomData.IterEnd(); iter++) { - table->PushString(iter->first.c_str()); // key + table->PushString(iter->first); // key table->PushArgument(iter->second.Variable); // value } return table; } -bool CElement::GetCustomDataString(const char* szName, char* pOut, size_t sizeBuffer, bool bInheritData) +bool CElement::GetCustomDataString(const CStringName& name, char* pOut, size_t sizeBuffer, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Make sure it gets 0 terminated @@ -589,10 +589,10 @@ bool CElement::GetCustomDataString(const char* szName, char* pOut, size_t sizeBu return false; } -bool CElement::GetCustomDataInt(const char* szName, int& iOut, bool bInheritData) +bool CElement::GetCustomDataInt(const CStringName& name, int& iOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -627,10 +627,10 @@ bool CElement::GetCustomDataInt(const char* szName, int& iOut, bool bInheritData return false; } -bool CElement::GetCustomDataFloat(const char* szName, float& fOut, bool bInheritData) +bool CElement::GetCustomDataFloat(const CStringName& name, float& fOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -654,10 +654,10 @@ bool CElement::GetCustomDataFloat(const char* szName, float& fOut, bool bInherit return false; } -bool CElement::GetCustomDataBool(const char* szName, bool& bOut, bool bInheritData) +bool CElement::GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData) { // Grab the custom data variable - CLuaArgument* pData = GetCustomData(szName, bInheritData); + CLuaArgument* pData = GetCustomData(name, bInheritData); if (pData) { // Write the content depending on what type it is @@ -709,53 +709,53 @@ bool CElement::GetCustomDataBool(const char* szName, bool& bOut, bool bInheritDa return false; } -void CElement::SetCustomData(const char* szName, const CLuaArgument& Variable, ESyncType syncType, CPlayer* pClient, bool bTriggerEvent) +void CElement::SetCustomData(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType, CPlayer* pClient, bool bTriggerEvent) { - assert(szName); - if (strlen(szName) > MAX_CUSTOMDATA_NAME_LENGTH) + assert(name); + if (name->length() > MAX_CUSTOMDATA_NAME_LENGTH) { // Don't allow it to be set if the name is too long - CLogger::ErrorPrintf("Custom data name too long (%s)\n", *SStringX(szName).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1)); + CLogger::ErrorPrintf("Custom data name too long (%s)\n", *SStringX(name.ToCString()).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1)); return; } // Grab the old variable CLuaArgument oldVariable; - const SCustomData* pData = m_CustomData.Get(szName); + const SCustomData* pData = m_CustomData.Get(name); if (pData) { oldVariable = pData->Variable; } // Set the new data - m_CustomData.Set(szName, Variable, syncType); + m_CustomData.Set(name, Variable, syncType); if (bTriggerEvent) { // Trigger the onElementDataChange event on us CLuaArguments Arguments; - Arguments.PushString(szName); + Arguments.PushString(name); Arguments.PushArgument(oldVariable); Arguments.PushArgument(Variable); CallEvent("onElementDataChange", Arguments, pClient); } } -void CElement::DeleteCustomData(const char* szName) +void CElement::DeleteCustomData(const CStringName& name) { // Grab the old variable - SCustomData* pData = m_CustomData.Get(szName); + SCustomData* pData = m_CustomData.Get(name); if (pData) { CLuaArgument oldVariable; oldVariable = pData->Variable; // Delete the custom data - m_CustomData.Delete(szName); + m_CustomData.Delete(name); // Trigger the onElementDataChange event on us CLuaArguments Arguments; - Arguments.PushString(szName); + Arguments.PushString(name); Arguments.PushArgument(oldVariable); Arguments.PushArgument(CLuaArgument()); // Use nil as the new value to indicate the data has been removed CallEvent("onElementDataChange", Arguments); @@ -765,22 +765,22 @@ void CElement::DeleteCustomData(const char* szName) // Used to send the root element data when a player joins void CElement::SendAllCustomData(CPlayer* pPlayer) { - for (map::const_iterator iter = m_CustomData.SyncedIterBegin(); iter != m_CustomData.SyncedIterEnd(); ++iter) + for (auto iter = m_CustomData.SyncedIterBegin(); iter != m_CustomData.SyncedIterEnd(); ++iter) { - const std::string& strName = iter->first; + const CStringName& name = iter->first; const SCustomData& customData = iter->second; if (customData.syncType == ESyncType::LOCAL) continue; // Tell our clients to update their data - unsigned short usNameLength = static_cast(strName.length()); + unsigned short usNameLength = static_cast(name->length()); CBitStream BitStream; BitStream.pBitStream->WriteCompressed(usNameLength); - BitStream.pBitStream->Write(strName.c_str(), usNameLength); + BitStream.pBitStream->Write(name.ToCString(), usNameLength); customData.Variable.WriteToBitStream(*BitStream.pBitStream); - if (customData.syncType == ESyncType::BROADCAST || pPlayer->IsSubscribed(this, strName)) + if (customData.syncType == ESyncType::BROADCAST || pPlayer->IsSubscribed(this, name)) pPlayer->Send(CElementRPCPacket(this, SET_ELEMENT_DATA, *BitStream.pBitStream)); } } diff --git a/Server/mods/deathmatch/logic/CElement.h b/Server/mods/deathmatch/logic/CElement.h index 9660c8b8732..42e33c89064 100644 --- a/Server/mods/deathmatch/logic/CElement.h +++ b/Server/mods/deathmatch/logic/CElement.h @@ -20,6 +20,7 @@ #include #include "Enums.h" #include "CElementGroup.h" +#include "CStringName.h" // Used to check fast version of getElementsByType // #define CHECK_ENTITIES_FROM_ROOT MTA_DEBUG @@ -138,15 +139,15 @@ class CElement void ReadCustomData(CEvents* pEvents, CXMLNode& Node); CCustomData& GetCustomDataManager() { return m_CustomData; } - CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = nullptr, eCustomDataClientTrust* clientChangesMode = nullptr); + CLuaArgument* GetCustomData(const CStringName& name, bool bInheritData, ESyncType* pSyncType = nullptr, eCustomDataClientTrust* clientChangesMode = nullptr); CLuaArguments* GetAllCustomData(CLuaArguments* table); - bool GetCustomDataString(const char* szName, char* pOut, size_t sizeBuffer, bool bInheritData); - bool GetCustomDataInt(const char* szName, int& iOut, bool bInheritData); - bool GetCustomDataFloat(const char* szName, float& fOut, bool bInheritData); - bool GetCustomDataBool(const char* szName, bool& bOut, bool bInheritData); - void SetCustomData(const char* szName, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST, CPlayer* pClient = NULL, + bool GetCustomDataString(const CStringName& name, char* pOut, size_t sizeBuffer, bool bInheritData); + bool GetCustomDataInt(const CStringName& name, int& iOut, bool bInheritData); + bool GetCustomDataFloat(const CStringName& name, float& fOut, bool bInheritData); + bool GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData); + void SetCustomData(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST, CPlayer* pClient = NULL, bool bTriggerEvent = true); - void DeleteCustomData(const char* szName); + void DeleteCustomData(const CStringName& name); void SendAllCustomData(CPlayer* pPlayer); CXMLNode* OutputToXML(CXMLNode* pNode); diff --git a/Server/mods/deathmatch/logic/CPlayer.cpp b/Server/mods/deathmatch/logic/CPlayer.cpp index 35164b175e4..c57ebc3aaac 100644 --- a/Server/mods/deathmatch/logic/CPlayer.cpp +++ b/Server/mods/deathmatch/logic/CPlayer.cpp @@ -217,16 +217,16 @@ bool CPlayer::ShouldIgnoreMinClientVersionChecks() return false; } -bool CPlayer::SubscribeElementData(CElement* pElement, const std::string& strName) +bool CPlayer::SubscribeElementData(CElement* pElement, CStringName name) { - OutputDebugLine(SString("[Data] SubscribeElementData %s [%s]", GetNick(), strName.c_str())); - return m_DataSubscriptions.emplace(std::make_pair(pElement, strName)).second; + OutputDebugLine(SString("[Data] SubscribeElementData %s [%s]", GetNick(), name.ToCString())); + return m_DataSubscriptions.emplace(std::make_pair(pElement, name)).second; } -bool CPlayer::UnsubscribeElementData(CElement* pElement, const std::string& strName) +bool CPlayer::UnsubscribeElementData(CElement* pElement, CStringName name) { - OutputDebugLine(SString("[Data] UnsubscribeElementData %s [%s]", GetNick(), strName.c_str())); - return m_DataSubscriptions.erase(std::make_pair(pElement, strName)) > 0; + OutputDebugLine(SString("[Data] UnsubscribeElementData %s [%s]", GetNick(), name.ToCString())); + return m_DataSubscriptions.erase(std::make_pair(pElement, name)) > 0; } bool CPlayer::UnsubscribeElementData(CElement* pElement) @@ -237,7 +237,7 @@ bool CPlayer::UnsubscribeElementData(CElement* pElement) { if (it->first == pElement) { - OutputDebugLine(SString("[Data] UnsubscribeElementData %s [%s]", GetNick(), it->second.c_str())); + OutputDebugLine(SString("[Data] UnsubscribeElementData %s [%s]", GetNick(), it->second.ToCString())); it = m_DataSubscriptions.erase(it); erased = true; } @@ -248,9 +248,9 @@ bool CPlayer::UnsubscribeElementData(CElement* pElement) return erased; } -bool CPlayer::IsSubscribed(CElement* pElement, const std::string& strName) const +bool CPlayer::IsSubscribed(CElement* pElement, CStringName name) const { - return m_DataSubscriptions.find(std::make_pair(pElement, strName)) != m_DataSubscriptions.end(); + return m_DataSubscriptions.find(std::make_pair(pElement, name)) != m_DataSubscriptions.end(); } const char* CPlayer::GetSourceIP() diff --git a/Server/mods/deathmatch/logic/CPlayer.h b/Server/mods/deathmatch/logic/CPlayer.h index 97e944b7673..2db065e1247 100644 --- a/Server/mods/deathmatch/logic/CPlayer.h +++ b/Server/mods/deathmatch/logic/CPlayer.h @@ -23,6 +23,7 @@ class CPlayer; #include "CObject.h" #include "packets/CPacket.h" #include "packets/CPlayerStatsPacket.h" +#include "CStringName.h" class CKeyBinds; class CPlayerCamera; enum class eVehicleAimDirection : unsigned char; @@ -104,10 +105,10 @@ class CPlayer final : public CPed, public CClient bool IsJoined() { return m_bIsJoined; } void SetJoined() { m_bIsJoined = true; } - bool SubscribeElementData(CElement* pElement, const std::string& strName); - bool UnsubscribeElementData(CElement* pElement, const std::string& strName); + bool SubscribeElementData(CElement* pElement, CStringName name); + bool UnsubscribeElementData(CElement* pElement, CStringName name); bool UnsubscribeElementData(CElement* pElement); - bool IsSubscribed(CElement* pElement, const std::string& strName) const; + bool IsSubscribed(CElement* pElement, CStringName name) const; float GetCameraRotation() { return m_fCameraRotation; }; void SetCameraRotation(float fRotation) { m_fCameraRotation = fRotation; }; @@ -432,7 +433,7 @@ class CPlayer final : public CPed, public CClient std::map m_AnnounceValues; - std::set> m_DataSubscriptions; + std::set> m_DataSubscriptions; uint m_uiWeaponIncorrectCount; diff --git a/Server/mods/deathmatch/logic/CPlayerManager.cpp b/Server/mods/deathmatch/logic/CPlayerManager.cpp index 7075a1dba88..ab71d7964b9 100644 --- a/Server/mods/deathmatch/logic/CPlayerManager.cpp +++ b/Server/mods/deathmatch/logic/CPlayerManager.cpp @@ -351,7 +351,7 @@ bool CPlayerManager::IsValidPlayerModel(unsigned short model) } } -void CPlayerManager::ClearElementData(CElement* pElement, const std::string& name) +void CPlayerManager::ClearElementData(CElement* pElement, CStringName name) { list::const_iterator iter = m_Players.begin(); for (; iter != m_Players.end(); iter++) diff --git a/Server/mods/deathmatch/logic/CPlayerManager.h b/Server/mods/deathmatch/logic/CPlayerManager.h index b1097c03354..bc383a021fc 100644 --- a/Server/mods/deathmatch/logic/CPlayerManager.h +++ b/Server/mods/deathmatch/logic/CPlayerManager.h @@ -56,7 +56,7 @@ class CPlayerManager static bool IsValidPlayerModel(unsigned short model); - void ClearElementData(CElement* pElement, const std::string& name); + void ClearElementData(CElement* pElement, CStringName name); void ClearElementData(CElement* pElement); void ResetAll(); diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index aadb739ac3a..9ca3da17d43 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -478,13 +478,13 @@ CElement* CStaticFunctionDefinitions::GetElementByIndex(const char* szType, unsi return m_pMapManager->GetRootElement()->FindChildByType(szType, uiIndex, true); } -CLuaArgument* CStaticFunctionDefinitions::GetElementData(CElement* pElement, const char* szName, bool bInherit) +CLuaArgument* CStaticFunctionDefinitions::GetElementData(CElement* pElement, CStringName name, bool bInherit) { assert(pElement); - assert(szName); + assert(name); // Return its custom data - return pElement->GetCustomData(szName, bInherit); + return pElement->GetCustomData(name, bInherit); } CLuaArguments* CStaticFunctionDefinitions::GetAllElementData(CElement* pElement, CLuaArguments* table) @@ -998,71 +998,71 @@ bool CStaticFunctionDefinitions::SetElementID(CElement* pElement, const char* sz return true; } -bool CStaticFunctionDefinitions::SetElementData(CElement* pElement, const char* szName, const CLuaArgument& Variable, ESyncType syncType, +bool CStaticFunctionDefinitions::SetElementData(CElement* pElement, CStringName name, const CLuaArgument& Variable, ESyncType syncType, std::optional clientTrust) { assert(pElement); - assert(szName); - assert(strlen(szName) <= MAX_CUSTOMDATA_NAME_LENGTH); + assert(name); + assert(name->length() <= MAX_CUSTOMDATA_NAME_LENGTH); ESyncType lastSyncType = ESyncType::BROADCAST; eCustomDataClientTrust lastClientTrust{}; - CLuaArgument* pCurrentVariable = pElement->GetCustomData(szName, false, &lastSyncType, &lastClientTrust); + CLuaArgument* pCurrentVariable = pElement->GetCustomData(name, false, &lastSyncType, &lastClientTrust); if (clientTrust.has_value() && lastClientTrust != clientTrust.value()) - pElement->GetCustomDataManager().SetClientChangesMode(szName, clientTrust.value()); + pElement->GetCustomDataManager().SetClientChangesMode(name, clientTrust.value()); if (!pCurrentVariable || *pCurrentVariable != Variable || lastSyncType != syncType) { if (syncType != ESyncType::LOCAL) { // Tell our clients to update their data - unsigned short usNameLength = static_cast(strlen(szName)); + unsigned short usNameLength = static_cast(name->length()); CBitStream BitStream; BitStream.pBitStream->WriteCompressed(usNameLength); - BitStream.pBitStream->Write(szName, usNameLength); + BitStream.pBitStream->Write(name.ToCString(), usNameLength); Variable.WriteToBitStream(*BitStream.pBitStream); const CElementRPCPacket packet(pElement, SET_ELEMENT_DATA, *BitStream.pBitStream); const size_t numPlayers = syncType == ESyncType::BROADCAST ? m_pPlayerManager->BroadcastOnlyJoined(packet) - : m_pPlayerManager->BroadcastOnlySubscribed(packet, pElement, szName); + : m_pPlayerManager->BroadcastOnlySubscribed(packet, pElement, name.ToCString()); - CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageOut(szName, numPlayers, BitStream.pBitStream->GetNumberOfBytesUsed()); + CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageOut(name.ToCString(), numPlayers, BitStream.pBitStream->GetNumberOfBytesUsed()); } // Unsubscribe all the players if (lastSyncType == ESyncType::SUBSCRIBE && syncType != ESyncType::SUBSCRIBE) - m_pPlayerManager->ClearElementData(pElement, szName); + m_pPlayerManager->ClearElementData(pElement, name); // Set its custom data - pElement->SetCustomData(szName, Variable, syncType); + pElement->SetCustomData(name, Variable, syncType); return true; } return false; } -bool CStaticFunctionDefinitions::RemoveElementData(CElement* pElement, const char* szName) +bool CStaticFunctionDefinitions::RemoveElementData(CElement* pElement, CStringName name) { assert(pElement); - assert(szName); - assert(strlen(szName) <= MAX_CUSTOMDATA_NAME_LENGTH); + assert(name); + assert(name->length() <= MAX_CUSTOMDATA_NAME_LENGTH); // Check it exists - if (pElement->GetCustomData(szName, false)) + if (pElement->GetCustomData(name, false)) { // Tell our clients to update their data - unsigned short usNameLength = static_cast(strlen(szName)); + unsigned short usNameLength = static_cast(name->length()); CBitStream BitStream; BitStream.pBitStream->WriteCompressed(usNameLength); - BitStream.pBitStream->Write(szName, usNameLength); + BitStream.pBitStream->Write(name.ToCString(), usNameLength); BitStream.pBitStream->WriteBit(false); // Unused (was recursive flag) m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pElement, REMOVE_ELEMENT_DATA, *BitStream.pBitStream)); // Clean up after the data removal - m_pPlayerManager->ClearElementData(pElement, szName); + m_pPlayerManager->ClearElementData(pElement, name); // Delete here - pElement->DeleteCustomData(szName); + pElement->DeleteCustomData(name); return true; } @@ -1070,30 +1070,30 @@ bool CStaticFunctionDefinitions::RemoveElementData(CElement* pElement, const cha return false; } -bool CStaticFunctionDefinitions::AddElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer) +bool CStaticFunctionDefinitions::AddElementDataSubscriber(CElement* pElement, CStringName name, CPlayer* pPlayer) { assert(pElement); - assert(szName); + assert(name); assert(pPlayer); ESyncType lastSyncType = ESyncType::LOCAL; - CLuaArgument* pCurrentVariable = pElement->GetCustomData(szName, false, &lastSyncType); + CLuaArgument* pCurrentVariable = pElement->GetCustomData(name, false, &lastSyncType); if (pCurrentVariable != nullptr && lastSyncType == ESyncType::SUBSCRIBE) { - if (!pPlayer->SubscribeElementData(pElement, szName)) + if (!pPlayer->SubscribeElementData(pElement, name)) return false; // Tell our clients to update their data - unsigned short usNameLength = static_cast(strlen(szName)); + unsigned short usNameLength = static_cast(name->length()); CBitStream BitStream; BitStream.pBitStream->WriteCompressed(usNameLength); - BitStream.pBitStream->Write(szName, usNameLength); + BitStream.pBitStream->Write(name.ToCString(), usNameLength); pCurrentVariable->WriteToBitStream(*BitStream.pBitStream); pPlayer->Send(CElementRPCPacket(pElement, SET_ELEMENT_DATA, *BitStream.pBitStream)); - CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageOut(szName, 1, BitStream.pBitStream->GetNumberOfBytesUsed()); + CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageOut(name.ToCString(), 1, BitStream.pBitStream->GetNumberOfBytesUsed()); return true; } @@ -1101,22 +1101,22 @@ bool CStaticFunctionDefinitions::AddElementDataSubscriber(CElement* pElement, co return false; } -bool CStaticFunctionDefinitions::RemoveElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer) +bool CStaticFunctionDefinitions::RemoveElementDataSubscriber(CElement* pElement, CStringName name, CPlayer* pPlayer) { assert(pElement); - assert(szName); + assert(name); assert(pPlayer); - return pPlayer->UnsubscribeElementData(pElement, szName); + return pPlayer->UnsubscribeElementData(pElement, name); } -bool CStaticFunctionDefinitions::HasElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer) +bool CStaticFunctionDefinitions::HasElementDataSubscriber(CElement* pElement, CStringName name, CPlayer* pPlayer) { assert(pElement); - assert(szName); + assert(name); assert(pPlayer); - return pPlayer->IsSubscribed(pElement, szName); + return pPlayer->IsSubscribed(pElement, name); } bool CStaticFunctionDefinitions::SetElementParent(CElement* pElement, CElement* pParent) diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h index b024b1136aa..388f38b4439 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h @@ -51,7 +51,7 @@ class CStaticFunctionDefinitions static CElement* GetElementByIndex(const char* szType, unsigned int uiIndex); static CElement* GetElementChild(CElement* pElement, unsigned int uiIndex); static bool GetElementChildrenCount(CElement* pElement, unsigned int& uiCount); - static CLuaArgument* GetElementData(CElement* pElement, const char* szName, bool bInherit); + static CLuaArgument* GetElementData(CElement* pElement, CStringName name, bool bInherit); static CLuaArguments* GetAllElementData(CElement* pElement, CLuaArguments* table); static CElement* GetElementParent(CElement* pElement); static bool GetElementMatrix(CElement* pElement, CMatrix& matrix); @@ -83,12 +83,12 @@ class CStaticFunctionDefinitions // Element set funcs static bool ClearElementVisibleTo(CElement* pElement); static bool SetElementID(CElement* pElement, const char* szID); - static bool SetElementData(CElement* pElement, const char* szName, const CLuaArgument& Variable, ESyncType syncType, + static bool SetElementData(CElement* pElement, CStringName name, const CLuaArgument& Variable, ESyncType syncType, std::optional clientTrust); - static bool RemoveElementData(CElement* pElement, const char* szName); - static bool AddElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer); - static bool RemoveElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer); - static bool HasElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer); + static bool RemoveElementData(CElement* pElement, CStringName name); + static bool AddElementDataSubscriber(CElement* pElement, CStringName name, CPlayer* pPlayer); + static bool RemoveElementDataSubscriber(CElement* pElement, CStringName name, CPlayer* pPlayer); + static bool HasElementDataSubscriber(CElement* pElement, CStringName name, CPlayer* pPlayer); static bool SetElementParent(CElement* pElement, CElement* pParent); static bool SetElementMatrix(CElement* pElement, const CMatrix& matrix); static bool SetElementPosition(CElement* pElement, const CVector& vecPosition, bool bWarp = true); diff --git a/Server/mods/deathmatch/logic/lua/CLuaArgument.cpp b/Server/mods/deathmatch/logic/lua/CLuaArgument.cpp index b189658ee97..c245247d5d9 100644 --- a/Server/mods/deathmatch/logic/lua/CLuaArgument.cpp +++ b/Server/mods/deathmatch/logic/lua/CLuaArgument.cpp @@ -339,6 +339,13 @@ void CLuaArgument::ReadString(const std::string_view& string) m_strString = string; } +void CLuaArgument::ReadString(const CStringName& string) +{ + m_iType = LUA_TSTRING; + DeleteTableData(); + m_strString = string.ToString(); +} + void CLuaArgument::ReadString(const char* string) { m_iType = LUA_TSTRING; diff --git a/Server/mods/deathmatch/logic/lua/CLuaArgument.h b/Server/mods/deathmatch/logic/lua/CLuaArgument.h index db11a1676b4..0414975ee51 100644 --- a/Server/mods/deathmatch/logic/lua/CLuaArgument.h +++ b/Server/mods/deathmatch/logic/lua/CLuaArgument.h @@ -45,6 +45,7 @@ class CLuaArgument void ReadNumber(double dNumber); void ReadString(const std::string& string); void ReadString(const std::string_view& string); + void ReadString(const CStringName& string); void ReadString(const char* string); void ReadElement(CElement* pElement); void ReadElementID(ElementID ID); diff --git a/Server/mods/deathmatch/logic/lua/CLuaArguments.cpp b/Server/mods/deathmatch/logic/lua/CLuaArguments.cpp index dd0458e821b..245838165ce 100644 --- a/Server/mods/deathmatch/logic/lua/CLuaArguments.cpp +++ b/Server/mods/deathmatch/logic/lua/CLuaArguments.cpp @@ -372,6 +372,14 @@ CLuaArgument* CLuaArguments::PushString(const std::string_view& string) return arg; } +CLuaArgument* CLuaArguments::PushString(const CStringName& string) +{ + CLuaArgument* arg = new CLuaArgument(); + arg->ReadString(string); + m_Arguments.push_back(arg); + return arg; +} + CLuaArgument* CLuaArguments::PushString(const char* string) { CLuaArgument* arg = new CLuaArgument(); diff --git a/Server/mods/deathmatch/logic/lua/CLuaArguments.h b/Server/mods/deathmatch/logic/lua/CLuaArguments.h index c41e58eeeee..9550235fcd7 100644 --- a/Server/mods/deathmatch/logic/lua/CLuaArguments.h +++ b/Server/mods/deathmatch/logic/lua/CLuaArguments.h @@ -69,6 +69,7 @@ class CLuaArguments CLuaArgument* PushNumber(double dNumber); CLuaArgument* PushString(const std::string& string); CLuaArgument* PushString(const std::string_view& string); + CLuaArgument* PushString(const CStringName& string); CLuaArgument* PushString(const char* string); CLuaArgument* PushElement(CElement* pElement); CLuaArgument* PushBan(CBan* pBan); diff --git a/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp b/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp index c13299374cc..7d339104fb3 100644 --- a/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp +++ b/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp @@ -1579,7 +1579,7 @@ int CLuaElementDefs::setElementData(lua_State* luaVM) key = key->substr(0, MAX_CUSTOMDATA_NAME_LENGTH); } - if (CStaticFunctionDefinitions::SetElementData(pElement, key.ToCString(), value, syncType, clientTrust)) + if (CStaticFunctionDefinitions::SetElementData(pElement, key, value, syncType, clientTrust)) { lua_pushboolean(luaVM, true); return 1; diff --git a/Server/mods/deathmatch/logic/packets/CEntityAddPacket.cpp b/Server/mods/deathmatch/logic/packets/CEntityAddPacket.cpp index 5e7fca237be..f87a776f1ea 100644 --- a/Server/mods/deathmatch/logic/packets/CEntityAddPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CEntityAddPacket.cpp @@ -177,15 +177,15 @@ bool CEntityAddPacket::Write(NetBitStreamInterface& BitStream) const // Write custom data CCustomData& pCustomData = pElement->GetCustomDataManager(); BitStream.WriteCompressed(pCustomData.CountOnlySynchronized()); - map::const_iterator iter = pCustomData.SyncedIterBegin(); + auto iter = pCustomData.SyncedIterBegin(); for (; iter != pCustomData.SyncedIterEnd(); ++iter) { - const char* szName = iter->first.c_str(); + const CStringName name = iter->first; const CLuaArgument* pArgument = &iter->second.Variable; - unsigned char ucNameLength = static_cast(strlen(szName)); + unsigned char ucNameLength = static_cast(name->length()); BitStream.Write(ucNameLength); - BitStream.Write(szName, ucNameLength); + BitStream.Write(name.ToCString(), ucNameLength); pArgument->WriteToBitStream(BitStream); } diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp index 32d221f66d9..b85b7dfc4ce 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp @@ -42,9 +42,9 @@ int CLuaElementDefs::GetElementData(lua_State* luaVM) } #ifdef MTA_CLIENT - CLuaArgument* pVariable = pElement->GetCustomData(key.ToCString(), bInherit); + CLuaArgument* pVariable = pElement->GetCustomData(key, bInherit); #else - CLuaArgument* pVariable = CStaticFunctionDefinitions::GetElementData(pElement, key.ToCString(), bInherit); + CLuaArgument* pVariable = CStaticFunctionDefinitions::GetElementData(pElement, key, bInherit); #endif if (pVariable) {