Skip to content

Commit 007336f

Browse files
committed
First iteration
1 parent e92702c commit 007336f

File tree

7 files changed

+58
-6
lines changed

7 files changed

+58
-6
lines changed

Server/mods/deathmatch/logic/CCustomData.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void CCustomData::Copy(CCustomData* pCustomData)
2121
}
2222
}
2323

24-
SCustomData* CCustomData::Get(const char* szName)
24+
SCustomData* CCustomData::Get(const char* szName) const
2525
{
2626
assert(szName);
2727

@@ -100,6 +100,7 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, ESyncTyp
100100
SCustomData newData;
101101
newData.Variable = Variable;
102102
newData.syncType = syncType;
103+
newData.allowClientChanges = true;
103104
m_Data[szName] = newData;
104105
UpdateSynced(szName, Variable, syncType);
105106
}
@@ -120,6 +121,18 @@ bool CCustomData::Delete(const char* szName)
120121
return false;
121122
}
122123

124+
bool CCustomData::IsClientChangesAllowed(const char* szName) const
125+
{
126+
SCustomData* pData = Get(szName);
127+
return pData ? pData->allowClientChanges : true;
128+
}
129+
130+
void CCustomData::SetClientChangesAllowed(const char* szName, bool enabled)
131+
{
132+
SCustomData& pData = m_Data[szName];
133+
pData.allowClientChanges = enabled;
134+
}
135+
123136
CXMLNode* CCustomData::OutputToXML(CXMLNode* pNode)
124137
{
125138
std::map<std::string, SCustomData>::const_iterator iter = m_Data.begin();

Server/mods/deathmatch/logic/CCustomData.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ struct SCustomData
2929
{
3030
CLuaArgument Variable;
3131
ESyncType syncType;
32+
bool allowClientChanges;
3233
};
3334

3435
class CCustomData
3536
{
3637
public:
3738
void Copy(CCustomData* pCustomData);
3839

39-
SCustomData* Get(const char* szName);
40+
SCustomData* Get(const char* szName) const;
4041
SCustomData* GetSynced(const char* szName);
4142
void Set(const char* szName, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST);
4243

4344
bool Delete(const char* szName);
45+
bool IsClientChangesAllowed(const char* szName) const;
46+
void SetClientChangesAllowed(const char* szName, bool enabled);
4447

4548
unsigned short CountOnlySynchronized();
4649

Server/mods/deathmatch/logic/CElement.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ void CElement::ReadCustomData(CEvents* pEvents, CXMLNode& Node)
508508
}
509509
}
510510

511-
CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType)
511+
CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType, bool* clientChangesAllowed)
512512
{
513513
assert(szName);
514514

@@ -518,13 +518,17 @@ CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESy
518518
{
519519
if (pSyncType)
520520
*pSyncType = pData->syncType;
521+
522+
if (clientChangesAllowed)
523+
*clientChangesAllowed = pData->allowClientChanges;
524+
521525
return &pData->Variable;
522526
}
523527

524528
// If none, try returning parent's custom data
525529
if (bInheritData && m_pParent)
526530
{
527-
return m_pParent->GetCustomData(szName, true, pSyncType);
531+
return m_pParent->GetCustomData(szName, true, pSyncType, clientChangesAllowed);
528532
}
529533

530534
// None available

Server/mods/deathmatch/logic/CElement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class CElement
136136

137137
void ReadCustomData(CEvents* pEvents, CXMLNode& Node);
138138
CCustomData& GetCustomDataManager() { return m_CustomData; }
139-
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = NULL);
139+
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = nullptr, bool* clientChangesAllowed = nullptr);
140140
CLuaArguments* GetAllCustomData(CLuaArguments* table);
141141
bool GetCustomDataString(const char* szName, char* pOut, size_t sizeBuffer, bool bInheritData);
142142
bool GetCustomDataInt(const char* szName, int& iOut, bool bInheritData);

Server/mods/deathmatch/logic/CGame.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,7 @@ void CGame::AddBuiltInEvents()
16071607
m_Events.AddEvent("onPlayerTriggerEventThreshold", "", nullptr, false);
16081608
m_Events.AddEvent("onPlayerTeamChange", "oldTeam, newTeam", nullptr, false);
16091609
m_Events.AddEvent("onPlayerTriggerInvalidEvent", "eventName, isAdded, isRemote", nullptr, false);
1610+
m_Events.AddEvent("onPlayerChangesProtectedData", "element, key, value", nullptr, false);
16101611

