1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Implement vector and node conversion in Lua (#12609)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
Jude Melton-Houghton 2022-10-18 18:01:44 -04:00 committed by GitHub
parent 23e9f5db43
commit b38ffdec27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 191 additions and 167 deletions

View file

@ -120,16 +120,32 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
#endif
// Add basic globals
lua_newtable(m_luastack);
lua_setglobal(m_luastack, "core");
// vector.metatable is stored in the registry for quick access from C++.
// "core" table:
lua_newtable(m_luastack);
lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE);
lua_newtable(m_luastack);
lua_rawgeti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE);
lua_setfield(m_luastack, -2, "metatable");
lua_setglobal(m_luastack, "vector");
// Populate with some internal functions which will be removed in Lua:
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
return 0;
});
lua_setfield(m_luastack, -2, "set_read_vector");
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
return 0;
});
lua_setfield(m_luastack, -2, "set_push_vector");
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_NODE);
return 0;
});
lua_setfield(m_luastack, -2, "set_read_node");
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_NODE);
return 0;
});
lua_setfield(m_luastack, -2, "set_push_node");
// Finally, put the table into the global environment:
lua_setglobal(m_luastack, "core");
if (m_type == ScriptingType::Client)
lua_pushstring(m_luastack, "/");
@ -180,6 +196,29 @@ void ScriptApiBase::clientOpenLibs(lua_State *L)
}
}
void ScriptApiBase::checkSetByBuiltin()
{
lua_State *L = getStack();
if (m_gamedef) {
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing read_vector");
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing push_vector");
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_NODE);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing read_node");
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_NODE);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing push_node");
lua_pop(L, 1);
}
}
void ScriptApiBase::loadMod(const std::string &script_path,
const std::string &mod_name)
{