Skip to content

Commit 06e4c82

Browse files
committed
Allow changes for all element data
1 parent 007336f commit 06e4c82

File tree

7 files changed

+60
-26
lines changed

7 files changed

+60
-26
lines changed

Server/mods/deathmatch/logic/CCustomData.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +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;
103+
newData.clientChangesMode = ECustomDataClientTrust::UNSET;
104104
m_Data[szName] = newData;
105105
UpdateSynced(szName, Variable, syncType);
106106
}
@@ -124,13 +124,16 @@ bool CCustomData::Delete(const char* szName)
124124
bool CCustomData::IsClientChangesAllowed(const char* szName) const
125125
{
126126
SCustomData* pData = Get(szName);
127-
return pData ? pData->allowClientChanges : true;
127+
if (!pData || pData->clientChangesMode == ECustomDataClientTrust::UNSET)
128+
return IsClientChangesAllowed();
129+
130+
return pData->clientChangesMode == ECustomDataClientTrust::ALLOW;
128131
}
129132

130-
void CCustomData::SetClientChangesAllowed(const char* szName, bool enabled)
133+
void CCustomData::SetClientChangesMode(const char* szName, ECustomDataClientTrust mode)
131134
{
132135
SCustomData& pData = m_Data[szName];
133-
pData.allowClientChanges = enabled;
136+
pData.clientChangesMode = mode;
134137
}
135138

136139
CXMLNode* CCustomData::OutputToXML(CXMLNode* pNode)

Server/mods/deathmatch/logic/CCustomData.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ enum class ESyncType
2525
SUBSCRIBE,
2626
};
2727

28+
enum class ECustomDataClientTrust : std::uint8_t
29+
{
30+
UNSET,
31+
ALLOW,
32+
DENY,
33+
};
34+
2835
struct SCustomData
2936
{
30-
CLuaArgument Variable;
31-
ESyncType syncType;
32-
bool allowClientChanges;
37+
CLuaArgument Variable;
38+
ESyncType syncType;
39+
ECustomDataClientTrust clientChangesMode;
3340
};
3441

3542
class CCustomData
@@ -42,8 +49,12 @@ class CCustomData
4249
void Set(const char* szName, const CLuaArgument& Variable, ESyncType syncType = ESyncType::BROADCAST);
4350

4451
bool Delete(const char* szName);
52+
4553
bool IsClientChangesAllowed(const char* szName) const;
46-
void SetClientChangesAllowed(const char* szName, bool enabled);
54+
void SetClientChangesMode(const char* szName, ECustomDataClientTrust mode);
55+
56+
bool IsClientChangesAllowed() const noexcept { return m_clientChangesAllowed; };
57+
void SetClientChangesAllowed(bool enabled) noexcept { m_clientChangesAllowed = enabled; };
4758

4859
unsigned short CountOnlySynchronized();
4960

@@ -61,4 +72,5 @@ class CCustomData
6172

6273
std::map<std::string, SCustomData> m_Data;
6374
std::map<std::string, SCustomData> m_SyncedData;
75+
bool m_clientChangesAllowed{true};
6476
};

Server/mods/deathmatch/logic/CElement.cpp

Lines changed: 4 additions & 4 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, bool* clientChangesAllowed)
511+
CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType, ECustomDataClientTrust* clientChangesMode)
512512
{
513513
assert(szName);
514514

@@ -519,16 +519,16 @@ CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESy
519519
if (pSyncType)
520520
*pSyncType = pData->syncType;
521521

522-
if (clientChangesAllowed)
523-
*clientChangesAllowed = pData->allowClientChanges;
522+
if (clientChangesMode)
523+
*clientChangesMode = pData->clientChangesMode;
524524

525525
return &pData->Variable;
526526
}
527527

528528
// If none, try returning parent's custom data
529529
if (bInheritData && m_pParent)
530530
{
531-
return m_pParent->GetCustomData(szName, true, pSyncType, clientChangesAllowed);
531+
return m_pParent->GetCustomData(szName, true, pSyncType, clientChangesMode);
532532
}
533533

534534
// 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 = nullptr, bool* clientChangesAllowed = nullptr);
139+
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = nullptr, ECustomDataClientTrust* clientChangesMode = 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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,11 +2653,13 @@ void CGame::Packet_CustomData(CCustomDataPacket& Packet)
26532653
}
26542654

26552655
ESyncType lastSyncType = ESyncType::BROADCAST;
2656-
bool clientChangesAllowed = true;
2656+
ECustomDataClientTrust clientChangesMode{};
26572657

2658-
pElement->GetCustomData(szName, false, &lastSyncType, &clientChangesAllowed);
2658+
pElement->GetCustomData(szName, false, &lastSyncType, &clientChangesMode);
26592659

