Allow specifying name for dynamic media files

This commit is contained in:
sfan5 2024-01-22 21:24:42 +01:00
parent af69d4f7a9
commit c90ebad46b
5 changed files with 15 additions and 9 deletions

View File

@ -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,

View File

@ -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", {

View File

@ -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);

View File

@ -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

View File

@ -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(); }