Skip to content

Commit 0ff6607

Browse files
Unde-RqaisjpStrixGpatrikjuvonen
authored
Fix #1474: Implement getAllElementData client-side (#1612)
Co-authored-by: Qais Patankar <qaisjp@gmail.com> Co-authored-by: Nikita Obrekht <obrekht@gmail.com> Co-authored-by: Patrik Juvonen <22572159+patrikjuvonen@users.noreply.github.com>
1 parent 075dfee commit 0ff6607

File tree

8 files changed

+42
-52
lines changed

8 files changed

+42
-52
lines changed

Client/mods/deathmatch/logic/CClientEntity.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@ CLuaArgument* CClientEntity::GetCustomData(const char* szName, bool bInheritData
302302
return NULL;
303303
}
304304

305+
CLuaArguments* CClientEntity::GetAllCustomData(CLuaArguments* table)
306+
{
307+
assert(table);
308+
309+
for (auto it = m_pCustomData->IterBegin(); it != m_pCustomData->IterEnd(); it++)
310+
{
311+
table->PushString(it->first); // key
312+
table->PushArgument(it->second.Variable); // value
313+
}
314+
315+
return table;
316+
}
317+
305318
bool CClientEntity::GetCustomDataString(const char* szName, SString& strOut, bool bInheritData)
306319
{
307320
// Grab the custom data variable

Client/mods/deathmatch/logic/CClientEntity.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,15 @@ class CClientEntity : public CClientEntityBase
198198
ElementID GetID() { return m_ID; };
199199
void SetID(ElementID ID);
200200

201-
CCustomData* GetCustomDataPointer() { return m_pCustomData; }
202-
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced = nullptr);
203-
bool GetCustomDataString(const char* szKey, SString& strOut, bool bInheritData);
204-
bool GetCustomDataFloat(const char* szKey, float& fOut, bool bInheritData);
205-
bool GetCustomDataInt(const char* szKey, int& iOut, bool bInheritData);
206-
bool GetCustomDataBool(const char* szKey, bool& bOut, bool bInheritData);
207-
void SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
208-
void DeleteCustomData(const char* szName);
201+
CCustomData* GetCustomDataPointer() { return m_pCustomData; }
202+
CLuaArgument* GetCustomData(const char* szName, bool bInheritData, bool* pbIsSynced = nullptr);
203+
CLuaArguments* GetAllCustomData(CLuaArguments* table);
204+
bool GetCustomDataString(const char* szKey, SString& strOut, bool bInheritData);
205+
bool GetCustomDataFloat(const char* szKey, float& fOut, bool bInheritData);
206+
bool GetCustomDataInt(const char* szKey, int& iOut, bool bInheritData);
207+
bool GetCustomDataBool(const char* szKey, bool& bOut, bool bInheritData);
208+
void SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
209+
void DeleteCustomData(const char* szName);
209210

210211
virtual bool GetMatrix(CMatrix& matrix) const;
211212
virtual bool SetMatrix(const CMatrix& matrix);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void CLuaElementDefs::LoadFunctions()
2323
{"getElementByID", GetElementByID},
2424
{"getElementByIndex", GetElementByIndex},
2525
{"getElementData", GetElementData},
26+
{"getAllElementData", ArgumentParserWarn<false, GetAllElementData>},
2627
{"getElementMatrix", GetElementMatrix},
2728
{"getElementPosition", GetElementPosition},
2829
{"getElementRotation", GetElementRotation},
@@ -165,6 +166,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
165166
lua_classfunction(luaVM, "getAttachedTo", "getElementAttachedTo");
166167
lua_classfunction(luaVM, "getAttachedOffsets", "getElementAttachedOffsets");
167168
lua_classfunction(luaVM, "getData", "getElementData");
169+
lua_classfunction(luaVM, "getAllData", "getAllElementData");
168170

169171
lua_classfunction(luaVM, "setAttachedOffsets", "setElementAttachedOffsets");
170172
lua_classfunction(luaVM, "setData", "setElementData");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CLuaElementDefs : public CLuaDefs
2727
LUA_DECLARE(GetElementByID);
2828
LUA_DECLARE(GetElementByIndex);
2929
LUA_DECLARE(GetElementData);
30+
static CLuaArguments GetAllElementData(CClientEntity* pElement);
3031
LUA_DECLARE(HasElementData);
3132
LUA_DECLARE(GetElementParent);
3233
LUA_DECLARE_OOP(GetElementMatrix);

