From 0b423bdb223d43516863a7bc23adc874d570b061 Mon Sep 17 00:00:00 2001 From: SFENCE Date: Mon, 9 Jun 2025 19:09:16 +0200 Subject: [PATCH] Apply review. --- doc/lua_api.md | 14 +++++++------- doc/world_format.md | 2 +- games/devtest/mods/unittests/entity.lua | 12 ++++-------- games/devtest/mods/unittests/player.lua | 2 +- src/script/cpp_api/s_base.cpp | 4 +++- src/server/luaentity_sao.cpp | 2 +- src/server/serveractiveobject.h | 4 ++-- 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index d387412b9f..c032205571 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -7849,12 +7849,12 @@ Global tables * Values in this table may be modified directly. Note: changes to initial properties will only affect entities spawned afterwards, as they are only read when spawning. -* `core.object_refs` - * Map of active object references, indexed by active object id - * Obsolete: Use `core.objects_by_guid` instead. - GUIDs are strictly more useful than active object IDs. * `core.objects_by_guid` * Map of active object references, indexed by object GUID +* `core.object_refs` + * **Obsolete:** Use `core.objects_by_guid` instead. + GUIDs are strictly more useful than active object IDs. + * Map of active object references, indexed by active object id * `core.luaentities` * Map of Lua entities, indexed by active object id * `core.registered_abms` @@ -8555,10 +8555,10 @@ child will follow movement and rotation of that bone. * `get_guid()`: returns a global unique identifier (a string) * For players, this is a player name. * For Lua entities, this is a uniquely generated string, guaranteed not to collide with player names. - * Example: `@bGh3p2AbRE29Mb4biqX6OA` + * example: `@bGh3p2AbRE29Mb4biqX6OA` * GUIDs only use printable ASCII characters. - * GUIDs are persisted internally between object reloads; their format is guaranteed not to change. - Thus you can store GUIDs to identify objects persistently. + * GUIDs persist internally between object reloads; their format is guaranteed not to change. + Thus you can use the GUID to identify an object in a particular world online nad offline. #### Lua entity only (no-op for other objects) diff --git a/doc/world_format.md b/doc/world_format.md index c7bd0f6cd1..555c0793d7 100644 --- a/doc/world_format.md +++ b/doc/world_format.md @@ -598,7 +598,7 @@ Since protocol version 37: * `s32` pitch * 1000 * `s32` roll * 1000 * if version2 >= 2: - * u8[16] guid + * `u8[16]` guid # Itemstring Format diff --git a/games/devtest/mods/unittests/entity.lua b/games/devtest/mods/unittests/entity.lua index eed6ff334d..dfbe541152 100644 --- a/games/devtest/mods/unittests/entity.lua +++ b/games/devtest/mods/unittests/entity.lua @@ -257,19 +257,15 @@ end unittests.register("test_item_drop", test_item_drop, {map=true}) local function test_entity_guid(_, pos) - log = {} - - local obj0 = core.add_entity(pos, "unittests:callbacks") - check_log({"on_activate(0)"}) - local obj1 = core.add_entity(pos, "unittests:callbacks") - check_log({"on_activate(0)"}) + local obj0 = core.add_entity(pos, "unittests:dummy") + local obj1 = core.add_entity(pos, "unittests:dummy") + assert(obj0 ~= obj1) + assert(obj0:get_guid() ~= obj1:get_guid()) assert(core.objects_by_guid[obj0:get_guid()] == obj0) assert(core.objects_by_guid[obj1:get_guid()] == obj1) obj0:remove() - check_log({"on_deactivate(true)"}) obj1:remove() - check_log({"on_deactivate(true)"}) end unittests.register("test_entity_guid", test_entity_guid, {map=true}) diff --git a/games/devtest/mods/unittests/player.lua b/games/devtest/mods/unittests/player.lua index b6dcec86f8..cc4b08b317 100644 --- a/games/devtest/mods/unittests/player.lua +++ b/games/devtest/mods/unittests/player.lua @@ -209,6 +209,6 @@ unittests.register("test_player_hotbar_clamp", run_player_hotbar_clamp_tests, {p -- Player get GUID -- local function test_player_guid_tests(player) - assert(player:get_guid()==player:get_player_name()) + assert(player:get_guid() == player:get_player_name()) end unittests.register("test_player_guid", test_player_guid_tests, {player=true}) diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 336ffc25f7..d6b041254a 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -444,7 +444,9 @@ void ScriptApiBase::addObjectReference(ServerActiveObject *cobj) objectstable = lua_gettop(L); // objects_by_guid[guid] = object - lua_pushstring(L, cobj->getGUID().c_str()); + auto guid = cobj->getGUID(); + assert(!guid.empty()); + lua_pushstring(L, guid.c_str()); lua_pushvalue(L, object); lua_settable(L, objectstable); } diff --git a/src/server/luaentity_sao.cpp b/src/server/luaentity_sao.cpp index 261783f6e5..368bf9eebe 100644 --- a/src/server/luaentity_sao.cpp +++ b/src/server/luaentity_sao.cpp @@ -309,7 +309,7 @@ void LuaEntitySAO::getStaticData(std::string *result) const writeF1000(os, m_rotation.Y); // version2. Increase this variable for new values - writeU8(os, 2); // PROTOCOL_VERSION >= 37 + writeU8(os, 2); writeF1000(os, m_rotation.X); writeF1000(os, m_rotation.Z); diff --git a/src/server/serveractiveobject.h b/src/server/serveractiveobject.h index ba78634207..3fe8eb8b5d 100644 --- a/src/server/serveractiveobject.h +++ b/src/server/serveractiveobject.h @@ -141,8 +141,8 @@ public: virtual u16 getHP() const { return 0; } - /// Always returns the same unique string for the same object. - /// Because these strings are very short, copying them is not expensive. + /// @brief Returns an unique ID for this object (persistent across unload, server restarts). + /// @note Because these strings are very short, copying them is not expensive. virtual std::string getGUID() = 0; virtual void setArmorGroups(const ItemGroupList &armor_groups)