From c90ebad46bb93b57cda888e80013f3bf781e07ee Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 22 Jan 2024 21:24:42 +0100 Subject: [PATCH] Allow specifying name for dynamic media files --- doc/lua_api.md | 4 +++- games/devtest/mods/testnodes/textures.lua | 7 +++++-- src/script/lua_api/l_server.cpp | 6 +++--- src/server.cpp | 5 +++-- src/server.h | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 5e0393767..1688193b0 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6601,7 +6601,9 @@ Server * Returns boolean indicating success (false if player nonexistent) * `minetest.dynamic_add_media(options, callback)` * `options`: table containing the following parameters - * `filepath`: path to a media file on the filesystem + * `filename`: name the media file will be usable as + (optional, default taken from path) + * `filepath`: path to the file on the filesystem * `to_player`: name of the player the media should be sent to instead of all players (optional) * `ephemeral`: boolean that marks the media as ephemeral, diff --git a/games/devtest/mods/testnodes/textures.lua b/games/devtest/mods/testnodes/textures.lua index cf88d4a7d..82e8b2d30 100644 --- a/games/devtest/mods/testnodes/textures.lua +++ b/games/devtest/mods/testnodes/textures.lua @@ -159,14 +159,17 @@ end local textures_path = core.get_worldpath() .. "/" core.safe_file_write( - textures_path .. "testnodes_generated_mb.png", + textures_path .. "testnodes1.png", encode_and_check(512, 512, "rgb", data_mb) ) core.safe_file_write( textures_path .. "testnodes_generated_ck.png", encode_and_check(512, 512, "gray", data_ck) ) -core.dynamic_add_media(textures_path .. "testnodes_generated_mb.png") +core.dynamic_add_media({ + filename = "testnodes_generated_mb.png", + filepath = textures_path .. "testnodes1.png" +}) core.dynamic_add_media(textures_path .. "testnodes_generated_ck.png") minetest.register_node("testnodes:generated_png_mb", { diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index f0e79cd62..a2b0e859e 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -548,11 +548,11 @@ int ModApiServer::l_dynamic_add_media(lua_State *L) Server *server = getServer(L); const bool at_startup = !getEnv(L); - std::string filepath; - std::string to_player; + std::string filename, filepath, to_player; bool ephemeral = false; if (lua_istable(L, 1)) { + getstringfield(L, 1, "filename", filename); getstringfield(L, 1, "filepath", filepath); getstringfield(L, 1, "to_player", to_player); getboolfield(L, 1, "ephemeral", ephemeral); @@ -576,7 +576,7 @@ int ModApiServer::l_dynamic_add_media(lua_State *L) u32 token = server->getScriptIface()->allocateDynamicMediaCallback(L, 2); - bool ok = server->dynamicAddMedia(filepath, token, to_player, ephemeral); + bool ok = server->dynamicAddMedia(filename, filepath, token, to_player, ephemeral); if (!ok) server->getScriptIface()->freeDynamicMediaCallback(token); lua_pushboolean(L, ok); diff --git a/src/server.cpp b/src/server.cpp index ef8922635..91c42a51f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3568,10 +3568,11 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) SendDeleteParticleSpawner(peer_id, id); } -bool Server::dynamicAddMedia(std::string filepath, +bool Server::dynamicAddMedia(std::string filename, std::string filepath, const u32 token, const std::string &to_player, bool ephemeral) { - std::string filename = fs::GetFilenameFromPath(filepath.c_str()); + if (filename.empty()) + filename = fs::GetFilenameFromPath(filepath.c_str()); auto it = m_media.find(filename); if (it != m_media.end()) { // Allow the same path to be "added" again in certain conditions diff --git a/src/server.h b/src/server.h index a7fc126b6..988c5937e 100644 --- a/src/server.h +++ b/src/server.h @@ -258,7 +258,7 @@ public: void deleteParticleSpawner(const std::string &playername, u32 id); - bool dynamicAddMedia(std::string filepath, u32 token, + bool dynamicAddMedia(std::string filename, std::string filepath, u32 token, const std::string &to_player, bool ephemeral); ServerInventoryManager *getInventoryMgr() const { return m_inventory_mgr.get(); }