Server/mods/deathmatch/logic/CElement.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,10 @@ CLuaArguments* CElement::GetAllCustomData(CLuaArguments* table)
537537
{
538538
assert(table);
539539

540-
// Grab it and return a pointer to the variable
541-
map<string, SCustomData>::const_iterator iter = m_pCustomData->IterBegin();
542-
for (; iter != m_pCustomData->IterEnd(); iter++)
540+
for (auto it = m_pCustomData->IterBegin(); it != m_pCustomData->IterEnd(); it++)
543541
{
544-
table->PushString(iter->first.c_str()); // key
545-
table->PushArgument(iter->second.Variable); // value
542+
table->PushString(it->first); // key
543+
table->PushArgument(it->second.Variable); // value
546544
}
547545

548546
return table;

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void CLuaElementDefs::LoadFunctions()
3939
{"getElementChildren", getElementChildren},
4040
{"getElementChild", getElementChild},
4141
{"getElementChildrenCount", getElementChildrenCount},
42-
{"getAllElementData", getAllElementData},
4342
{"getElementID", getElementID},
4443
{"getElementParent", getElementParent},
4544
{"getElementMatrix", getElementMatrix},
@@ -74,6 +73,7 @@ void CLuaElementDefs::LoadFunctions()
7473

7574
// Element data
7675
{"getElementData", GetElementData},
76+
{"getAllElementData", ArgumentParserWarn<false, GetAllElementData>},
7777
{"hasElementData", HasElementData},
7878
{"setElementData", setElementData},
7979
{"removeElementData", removeElementData},
@@ -504,30 +504,6 @@ int CLuaElementDefs::getElementByIndex(lua_State* luaVM)
504504
return 1;
505505
}
506506

507-
int CLuaElementDefs::getAllElementData(lua_State* luaVM)
508-
{
509-
// table getAllElementData ( element theElement )
510-
CElement* pElement;
511-
512-
CScriptArgReader argStream(luaVM);
513-
argStream.ReadUserData(pElement);
514-
515-
if (!argStream.HasErrors())
516-
{
517-
CLuaArguments Args;
518-
if (CStaticFunctionDefinitions::GetAllElementData(pElement, &Args))
519-
{
520-
Args.PushAsTable(luaVM);
521-
return 1;
522-
}
523-
}
524-
else
525-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
526-
527-
lua_pushboolean(luaVM, false);
528-
return 1;
529-
}
530-
531507
int CLuaElementDefs::getElementParent(lua_State* luaVM)
532508
{
533509
// element getElementParent ( element theElement )

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class CLuaElementDefs : public CLuaDefs
3636
LUA_DECLARE(getElementID);
3737
LUA_DECLARE(getElementByID);
3838
LUA_DECLARE(getElementByIndex);
39-
LUA_DECLARE(getAllElementData);
4039
LUA_DECLARE(getElementParent);
4140
LUA_DECLARE_OOP(getElementMatrix);
4241
LUA_DECLARE_OOP(getElementPosition);
@@ -71,6 +70,7 @@ class CLuaElementDefs : public CLuaDefs
7170

7271
// Element data
7372
LUA_DECLARE(GetElementData);
73+
static CLuaArguments GetAllElementData(CElement* pElement);
7474
LUA_DECLARE(HasElementData);
7575
LUA_DECLARE(setElementData);
7676
LUA_DECLARE(removeElementData);

Shared/mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* PROJECT: Multi Theft Auto v1.0
44
* LICENSE: See LICENSE in the top level directory
5-
* FILE: mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp
5+
* FILE: Shared/mods/deathmatch/logic/luadefs/CLuaElementDefsShared.cpp
66
* PURPOSE: Lua element definitions class
77
*
88
* Multi Theft Auto is available from https://multitheftauto.com/
@@ -18,13 +18,9 @@ int CLuaElementDefs::GetElementData(lua_State* luaVM)
1818
{
1919
// var getElementData ( element theElement, string key [, inherit = true] )
2020

21-
#ifdef MTA_CLIENT
22-
CClientEntity* pElement;
23-
#else
2421
CElement* pElement;
25-
#endif
26-
SString strKey;
27-
bool bInherit;
22+
SString strKey;
23+
bool bInherit;
2824

2925
CScriptArgReader argStream(luaVM);
3026
argStream.ReadUserData(pElement);
@@ -64,17 +60,20 @@ int CLuaElementDefs::GetElementData(lua_State* luaVM)
6460
return 1;
6561
}
6662

63+
CLuaArguments CLuaElementDefs::GetAllElementData(CElement* pElement)
64+
{
65+
CLuaArguments customData;
66+
pElement->GetAllCustomData(&customData);
67+
return customData;
68+
}
69+
6770
int CLuaElementDefs::HasElementData(lua_State* luaVM)
6871
{
6972
// bool hasElementData ( element theElement, string key [, bool inherit = true ] )
7073

71-
#ifdef MTA_CLIENT
72-
CClientEntity* pElement;
73-
#else
7474
CElement* pElement;
75-
#endif
76-
SString strKey;
77-
bool bInherit;
75+
SString strKey;
76+
bool bInherit;
7877

7978
CScriptArgReader argStream(luaVM);
8079
argStream.ReadUserData(pElement);

0 commit comments

Comments
 (0)