Skip to content

Commit

Permalink
Steam: Handle "Join game" when already in-game.
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Nov 18, 2023
1 parent 1a7ff0b commit 0820e9e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmake/steam_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")

set_property(TARGET steam_integration PROPERTY CXX_STANDARD 17)

# Set the installation path to a local directory
set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/bin")

Expand Down
24 changes: 24 additions & 0 deletions cmake/steam_integration/steam_integration.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#include "steam_integration.h"

#if BUILD_STEAM
#include "steam_integration_callbacks.h"

const int steam_app_id = 2660970;
#include "steam_api.h"

steam_callback_queue_type steam_event_queue;

class new_url_launch_parameters_t {
private:
STEAM_CALLBACK( new_url_launch_parameters_t, OnReceived, NewUrlLaunchParameters_t );
};

void new_url_launch_parameters_t::OnReceived(NewUrlLaunchParameters_t* pCallback) {
steam_event_queue.push_back(steam_new_url_launch_parameters {});
}

extern "C" {
int steam_get_appid() {
return steam_app_id;
Expand Down Expand Up @@ -59,6 +71,14 @@ extern "C" {
int steam_get_launch_command_line(char* buf, int bufsize) {
return SteamApps()->GetLaunchCommandLine(buf, bufsize);
}

void* steam_run_callbacks() {
steam_event_queue.clear();

SteamAPI_RunCallbacks();

return &steam_event_queue;
}
}
#else
// non-steam version
Expand Down Expand Up @@ -101,6 +121,10 @@ extern "C" {
int steam_get_launch_command_line(char*, int) {
return 0;
}

void* steam_run_callbacks() {
return nullptr;
}
}

#endif
2 changes: 2 additions & 0 deletions cmake/steam_integration/steam_integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern "C" {
DLL_EXPORT bool steam_set_rich_presence(const char*, const char*);
DLL_EXPORT void steam_clear_rich_presence();
DLL_EXPORT int steam_get_launch_command_line(char* buf, int bufsize);

DLL_EXPORT void* steam_run_callbacks();
}

#undef DLL_EXPORT
15 changes: 15 additions & 0 deletions cmake/steam_integration/steam_integration_callbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <vector>
#include <variant>

struct steam_new_url_launch_parameters {};

using steam_callback_variant_type = std::variant<
steam_new_url_launch_parameters
>;

using steam_callback_queue_type = std::vector<
steam_callback_variant_type
>;


5 changes: 3 additions & 2 deletions cmake/steam_integration/steam_integration_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ inline std::string steam_get_launch_command_line_string() {
char buffer[512];
int num_bytes = steam_get_launch_command_line(buffer, sizeof(buffer));

if (num_bytes != 0) {
return std::string(buffer, buffer + num_bytes);
if (num_bytes > 0 && uint32_t(num_bytes) < sizeof(buffer)) {
/* Exclude the terminating zero */
return std::string(buffer, buffer + num_bytes - 1);
}

return std::string();
Expand Down
35 changes: 34 additions & 1 deletion src/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
#include "augs/string/parse_url.h"
#include "steam_integration.h"
#include "steam_integration_helpers.hpp"
#include "steam_integration_callbacks.h"

#include "work_result.h"

Expand Down Expand Up @@ -2132,6 +2133,37 @@ work_result work(const int argc, const char* const * const argv) try {
});
};

auto process_steam_callbacks = [&]() {
if (const auto steam_queue = reinterpret_cast<steam_callback_queue_type*>(steam_run_callbacks())) {
for (const auto steam_event : *steam_queue) {
std::visit(
[&]<typename E>(const E&) {
if constexpr(std::is_same_v<E, steam_new_url_launch_parameters>) {
LOG("steam_new_url_launch_parameters received.");

const auto steam_cli = steam_get_launch_command_line_string();

if (steam_cli.length() > 1) {
LOG("Detected Steam CLI (length: %x): %x", steam_cli.length(), steam_cli);

config.client_start.set_custom(steam_cli);

start_client_setup();
}
else {
LOG("Invalid Steam CLI: %x", steam_cli);
}
}
else {
static_assert(always_false_v<E>, "Non-exhaustive");
}
},
steam_event
);
}
}
};

auto do_imgui_pass = [&](const auto frame_num, auto& new_window_entropy, const auto& frame_delta, const bool in_direct_gameplay) {
bool freeze_imgui_inputs = false;

Expand Down Expand Up @@ -2895,7 +2927,7 @@ work_result work(const int argc, const char* const * const argv) try {

const auto steam_cli = steam_get_launch_command_line_string();

if (steam_cli.length() > 1) {
if (steam_cli.length() > 0) {
LOG("Detected Steam CLI (length: %x): %x", steam_cli.length(), steam_cli);
connect_to(steam_cli);
}
Expand Down Expand Up @@ -3214,6 +3246,7 @@ work_result work(const int argc, const char* const * const argv) try {
const bool was_any_imgui_popup_opened = ImGui::IsPopupOpen(0u, ImGuiPopupFlags_AnyPopupId);

do_imgui_pass(get_current_frame_num(), new_window_entropy, frame_delta, in_direct_gameplay);
process_steam_callbacks();

const auto viewing_config = get_setup_customized_config();
out.viewing_config = viewing_config;
Expand Down

0 comments on commit 0820e9e

Please sign in to comment.