Skip to content

Commit

Permalink
First iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Nov 3, 2024
1 parent e92702c commit 007336f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
15 changes: 14 additions & 1 deletion Server/mods/deathmatch/logic/CCustomData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void CCustomData::Copy(CCustomData* pCustomData)
}
}

SCustomData* CCustomData::Get(const char* szName)
SCustomData* CCustomData::Get(const char* szName) const
{
assert(szName);

Expand Down Expand Up @@ -100,6 +100,7 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, ESyncTyp
SCustomData newData;
newData.Variable = Variable;
newData.syncType = syncType;
newData.allowClientChanges = true;
m_Data[szName] = newData;
UpdateSynced(szName, Variable, syncType);
}
Expand All @@ -120,6 +121,18 @@ bool CCustomData::Delete(const char* szName)
return false;
}

bool CCustomData::IsClientChangesAllowed(const char* szName) const
{
SCustomData* pData = Get(szName);
return pData ? pData->allowClientChanges : true;
}

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

CXMLNode* CCustomData::OutputToXML(CXMLNode* pNode)
{
std::map<std::string, SCustomData>::const_iterator iter = m_Data.begin();
Expand Down
5 changes: 4 additions & 1 deletion Server/mods/deathmatch/logic/CCustomData.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ struct SCustomData
{
CLuaArgument Variable;
ESyncType syncType;
bool allowClientChanges;
};

class CCustomData
{
public:
void Copy(CCustomData* pCustomData);

SCustomData* Get(const char* szName);
SCustomData* Get(const char* szName) const;
SCustomData* GetSynced(const char* szName);
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);

unsigned short CountOnlySynchronized();

Expand Down
8 changes: 6 additions & 2 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)
CLuaArgument* CElement::GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType, bool* clientChangesAllowed)
{
assert(szName);

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

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

return &pData->Variable;
}

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

// 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 = NULL);
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, ESyncType* pSyncType = nullptr, bool* clientChangesAllowed = 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
18 changes: 17 additions & 1 deletion Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,7 @@ void CGame::AddBuiltInEvents()
m_Events.AddEvent("onPlayerTriggerEventThreshold", "", nullptr, false);
m_Events.AddEvent("onPlayerTeamChange", "oldTeam, newTeam", nullptr, false);
m_Events.AddEvent("onPlayerTriggerInvalidEvent", "eventName, isAdded, isRemote", nullptr, false);
m_Events.AddEvent("onPlayerChangesProtectedData", "element, key, value", nullptr, false);

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

ESyncType lastSyncType = ESyncType::BROADCAST;
pElement->GetCustomData(szName, false, &lastSyncType);
bool clientChangesAllowed = true;

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

if (!clientChangesAllowed)
{
CLogger::ErrorPrintf("Client trying to change protected element data %s (%s)", Packet.GetSourcePlayer()->GetNick(),
szName);

CLuaArguments arguments;
arguments.PushElement(pElement);
arguments.PushString(szName);
arguments.PushArgument(Value);
pSourcePlayer->CallEvent("onPlayerChangesProtectedData", arguments);
return;
}

if (lastSyncType != ESyncType::LOCAL)
{
Expand Down
14 changes: 14 additions & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ void CLuaElementDefs::LoadFunctions()
{"addElementDataSubscriber", addElementDataSubscriber},
{"removeElementDataSubscriber", removeElementDataSubscriber},
{"hasElementDataSubscriber", hasElementDataSubscriber},
{"setElementDataClientTrustEnabled", ArgumentParser<SetElementDataClientTrustEnabled>},
{"isElementDataClientTrustEnabled", ArgumentParser<IsElementDataClientTrustEnabled>},

// Set
{"setElementID", setElementID},
Expand Down Expand Up @@ -151,6 +153,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, "getAttachedOffsets", "getElementAttachedOffsets");
lua_classfunction(luaVM, "getChild", "getElementChild");
Expand Down Expand Up @@ -189,6 +192,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_classvariable(luaVM, "id", "setElementID", "getElementID");
lua_classvariable(luaVM, "callPropagationEnabled", "setElementCallPropagationEnabled", "isElementCallPropagationEnabled");
Expand Down Expand Up @@ -2437,3 +2441,13 @@ int CLuaElementDefs::isElementCallPropagationEnabled(lua_State* luaVM)
lua_pushboolean(luaVM, false);
return 1;
}

void CLuaElementDefs::SetElementDataClientTrustEnabled(CElement* pElement, std::string_view key, bool enabled)
{
pElement->GetCustomDataManager().SetClientChangesAllowed(key.data(), enabled);
}

bool CLuaElementDefs::IsElementDataClientTrustEnabled(CElement* pElement, std::string_view key)
{
return pElement->GetCustomDataManager().IsClientChangesAllowed(key.data());
}
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaElementDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ 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);

// Attachement
LUA_DECLARE(attachElements);
Expand Down

0 comments on commit 007336f

Please sign in to comment.