Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/CNetAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum eServerRPCFunctions
KEY_BIND,
CURSOR_EVENT,
REQUEST_STEALTH_KILL,
REMOVE_ELEMENT_DATA_RPC,
};

class CNetAPI
Expand Down
26 changes: 25 additions & 1 deletion Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,31 @@ bool CStaticFunctionDefinitions::SetElementData(CClientEntity& Entity, CStringNa

bool CStaticFunctionDefinitions::RemoveElementData(CClientEntity& Entity, CStringName name)
{
// TODO
assert(name);
assert(name->length() <= MAX_CUSTOMDATA_NAME_LENGTH);

bool isSynced;
CLuaArgument* currentVariable = Entity.GetCustomData(name, false, &isSynced);
if (currentVariable)
Comment thread
ArranTuna marked this conversation as resolved.
Outdated
{
if (isSynced && !Entity.IsLocalEntity())
{
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
// Write element ID, name length and name for server-side removal handling
pBitStream->Write(Entity.GetID());
std::uint16_t nameLength = static_cast<std::uint16_t>(name->length());
pBitStream->WriteCompressed(nameLength);
pBitStream->Write(name.ToCString(), nameLength);
Comment thread
ArranTuna marked this conversation as resolved.
Outdated

// Send RPC and deallocate
g_pClientGame->GetNetAPI()->RPC(REMOVE_ELEMENT_DATA_RPC, pBitStream);
g_pNet->DeallocateNetBitStream(pBitStream);
Comment thread
ArranTuna marked this conversation as resolved.
Outdated
}

Entity.DeleteCustomData(name);
return true;
}

return false;
}

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 @@ -77,7 +77,7 @@ void CLuaElementDefs::LoadFunctions()
{"setElementID", SetElementID},
{"setElementParent", SetElementParent},
{"setElementData", SetElementData},
// {"removeElementData", RemoveElementData}, TODO Clientside
{"removeElementData", RemoveElementData},
{"setElementMatrix", SetElementMatrix},
{"setElementPosition", SetElementPosition},
{"setElementRotation", SetElementRotation},
Expand Down Expand Up @@ -121,7 +121,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "attach", "attachElements");
lua_classfunction(luaVM, "detach", "detachElements");
lua_classfunction(luaVM, "destroy", "destroyElement");

lua_classfunction(luaVM, "removeData", "removeElementData");
// Get functions
lua_classfunction(luaVM, "getCollisionsEnabled", "getElementCollisionsEnabled");
lua_classfunction(luaVM, "isWithinColShape", "isElementWithinColShape");
Expand Down
4 changes: 2 additions & 2 deletions Server/mods/deathmatch/logic/CElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ bool CElement::SetCustomData(const CStringName& name, const CLuaArgument& Variab
return true;
}

bool CElement::DeleteCustomData(const CStringName& name)
bool CElement::DeleteCustomData(const CStringName& name, CPlayer* pClient)
{
// Grab the old variable
SCustomData* pData = m_CustomData.Get(name);
Expand All @@ -770,7 +770,7 @@ bool CElement::DeleteCustomData(const CStringName& name)
Arguments.PushString(name);
Arguments.PushArgument(oldVariable);
Arguments.PushArgument(CLuaArgument()); // Use nil as the new value to indicate the data has been removed
if (!CallEvent("onElementDataChange", Arguments))
if (!CallEvent("onElementDataChange", Arguments, pClient))
{
// Event was cancelled, restore previous value
m_CustomData.Set(name, oldVariable, oldSyncType);
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class CElement
bool GetCustomDataBool(const CStringName& name, bool& bOut, bool bInheritData);
bool SetCustomData(const CStringName& name, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST, CPlayer* pClient = NULL,
bool bTriggerEvent = true);
bool DeleteCustomData(const CStringName& name);
bool DeleteCustomData(const CStringName& name, CPlayer* pClient = nullptr);
void SendAllCustomData(CPlayer* pPlayer);

CXMLNode* OutputToXML(CXMLNode* pNode);
Expand Down
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CPerfStat.RPCPacketUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ ADD_ENUM1(CRPCFunctions::PLAYER_WEAPON)
ADD_ENUM1(CRPCFunctions::KEY_BIND)
ADD_ENUM1(CRPCFunctions::CURSOR_EVENT)
ADD_ENUM1(CRPCFunctions::REQUEST_STEALTH_KILL)
ADD_ENUM1(CRPCFunctions::REMOVE_ELEMENT_DATA_RPC)
IMPLEMENT_ENUM_END("eRPCFunctions")

struct SRPCPacketStat
Expand Down
72 changes: 72 additions & 0 deletions Server/mods/deathmatch/logic/CRPCFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "CPerfStatManager.h"
#include "CKeyBinds.h"
#include "CStaticFunctionDefinitions.h"
#include "packets/CElementRPCPacket.h"
#include "net/SyncStructures.h"

CRPCFunctions* g_pRPCFunctions = NULL;
Expand Down Expand Up @@ -57,6 +58,7 @@ void CRPCFunctions::AddHandlers()
AddHandler(KEY_BIND, KeyBind);
AddHandler(CURSOR_EVENT, CursorEvent);
AddHandler(REQUEST_STEALTH_KILL, RequestStealthKill);
AddHandler(REMOVE_ELEMENT_DATA_RPC, RemoveElementData);
}

void CRPCFunctions::AddHandler(unsigned char ucID, pfnRPCHandler Callback)
Expand Down Expand Up @@ -366,3 +368,73 @@ void CRPCFunctions::RequestStealthKill(NetBitStreamInterface& bitStream)
}
UNCLOCK("NetServerPulse::RPC", "RequestStealthKill");
}

