mtasa-blue
mtasa-blue copied to clipboard
Support mtasa:// protocol connect args
Changes
- Allows arguments to be supplied to the mtasa:// protocol and received by the server in Lua via
onPlayerConnectevent parameters. - Adds an extra argument to
redirectPlayerwhich allows you to pass a table to achieve the same result (all keys, values are automatically converted to strings, in order to form the 'argument path'). - A fix for some of the SharedUtil tests (#3209) has been modified and merged into this PR.
Additional Info
Total length of arguments is limited to 1024 including / delimiter per key/value pair (arguments are treated as path string for 99% of lifetime)
"But... why didn't you use the traditional ?foo=123&bar=456 format?" Because Windows
This feature is useful for the Discord RPC functionality, servers can create a party system using this or give rewards for joining via Discord (of course this can extend to other platforms).
Example
Have serverside script running:
SERVER_IP, SERVER_PORT, SERVER_PASS = "localhost", 22003, ""
DEFAULT_ARGS = {["foo"] = "abc", ["bar"] = "def"}
local storedArgs
function connect(nick, ip, username, serial, version, versionString, args)
storedArgs = args
iprint(args)
end
addEventHandler("onPlayerConnect", root, connect)
function redirect(player)
redirectPlayer(player, SERVER_IP, SERVER_PORT, SERVER_PASS, (type(storedArgs) == "table") and storedArgs or DEFAULT_ARGS)
end
addCommandHandler("redirect", redirect)
| Action | Result |
|---|---|
Player connects with mtasa://123.456.78.9:22003/foo/123/bar/456/baz/789 |
|
Player uses redirect command |
Same as above |
Notes
- To test mtasa:// protocol with debug build, edit registry
Computer\HKEY_CLASSES_ROOT\mtasa\shell\open\commandand set to your mtasa build path e.g"C:\mtasa-blue\Bin\Multi Theft Auto_d.exe"%1
To-do
- [x] Handle
/in user provided keys/values, either remove character or URL encode? (/is our delimiter so would affect converting to pairs)/will be removed from user provided args
- [x] Fix argument parser issue when using
std::unordered_map<type, type>for Lua args- See #3246
marge :shipit:
Also it should work with connect console command, as I see i can't connect the game with connect command.
What about also changing redirectPlayer?
well done, lgtm
the redirectPlayer functionality has uncovered an issue with the argument parser which needs resolving before this can be merged. See discussion on dev discord
Also it should work with
connectconsole command, as I see i can't connect the game with connect command.
Works fine for me, can you confirm how you're using the command and what errors you're seeing? @fresholia
We can't use std::unordered_map<std::string, std::string> to retrieve table arguments in a Lua definition (because a script providing the wrong type ie {1, 2, 3} will cause a crash, and I couldn't find a solution for this).
For this reason I've changed to use CLuaArgument in the unordered_map, as it can handle any value from the Lua side, then convert this to a string using the already available CLuaArgument::GetAsString method
However, I did have to implement a hash function for CLuaArgument in order to use it with std::unordered_map - could do with some opinions on this; not sure if it's correct (approach and implementation-wise). This is an issue we inevitably need to tackle at some point anyway (accepting tables of any value using new arg parser)...
https://github.com/multitheftauto/mtasa-blue/blob/c96a8fffa063577b1273f033ee8728d7a71c0b9f/Client/mods/deathmatch/logic/lua/CLuaArgument.h#L68-L78
We can't use
std::unordered_map<std::string, std::string>to retrieve table arguments in a Lua definition (because a script providing the wrong type ie{1, 2, 3}will cause a crash, and I couldn't find a solution for this).For this reason I've changed to use
CLuaArgumentin the unordered_map, as it can handle any value from the Lua side, then convert this to a string using the already availableCLuaArgument::GetAsStringmethodHowever, I did have to implement a hash function for
CLuaArgumentin order to use it withstd::unordered_map- could do with some opinions on this; not sure if it's correct (approach and implementation-wise). This is an issue we inevitably need to tackle at some point anyway (accepting tables of any value using new arg parser)...https://github.com/multitheftauto/mtasa-blue/blob/c96a8fffa063577b1273f033ee8728d7a71c0b9f/Client/mods/deathmatch/logic/lua/CLuaArgument.h#L68-L78
I think it would be better to fix a crash instead of constructing hash related complicated things. I hope my PR #3246 will help.
Here's the last status update on this PR:
Dutchman101 — 01/07/2024 1:42 AM If you address the final Dec 1 code reviews on https://github.com/multitheftauto/mtasa-blue/pull/3211, it can be merged
lopsi — 01/07/2024 3:04 PM there is more to do than that I would like to use other arg format, ?foo=bar&baz=boz I know it's possible, I already started working on it, but ran into few issues MTA updater also uses this format in some weird way, I would like to change it too
Any updates on this? Would be a really nice feature.