Skip to content

Commit

Permalink
Replace with server config setting and setElementData parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Nov 6, 2024
1 parent 06e4c82 commit 640c46f
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 54 deletions.
6 changes: 6 additions & 0 deletions Server/mods/deathmatch/local.conf
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@
Values: 0 - Off, 1 - Enabled. Default - 1 -->
<database_credentials_protection>1</database_credentials_protection>

<!-- Enables extra protection for element data.
When this option is enabled, the server ignores element data sync packets from the client,
except for allowed keys.
Values: 0 - Off, 1 - Enabled. Default - 0 -->
<elementdata_whitelisted>0</elementdata_whitelisted>

<!-- Specifies the module(s) which are loaded with the server. To load several modules, add more <module>
parameter(s). Optional parameter. -->
<!-- <module src="sample_win32.dll"/> -->
Expand Down
9 changes: 0 additions & 9 deletions Server/mods/deathmatch/logic/CCustomData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
5 changes: 0 additions & 5 deletions Server/mods/deathmatch/logic/CCustomData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -72,5 +68,4 @@ class CCustomData

std::map<std::string, SCustomData> m_Data;
std::map<std::string, SCustomData> m_SyncedData;
bool m_clientChangesAllowed{true};
};
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions Server/mods/deathmatch/logic/CMainConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CMainConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class CMainConfig : public CXMLConfig
const std::vector<SString>& 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);
Expand Down Expand Up @@ -227,4 +228,5 @@ class CMainConfig : public CXMLConfig
int m_bFakeLagCommandEnabled;
int m_iPlayerTriggeredEventIntervalMs;
int m_iMaxPlayerTriggeredEventsPerInterval;
bool m_elementDataWhitelisted;
};
13 changes: 9 additions & 4 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ECustomDataClientTrust> 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)
{
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ECustomDataClientTrust> 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);
Expand Down
6 changes: 6 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
40 changes: 9 additions & 31 deletions Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ void CLuaElementDefs::LoadFunctions()
{"addElementDataSubscriber", addElementDataSubscriber},
{"removeElementDataSubscriber", removeElementDataSubscriber},
{"hasElementDataSubscriber", hasElementDataSubscriber},
{"setElementDataClientTrust", ArgumentParser<SetElementDataClientTrust>},
{"isElementDataClientTrusted", ArgumentParser<IsElementDataClientTrusted>},
{"resetElementDataClientTrust", ArgumentParser<ResetElementDataClientTrust>},

// Set
{"setElementID", setElementID},
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1544,6 +1538,7 @@ int CLuaElementDefs::setElementData(lua_State* luaVM)
SString strKey;
CLuaArgument value;
ESyncType syncType = ESyncType::BROADCAST;
std::optional<ECustomDataClientTrust> clientTrust{};

CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pElement);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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<std::string_view> 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<std::string_view> key)
{
if (key.has_value())
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.value().data());
else
return pElement->GetCustomDataManager().IsClientChangesAllowed();
}

void CLuaElementDefs::ResetElementDataClientTrust(CElement* pElement, std::optional<std::string_view> key)
{
if (key.has_value())
pElement->GetCustomDataManager().SetClientChangesMode(key.value().data(), ECustomDataClientTrust::UNSET);
else
pElement->GetCustomDataManager().SetClientChangesAllowed(true);
}
3 changes: 0 additions & 3 deletions Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
6 changes: 6 additions & 0 deletions Server/mods/deathmatch/mtaserver.conf
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@
Values: 0 - Off, 1 - Enabled. Default - 1 -->
<database_credentials_protection>1</database_credentials_protection>

<!-- Enables extra protection for element data.
When this option is enabled, the server ignores element data sync packets from the client,
except for allowed keys.
Values: 0 - Off, 1 - Enabled. Default - 0 -->
<elementdata_whitelisted>0</elementdata_whitelisted>

<!-- These parameters specify the maximum amount of events that can be triggered by the client (via triggerServerEvent) within the given interval.
Note: The interval is given in milliseconds
Interval range: 50 to 5000. Default: 1000
Expand Down
6 changes: 6 additions & 0 deletions Server/mods/deathmatch/mtaserver.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@
Values: 0 - Off, 1 - Enabled. Default - 1 -->
<database_credentials_protection>1</database_credentials_protection>

<!-- Enables extra protection for element data.
When this option is enabled, the server ignores element data sync packets from the client,
except for allowed keys.
Values: 0 - Off, 1 - Enabled. Default - 0 -->
<elementdata_whitelisted>0</elementdata_whitelisted>

<!-- These parameters specify the maximum amount of events that can be triggered by the client (via triggerServerEvent) within the given interval.
Note: The interval is given in milliseconds
Interval range: 50 to 5000. Default: 1000
Expand Down

0 comments on commit 640c46f

Please sign in to comment.