Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions Client/mods/deathmatch/logic/CClientEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions Client/mods/deathmatch/logic/CClientEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CClientEntity;
#include "CClientCommon.h"
#include <core/CClientEntityBase.h>
#include "logic/CClientEntityRefManager.h"
#include "CStringName.h"
class CLuaFunctionRef;

// Used to check fast version of getElementsByType
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions Client/mods/deathmatch/logic/CCustomData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@

void CCustomData::Copy(CCustomData* pCustomData)
{
std::map<std::string, SCustomData>::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<std::string, SCustomData>::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
Expand All @@ -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<std::string, SCustomData>::iterator it = m_Data.find(szName);
auto it = m_Data.find(name);
if (it != m_Data.end())
{
m_Data.erase(it);
Expand Down
13 changes: 7 additions & 6 deletions Client/mods/deathmatch/logic/CCustomData.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#pragma once

#include "lua/CLuaArgument.h"
#include "CStringName.h"

#define MAX_CUSTOMDATA_NAME_LENGTH 128

Expand All @@ -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<std::string, SCustomData>::const_iterator IterBegin() { return m_Data.begin(); }
std::map<std::string, SCustomData>::const_iterator IterEnd() { return m_Data.end(); }
std::unordered_map<CStringName, SCustomData>::const_iterator IterBegin() { return m_Data.begin(); }
std::unordered_map<CStringName, SCustomData>::const_iterator IterEnd() { return m_Data.end(); }

private:
std::map<std::string, SCustomData> m_Data;
std::unordered_map<CStringName, SCustomData> m_Data;
};
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
16 changes: 8 additions & 8 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,23 +1026,23 @@ 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())
{
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<unsigned short>(strlen(szName));
unsigned short usNameLength = static_cast<unsigned short>(name->length());
pBitStream->WriteCompressed(usNameLength);
pBitStream->Write(szName, usNameLength);
pBitStream->Write(name.ToCString(), usNameLength);
Variable.WriteToBitStream(*pBitStream);

// Send the packet and deallocate
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C"
#include <net/bitstream.h>
#include <string>
#include "json.h"
#include "CStringName.h"

class CClientEntity;
class CLuaArguments;
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/lua/CLuaArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading