Skip to content

Commit

Permalink
Allow changes for all element data
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Nov 3, 2024
1 parent 007336f commit 06e4c82
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 26 deletions.
11 changes: 7 additions & 4 deletions Server/mods/deathmatch/logic/CCustomData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, ESyncTyp
SCustomData newData;
newData.Variable = Variable;
newData.syncType = syncType;
newData.allowClientChanges = true;
newData.clientChangesMode = ECustomDataClientTrust::UNSET;
m_Data[szName] = newData;
UpdateSynced(szName, Variable, syncType);
}
Expand All @@ -124,13 +124,16 @@ bool CCustomData::Delete(const char* szName)
bool CCustomData::IsClientChangesAllowed(const char* szName) const
{
SCustomData* pData = Get(szName);
return pData ? pData->allowClientChanges : true;
if (!pData || pData->clientChangesMode == ECustomDataClientTrust::UNSET)
return IsClientChangesAllowed();

return pData->clientChangesMode == ECustomDataClientTrust::ALLOW;
}

void CCustomData::SetClientChangesAllowed(const char* szName, bool enabled)
void CCustomData::SetClientChangesMode(const char* szName, ECustomDataClientTrust mode)
{
SCustomData& pData = m_Data[szName];
pData.allowClientChanges = enabled;
pData.clientChangesMode = mode;
}

CXMLNode* CCustomData::OutputToXML(CXMLNode* pNode)
Expand Down
20 changes: 16 additions & 4 deletions Server/mods/deathmatch/logic/CCustomData.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ enum class ESyncType
SUBSCRIBE,
};

enum class ECustomDataClientTrust : std::uint8_t
{
UNSET,
ALLOW,
DENY,
};

struct SCustomData
{
CLuaArgument Variable;
ESyncType syncType;
bool allowClientChanges;
CLuaArgument Variable;
ESyncType syncType;
ECustomDataClientTrust clientChangesMode;
};

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

bool Delete(const char* szName);

bool IsClientChangesAllowed(const char* szName) const;
void SetClientChangesAllowed(const char* szName, bool enabled);
void SetClientChangesMode(const char* szName, ECustomDataClientTrust mode);

bool IsClientChangesAllowed() const noexcept { return m_clientChangesAllowed; };
void SetClientChangesAllowed(bool enabled) noexcept { m_clientChangesAllowed = enabled; };

unsigned short CountOnlySynchronized();

Expand All @@ -61,4 +72,5 @@ class CCustomData

std::map<std::string, SCustomData> m_Data;
std::map<std::string, SCustomData> m_SyncedData;
bool m_clientChangesAllowed{true};
};
8 changes: 4 additions & 4 deletions Server/mods/deathmatch/logic/CElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ void CElement::ReadCustomData(CEvents* pEvents, CXMLNode& Node)
}
}

CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType, bool* clientChangesAllowed)
CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType, ECustomDataClientTrust* clientChangesMode)
{
assert(szName);

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

if (clientChangesAllowed)
*clientChangesAllowed = pData->allowClientChanges;
if (clientChangesMode)
*clientChangesMode = pData->clientChangesMode;

return &pData->Variable;
}

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

// None available
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 @@ -136,7 +136,7 @@ class CElement

void ReadCustomData(CEvents* pEvents, CXMLNode& Node);
CCustomData& GetCustomDataManager() { return m_CustomData; }
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = nullptr, bool* clientChangesAllowed = nullptr);
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = nullptr, ECustomDataClientTrust* clientChangesMode = nullptr);
CLuaArguments* GetAllCustomData(CLuaArguments* table);
bool GetCustomDataString(const char* szName, char* pOut, size_t sizeBuffer, bool bInheritData);
bool GetCustomDataInt(const char* szName, int& iOut, bool bInheritData);
Expand Down
8 changes: 5 additions & 3 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2653,11 +2653,13 @@ void CGame::Packet_CustomData(CCustomDataPacket& Packet)
}

ESyncType lastSyncType = ESyncType::BROADCAST;
bool clientChangesAllowed = true;
ECustomDataClientTrust clientChangesMode{};

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

if (!clientChangesAllowed)
const bool changesAllowed = clientChangesMode == ECustomDataClientTrust::UNSET ? pElement->GetCustomDataManager().IsClientChangesAllowed()
: clientChangesMode == ECustomDataClientTrust::ALLOW;
if (!changesAllowed)
{
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)", Packet.GetSourcePlayer()->GetNick(),
szName);
Expand Down
32 changes: 24 additions & 8 deletions Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ void CLuaElementDefs::LoadFunctions()
{"addElementDataSubscriber", addElementDataSubscriber},
{"removeElementDataSubscriber", removeElementDataSubscriber},
{"hasElementDataSubscriber", hasElementDataSubscriber},
{"setElementDataClientTrustEnabled", ArgumentParser<SetElementDataClientTrustEnabled>},
{"isElementDataClientTrustEnabled", ArgumentParser<IsElementDataClientTrustEnabled>},
{"setElementDataClientTrust", ArgumentParser<SetElementDataClientTrust>},
{"isElementDataClientTrusted", ArgumentParser<IsElementDataClientTrusted>},
{"resetElementDataClientTrust", ArgumentParser<ResetElementDataClientTrust>},

