/
CLuaElementDefsShared.cpp
87 lines (75 loc) · 2.7 KB
/
CLuaElementDefsShared.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "StdInc.h"
int CLuaElementDefs::GetElementData(lua_State* luaVM)
{
// var getElementData ( element theElement, string key [, inherit = true] )
#ifdef MTA_CLIENT
CClientEntity* pElement;
#else
CElement* pElement;
#endif
SString strKey;
bool bInherit;
CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pElement);
argStream.ReadString(strKey);
argStream.ReadBool(bInherit, true);
if (!argStream.HasErrors())
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (pLuaMain)
{
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
{
// Warn and truncate if key is too long
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
}
#ifdef MTA_CLIENT
CLuaArgument* pVariable = pElement->GetCustomData(strKey, bInherit);
#else
CLuaArgument* pVariable = CStaticFunctionDefinitions::GetElementData(pElement, strKey, bInherit);
#endif
if (pVariable)
{
pVariable->Push(luaVM);
return 1;
}
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
// Failed
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaElementDefs::HasElementData(lua_State* luaVM)
{
// bool hasElementData ( element theElement, string key [, bool inherit = true ] )
#ifdef MTA_CLIENT
CClientEntity* pElement;
#else
CElement* pElement;
#endif
SString strKey;
bool bInherit;
CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pElement);
argStream.ReadString(strKey);
argStream.ReadBool(bInherit, true);
if (argStream.HasErrors())
{
return luaL_error(luaVM, argStream.GetFullErrorMessage());
}
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
{
// Warn and truncate if key is too long
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
}
// Check if data exists with the given key
bool exists = pElement->GetCustomData(strKey, bInherit) != nullptr;
lua_pushboolean(luaVM, exists);
return 1;
}