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:
parent
23e9f5db43
commit
b38ffdec27
29 changed files with 191 additions and 167 deletions
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue