Skip to content

Commit e0b87f9

Browse files
committed
Add early returns
1 parent de3a2d3 commit e0b87f9

2 files changed

Lines changed: 62 additions & 61 deletions

File tree

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,25 +1067,23 @@ bool CStaticFunctionDefinitions::RemoveElementData(CClientEntity& Entity, CStrin
10671067

10681068
bool isSynced;
10691069
CLuaArgument* currentVariable = Entity.GetCustomData(name, false, &isSynced);
1070-
if (currentVariable)
1071-
{
1072-
if (isSynced && !Entity.IsLocalEntity())
1073-
{
1074-
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
1075-
// Write element ID, name length and name for server-side removal handling
1076-
pBitStream->Write(Entity.GetID());
1077-
pBitStream->WriteString(name.ToCString());
1070+
if (!currentVariable)
1071+
return false;
10781072

1079-
// Send RPC and deallocate
1080-
g_pClientGame->GetNetAPI()->RPC(REMOVE_ELEMENT_DATA_RPC, pBitStream);
1081-
g_pNet->DeallocateNetBitStream(pBitStream);
1082-
}
1073+
if (isSynced && !Entity.IsLocalEntity())
1074+
{
1075+
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
1076+
// Write element ID, name length and name for server-side removal handling
1077+
pBitStream->Write(Entity.GetID());
1078+
pBitStream->WriteString(name.ToCString());
10831079

1084-
Entity.DeleteCustomData(name);
1085-
return true;
1080+
// Send RPC and deallocate
1081+
g_pClientGame->GetNetAPI()->RPC(REMOVE_ELEMENT_DATA_RPC, pBitStream);
1082+
g_pNet->DeallocateNetBitStream(pBitStream);
10861083
}
10871084

1088-
return false;
1085+
Entity.DeleteCustomData(name);
1086+
return true;
10891087
}
10901088

10911089
bool CStaticFunctionDefinitions::SetElementMatrix(CClientEntity& Entity, const CMatrix& matrix)

Server/mods/deathmatch/logic/CRPCFunctions.cpp

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -371,64 +371,67 @@ void CRPCFunctions::RequestStealthKill(NetBitStreamInterface& bitStream)
371371

372372
void CRPCFunctions::RemoveElementData(NetBitStreamInterface& bitStream)
373373
{
374+
if (!m_pSourcePlayer->IsJoined())
375+
return;
376+
374377
CLOCK("NetServerPulse::RPC", "RemoveElementData");
375378

376-
if (!m_pSourcePlayer->IsJoined())
379+
ElementID elementId;
380+
std::string customDataName;
381+
if (!bitStream.Read(elementId) || !bitStream.ReadString(customDataName) || customDataName.empty() || customDataName.length() > MAX_CUSTOMDATA_NAME_LENGTH)
377382
{
378383
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
379384
return;
380385
}
381386

382-
ElementID elementId;
383-
std::string customDataName;
384-
if (bitStream.Read(elementId) && bitStream.ReadString(customDataName) && !customDataName.empty() && customDataName.length() <= MAX_CUSTOMDATA_NAME_LENGTH)
387+
CElement* element = CElementIDs::GetElement(elementId);
388+
if (!element)
385389
{
386-
CElement* element = CElementIDs::GetElement(elementId);
387-
if (element)
388-
{
389-
ESyncType lastSyncType = ESyncType::BROADCAST;
390-
eCustomDataClientTrust clientChangesMode{};
391-
element->GetCustomData(customDataName.c_str(), false, &lastSyncType, &clientChangesMode);
390+
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
391+
return;
392+
}
392393

393-
const bool changesAllowed = clientChangesMode == eCustomDataClientTrust::UNSET ? !g_pGame->GetConfig()->IsElementDataWhitelisted()
394-
: clientChangesMode == eCustomDataClientTrust::ALLOW;
395-
if (!changesAllowed)
396-
{
397-
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)\n", m_pSourcePlayer->GetNick(), customDataName.c_str());
398-
399-
CLuaArguments arguments;
400-
arguments.PushElement(element);
401-
arguments.PushString(customDataName.c_str());
402-
arguments.PushArgument(CLuaArgument());
403-
m_pSourcePlayer->CallEvent("onPlayerChangesProtectedData", arguments);
404-
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
405-
return;
406-
}
394+
ESyncType lastSyncType = ESyncType::BROADCAST;
395+
eCustomDataClientTrust clientChangesMode{};
396+
element->GetCustomData(customDataName.c_str(), false, &lastSyncType, &clientChangesMode);
407397

408-
if (element->DeleteCustomData(customDataName.c_str(), m_pSourcePlayer))
409-
{
410-
if (lastSyncType != ESyncType::LOCAL)
411-
{
412-
CBitStream outBitStream;
413-
std::uint16_t nameLength = static_cast<std::uint16_t>(customDataName.length());
414-
outBitStream.pBitStream->WriteCompressed(nameLength);
415-
outBitStream.pBitStream->Write(customDataName.c_str(), nameLength);
416-
outBitStream.pBitStream->WriteBit(false); // Unused (was recursive flag)
417-
418-
if (lastSyncType == ESyncType::BROADCAST)
419-
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(element, REMOVE_ELEMENT_DATA, *outBitStream.pBitStream), m_pSourcePlayer);
420-
else
421-
m_pPlayerManager->BroadcastOnlySubscribed(CElementRPCPacket(element, REMOVE_ELEMENT_DATA, *outBitStream.pBitStream), element,
422-
customDataName.c_str(), m_pSourcePlayer);
423-
424-
CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageRelayed(customDataName.c_str(), m_pPlayerManager->Count(),
425-
outBitStream.pBitStream->GetNumberOfBytesUsed());
426-
}
398+
const bool changesAllowed = clientChangesMode == eCustomDataClientTrust::UNSET ? !g_pGame->GetConfig()->IsElementDataWhitelisted()
399+
: clientChangesMode == eCustomDataClientTrust::ALLOW;
400+
if (!changesAllowed)
401+
{
402+
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)\n", m_pSourcePlayer->GetNick(), customDataName.c_str());
427403

428-
if (lastSyncType == ESyncType::SUBSCRIBE)
429-
m_pPlayerManager->ClearElementData(element, customDataName.c_str());
430-
}
404+
CLuaArguments arguments;
405+
arguments.PushElement(element);
406+
arguments.PushString(customDataName.c_str());
407+
arguments.PushArgument(CLuaArgument());
408+
m_pSourcePlayer->CallEvent("onPlayerChangesProtectedData", arguments);
409+
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");
410+
return;
411+
}
412+
413+
if (element->DeleteCustomData(customDataName.c_str(), m_pSourcePlayer))
414+
{
415+
if (lastSyncType != ESyncType::LOCAL)
416+
{
417+
CBitStream outBitStream;
418+
std::uint16_t nameLength = static_cast<std::uint16_t>(customDataName.length());
419+
outBitStream.pBitStream->WriteCompressed(nameLength);
420+
outBitStream.pBitStream->Write(customDataName.c_str(), nameLength);
421+
outBitStream.pBitStream->WriteBit(false); // Unused (was recursive flag)
422+
423+
if (lastSyncType == ESyncType::BROADCAST)
424+
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(element, REMOVE_ELEMENT_DATA, *outBitStream.pBitStream), m_pSourcePlayer);
425+
else
426+
m_pPlayerManager->BroadcastOnlySubscribed(CElementRPCPacket(element, REMOVE_ELEMENT_DATA, *outBitStream.pBitStream), element,
427+
customDataName.c_str(), m_pSourcePlayer);
428+
429+
CPerfStatEventPacketUsage::GetSingleton()->UpdateElementDataUsageRelayed(customDataName.c_str(), m_pPlayerManager->Count(),
430+
outBitStream.pBitStream->GetNumberOfBytesUsed());
431431
}
432+
433+
if (lastSyncType == ESyncType::SUBSCRIBE)
434+
m_pPlayerManager->ClearElementData(element, customDataName.c_str());
432435
}
433436

434437
UNCLOCK("NetServerPulse::RPC", "RemoveElementData");

0 commit comments

Comments
 (0)