2660-
if (!clientChangesAllowed)
2660+
const bool changesAllowed = clientChangesMode == ECustomDataClientTrust::UNSET ? pElement->GetCustomDataManager().IsClientChangesAllowed()
2661+
: clientChangesMode == ECustomDataClientTrust::ALLOW;
2662+
if (!changesAllowed)
26612663
{
26622664
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)", Packet.GetSourcePlayer()->GetNick(),
26632665
szName);

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ void CLuaElementDefs::LoadFunctions()
8080
{"addElementDataSubscriber", addElementDataSubscriber},
8181
{"removeElementDataSubscriber", removeElementDataSubscriber},
8282
{"hasElementDataSubscriber", hasElementDataSubscriber},
83-
{"setElementDataClientTrustEnabled", ArgumentParser<SetElementDataClientTrustEnabled>},
84-
{"isElementDataClientTrustEnabled", ArgumentParser<IsElementDataClientTrustEnabled>},
83+
{"setElementDataClientTrust", ArgumentParser<SetElementDataClientTrust>},
84+
{"isElementDataClientTrusted", ArgumentParser<IsElementDataClientTrusted>},
85+
{"resetElementDataClientTrust", ArgumentParser<ResetElementDataClientTrust>},
8586

8687
// Set
8788
{"setElementID", setElementID},
@@ -131,6 +132,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
131132
lua_classfunction(luaVM, "addDataSubscriber", "addElementDataSubscriber");
132133
lua_classfunction(luaVM, "removeDataSubscriber", "removeElementDataSubscriber");
133134
lua_classfunction(luaVM, "hasDataSubscriber", "hasElementDataSubscriber");
135+
lua_classfunction(luaVM, "resetDataClientTrust", "resetElementDataClientTrust");
134136

135137
lua_classfunction(luaVM, "setParent", "setElementParent");
136138
lua_classfunction(luaVM, "setFrozen", "setElementFrozen");
@@ -153,7 +155,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
153155
lua_classfunction(luaVM, "setLowLOD", "setLowLODElement");
154156
lua_classfunction(luaVM, "setAttachedOffsets", "setElementAttachedOffsets");
155157
lua_classfunction(luaVM, "setCallPropagationEnabled", "setElementCallPropagationEnabled");
156-
lua_classfunction(luaVM, "setDataClientTrustEnabled", "setElementDataClientTrustEnabled");
158+
lua_classfunction(luaVM, "setDataClientTrust", "setElementDataClientTrust");
157159

158160
lua_classfunction(luaVM, "getAttachedOffsets", "getElementAttachedOffsets");
159161
lua_classfunction(luaVM, "getChild", "getElementChild");
@@ -192,7 +194,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
192194
lua_classfunction(luaVM, "isVisibleTo", "isElementVisibleTo");
193195
lua_classfunction(luaVM, "isLowLOD", "isElementLowLOD");
194196
lua_classfunction(luaVM, "isAttached", "isElementAttached");
195-
lua_classfunction(luaVM, "isDataClientTrustEnabled", "isElementDataClientTrustEnabled");
197+
lua_classfunction(luaVM, "isDataClientTrusted", "isElementDataClientTrusted");
196198

197199
lua_classvariable(luaVM, "id", "setElementID", "getElementID");
198200
lua_classvariable(luaVM, "callPropagationEnabled", "setElementCallPropagationEnabled", "isElementCallPropagationEnabled");
@@ -2442,12 +2444,26 @@ int CLuaElementDefs::isElementCallPropagationEnabled(lua_State* luaVM)
24422444
return 1;
24432445
}
24442446

2445-
void CLuaElementDefs::SetElementDataClientTrustEnabled(CElement* pElement, std::string_view key, bool enabled)
2447+
void CLuaElementDefs::SetElementDataClientTrust(CElement* pElement, bool enabled, std::optional<std::string_view> key)
24462448
{
2447-
pElement->GetCustomDataManager().SetClientChangesAllowed(key.data(), enabled);
2449+
if (key.has_value())
2450+
pElement->GetCustomDataManager().SetClientChangesMode(key.value().data(), enabled ? ECustomDataClientTrust::ALLOW : ECustomDataClientTrust::DENY);
2451+
else
2452+
pElement->GetCustomDataManager().SetClientChangesAllowed(enabled);
2453+
}
2454+
2455+
bool CLuaElementDefs::IsElementDataClientTrusted(CElement* pElement, std::optional<std::string_view> key)
2456+
{
2457+
if (key.has_value())
2458+
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.value().data());
2459+
else
2460+
return pElement->GetCustomDataManager().IsClientChangesAllowed();
24482461
}
24492462

2450-
bool CLuaElementDefs::IsElementDataClientTrustEnabled(CElement* pElement, std::string_view key)
2463+
void CLuaElementDefs::ResetElementDataClientTrust(CElement* pElement, std::optional<std::string_view> key)
24512464
{
2452-
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.data());
2465+
if (key.has_value())
2466+
pElement->GetCustomDataManager().SetClientChangesMode(key.value().data(), ECustomDataClientTrust::UNSET);
2467+
else
2468+
pElement->GetCustomDataManager().SetClientChangesAllowed(true);
24532469
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ 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);
80+
static void SetElementDataClientTrust(CElement* pElement, bool enabled, std::optional<std::string_view> key);
81+
static void ResetElementDataClientTrust(CElement* pElement, std::optional<std::string_view> key);
82+
static bool IsElementDataClientTrusted(CElement* pElement, std::optional<std::string_view> key);
8283

8384
// Attachement
8485
LUA_DECLARE(attachElements);

0 commit comments

Comments
 (0)