mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-15 18:57:08 +00:00
Adjust Server::dynamicAddMedia() and related parts a bit
This commit is contained in:
parent
c7d45fe51a
commit
0b66465f33
4 changed files with 60 additions and 41 deletions
|
@ -7379,7 +7379,7 @@ Server
|
||||||
it will not be cached on the client (optional, default false)
|
it will not be cached on the client (optional, default false)
|
||||||
* Exactly one of the parameters marked [*] must be specified.
|
* Exactly one of the parameters marked [*] must be specified.
|
||||||
* `callback`: function with arguments `name`, which is a player name
|
* `callback`: function with arguments `name`, which is a player name
|
||||||
* Pushes the specified media file to client(s). (details below)
|
* Pushes the specified media file to client(s) as detailed below.
|
||||||
The file must be a supported image, sound or model format.
|
The file must be a supported image, sound or model format.
|
||||||
Dynamically added media is not persisted between server restarts.
|
Dynamically added media is not persisted between server restarts.
|
||||||
* Returns false on error, true if the request was accepted
|
* Returns false on error, true if the request was accepted
|
||||||
|
@ -7388,19 +7388,17 @@ Server
|
||||||
* Details/Notes:
|
* Details/Notes:
|
||||||
* If `ephemeral`=false and `to_player` is unset the file is added to the media
|
* If `ephemeral`=false and `to_player` is unset the file is added to the media
|
||||||
sent to clients on startup, this means the media will appear even on
|
sent to clients on startup, this means the media will appear even on
|
||||||
old clients if they rejoin the server.
|
old clients (<5.3.0) if they rejoin the server.
|
||||||
* If `ephemeral`=false the file must not be modified, deleted, moved or
|
* If `ephemeral`=false the file must not be modified, deleted, moved or
|
||||||
renamed after calling this function.
|
renamed after calling this function. This is allowed otherwise.
|
||||||
* Regardless of any use of `ephemeral`, adding media files with the same
|
* Adding media files with the same name twice is not possible.
|
||||||
name twice is not possible/guaranteed to work. An exception to this is the
|
An exception to this is the use of `to_player` to send the same,
|
||||||
use of `to_player` to send the same, already existent file to multiple
|
already existent file to multiple chosen players (`ephemeral`=false only).
|
||||||
chosen players.
|
|
||||||
* You can also call this at startup time. In that case `callback` MUST
|
* You can also call this at startup time. In that case `callback` MUST
|
||||||
be `nil` and you cannot use `ephemeral` or `to_player`, as these logically
|
be `nil` and you cannot use `ephemeral` or `to_player`, as these logically
|
||||||
do not make sense.
|
do not make sense.
|
||||||
* Clients will attempt to fetch files added this way via remote media,
|
* Clients will attempt to fetch files added this way via remote media,
|
||||||
this can make transfer of bigger files painless (if set up). Nevertheless
|
this can make transfer of bigger files painless (if set up).
|
||||||
it is advised not to use dynamic media for big media files.
|
|
||||||
|
|
||||||
IPC
|
IPC
|
||||||
---
|
---
|
||||||
|
|
|
@ -554,6 +554,7 @@ int ModApiServer::l_dynamic_add_media(lua_State *L)
|
||||||
} else {
|
} else {
|
||||||
tmp = readParam<std::string>(L, 1);
|
tmp = readParam<std::string>(L, 1);
|
||||||
args.filepath = tmp;
|
args.filepath = tmp;
|
||||||
|
log_deprecated(L, "Deprecated call to core.dynamic_add_media() with string argument", 1, true);
|
||||||
}
|
}
|
||||||
if (at_startup) {
|
if (at_startup) {
|
||||||
if (!lua_isnoneornil(L, 2))
|
if (!lua_isnoneornil(L, 2))
|
||||||
|
|
|
@ -2764,9 +2764,8 @@ void Server::sendRequestedMedia(session_t peer_id,
|
||||||
}
|
}
|
||||||
const auto &m = it->second;
|
const auto &m = it->second;
|
||||||
|
|
||||||
// no_announce <=> usually ephemeral dynamic media, which may
|
// Ephemeral dynamic media may have duplicate filenames. So we can't check it.
|
||||||
// have duplicate filenames. So we can't check it.
|
if (!m.ephemeral) {
|
||||||
if (!m.no_announce) {
|
|
||||||
if (!client->markMediaSent(name)) {
|
if (!client->markMediaSent(name)) {
|
||||||
warningstream << "Server::sendRequestedMedia(): Client has "
|
warningstream << "Server::sendRequestedMedia(): Client has "
|
||||||
"requested \"" << name << "\" before, not sending it again."
|
"requested \"" << name << "\" before, not sending it again."
|
||||||
|
@ -2834,31 +2833,41 @@ void Server::sendRequestedMedia(session_t peer_id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// unordered_map erase_if is only C++20
|
||||||
|
template <typename C, typename F>
|
||||||
|
void erase_if(C &container, F predicate) {
|
||||||
|
for (auto it = container.begin(); it != container.end(); ) {
|
||||||
|
if (predicate(*it))
|
||||||
|
it = container.erase(it);
|
||||||
|
else
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Server::stepPendingDynMediaCallbacks(float dtime)
|
void Server::stepPendingDynMediaCallbacks(float dtime)
|
||||||
{
|
{
|
||||||
EnvAutoLock lock(this);
|
EnvAutoLock lock(this);
|
||||||
|
|
||||||
for (auto it = m_pending_dyn_media.begin(); it != m_pending_dyn_media.end();) {
|
erase_if(m_pending_dyn_media, [&] (decltype(m_pending_dyn_media)::value_type &it) {
|
||||||
it->second.expiry_timer -= dtime;
|
auto &[token, state] = it;
|
||||||
bool del = it->second.waiting_players.empty() || it->second.expiry_timer < 0;
|
|
||||||
|
|
||||||
if (!del) {
|
state.expiry_timer -= dtime;
|
||||||
it++;
|
if (!state.waiting_players.empty() && state.expiry_timer >= 0)
|
||||||
continue;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
const auto &name = it->second.filename;
|
const auto &name = state.filename;
|
||||||
if (!name.empty()) {
|
if (!name.empty()) {
|
||||||
assert(m_media.count(name));
|
assert(m_media.count(name));
|
||||||
// if no_announce isn't set we're definitely deleting the wrong file!
|
sanity_check(m_media[name].ephemeral);
|
||||||
sanity_check(m_media[name].no_announce);
|
|
||||||
|
|
||||||
fs::DeleteSingleFileOrEmptyDirectory(m_media[name].path);
|
fs::DeleteSingleFileOrEmptyDirectory(m_media[name].path);
|
||||||
m_media.erase(name);
|
m_media.erase(name);
|
||||||
}
|
}
|
||||||
getScriptIface()->freeDynamicMediaCallback(it->first);
|
getScriptIface()->freeDynamicMediaCallback(token);
|
||||||
it = m_pending_dyn_media.erase(it);
|
return true;
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::SendMinimapModes(session_t peer_id,
|
void Server::SendMinimapModes(session_t peer_id,
|
||||||
|
@ -3677,11 +3686,17 @@ namespace {
|
||||||
|
|
||||||
bool Server::dynamicAddMedia(const DynamicMediaArgs &a)
|
bool Server::dynamicAddMedia(const DynamicMediaArgs &a)
|
||||||
{
|
{
|
||||||
std::string filename = a.filename;
|
if (!m_env && (!a.to_player.empty() || a.ephemeral)) {
|
||||||
std::string filepath;
|
errorstream << "Server: "
|
||||||
|
"adding ephemeral or player-specific media at startup is nonsense"
|
||||||
|
<< std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Deal with file -or- data, as provided
|
// Deal with file -or- data, as provided
|
||||||
// (Note: caller must ensure validity, so sanity_check is okay)
|
// (Note: caller must ensure validity, so sanity_check is okay)
|
||||||
|
std::string filename = a.filename;
|
||||||
|
std::string filepath;
|
||||||
if (a.filepath) {
|
if (a.filepath) {
|
||||||
sanity_check(!a.data);
|
sanity_check(!a.data);
|
||||||
filepath = *a.filepath;
|
filepath = *a.filepath;
|
||||||
|
@ -3703,29 +3718,30 @@ bool Server::dynamicAddMedia(const DynamicMediaArgs &a)
|
||||||
<< filepath << std::endl;
|
<< filepath << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do some checks
|
{
|
||||||
auto it = m_media.find(filename);
|
auto it = m_media.find(filename);
|
||||||
if (it != m_media.end()) {
|
if (it == m_media.end()) {
|
||||||
// Allow the same path to be "added" again in certain conditions
|
// standard case
|
||||||
if (a.ephemeral || it->second.path != filepath) {
|
} else if (a.ephemeral || it->second.ephemeral || it->second.path != filepath) {
|
||||||
|
// If the path is the same we can safely allow adding the same file twice.
|
||||||
|
// Note that we already trust mods to not to modify files after the fact.
|
||||||
|
// Ephemeral files are excluded too, because currently each
|
||||||
|
// PendingDynamicMediaCallback "owns" the matching m_media[] entry
|
||||||
|
// so that would mess up.
|
||||||
errorstream << "Server::dynamicAddMedia(): file \"" << filename
|
errorstream << "Server::dynamicAddMedia(): file \"" << filename
|
||||||
<< "\" already exists in media cache" << std::endl;
|
<< "\" already exists in media list" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_env && (!a.to_player.empty() || a.ephemeral)) {
|
|
||||||
errorstream << "Server::dynamicAddMedia(): "
|
|
||||||
"adding ephemeral or player-specific media at startup is nonsense"
|
|
||||||
<< std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the file and add it to our media cache
|
// Load the file and add it to our media cache
|
||||||
std::string filedata, raw_hash;
|
std::string filedata, raw_hash;
|
||||||
bool ok = addMediaFile(filename, filepath, &filedata, &raw_hash);
|
bool ok = addMediaFile(filename, filepath, &filedata, &raw_hash);
|
||||||
if (!ok)
|
if (!ok) {
|
||||||
|
if (a.data) // file was temporary
|
||||||
|
fs::DeleteSingleFileOrEmptyDirectory(filepath);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
assert(!filedata.empty());
|
assert(!filedata.empty());
|
||||||
|
|
||||||
const auto &media_it = m_media.find(filename);
|
const auto &media_it = m_media.find(filename);
|
||||||
|
@ -3748,6 +3764,7 @@ bool Server::dynamicAddMedia(const DynamicMediaArgs &a)
|
||||||
}
|
}
|
||||||
|
|
||||||
media_it->second.no_announce = true;
|
media_it->second.no_announce = true;
|
||||||
|
media_it->second.ephemeral = true;
|
||||||
// stepPendingDynMediaCallbacks will clean the file up later
|
// stepPendingDynMediaCallbacks will clean the file up later
|
||||||
} else if (a.data) {
|
} else if (a.data) {
|
||||||
// data is in a temporary file but not ephemeral, so the cleanup point
|
// data is in a temporary file but not ephemeral, so the cleanup point
|
||||||
|
|
|
@ -94,6 +94,8 @@ struct MediaInfo
|
||||||
std::string sha1_digest;
|
std::string sha1_digest;
|
||||||
// true = not announced in TOCLIENT_ANNOUNCE_MEDIA (at player join)
|
// true = not announced in TOCLIENT_ANNOUNCE_MEDIA (at player join)
|
||||||
bool no_announce;
|
bool no_announce;
|
||||||
|
// if true, this is an ephemeral entry. used by dynamic media.
|
||||||
|
bool ephemeral;
|
||||||
// does what it says. used by some cases of dynamic media.
|
// does what it says. used by some cases of dynamic media.
|
||||||
bool delete_at_shutdown;
|
bool delete_at_shutdown;
|
||||||
|
|
||||||
|
@ -102,6 +104,7 @@ struct MediaInfo
|
||||||
path(path_),
|
path(path_),
|
||||||
sha1_digest(sha1_digest_),
|
sha1_digest(sha1_digest_),
|
||||||
no_announce(false),
|
no_announce(false),
|
||||||
|
ephemeral(false),
|
||||||
delete_at_shutdown(false)
|
delete_at_shutdown(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue