diff --git a/docs/apps.rst b/docs/apps.rst index f3f6332..a73008f 100644 --- a/docs/apps.rst +++ b/docs/apps.rst @@ -10,6 +10,11 @@ List of Functions * :func:`apps.getLaunchCommandLineParam` +List of Callbacks +----------------- + +* :func:`apps.onNewUrlLaunchParameters` + Function Reference ------------------ @@ -43,16 +48,19 @@ Function Reference -- Unlock game content end -.. function:: apps.getLaunchCommandLineParam() +.. function:: apps.getLaunchCommandLine() :returns: (`string`) The launch command line parameters :SteamWorks: `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///` URL. + + **Example**:: - local params = Steam.apps.getLaunchCommandLineParam() + local params = Steam.apps.getLaunchCommandLine() local connect_string = tryParseConnectString(params) if connect_string then initiateJoinGame(connect_string) @@ -60,4 +68,18 @@ Function Reference .. warning:: - This is currently hardcoded in luasteam to a 1024 characters maximum. \ No newline at end of file + This is currently hardcoded in luasteam to a 1024 characters maximum. + + + +Callbacks Reference +------------------- + +.. function:: apps.onNewUrlLaunchParameters() + + :returns: nothing + :SteamWorks: `NewUrlLaunchParameters_t `_ + + Called by Steam when a `steam://run///` 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`. diff --git a/src/apps.cpp b/src/apps.cpp index 4798042..f544896 100644 --- a/src/apps.cpp +++ b/src/apps.cpp @@ -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()); @@ -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