mtasa-blue icon indicating copy to clipboard operation
mtasa-blue copied to clipboard

Support mtasa:// protocol connect args

Open Lpsd opened this issue 2 years ago • 10 comments

Changes

  • Allows arguments to be supplied to the mtasa:// protocol and received by the server in Lua via onPlayerConnect event parameters.
  • Adds an extra argument to redirectPlayer which 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 image
Player uses redirect command Same as above

Notes

  • To test mtasa:// protocol with debug build, edit registry Computer\HKEY_CLASSES_ROOT\mtasa\shell\open\command and 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

Lpsd avatar Oct 15 '23 06:10 Lpsd

marge :shipit:

Inder00 avatar Oct 19 '23 11:10 Inder00

Also it should work with connect console command, as I see i can't connect the game with connect command.

fresholia avatar Nov 04 '23 12:11 fresholia

What about also changing redirectPlayer?

Fernando-A-Rocha avatar Nov 04 '23 14:11 Fernando-A-Rocha

well done, lgtm

Fernando-A-Rocha avatar Nov 16 '23 21:11 Fernando-A-Rocha

the redirectPlayer functionality has uncovered an issue with the argument parser which needs resolving before this can be merged. See discussion on dev discord

Lpsd avatar Nov 16 '23 21:11 Lpsd

Also it should work with connect console 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

Lpsd avatar Nov 16 '23 21:11 Lpsd

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

Lpsd avatar Nov 18 '23 06:11 Lpsd

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

I think it would be better to fix a crash instead of constructing hash related complicated things. I hope my PR #3246 will help.

tederis avatar Nov 19 '23 04:11 tederis

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

Dutchman101 avatar Jan 28 '24 23:01 Dutchman101

Any updates on this? Would be a really nice feature.

Nico8340 avatar Apr 07 '24 18:04 Nico8340