New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dynamic media without loading from disk #12757
Conversation
This is my first time writing C++, so for now there's quite a bit of code duplication (two functions are copies of others with small changes) and I've been avoiding figuring out what combination of But it works! Just the code needs tidying. |
Shoot, misclick… checks thing disappeared and placed the button right below my cursor as I clicked, thanks GitHub. |
|
||
if (lua_istable(L, 1)) { | ||
getstringfield(L, 1, "filepath", filepath); | ||
getstringfield(L, 1, "to_player", to_player); | ||
getboolfield(L, 1, "ephemeral", ephemeral); | ||
getstringfield(L, 1, "filename", filename); | ||
fromdata = getstringfield(L, 1, "data", filedata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of copying here, you could use a const char *
to the lua string, and the lua string's size, and pass that around.
Then you only have to memcpy the filedata once: When you put the string into the network packet.
In such cases I wish we had string views...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::string_view
is C++17 :(
Is changing everything to a const char *
(+keeping track of length) really the only way?
Would require re-working addMediaFile
, as that expects a std::string&
which it can assign to, managing that's above my C++ knowledge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's too much work, just keep it at copying into std::string
.
Doesn't it still require to generate a unique filename? For me it looks like it doesn't solve much. If you already generated a filename, you can just save your content to that file. |
Without a filename, the client and server have no common way to identify it, no way to tell it's type (MT uses file extensions), and no way for it actually get used anywhere. This PR simply adds the ability to send ephemeral media from a file already in memory as a Lua string. Rather than having to save it to disk, have the engine read it, and then delete it from disk again. |
Any features of replacing already sent media are unrelated & waaaay outside the scope of this PR. They would go quite nicely hand-in-hand with this however. |
I would appreciate someone looking over the changes to lua_api.txt, as the explanation for dynamic media has got a bit long… which might be a sign the API changes themselves aren't so good. |
std::string to_player; | ||
bool ephemeral = false; | ||
|
||
if (lua_istable(L, 1)) { | ||
getstringfield(L, 1, "filepath", filepath); | ||
fromfile = getstringfield(L, 1, "filepath", filepath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filepath
should be checked for emptiness like in the other branch.
bool ok = true; | ||
std::string filedata_file; | ||
std::string &filedata = [&]() -> std::string & { | ||
if (filedata_to && !filedata_to->empty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if someone uses data = ""
? Won't that cause addMediaFile
to try to read from filepath
? IMO you should add a separate parameter to signal that filedata_to
is an input, rather than checking whether it is empty. data = ""
should be supported out of principle.
@@ -3557,7 +3566,7 @@ bool Server::dynamicAddMedia(std::string filepath, | |||
// Newer clients get asked to fetch the file (asynchronous) | |||
pkt << token; | |||
// Older clients have an awful hack that just throws the data at them | |||
legacy_pkt.putLongString(filedata); | |||
legacy_pkt.putLongString(*filedata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filedata
is allowed to be nullptr
in the header declaration, but here it seems it is assumed not to be nullptr
.
Do you plan to continue working on this? |
I am not knowledgeable of C++ enough to work on it. I just happened to bash something semi-working together. |
Ready for review. Implements & closes #12755.
This PR simply adds the ability to send ephemeral media from a file already in memory as a Lua string. Rather than having to save it to disk, have the engine read it, and then delete it from disk again.
updated doc for
dynamic_add_media
inlua_api.txt
As a side-effect, this allows the following, which will read
bar.png
from disk but send it to the client asfoo.png
. You could add an error for this, but why remove a free feature that harms no-one?To do
dynamic_add_media
.How to test