void CRPCFunctions::RemoveElementData(NetBitStreamInterface& bitStream)
Comment thread
ArranTuna marked this conversation as resolved.
{
CLOCK("NetServerPulse::RPC", "RemoveElementData");

if (!m_pSourcePlayer->IsJoined())
{
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
return;
}
Comment thread
ArranTuna marked this conversation as resolved.

ElementID elementId;
std::uint16_t nameLength;
if (bitStream.Read(elementId) && bitStream.ReadCompressed(nameLength) && nameLength > 0 && nameLength <= MAX_CUSTOMDATA_NAME_LENGTH)
{
char customDataName[MAX_CUSTOMDATA_NAME_LENGTH + 1];
if (bitStream.Read(customDataName, nameLength))
{
customDataName[nameLength] = 0;

CElement* element = CElementIDs::GetElement(elementId);
if (element)
{
ESyncType lastSyncType = ESyncType::BROADCAST;
eCustomDataClientTrust clientChangesMode{};
element->GetCustomData(customDataName, false, &lastSyncType, &clientChangesMode);

const bool changesAllowed = clientChangesMode == eCustomDataClientTrust::UNSET ? !g_pGame->GetConfig()->IsElementDataWhitelisted()
: clientChangesMode == eCustomDataClientTrust::ALLOW;
if (!changesAllowed)
{
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)\n", m_pSourcePlayer->GetNick(), customDataName);

CLuaArguments arguments;
arguments.PushElement(element);
arguments.PushString(customDataName);
arguments.PushArgument(CLuaArgument());
Comment thread
FileEX marked this conversation as resolved.
Outdated
m_pSourcePlayer->CallEvent("onPlayerChangesProtectedData", arguments);
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
return;
}

if (element->DeleteCustomData(customDataName, m_pSourcePlayer))
{
if (lastSyncType != ESyncType::LOCAL)
{
CBitStream outBitStream;
outBitStream.pBitStream->WriteCompressed(nameLength);
outBitStream.pBitStream->Write(customDataName, nameLength);
outBitStream.pBitStream->WriteBit(false); // Unused (was recursive flag)

if (lastSyncType == ESyncType::BROADCAST)
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(element, REMOVE_ELEMENT_DATA, *outBitStream.pBitStream), m_pSourcePlayer);
else
m_pPlayerManager->BroadcastOnlySubscribed(CElementRPCPacket(element, REMOVE_ELEMENT_DATA, *outBitStream.pBitStream), element,
customDataName, m_pSourcePlayer);

CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageRelayed(customDataName, m_pPlayerManager->Count(),
outBitStream.pBitStream->GetNumberOfBytesUsed());
}

if (lastSyncType == ESyncType::SUBSCRIBE)
m_pPlayerManager->ClearElementData(element, customDataName);
}
}
}
}

UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
}
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CRPCFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class CRPCFunctions
DECLARE_RPC(KeyBind);
DECLARE_RPC(CursorEvent);
DECLARE_RPC(RequestStealthKill);
DECLARE_RPC(RemoveElementData);

protected:
static CPlayer* m_pSourcePlayer;
Expand All @@ -66,5 +67,6 @@ class CRPCFunctions
KEY_BIND,
CURSOR_EVENT,
REQUEST_STEALTH_KILL,
REMOVE_ELEMENT_DATA_RPC,
};
};
Loading