1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Add GUIDs and unit tests.

This commit is contained in:
SFENCE 2025-02-06 20:44:27 +01:00
parent aba2b6638e
commit 1df282c635
19 changed files with 246 additions and 14 deletions

View file

@ -406,17 +406,17 @@ void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn)
/*
* How ObjectRefs are handled in Lua:
* When an active object is created, an ObjectRef is created on the Lua side
* and stored in core.object_refs[id].
* and stored in core.object_refs[id] and in core.objects_by_guids[GUID].
* Methods that require an ObjectRef to a certain object retrieve it from that
* table instead of creating their own.(*)
* When an active object is removed, the existing ObjectRef is invalidated
* using ::set_null() and removed from the core.object_refs table.
* using ::set_null() and removed from the core.object_refs and
* core.object_by_guids tables.
* (*) An exception to this are NULL ObjectRefs and anonymous ObjectRefs
* for objects without ID.
* It's unclear what the latter are needed for and their use is problematic
* since we lose control over the ref and the contained pointer.
*/
void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
{
SCRIPTAPI_PRECHECKHEADER
@ -434,7 +434,18 @@ void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
// object_refs[id] = object
lua_pushinteger(L, cobj->getId()); // Push id
lua_pushvalue(L, object); // Copy object to top of stack
lua_pushvalue(L, object);
lua_settable(L, objectstable);
// Get core.objects_by_guid table
lua_getglobal(L, "core");
lua_getfield(L, -1, "objects_by_guid");
luaL_checktype(L, -1, LUA_TTABLE);
objectstable = lua_gettop(L);
// objects_by_guid[guid] = object
lua_pushstring(L, cobj->getGUID().c_str());
lua_pushvalue(L, object);
lua_settable(L, objectstable);
}
@ -445,6 +456,7 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
// Get core.object_refs table
lua_getglobal(L, "core");
int core = lua_gettop(L);
lua_getfield(L, -1, "object_refs");
luaL_checktype(L, -1, LUA_TTABLE);
int objectstable = lua_gettop(L);
@ -460,6 +472,16 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
lua_pushinteger(L, cobj->getId()); // Push id
lua_pushnil(L);
lua_settable(L, objectstable);
// Get core.objects_by_guid
lua_getfield(L, core, "objects_by_guid");
luaL_checktype(L, -1, LUA_TTABLE);
objectstable = lua_gettop(L);
// Set objects_by_guid[guid] = nil
lua_pushstring(L, cobj->getGUID().c_str());
lua_pushnil(L);
lua_settable(L, objectstable);
}
void ScriptApiBase::objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj)