Skip to content

Commit 3eb0bf6

Browse files
Pirulaxbotder
andauthored
Add lua_getowner for fast access to owner resource (#2146)
* Implement add `owner` field to Lua for faster owner resource access * Refactor CanUseFunction server side to use lua_getowner * Set CLuaMain* as owner instead of the resource. * Rename: lua_getowner => lua_getmtasaowner. And global_State::owner => mtasaowner * Introduce lua_getownercluamain and lua_getownerresource to LuaCommon.h * Retrun a reference instead of pointer * Update Server/mods/deathmatch/logic/luadefs/CLuaDefs.cpp Co-authored-by: Marek Kulik <me@botder.com> * Rename argument owner -> mtasaowner Co-authored-by: Marek Kulik <me@botder.com>
1 parent ece68e5 commit 3eb0bf6

File tree

13 files changed

+53
-17
lines changed

13 files changed

+53
-17
lines changed

Client/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void CLuaMain::InitVM()
143143
assert(!m_luaVM);
144144

145145
// Create a new VM
146-
m_luaVM = lua_open();
146+
m_luaVM = lua_open(this);
147147
m_pLuaManager->OnLuaMainOpenVM(this, m_luaVM);
148148

149149
// Set the instruction count hook

Client/mods/deathmatch/logic/lua/LuaCommon.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ void lua_pushmatrix(lua_State* luaVM, const CMatrix& matrix)
192192
lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES);
193193
}
194194

195+
CLuaMain& lua_getownercluamain(lua_State* L)
196+
{
197+
return *static_cast<class CLuaMain*>(lua_getmtasaowner(L));
198+
}
199+
200+
CResource& lua_getownerresource(lua_State* L)
201+
{
202+
return *lua_getownercluamain(L).GetResource();
203+
}
204+
195205
// Just do a type check vs LUA_TNONE before calling this, or bant
196206
const char* lua_makestring(lua_State* luaVM, int iArgument)
197207
{

Client/mods/deathmatch/logic/lua/LuaCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ void lua_classmetamethod(lua_State* luaVM, const char* szName, lua_CFunction fn)
7878

7979
const char* lua_makestring(lua_State* luaVM, int iArgument);
8080

81+
class CLuaMain& lua_getownercluamain(lua_State* L);
82+
class CResource& lua_getownerresource(lua_State* L);
83+
8184
// Lua debug info for logging
8285
enum
8386
{

Server/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void CLuaMain::InitVM()
172172
assert(!m_luaVM);
173173

174174
// Create a new VM
175-
m_luaVM = lua_open();
175+
m_luaVM = lua_open(this);
176176
m_pLuaManager->OnLuaMainOpenVM(this, m_luaVM);
177177

178178
// Set the instruction count hook

Server/mods/deathmatch/logic/lua/LuaCommon.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ void lua_pushmatrix(lua_State* luaVM, const CMatrix& matrix)
274274
lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES);
275275
}
276276

277+
CLuaMain& lua_getownercluamain(lua_State* L)
278+
{
279+
return *static_cast<class CLuaMain*>(lua_getmtasaowner(L));
280+
}
281+
282+
CResource& lua_getownerresource(lua_State* L)
283+
{
284+
return *lua_getownercluamain(L).GetResource();
285+
}
286+
277287
// Just do a type check vs LUA_TNONE before calling this, or bant
278288
const char* lua_makestring(lua_State* luaVM, int iArgument)
279289
{

Server/mods/deathmatch/logic/lua/LuaCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ void lua_pushvector(lua_State* luaVM, const CVector& vector);
4545
void lua_pushvector(lua_State* luaVM, const CVector4D& vector);
4646
void lua_pushmatrix(lua_State* luaVM, const CMatrix& matrix);
4747

48+
class CLuaMain& lua_getownercluamain(lua_State* L);
49+
class CResource& lua_getownerresource(lua_State* L);
50+
4851
// Converts any type to string
4952
const char* lua_makestring(lua_State* luaVM, int iArgument);
5053

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include "lua/LuaCommon.h"
1314
extern uint g_uiNetSentByteCounter;
1415

1516
namespace
@@ -113,20 +114,18 @@ int CLuaDefs::CanUseFunction(lua_CFunction f, lua_State* luaVM)
113114
{
114115
return true;
115116
}
116-
117+
117118
// Get associated resource
118-
CResource* pResource = m_pResourceManager->GetResourceFromLuaState(luaVM);
119-
if (!pResource)
120-
return true;
119+
CResource& resource{ lua_getownerresource(luaVM) };
121120

122121
// Update execution time check
123-
pResource->GetVirtualMachine()->CheckExecutionTime();
122+
resource.GetVirtualMachine()->CheckExecutionTime();
124123

125124
// Check function right cache in resource
126125
bool bAllowed;
127126

128127
// Check cached ACL rights
129-
if (pResource->CheckFunctionRightCache(f, &bAllowed))
128+
if (resource.CheckFunctionRightCache(f, &bAllowed))
130129
{
131130
// If in cache, and not allowed, do warning here
132131
if (!bAllowed)
@@ -168,7 +167,7 @@ int CLuaDefs::CanUseFunction(lua_CFunction f, lua_State* luaVM)
168167
}
169168
}
170169
// Update cache in resource
171-
pResource->UpdateFunctionRightCache(f, bAllowed);
170+
resource.UpdateFunctionRightCache(f, bAllowed);
172171
}
173172

174173
if (!g_pGame->GetDebugHookManager()->OnPreFunction(f, luaVM, bAllowed))

vendor/lua/src/lapi.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,4 +1139,10 @@ LUA_API void lua_addtotalbytes(lua_State *L, int n)
11391139
LUA_API int lua_ncallresult(lua_State *L)
11401140
{
11411141
return L->nexpectedresults;
1142-
}
1142+
}
1143+
1144+
// MTA Addition to access `owner` value
1145+
LUA_API void *lua_getmtasaowner(lua_State* L)
1146+
{
1147+
return G(L)->mtasaowner;
1148+
}

vendor/lua/src/lauxlib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ static int panic (lua_State *L) {
644644
}
645645

646646

647-
LUALIB_API lua_State *luaL_newstate (void) {
648-
lua_State *L = lua_newstate(l_alloc, NULL);
647+
LUALIB_API lua_State *luaL_newstate (void* mtasaowner) {
648+
lua_State *L = lua_newstate(l_alloc, NULL, mtasaowner);
649649
if (L) lua_atpanic(L, &panic);
650650
return L;
651651
}

vendor/lua/src/lauxlib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
7979
const char *name);
8080
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
8181

82-
LUALIB_API lua_State *(luaL_newstate) (void);
82+
LUALIB_API lua_State *(luaL_newstate) (void *mtasaowner);
8383

8484

8585
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,

0 commit comments

Comments
 (0)