From 640c46f02c3a206cf7075f172aca5c9a3143962b Mon Sep 17 00:00:00 2001 From: Uladzislau Nikalayevich Date: Wed, 6 Nov 2024 18:44:43 +0300 Subject: [PATCH] Replace with server config setting and setElementData parameter --- Server/mods/deathmatch/local.conf | 6 +++ Server/mods/deathmatch/logic/CCustomData.cpp | 9 ----- Server/mods/deathmatch/logic/CCustomData.h | 5 --- Server/mods/deathmatch/logic/CGame.cpp | 2 +- Server/mods/deathmatch/logic/CMainConfig.cpp | 3 ++ Server/mods/deathmatch/logic/CMainConfig.h | 2 + .../logic/CStaticFunctionDefinitions.cpp | 13 ++++-- .../logic/CStaticFunctionDefinitions.h | 3 +- .../logic/lua/CLuaFunctionParseHelpers.cpp | 6 +++ .../logic/lua/CLuaFunctionParseHelpers.h | 1 + .../logic/luadefs/CLuaElementDefs.cpp | 40 +++++-------------- .../logic/luadefs/CLuaElementDefs.h | 3 -- Server/mods/deathmatch/mtaserver.conf | 6 +++ .../mods/deathmatch/mtaserver.conf.template | 6 +++ 14 files changed, 51 insertions(+), 54 deletions(-) diff --git a/Server/mods/deathmatch/local.conf b/Server/mods/deathmatch/local.conf index ec3defd37d..4b83df674a 100644 --- a/Server/mods/deathmatch/local.conf +++ b/Server/mods/deathmatch/local.conf @@ -264,6 +264,12 @@ Values: 0 - Off, 1 - Enabled. Default - 1 --> 1 + + 0 + diff --git a/Server/mods/deathmatch/logic/CCustomData.cpp b/Server/mods/deathmatch/logic/CCustomData.cpp index 12563e04dd..27e99ec015 100644 --- a/Server/mods/deathmatch/logic/CCustomData.cpp +++ b/Server/mods/deathmatch/logic/CCustomData.cpp @@ -121,15 +121,6 @@ bool CCustomData::Delete(const char* szName) return false; } -bool CCustomData::IsClientChangesAllowed(const char* szName) const -{ - SCustomData* pData = Get(szName); - if (!pData || pData->clientChangesMode == ECustomDataClientTrust::UNSET) - return IsClientChangesAllowed(); - - return pData->clientChangesMode == ECustomDataClientTrust::ALLOW; -} - void CCustomData::SetClientChangesMode(const char* szName, ECustomDataClientTrust mode) { SCustomData& pData = m_Data[szName]; diff --git a/Server/mods/deathmatch/logic/CCustomData.h b/Server/mods/deathmatch/logic/CCustomData.h index b22674e580..6f1dbc597f 100644 --- a/Server/mods/deathmatch/logic/CCustomData.h +++ b/Server/mods/deathmatch/logic/CCustomData.h @@ -50,12 +50,8 @@ class CCustomData bool Delete(const char* szName); - bool IsClientChangesAllowed(const char* szName) const; 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(); CXMLNode* OutputToXML(CXMLNode* pNode); @@ -72,5 +68,4 @@ class CCustomData std::map m_Data; std::map m_SyncedData; - bool m_clientChangesAllowed{true}; }; diff --git a/Server/mods/deathmatch/logic/CGame.cpp b/Server/mods/deathmatch/logic/CGame.cpp index 324d2ed277..52029f2d76 100644 --- a/Server/mods/deathmatch/logic/CGame.cpp +++ b/Server/mods/deathmatch/logic/CGame.cpp @@ -2657,7 +2657,7 @@ void CGame::Packet_CustomData(CCustomDataPacket& Packet) pElement->GetCustomData(szName, false, &lastSyncType, &clientChangesMode); - const bool changesAllowed = clientChangesMode == ECustomDataClientTrust::UNSET ? pElement->GetCustomDataManager().IsClientChangesAllowed() + const bool changesAllowed = clientChangesMode == ECustomDataClientTrust::UNSET ? !m_pMainConfig->IsElementDataWhitelisted() : clientChangesMode == ECustomDataClientTrust::ALLOW; if (!changesAllowed) { diff --git a/Server/mods/deathmatch/logic/CMainConfig.cpp b/Server/mods/deathmatch/logic/CMainConfig.cpp index d47950e73e..87881e3370 100644 --- a/Server/mods/deathmatch/logic/CMainConfig.cpp +++ b/Server/mods/deathmatch/logic/CMainConfig.cpp @@ -79,6 +79,7 @@ CMainConfig::CMainConfig(CConsole* pConsole) : CXMLConfig(NULL) m_iBackupInterval = 3; m_iBackupAmount = 5; m_bSyncMapElementData = true; + m_elementDataWhitelisted = false; } bool CMainConfig::Load() @@ -526,6 +527,8 @@ bool CMainConfig::Load() g_TickRateSettings.iLightSync = Clamp(200, g_TickRateSettings.iLightSync, 4000); } + GetBoolean(m_pRootNode, "elementdata_whitelisted", m_elementDataWhitelisted); + ApplyNetOptions(); return true; diff --git a/Server/mods/deathmatch/logic/CMainConfig.h b/Server/mods/deathmatch/logic/CMainConfig.h index b52733229a..8c1c1f30fb 100644 --- a/Server/mods/deathmatch/logic/CMainConfig.h +++ b/Server/mods/deathmatch/logic/CMainConfig.h @@ -126,6 +126,7 @@ class CMainConfig : public CXMLConfig const std::vector& GetOwnerEmailAddressList() const { return m_OwnerEmailAddressList; } bool IsDatabaseCredentialsProtectionEnabled() const { return m_bDatabaseCredentialsProtectionEnabled != 0; } bool IsFakeLagCommandEnabled() const { return m_bFakeLagCommandEnabled != 0; } + bool IsElementDataWhitelisted() const { return m_elementDataWhitelisted; }; SString GetSetting(const SString& configSetting); bool GetSetting(const SString& configSetting, SString& strValue); @@ -227,4 +228,5 @@ class CMainConfig : public CXMLConfig int m_bFakeLagCommandEnabled; int m_iPlayerTriggeredEventIntervalMs; int m_iMaxPlayerTriggeredEventsPerInterval; + bool m_elementDataWhitelisted; }; diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 67764f8057..2d65af0eaf 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -954,14 +954,19 @@ bool CStaticFunctionDefinitions::SetElementID(CElement* pElement, const char* sz return true; } -bool CStaticFunctionDefinitions::SetElementData(CElement* pElement, const char* szName, const CLuaArgument& Variable, ESyncType syncType) +bool CStaticFunctionDefinitions::SetElementData(CElement* pElement, const char* szName, const CLuaArgument& Variable, ESyncType syncType, + std::optional clientTrust) { assert(pElement); assert(szName); assert(strlen(szName) <= MAX_CUSTOMDATA_NAME_LENGTH); - ESyncType lastSyncType = ESyncType::BROADCAST; - CLuaArgument* pCurrentVariable = pElement->GetCustomData(szName, false, &lastSyncType); + ESyncType lastSyncType = ESyncType::BROADCAST; + ECustomDataClientTrust lastClientTrust{}; + CLuaArgument* pCurrentVariable = pElement->GetCustomData(szName, false, &lastSyncType, &lastClientTrust); + + if (clientTrust.has_value() && lastClientTrust != clientTrust.value()) + pElement->GetCustomDataManager().SetClientChangesMode(szName, clientTrust.value()); if (!pCurrentVariable || *pCurrentVariable != Variable || lastSyncType != syncType) { @@ -984,7 +989,7 @@ bool CStaticFunctionDefinitions::SetElementData(CElement* pElement, const char* // Unsubscribe all the players if (lastSyncType == ESyncType::SUBSCRIBE && syncType != ESyncType::SUBSCRIBE) m_pPlayerManager->ClearElementData(pElement, szName); - + // Set its custom data pElement->SetCustomData(szName, Variable, syncType); return true; diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h index dd6202208f..e36b04734e 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h @@ -83,7 +83,8 @@ class CStaticFunctionDefinitions // Element set funcs static bool ClearElementVisibleTo(CElement* pElement); static bool SetElementID(CElement* pElement, const char* szID); - static bool SetElementData(CElement* pElement, const char* szName, const CLuaArgument& Variable, ESyncType syncType); + static bool SetElementData(CElement* pElement, const char* szName, const CLuaArgument& Variable, ESyncType syncType, + std::optional clientTrust); static bool RemoveElementData(CElement* pElement, const char* szName); static bool AddElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer); static bool RemoveElementDataSubscriber(CElement* pElement, const char* szName, CPlayer* pPlayer); diff --git a/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp b/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp index ef25750c05..a8bb8d8222 100644 --- a/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp +++ b/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp @@ -285,6 +285,12 @@ ADD_ENUM(ESyncType::LOCAL, "local") ADD_ENUM(ESyncType::SUBSCRIBE, "subscribe") IMPLEMENT_ENUM_CLASS_END("sync-mode") +IMPLEMENT_ENUM_CLASS_BEGIN(ECustomDataClientTrust) +ADD_ENUM(ECustomDataClientTrust::UNSET, "default") +ADD_ENUM(ECustomDataClientTrust::ALLOW, "allow") +ADD_ENUM(ECustomDataClientTrust::DENY, "deny") +IMPLEMENT_ENUM_CLASS_END("client-trust-mode") + // // CResource from userdata // diff --git a/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h b/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h index 52a4536012..f9cd0c85d9 100644 --- a/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h +++ b/Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h @@ -38,6 +38,7 @@ DECLARE_ENUM(CAccessControlListRight::ERightType); DECLARE_ENUM(CElement::EElementType); DECLARE_ENUM(CAccountPassword::EAccountPasswordType); DECLARE_ENUM_CLASS(ESyncType); +DECLARE_ENUM_CLASS(ECustomDataClientTrust) enum eHudComponent { diff --git a/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp b/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp index 673edc9763..8e3e2f3969 100644 --- a/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp +++ b/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp @@ -80,9 +80,6 @@ void CLuaElementDefs::LoadFunctions() {"addElementDataSubscriber", addElementDataSubscriber}, {"removeElementDataSubscriber", removeElementDataSubscriber}, {"hasElementDataSubscriber", hasElementDataSubscriber}, - {"setElementDataClientTrust", ArgumentParser}, - {"isElementDataClientTrusted", ArgumentParser}, - {"resetElementDataClientTrust", ArgumentParser}, // Set {"setElementID", setElementID}, @@ -132,7 +129,6 @@ 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"); @@ -155,7 +151,6 @@ void CLuaElementDefs::AddClass(lua_State* luaVM) lua_classfunction(luaVM, "setLowLOD", "setLowLODElement"); lua_classfunction(luaVM, "setAttachedOffsets", "setElementAttachedOffsets"); lua_classfunction(luaVM, "setCallPropagationEnabled", "setElementCallPropagationEnabled"); - lua_classfunction(luaVM, "setDataClientTrust", "setElementDataClientTrust"); lua_classfunction(luaVM, "getAttachedOffsets", "getElementAttachedOffsets"); lua_classfunction(luaVM, "getChild", "getElementChild"); @@ -194,7 +189,6 @@ void CLuaElementDefs::AddClass(lua_State* luaVM) lua_classfunction(luaVM, "isVisibleTo", "isElementVisibleTo"); lua_classfunction(luaVM, "isLowLOD", "isElementLowLOD"); lua_classfunction(luaVM, "isAttached", "isElementAttached"); - lua_classfunction(luaVM, "isDataClientTrusted", "isElementDataClientTrusted"); lua_classvariable(luaVM, "id", "setElementID", "getElementID"); lua_classvariable(luaVM, "callPropagationEnabled", "setElementCallPropagationEnabled", "isElementCallPropagationEnabled"); @@ -1544,6 +1538,7 @@ int CLuaElementDefs::setElementData(lua_State* luaVM) SString strKey; CLuaArgument value; ESyncType syncType = ESyncType::BROADCAST; + std::optional clientTrust{}; CScriptArgReader argStream(luaVM); argStream.ReadUserData(pElement); @@ -1560,6 +1555,13 @@ int CLuaElementDefs::setElementData(lua_State* luaVM) else argStream.ReadEnumString(syncType, ESyncType::BROADCAST); + if (!argStream.NextIsNone()) + { + ECustomDataClientTrust trustReaded; + argStream.ReadEnumString(trustReaded); + clientTrust = trustReaded; + } + if (!argStream.HasErrors()) { LogWarningIfPlayerHasNotJoinedYet(luaVM, pElement); @@ -1572,7 +1574,7 @@ int CLuaElementDefs::setElementData(lua_State* luaVM) strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH); } - if (CStaticFunctionDefinitions::SetElementData(pElement, strKey, value, syncType)) + if (CStaticFunctionDefinitions::SetElementData(pElement, strKey, value, syncType, clientTrust)) { lua_pushboolean(luaVM, true); return 1; @@ -2443,27 +2445,3 @@ int CLuaElementDefs::isElementCallPropagationEnabled(lua_State* luaVM) lua_pushboolean(luaVM, false); return 1; } - -void CLuaElementDefs::SetElementDataClientTrust(CElement* pElement, bool enabled, std::optional key) -{ - 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 key) -{ - if (key.has_value()) - return pElement->GetCustomDataManager().IsClientChangesAllowed(key.value().data()); - else - return pElement->GetCustomDataManager().IsClientChangesAllowed(); -} - -void CLuaElementDefs::ResetElementDataClientTrust(CElement* pElement, std::optional key) -{ - if (key.has_value()) - pElement->GetCustomDataManager().SetClientChangesMode(key.value().data(), ECustomDataClientTrust::UNSET); - else - pElement->GetCustomDataManager().SetClientChangesAllowed(true); -} diff --git a/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h b/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h index aa69183f27..b58f613b59 100644 --- a/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h +++ b/Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h @@ -77,9 +77,6 @@ class CLuaElementDefs : public CLuaDefs LUA_DECLARE(addElementDataSubscriber); LUA_DECLARE(removeElementDataSubscriber); LUA_DECLARE(hasElementDataSubscriber); - static void SetElementDataClientTrust(CElement* pElement, bool enabled, std::optional key); - static void ResetElementDataClientTrust(CElement* pElement, std::optional key); - static bool IsElementDataClientTrusted(CElement* pElement, std::optional key); // Attachement LUA_DECLARE(attachElements); diff --git a/Server/mods/deathmatch/mtaserver.conf b/Server/mods/deathmatch/mtaserver.conf index 19fe4a05d8..7acb9af9eb 100644 --- a/Server/mods/deathmatch/mtaserver.conf +++ b/Server/mods/deathmatch/mtaserver.conf @@ -268,6 +268,12 @@ Values: 0 - Off, 1 - Enabled. Default - 1 --> 1 + + 0 + 1 + + 0 +