Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ both leave the runtime unloaded. A dedicated server passes `initializeClient = 0
only uses the game server functions.

* Client: game connection tickets (`InitiateGameConnection_DEPRECATED`), SteamID,
persona name, app subscription, current game language, low violence and voice
persona name, app subscription, current game language, low violence, launch command line and voice
(User023, Friends018, Apps009). Missing interfaces make the corresponding calls return
`SB_UNAVAILABLE`.
* Game server (`SteamBridge_ServerInit`): GameServer015 logon, server rules and
Expand All @@ -91,9 +91,10 @@ only uses the game server functions.
shutdown releases and cancels the rest. Favorites and history (MatchMaking009): count,
add and remove. Friends018: a user's persona name, game, relationship and avatar image handles;
Utils011: the size and RGBA pixels of those images; Apps009: VAC ban status.
* Events: ClientGameServerDeny, GameServerChangeRequested and GameOverlayActivated
reach the client callback during `SteamBridge_HostRunCallbacks`, and FavoritesListChanged
and PersonaStateChange the separate matchmaking callback; SteamServersConnected,
* Events: ClientGameServerDeny, GameServerChangeRequested, GameOverlayActivated,
NewUrlLaunchParameters and GameRichPresenceJoinRequested reach the client callback
during `SteamBridge_HostRunCallbacks`, and FavoritesListChanged and PersonaStateChange
the separate matchmaking callback; SteamServersConnected,
SteamServerConnectFailure, GSPolicyResponse, GSClientApprove/Deny/Kick and
SteamNetworkingFakeIPResult reach the server callback during
`SteamBridge_ServerRunCallbacks`. Fields are copied out of
Expand Down Expand Up @@ -142,7 +143,8 @@ runtime for the target architecture beside the bridge, as host mode expects;

CTest covers, on every supported platform, absent runtimes and host mode against a
fixture runtime: client initialization failure and retry, connection tickets, app
subscription, game language (including truncation and a missing language), low violence, voice
subscription, game language (including truncation and a missing language), low violence,
launch command line (size probing, truncation, empty and invalid arguments), voice
capture/decoding and non-OK voice results, client and game server event translation
and dispatcher separation, deferred shutdown from callbacks, game server setters and
authentication, packets, dedicated (server-only) sessions, and FakeIP ports: datagram
Expand Down
19 changes: 14 additions & 5 deletions include/SteamAPIBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ extern "C"
// thread-safe; call them from the host's main thread.
enum
{
SB_CLIENT_GAMESERVER_DENY = 1,
SB_CLIENT_GAMESERVER_CHANGE = 2,
SB_CLIENT_OVERLAY = 3
SB_CLIENT_GAMESERVER_DENY = 1,
SB_CLIENT_GAMESERVER_CHANGE = 2,
SB_CLIENT_OVERLAY = 3,
SB_CLIENT_NEW_URL_LAUNCH_PARAMETERS = 4,
SB_CLIENT_RICH_PRESENCE_JOIN = 5
};
enum
{
Expand All @@ -128,11 +130,13 @@ extern "C"
};
enum
{
SB_SERVER_TEXT_BYTES = 128,
SB_CLIENT_SERVER_BYTES = 64
SB_SERVER_TEXT_BYTES = 128,
SB_CLIENT_SERVER_BYTES = 64,
SB_CLIENT_CONNECT_BYTES = 256
};

// serverIP/serverPort are copied from Steam's ClientGameServerDeny_t unchanged.
// friendSteamID and connect are copied from GameRichPresenceJoinRequested_t.
typedef struct SteamBridgeClientEvent
{
uint32_t kind;
Expand All @@ -143,6 +147,8 @@ extern "C"
uint32_t active;
char server[SB_CLIENT_SERVER_BYTES];
char password[SB_CLIENT_SERVER_BYTES];
uint64_t friendSteamID;
char connect[SB_CLIENT_CONNECT_BYTES];
} SteamBridgeClientEvent;
// result is the Steam EResult of a logon failure or a FakeIP request; reason is the EDenyReason of a deny or kick.
typedef struct SteamBridgeServerEvent
Expand Down Expand Up @@ -185,6 +191,9 @@ extern "C"
// NUL-terminated.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_UserGetCurrentGameLanguage(char* language, uint32_t capacity);
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_UserIsLowViolence(uint32_t* lowViolence);
// Copies the Steam URL launch command line, NUL-terminated. Pass NULL and capacity 0 to
// query only the required size (including NUL) in *bytes; *bytes receives zero when none is set.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_UserGetLaunchCommandLine(char* commandLine, uint32_t capacity, uint32_t* bytes);

// Voice. Get and Decompress succeed only for Steam's k_EVoiceResultOK; any other
// voice result returns SB_FAILED with zero lengths.
Expand Down
72 changes: 52 additions & 20 deletions src/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct HostCallback final : CCallbackBase
void Run(void* payload, bool, SteamAPICall_t) override { Run(payload); }
void Run(void* payload) override
{
if (!payload) return;
if (!payload && m_iCallback != NewUrlLaunchParameters_t::k_iCallback) return;
if (m_nCallbackFlags & k_ECallbackFlagsGameServer) RunServer(payload);
else if (m_iCallback == FavoritesListChanged_t::k_iCallback || m_iCallback == PersonaStateChange_t::k_iCallback)
RunMatchmaking(payload);
Expand Down Expand Up @@ -155,26 +155,43 @@ struct HostCallback final : CCallbackBase
void RunClient(void* payload)
{
SteamBridgeClientEvent event{};
if (m_iCallback == ClientGameServerDeny_t::k_iCallback)
{
auto p = static_cast<ClientGameServerDeny_t*>(payload);
event.kind = SB_CLIENT_GAMESERVER_DENY;
event.serverIP = p->m_unGameServerIP;
event.serverPort = p->m_usGameServerPort;
event.secure = p->m_bSecure;
event.reason = p->m_uReason;
}
else if (m_iCallback == GameServerChangeRequested_t::k_iCallback)
{
auto p = static_cast<GameServerChangeRequested_t*>(payload);
event.kind = SB_CLIENT_GAMESERVER_CHANGE;
CopyText(event.server, p->m_rgchServer);
CopyText(event.password, p->m_rgchPassword);
}
else
switch (m_iCallback)
{
event.kind = SB_CLIENT_OVERLAY;
event.active = static_cast<GameOverlayActivated_t*>(payload)->m_bActive != 0;
case ClientGameServerDeny_t::k_iCallback:
{
auto p = static_cast<ClientGameServerDeny_t*>(payload);
event.kind = SB_CLIENT_GAMESERVER_DENY;
event.serverIP = p->m_unGameServerIP;
event.serverPort = p->m_usGameServerPort;
event.secure = p->m_bSecure;
event.reason = p->m_uReason;
break;
}
case GameServerChangeRequested_t::k_iCallback:
{
auto p = static_cast<GameServerChangeRequested_t*>(payload);
event.kind = SB_CLIENT_GAMESERVER_CHANGE;
CopyText(event.server, p->m_rgchServer);
CopyText(event.password, p->m_rgchPassword);
break;
}
case GameOverlayActivated_t::k_iCallback:
event.kind = SB_CLIENT_OVERLAY;
event.active = static_cast<GameOverlayActivated_t*>(payload)->m_bActive != 0;
break;
case NewUrlLaunchParameters_t::k_iCallback:
event.kind = SB_CLIENT_NEW_URL_LAUNCH_PARAMETERS;
break;
case GameRichPresenceJoinRequested_t::k_iCallback:
{
auto p = static_cast<GameRichPresenceJoinRequested_t*>(payload);
event.kind = SB_CLIENT_RICH_PRESENCE_JOIN;
event.friendSteamID = p->m_steamIDFriend.ConvertToUint64();
CopyText(event.connect, p->m_rgchConnect);
break;
}
default:
return;
}
Dispatch(host.clientFn, host.clientUser, &event);
}
Expand Down Expand Up @@ -421,6 +438,8 @@ SteamBridgeStatus SteamBridge_HostInit(const char* runtimePath, uint32_t initial
Register<ClientGameServerDeny_t>(host.clientCallbacks, false);
Register<GameServerChangeRequested_t>(host.clientCallbacks, false);
Register<GameOverlayActivated_t>(host.clientCallbacks, false);
Register<NewUrlLaunchParameters_t>(host.clientCallbacks, false);
Register<GameRichPresenceJoinRequested_t>(host.clientCallbacks, false);
Register<FavoritesListChanged_t>(host.clientCallbacks, false);
Register<PersonaStateChange_t>(host.clientCallbacks, false);
}
Expand Down Expand Up @@ -526,6 +545,19 @@ SteamBridgeStatus SteamBridge_UserIsLowViolence(uint32_t* lowViolence)
*lowViolence = host.apps->BIsLowViolence();
return SB_OK;
}
SteamBridgeStatus SteamBridge_UserGetLaunchCommandLine(char* commandLine, uint32_t capacity, uint32_t* bytes)
{
if (!bytes) return SB_INVALID_ARGUMENT;
*bytes = 0;
if (capacity > uint32_t(INT32_MAX) || (capacity && !commandLine)) return SB_INVALID_ARGUMENT;
if (commandLine && capacity) commandLine[0] = 0;
if (!host.apps) return SB_UNAVAILABLE;
int length = host.apps->GetLaunchCommandLine(capacity ? commandLine : nullptr, int(capacity));
if (length < 0) return SB_FAILED;
if (commandLine && capacity) commandLine[capacity - 1] = 0;
*bytes = uint32_t(length);
return SB_OK;
}

SteamBridgeStatus SteamBridge_VoiceStartRecording()
{
Expand Down
74 changes: 57 additions & 17 deletions tests/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using QueueFakeFn = void(S_CALLTYPE*)(int, uint32_t, uint16_t, int);
using SetFakeIPFn = void(S_CALLTYPE*)(int, uint32_t, uint16_t);
using EmitDebugFn = int(S_CALLTYPE*)(int, const char*);
using SetAppsFn = void(S_CALLTYPE*)(int, const char*, int, int);
using SetTextFn = void(S_CALLTYPE*)(const char*);
using SetDetailsFn = void(S_CALLTYPE*)(const char*, const char*, int);
using AnswerFn = void(S_CALLTYPE*)(int, int);
namespace mock
Expand All @@ -27,6 +28,7 @@ QueueFakeFn queueFake;
SetFakeIPFn setFakeIP;
EmitDebugFn emitDebug;
SetAppsFn setApps;
SetTextFn setLaunchCommandLine;
SetIntFn setFriends, pingAnswer, playersAnswer;
SetDetailsFn setServerDetails;
AnswerFn listAnswer;
Expand Down Expand Up @@ -129,6 +131,10 @@ void Unavailable(const char* missing)
uint32_t lowViolence = 1;
assert(SB_UNAVAILABLE == SteamBridge_UserIsLowViolence(&lowViolence));
assert(0 == lowViolence);
char launch[8] = "x";
uint32_t launchBytes = 9;
assert(SB_UNAVAILABLE == SteamBridge_UserGetLaunchCommandLine(launch, sizeof(launch), &launchBytes));
assert(0 == launch[0] && 0 == launchBytes);
SteamBridge_HostRunCallbacks();
SteamBridge_HostShutdown();
}
Expand All @@ -143,7 +149,7 @@ void Client(const char* mock)
assert(SB_OK == SteamBridge_HostInit(mock, 1));
assert(SB_INVALID_ARGUMENT == SteamBridge_HostInit(mock, 1));
assert(SteamBridge_HostIsActive() && SteamBridge_HostIsClientActive());
assert(5 == mock::stat(MOCK_HOST_CALLBACKS));
assert(7 == mock::stat(MOCK_HOST_CALLBACKS));

char ticket[8]{};
uint32_t size{};
Expand Down Expand Up @@ -175,6 +181,10 @@ void Client(const char* mock)
uint32_t lowViolence = 1;
assert(SB_UNAVAILABLE == SteamBridge_UserIsLowViolence(&lowViolence));
assert(0 == lowViolence);
char launch[8] = "x";
uint32_t launchBytes = 9;
assert(SB_UNAVAILABLE == SteamBridge_UserGetLaunchCommandLine(launch, sizeof(launch), &launchBytes));
assert(0 == launch[0] && 0 == launchBytes);

assert(SB_OK == SteamBridge_VoiceStartRecording());
uint32_t available{};
Expand Down Expand Up @@ -254,12 +264,25 @@ void Client(const char* mock)
assert(!std::strcmp("10.0.0.1:27015", events.last.server) && !std::strcmp("secret", events.last.password));
assert(1 == mock::stat(MOCK_HOST_QUEUED));

// NewUrlLaunchParameters_t has no fields; Steam may deliver a NULL payload.
queue(NewUrlLaunchParameters_t::k_iCallback, false, nullptr, 0);
SteamBridge_HostRunCallbacks();
assert(3 == events.count && SB_CLIENT_NEW_URL_LAUNCH_PARAMETERS == events.last.kind);

GameRichPresenceJoinRequested_t join{};
join.m_steamIDFriend = CSteamID(uint64(76561198000000005));
std::strcpy(join.m_rgchConnect, "+connect 10.0.0.1:27015");
Queue(join, false);
SteamBridge_HostRunCallbacks();
assert(4 == events.count && SB_CLIENT_RICH_PRESENCE_JOIN == events.last.kind);
assert(76561198000000005 == events.last.friendSteamID && !std::strcmp("+connect 10.0.0.1:27015", events.last.connect));

GameOverlayActivated_t overlay{};
overlay.m_bActive = 1;
Queue(overlay, false);
events.shutdown = true;
SteamBridge_HostRunCallbacks();
assert(3 == events.count && SB_CLIENT_OVERLAY == events.last.kind && events.last.active);
assert(5 == events.count && SB_CLIENT_OVERLAY == events.last.kind && events.last.active);
assert(!SteamBridge_HostIsActive());
assert(0 == mock::stat(MOCK_HOST_CALLBACKS) && 1 == mock::stat(MOCK_HOST_SHUTDOWNS));
assert(2 == forwarded()->portsDestroyed && !SteamBridge_IsFakeIPv4(0xa9fe0102));
Expand Down Expand Up @@ -287,6 +310,22 @@ void Apps(const char* mock)
assert(SB_OK == SteamBridge_UserIsLowViolence(&lowViolence));
assert(1 == lowViolence);
assert(SB_INVALID_ARGUMENT == SteamBridge_UserIsLowViolence(nullptr));
setLaunchCommandLine("");
char launch[64] = "x";
uint32_t launchBytes = 9;
assert(SB_OK == SteamBridge_UserGetLaunchCommandLine(nullptr, 0, &launchBytes) && 0 == launchBytes);
assert(SB_OK == SteamBridge_UserGetLaunchCommandLine(launch, sizeof(launch), &launchBytes) && 0 == launchBytes && 0 == launch[0]);
setLaunchCommandLine("+connect 10.0.0.1:27015");
assert(SB_OK == SteamBridge_UserGetLaunchCommandLine(nullptr, 0, &launchBytes) && 24 == launchBytes);
assert(SB_OK == SteamBridge_UserGetLaunchCommandLine(launch, launchBytes, &launchBytes) && 24 == launchBytes);
assert(!std::strcmp("+connect 10.0.0.1:27015", launch));
char smallLaunch[9]{};
assert(SB_OK == SteamBridge_UserGetLaunchCommandLine(smallLaunch, sizeof(smallLaunch), &launchBytes) && 9 == launchBytes);
assert(!std::strcmp("+connect", smallLaunch));
assert(SB_INVALID_ARGUMENT == SteamBridge_UserGetLaunchCommandLine(launch, sizeof(launch), nullptr));
assert(SB_INVALID_ARGUMENT == SteamBridge_UserGetLaunchCommandLine(nullptr, 4, &launchBytes) && 0 == launchBytes);
assert(SB_INVALID_ARGUMENT == SteamBridge_UserGetLaunchCommandLine(launch, uint32_t(INT32_MAX) + 1, &launchBytes) && 0 == launchBytes);
setLaunchCommandLine("");
SteamBridge_HostShutdown();

// SteamApps without a language fails the query and still answers the others.
Expand Down Expand Up @@ -587,22 +626,23 @@ int main(int argc, char** argv)
// Hold a fixture reference so its counters persist across bridge sessions.
auto module = platform::LoadRuntime(argv[1]);
assert(module);
mock::failInit = platform::Resolve<SetIntFn>(module, "MockHostSetFailInit");
mock::voiceResult = platform::Resolve<SetIntFn>(module, "MockHostSetVoiceResult");
mock::stat = platform::Resolve<StatFn>(module, "MockHostStat");
mock::forwarded = platform::Resolve<ForwardedFn>(module, "MockHostForwarded");
mock::queue = platform::Resolve<QueueFn>(module, "MockHostQueue");
mock::queueFake = platform::Resolve<QueueFakeFn>(module, "MockHostQueueFakeMessage");
mock::setFakeIP = platform::Resolve<SetFakeIPFn>(module, "MockHostSetFakeIP");
mock::emitDebug = platform::Resolve<EmitDebugFn>(module, "MockHostEmitDebug");
mock::setApps = platform::Resolve<SetAppsFn>(module, "MockHostSetApps");
mock::setFriends = platform::Resolve<SetIntFn>(module, "MockHostSetFriends");
mock::setServerDetails = platform::Resolve<SetDetailsFn>(module, "MockHostSetServerDetails");
mock::listAnswer = platform::Resolve<AnswerFn>(module, "MockHostServerListAnswer");
mock::pingAnswer = platform::Resolve<SetIntFn>(module, "MockHostPingAnswer");
mock::playersAnswer = platform::Resolve<SetIntFn>(module, "MockHostPlayersAnswer");
mock::failInit = platform::Resolve<SetIntFn>(module, "MockHostSetFailInit");
mock::voiceResult = platform::Resolve<SetIntFn>(module, "MockHostSetVoiceResult");
mock::stat = platform::Resolve<StatFn>(module, "MockHostStat");
mock::forwarded = platform::Resolve<ForwardedFn>(module, "MockHostForwarded");
mock::queue = platform::Resolve<QueueFn>(module, "MockHostQueue");
mock::queueFake = platform::Resolve<QueueFakeFn>(module, "MockHostQueueFakeMessage");
mock::setFakeIP = platform::Resolve<SetFakeIPFn>(module, "MockHostSetFakeIP");
mock::emitDebug = platform::Resolve<EmitDebugFn>(module, "MockHostEmitDebug");
mock::setApps = platform::Resolve<SetAppsFn>(module, "MockHostSetApps");
mock::setLaunchCommandLine = platform::Resolve<SetTextFn>(module, "MockHostSetLaunchCommandLine");
mock::setFriends = platform::Resolve<SetIntFn>(module, "MockHostSetFriends");
mock::setServerDetails = platform::Resolve<SetDetailsFn>(module, "MockHostSetServerDetails");
mock::listAnswer = platform::Resolve<AnswerFn>(module, "MockHostServerListAnswer");
mock::pingAnswer = platform::Resolve<SetIntFn>(module, "MockHostPingAnswer");
mock::playersAnswer = platform::Resolve<SetIntFn>(module, "MockHostPlayersAnswer");
assert(mock::failInit && mock::voiceResult && mock::stat && mock::forwarded && mock::queue);
assert(mock::queueFake && mock::setFakeIP && mock::emitDebug && mock::setApps);
assert(mock::queueFake && mock::setFakeIP && mock::emitDebug && mock::setApps && mock::setLaunchCommandLine);
assert(mock::setFriends && mock::setServerDetails && mock::listAnswer && mock::pingAnswer && mock::playersAnswer);
Unavailable(argv[2]);
Client(argv[1]);
Expand Down
Loading
Loading