Skip to content

Commit

Permalink
refactor rich presence, gets rid GetTickCount64 #217
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintWish committed Oct 24, 2023
1 parent 47640d6 commit d702f54
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 45 deletions.
6 changes: 4 additions & 2 deletions game/client/clientlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "fmod/soundengine.h"

CSoundEngine gSoundEngine;
CRichPresence gRichPresence;
CHud gHUD;

bool CClientLibrary::Initialize()
Expand Down Expand Up @@ -82,7 +83,7 @@ void CClientLibrary::HUDInit()
//debug outputs ; remove later - always add where they are set! This way let's us get data after something goes wrong out of a string
CVAR_CREATE("DEBUG_bestxpstat", "0", FCVAR_CLIENTDLL); // Called @ playerstats.cpp line 123

RichPresenceInitialize();
gRichPresence.Init();
}

void CClientLibrary::VideoInit()
Expand All @@ -99,6 +100,7 @@ void CClientLibrary::Shutdown()
gHUD.Shutdown();
FileSystem_Shutdown();
gSoundEngine.ExitFMOD();
gRichPresence.Shutdown();
}

void CClientLibrary::ResetClient()
Expand All @@ -125,7 +127,7 @@ void CClientLibrary::RunFrame()

gSoundEngine.Update();
steamhelper->Think();
RichPresenceUpdate();
gRichPresence.Update();
}

extern CClientLibrary gClient;
4 changes: 2 additions & 2 deletions game/client/ms/clglobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "ms/vgui_hud.h"
#include "logger.h"
#include "steamhelper.h"
#include "richpresence.h"
//#include "richpresence.h"

void VGUI_Think();

Expand Down Expand Up @@ -408,7 +408,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
//if( logfile.is_open() ) logfile << __FILE__ << ":" << ((int)__LINE__) << " client.dll being unloaded" << endl;
if (logfile.is_open())
(((logfile << Logger::LOG_INFO << __FILE__) << " client.dll being unloaded\n"));
RichPresenceShutdown();
//RichPresenceShutdown();
MSGlobals::EndMap();
MSCLGlobals::DLLDetach();
MSGlobals::DLLDetach();
Expand Down
58 changes: 26 additions & 32 deletions game/client/richpresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,37 @@
#include "steamhelper.h"
#include "discord/discord_rpc.h"
#include <time.h>
#include <sysinfoapi.h>

static bool loadedRichPresence = false;
static ULONG lastTimeUpdated = 0;
static int64_t startTime;
#define DISCORD_RPC_UPDATE_TIME 35.0 // sec

static char bufferDetails[128];
static char bufferState[128];

#define DISCORD_RPC_UPDATE_TIME 35 // sec

void RichPresenceInitialize()
void CRichPresence::Init(void)
{
loadedRichPresence = true;
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
Discord_Initialize("1063225093507010570", &handlers, 1, "1961680");
startTime = time(NULL);
if (!m_bLoaded)
{
m_bLoaded = true;
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
Discord_Initialize("1063225093507010570", &handlers, 1, "1961680");
m_numStartTime = time(NULL);
}
}

void RichPresenceShutdown()
void CRichPresence::Shutdown(void)
{
// Discord_Shutdown is broken --- game will hang endlessly if called!
loadedRichPresence = false;
//Discord_Shutdown is broken --- game will hang endlessly if called!
m_bLoaded = false;
}

void RichPresenceUpdate()
void CRichPresence::Update(void)
{
if (loadedRichPresence == false)
if (!m_bLoaded)
return;

const auto timeNow = (GetTickCount64() / 1000);

if (lastTimeUpdated >= timeNow)
const float timeNow = gEngfuncs.GetClientTime();
if (m_fLastUpdate >= timeNow)
return;

lastTimeUpdated = (timeNow + DISCORD_RPC_UPDATE_TIME);
m_fLastUpdate = (timeNow + DISCORD_RPC_UPDATE_TIME);
int maxClients = gEngfuncs.GetMaxClients(), clientsInGame = 0, currentHealth = 0;

if (maxClients > 0)
Expand All @@ -66,8 +60,8 @@ void RichPresenceUpdate()
clientsInGame++;
}

_snprintf(bufferDetails, sizeof(bufferDetails), "%s - %i HP", player.m_DisplayName.c_str(), currentHealth);
_snprintf(bufferState, sizeof(bufferState), "%s", MSGlobals::MapName.c_str());
_snprintf(m_cBufferDetails, sizeof(m_cBufferDetails), "%s - %i HP", player.m_DisplayName.c_str(), currentHealth);
_snprintf(m_cBufferState, sizeof(m_cBufferState), "%s", MSGlobals::MapName.c_str());

if (steamapicontext && steamapicontext->SteamFriends())
{
Expand All @@ -78,8 +72,8 @@ void RichPresenceUpdate()
}
else
{
strncpy(bufferDetails, "In Main-Menu", sizeof(bufferDetails));
strncpy(bufferState, "Awaiting Greatness", sizeof(bufferState));
strncpy(m_cBufferDetails, "In Main-Menu", sizeof(m_cBufferDetails));
strncpy(m_cBufferState, "Awaiting Greatness", sizeof(m_cBufferState));
clientsInGame = maxClients = 1;

if (steamapicontext && steamapicontext->SteamFriends())
Expand All @@ -88,9 +82,9 @@ void RichPresenceUpdate()

DiscordRichPresence discordPresence;
memset(&discordPresence, 0, sizeof(discordPresence));
discordPresence.state = bufferState;
discordPresence.details = bufferDetails;
discordPresence.startTimestamp = startTime;
discordPresence.state = m_cBufferState;
discordPresence.details = m_cBufferDetails;
discordPresence.startTimestamp = m_numStartTime;
discordPresence.partySize = clientsInGame;
discordPresence.partyMax = maxClients;
discordPresence.largeImageKey = "msr";
Expand Down
28 changes: 19 additions & 9 deletions game/client/richpresence.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#ifndef CLIENT_RICHPRESENCE_H
#define CLIENT_RICHPRESENCE_H

#ifdef _WIN32
#pragma once
#endif

void RichPresenceInitialize();
void RichPresenceShutdown();
void RichPresenceUpdate();
class CRichPresence
{
public:
CRichPresence() = default;
~CRichPresence() = default;

void Init();
void Shutdown();
void Update();

private:
bool m_bLoaded = false;
float m_fLastUpdate = 0.0f;
int64_t m_numStartTime;

char m_cBufferDetails[128];
char m_cBufferState[128];
};

#endif // CLIENT_RICHPRESENCE_H
extern CRichPresence gRichPresence;

0 comments on commit d702f54

Please sign in to comment.