// Set
{"setElementID", setElementID},
Expand Down Expand Up @@ -131,6 +132,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "addDataSubscriber", "addElementDataSubscriber");
lua_classfunction(luaVM, "removeDataSubscriber", "removeElementDataSubscriber");
lua_classfunction(luaVM, "hasDataSubscriber", "hasElementDataSubscriber");
lua_classfunction(luaVM, "resetDataClientTrust", "resetElementDataClientTrust");

lua_classfunction(luaVM, "setParent", "setElementParent");
lua_classfunction(luaVM, "setFrozen", "setElementFrozen");
Expand All @@ -153,7 +155,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "setLowLOD", "setLowLODElement");
lua_classfunction(luaVM, "setAttachedOffsets", "setElementAttachedOffsets");
lua_classfunction(luaVM, "setCallPropagationEnabled", "setElementCallPropagationEnabled");
lua_classfunction(luaVM, "setDataClientTrustEnabled", "setElementDataClientTrustEnabled");
lua_classfunction(luaVM, "setDataClientTrust", "setElementDataClientTrust");

lua_classfunction(luaVM, "getAttachedOffsets", "getElementAttachedOffsets");
lua_classfunction(luaVM, "getChild", "getElementChild");
Expand Down Expand Up @@ -192,7 +194,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "isVisibleTo", "isElementVisibleTo");
lua_classfunction(luaVM, "isLowLOD", "isElementLowLOD");
lua_classfunction(luaVM, "isAttached", "isElementAttached");
lua_classfunction(luaVM, "isDataClientTrustEnabled", "isElementDataClientTrustEnabled");
lua_classfunction(luaVM, "isDataClientTrusted", "isElementDataClientTrusted");

lua_classvariable(luaVM, "id", "setElementID", "getElementID");
lua_classvariable(luaVM, "callPropagationEnabled", "setElementCallPropagationEnabled", "isElementCallPropagationEnabled");
Expand Down Expand Up @@ -2442,12 +2444,26 @@ int CLuaElementDefs::isElementCallPropagationEnabled(lua_State* luaVM)
return 1;
}

void CLuaElementDefs::SetElementDataClientTrustEnabled(CElement* pElement, std::string_view key, bool enabled)
void CLuaElementDefs::SetElementDataClientTrust(CElement* pElement, bool enabled, std::optional<std::string_view> key)
{
pElement->GetCustomDataManager().SetClientChangesAllowed(key.data(), enabled);
if (key.has_value())
pElement->GetCustomDataManager().SetClientChangesMode(key.value().data(), enabled ? ECustomDataClientTrust::ALLOW : ECustomDataClientTrust::DENY);
else
pElement->GetCustomDataManager().SetClientChangesAllowed(enabled);
}

bool CLuaElementDefs::IsElementDataClientTrusted(CElement* pElement, std::optional<std::string_view> key)
{
if (key.has_value())
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.value().data());
else
return pElement->GetCustomDataManager().IsClientChangesAllowed();
}

bool CLuaElementDefs::IsElementDataClientTrustEnabled(CElement* pElement, std::string_view key)
void CLuaElementDefs::ResetElementDataClientTrust(CElement* pElement, std::optional<std::string_view> key)
{
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.data());
if (key.has_value())
pElement->GetCustomDataManager().SetClientChangesMode(key.value().data(), ECustomDataClientTrust::UNSET);
else
pElement->GetCustomDataManager().SetClientChangesAllowed(true);
}
5 changes: 3 additions & 2 deletions Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ class CLuaElementDefs : public CLuaDefs
LUA_DECLARE(addElementDataSubscriber);
LUA_DECLARE(removeElementDataSubscriber);
LUA_DECLARE(hasElementDataSubscriber);
static void SetElementDataClientTrustEnabled(CElement* pElement, std::string_view key, bool enabled);
static bool IsElementDataClientTrustEnabled(CElement* pElement, std::string_view key);
static void SetElementDataClientTrust(CElement* pElement, bool enabled, std::optional<std::string_view> key);
static void ResetElementDataClientTrust(CElement* pElement, std::optional<std::string_view> key);
static bool IsElementDataClientTrusted(CElement* pElement, std::optional<std::string_view> key);

// Attachement
LUA_DECLARE(attachElements);
Expand Down

0 comments on commit 06e4c82

Please sign in to comment.