2020-08-20 10:17:44 +00:00
|
|
|
#include <engine/steam.h>
|
|
|
|
|
2020-09-06 15:08:38 +00:00
|
|
|
#include <engine/shared/config.h>
|
|
|
|
|
2020-08-20 10:17:44 +00:00
|
|
|
#include <steam/steam_api_flat.h>
|
|
|
|
|
|
|
|
class CSteam : public ISteam
|
|
|
|
{
|
2020-09-06 15:08:38 +00:00
|
|
|
HSteamPipe m_SteamPipe;
|
2020-09-01 22:32:22 +00:00
|
|
|
ISteamApps *m_pSteamApps;
|
|
|
|
ISteamFriends *m_pSteamFriends;
|
2020-08-20 10:19:03 +00:00
|
|
|
char m_aPlayerName[16];
|
2020-09-06 11:37:48 +00:00
|
|
|
bool m_GotConnectAddr;
|
|
|
|
NETADDR m_ConnectAddr;
|
2020-08-20 10:17:44 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
CSteam()
|
|
|
|
{
|
2020-09-06 15:08:38 +00:00
|
|
|
SteamAPI_ManualDispatch_Init();
|
|
|
|
m_SteamPipe = SteamAPI_GetHSteamPipe();
|
2020-09-01 22:32:22 +00:00
|
|
|
m_pSteamApps = SteamAPI_SteamApps_v008();
|
|
|
|
m_pSteamFriends = SteamAPI_SteamFriends_v017();
|
|
|
|
|
2020-09-06 15:08:38 +00:00
|
|
|
ReadLaunchCommandLine();
|
|
|
|
str_copy(m_aPlayerName, SteamAPI_ISteamFriends_GetPersonaName(m_pSteamFriends), sizeof(m_aPlayerName));
|
|
|
|
}
|
|
|
|
~CSteam()
|
|
|
|
{
|
|
|
|
SteamAPI_Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseConnectString(const char *pConnect)
|
|
|
|
{
|
|
|
|
if(pConnect[0])
|
2020-09-06 11:37:48 +00:00
|
|
|
{
|
2020-09-06 15:08:38 +00:00
|
|
|
NETADDR Connect;
|
2021-05-05 15:32:37 +00:00
|
|
|
if(net_host_lookup(pConnect, &Connect, NETTYPE_ALL) == 0)
|
2020-09-06 11:37:48 +00:00
|
|
|
{
|
2020-09-06 15:08:38 +00:00
|
|
|
m_ConnectAddr = Connect;
|
2020-09-06 11:37:48 +00:00
|
|
|
m_GotConnectAddr = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-06 15:08:38 +00:00
|
|
|
dbg_msg("steam", "got unparsable connect string: '%s'", pConnect);
|
2020-09-06 11:37:48 +00:00
|
|
|
}
|
2020-09-01 22:32:22 +00:00
|
|
|
}
|
2020-08-20 10:17:44 +00:00
|
|
|
}
|
2020-09-06 15:08:38 +00:00
|
|
|
|
|
|
|
void ReadLaunchCommandLine()
|
2020-08-20 10:17:44 +00:00
|
|
|
{
|
2020-09-06 15:08:38 +00:00
|
|
|
char aConnect[NETADDR_MAXSTRSIZE];
|
|
|
|
int ConnectSize = SteamAPI_ISteamApps_GetLaunchCommandLine(m_pSteamApps, aConnect, sizeof(aConnect));
|
|
|
|
if(ConnectSize >= NETADDR_MAXSTRSIZE)
|
|
|
|
{
|
|
|
|
ConnectSize = NETADDR_MAXSTRSIZE - 1;
|
|
|
|
}
|
|
|
|
aConnect[ConnectSize] = 0;
|
|
|
|
m_GotConnectAddr = false;
|
|
|
|
ParseConnectString(aConnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnGameRichPresenceJoinRequested(GameRichPresenceJoinRequested_t *pEvent)
|
|
|
|
{
|
|
|
|
ParseConnectString(pEvent->m_rgchConnect);
|
2020-08-20 10:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetPlayerName()
|
|
|
|
{
|
2020-08-20 10:19:03 +00:00
|
|
|
return m_aPlayerName;
|
2020-08-20 10:17:44 +00:00
|
|
|
}
|
2020-09-01 22:32:22 +00:00
|
|
|
|
2020-09-06 15:08:38 +00:00
|
|
|
const NETADDR *GetConnectAddress()
|
2020-09-06 11:37:48 +00:00
|
|
|
{
|
|
|
|
if(m_GotConnectAddr)
|
|
|
|
{
|
|
|
|
return &m_ConnectAddr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 15:08:38 +00:00
|
|
|
void ClearConnectAddress()
|
|
|
|
{
|
|
|
|
m_GotConnectAddr = false;
|
|
|
|
}
|
2020-09-06 11:37:48 +00:00
|
|
|
|
2020-09-06 15:08:38 +00:00
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
SteamAPI_ManualDispatch_RunFrame(m_SteamPipe);
|
|
|
|
CallbackMsg_t Callback;
|
|
|
|
while(SteamAPI_ManualDispatch_GetNextCallback(m_SteamPipe, &Callback))
|
|
|
|
{
|
|
|
|
switch(Callback.m_iCallback)
|
|
|
|
{
|
|
|
|
case NewUrlLaunchParameters_t::k_iCallback:
|
|
|
|
ReadLaunchCommandLine();
|
|
|
|
break;
|
|
|
|
case GameRichPresenceJoinRequested_t::k_iCallback:
|
|
|
|
OnGameRichPresenceJoinRequested((GameRichPresenceJoinRequested_t *)Callback.m_pubParam);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(g_Config.m_Debug)
|
|
|
|
{
|
|
|
|
dbg_msg("steam/dbg", "unhandled callback id=%d", Callback.m_iCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SteamAPI_ManualDispatch_FreeLastCallback(m_SteamPipe);
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 22:32:22 +00:00
|
|
|
void ClearGameInfo()
|
|
|
|
{
|
|
|
|
SteamAPI_ISteamFriends_ClearRichPresence(m_pSteamFriends);
|
|
|
|
}
|
|
|
|
void SetGameInfo(NETADDR ServerAddr, const char *pMapName)
|
|
|
|
{
|
2020-09-06 11:37:48 +00:00
|
|
|
char aServerAddr[NETADDR_MAXSTRSIZE];
|
|
|
|
net_addr_str(&ServerAddr, aServerAddr, sizeof(aServerAddr), true);
|
|
|
|
|
|
|
|
SteamAPI_ISteamFriends_SetRichPresence(m_pSteamFriends, "connect", aServerAddr);
|
|
|
|
SteamAPI_ISteamFriends_SetRichPresence(m_pSteamFriends, "map", pMapName);
|
2020-09-01 22:32:22 +00:00
|
|
|
SteamAPI_ISteamFriends_SetRichPresence(m_pSteamFriends, "status", pMapName);
|
|
|
|
SteamAPI_ISteamFriends_SetRichPresence(m_pSteamFriends, "steam_display", "#Status");
|
2020-09-06 11:38:03 +00:00
|
|
|
SteamAPI_ISteamFriends_SetRichPresence(m_pSteamFriends, "steam_player_group", aServerAddr);
|
2020-09-01 22:32:22 +00:00
|
|
|
}
|
2020-08-20 10:17:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CSteamStub : public ISteam
|
|
|
|
{
|
2020-09-01 22:32:22 +00:00
|
|
|
const char *GetPlayerName() { return 0; }
|
2020-09-06 15:08:38 +00:00
|
|
|
const NETADDR *GetConnectAddress() { return 0; }
|
2020-09-26 19:41:58 +00:00
|
|
|
void ClearConnectAddress() {}
|
|
|
|
void Update() {}
|
|
|
|
void ClearGameInfo() {}
|
|
|
|
void SetGameInfo(NETADDR ServerAddr, const char *pMapName) {}
|
2020-08-20 10:17:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ISteam *CreateSteam()
|
|
|
|
{
|
|
|
|
if(!SteamAPI_Init())
|
|
|
|
{
|
|
|
|
return new CSteamStub();
|
|
|
|
}
|
|
|
|
return new CSteam();
|
|
|
|
}
|