16111612
// Ped events
16121613
m_Events.AddEvent("onPedVehicleEnter", "vehicle, seat, jacked", NULL, false);
@@ -2652,7 +2653,22 @@ void CGame::Packet_CustomData(CCustomDataPacket& Packet)
26522653
}
26532654

26542655
ESyncType lastSyncType = ESyncType::BROADCAST;
2655-
pElement->GetCustomData(szName, false, &lastSyncType);
2656+
bool clientChangesAllowed = true;
2657+
2658+
pElement->GetCustomData(szName, false, &lastSyncType, &clientChangesAllowed);
2659+
2660+
if (!clientChangesAllowed)
2661+
{
2662+
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)", Packet.GetSourcePlayer()->GetNick(),
2663+
szName);
2664+
2665+
CLuaArguments arguments;
2666+
arguments.PushElement(pElement);
2667+
arguments.PushString(szName);
2668+
arguments.PushArgument(Value);
2669+
pSourcePlayer->CallEvent("onPlayerChangesProtectedData", arguments);
2670+
return;
2671+
}
26562672

26572673
if (lastSyncType != ESyncType::LOCAL)
26582674
{

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ void CLuaElementDefs::LoadFunctions()
8080
{"addElementDataSubscriber", addElementDataSubscriber},
8181
{"removeElementDataSubscriber", removeElementDataSubscriber},
8282
{"hasElementDataSubscriber", hasElementDataSubscriber},
83+
{"setElementDataClientTrustEnabled", ArgumentParser<SetElementDataClientTrustEnabled>},
84+
{"isElementDataClientTrustEnabled", ArgumentParser<IsElementDataClientTrustEnabled>},
8385

8486
// Set
8587
{"setElementID", setElementID},
@@ -151,6 +153,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
151153
lua_classfunction(luaVM, "setLowLOD", "setLowLODElement");
152154
lua_classfunction(luaVM, "setAttachedOffsets", "setElementAttachedOffsets");
153155
lua_classfunction(luaVM, "setCallPropagationEnabled", "setElementCallPropagationEnabled");
156+
lua_classfunction(luaVM, "setDataClientTrustEnabled", "setElementDataClientTrustEnabled");
154157

155158
lua_classfunction(luaVM, "getAttachedOffsets", "getElementAttachedOffsets");
156159
lua_classfunction(luaVM, "getChild", "getElementChild");
@@ -189,6 +192,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
189192
lua_classfunction(luaVM, "isVisibleTo", "isElementVisibleTo");
190193
lua_classfunction(luaVM, "isLowLOD", "isElementLowLOD");
191194
lua_classfunction(luaVM, "isAttached", "isElementAttached");
195+
lua_classfunction(luaVM, "isDataClientTrustEnabled", "isElementDataClientTrustEnabled");
192196

193197
lua_classvariable(luaVM, "id", "setElementID", "getElementID");
194198
lua_classvariable(luaVM, "callPropagationEnabled", "setElementCallPropagationEnabled", "isElementCallPropagationEnabled");
@@ -2437,3 +2441,13 @@ int CLuaElementDefs::isElementCallPropagationEnabled(lua_State* luaVM)
24372441
lua_pushboolean(luaVM, false);
24382442
return 1;
24392443
}
2444+
2445+
void CLuaElementDefs::SetElementDataClientTrustEnabled(CElement* pElement, std::string_view key, bool enabled)
2446+
{
2447+
pElement->GetCustomDataManager().SetClientChangesAllowed(key.data(), enabled);
2448+
}
2449+
2450+
bool CLuaElementDefs::IsElementDataClientTrustEnabled(CElement* pElement, std::string_view key)
2451+
{
2452+
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.data());
2453+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class CLuaElementDefs : public CLuaDefs
7777
LUA_DECLARE(addElementDataSubscriber);
7878
LUA_DECLARE(removeElementDataSubscriber);
7979
LUA_DECLARE(hasElementDataSubscriber);
80+
static void SetElementDataClientTrustEnabled(CElement* pElement, std::string_view key, bool enabled);
81+
static bool IsElementDataClientTrustEnabled(CElement* pElement, std::string_view key);
8082

8183
// Attachement
8284
LUA_DECLARE(attachElements);

0 commit comments

Comments
 (0)