Skip to content

Commit

Permalink
add apps.onNewUrlLaunchParameters callback
Browse files Browse the repository at this point in the history
  • Loading branch information
hylje authored and yancouto committed Apr 11, 2024
1 parent 8bd3c1e commit 11a4315
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
28 changes: 25 additions & 3 deletions docs/apps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ List of Functions
* :func:`apps.getLaunchCommandLineParam`


List of Callbacks
-----------------

* :func:`apps.onNewUrlLaunchParameters`

Function Reference
------------------

Expand Down Expand Up @@ -43,21 +48,38 @@ Function Reference
-- Unlock game content
end

.. function:: apps.getLaunchCommandLineParam()
.. function:: apps.getLaunchCommandLine()

:returns: (`string`) The launch command line parameters
:SteamWorks: `GetLaunchCommandLine <https://partner.steamgames.com/doc/api/ISteamApps#GetLaunchCommandLine>`_

Gets the launch command line parameters. Use it to for example parse it for a connect string when implementing game invite functionality using :func:`friends.inviteUserToGame`.

If Steam is installed, you can request launching a Steam game by navigating to a `steam://run/<appid>//<params>` URL.


**Example**::

local params = Steam.apps.getLaunchCommandLineParam()
local params = Steam.apps.getLaunchCommandLine()
local connect_string = tryParseConnectString(params)
if connect_string then
initiateJoinGame(connect_string)
end

.. warning::

This is currently hardcoded in luasteam to a 1024 characters maximum.
This is currently hardcoded in luasteam to a 1024 characters maximum.



Callbacks Reference
-------------------

.. function:: apps.onNewUrlLaunchParameters()

:returns: nothing
:SteamWorks: `NewUrlLaunchParameters_t <https://partner.steamgames.com/doc/api/ISteamApps#NewUrlLaunchParameters_t>`_

Called by Steam when a `steam://run/<appid>//<params>` URL is navigated to when the game is already running. This callback has no data, use :func:`apps.getLaunchCommandLine` to get the launch parameters from the most recently used URL.

The callback that gets called when you join another player's game inside Steam while the game is running is :func:`friends.onGameRichPresenceJoinRequested`.
40 changes: 38 additions & 2 deletions src/apps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@
// ======== SteamApps =========
// ============================

class CallbackListener;
CallbackListener *apps_listener = nullptr;
int apps_ref = LUA_NOREF;

class CallbackListener {
private:
STEAM_CALLBACK(CallbackListener, OnNewUrlLaunchParameters, NewUrlLaunchParameters_t);
};

void CallbackListener::OnNewUrlLaunchParameters(NewUrlLaunchParameters_t *data) {
if (data == nullptr) {
return;
}
lua_State *L = luasteam::global_lua_state;
if (!lua_checkstack(L, 4)) {
return;
}
lua_rawgeti(L, LUA_REGISTRYINDEX, apps_ref);
lua_getfield(L, -1, "onNewUrlLaunchParameters");
if (lua_isnil(L, -1)) {
lua_pop(L, 2);
} else {
lua_call(L, 0, 0);
lua_pop(L, 1);
}
}

// const char * GetCurrentGameLanguage();
EXTERN int luasteam_getCurrentGameLanguage(lua_State *L) {
lua_pushstring(L, SteamApps()->GetCurrentGameLanguage());
Expand Down Expand Up @@ -32,11 +59,20 @@ void add_apps(lua_State *L) {
add_func(L, "getCurrentGameLanguage", luasteam_getCurrentGameLanguage);
add_func(L, "isDlcInstalled", luasteam_isDlcInstalled);
add_func(L, "getLaunchCommandLine", luasteam_getLaunchCommandLine);
lua_pushvalue(L, -1);
apps_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_setfield(L, -2, "apps");
}

void init_apps(lua_State *L) {}
void init_apps(lua_State *L) {
apps_listener = new CallbackListener();
}

void shutdown_apps(lua_State *L) {}
void shutdown_apps(lua_State *L) {
luaL_unref(L, LUA_REGISTRYINDEX, apps_ref);
apps_ref = LUA_NOREF;
delete apps_listener;
apps_listener = nullptr;
}

} // namespace luasteam

0 comments on commit 11a4315

Please sign in to comment.