Skip to content

Commit 1921c5f

Browse files
committed
Reworked Discord RPC integration and added more startup debug
Reworked the Discord RPC to not rely on a pointer to a class, and instead made everything static. Cleaner method instead of a pointer to the class. Added more logging messages and asserts for debug.
1 parent 65edf1d commit 1921c5f

3 files changed

Lines changed: 106 additions & 67 deletions

File tree

discordrpc.cpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ ConVar p2mm_discord_webhooks_defaultfooter("p2mm_discord_webhooks_defaultfooter"
105105
ConVar p2mm_discord_webhooks_customfooter("p2mm_discord_webhooks_customfooter", "", FCVAR_NONE, "Set a custom embed footer for webhook messages.");
106106

107107
static CURL* curl = nullptr;
108+
static bool bRPCRunning = false; // Flag bool for whether the RPC is running.
109+
static DiscordRichPresence RPC;
108110

109111
// Parameters that are sent through to the Discord webhook.
110112
struct WebHookParams
@@ -232,7 +234,6 @@ void CDiscordIntegration::SendWebHookEmbed(const std::string& title, const std::
232234
CreateSimpleThread(SendWebHook, webhookParams);
233235
}
234236

235-
236237
///-----------------------------------------------------------------------------
237238
/// Discord Rich Presence
238239
/// OLD API Source: https://github.com/discord/discord-rpc
@@ -243,50 +244,33 @@ static void RPCState(IConVar* var, const char* pOldValue, float flOldValue)
243244
{
244245
if (!g_P2MMServerPlugin.m_bPluginLoaded) return;
245246
const auto cvRPC = dynamic_cast<ConVar*>(var);
246-
if (cvRPC->GetBool() && !g_pDiscordIntegration->m_bRPCRunning)
247-
g_pDiscordIntegration->StartDiscordRPC();
248-
if (!cvRPC->GetBool() && g_pDiscordIntegration->m_bRPCRunning)
249-
g_pDiscordIntegration->ShutdownDiscordRPC();
247+
if (cvRPC->GetBool() && bRPCRunning)
248+
CDiscordIntegration::StartDiscordRPC();
249+
if (!cvRPC->GetBool() && bRPCRunning)
250+
CDiscordIntegration::ShutdownDiscordRPC();
250251
}
251252
ConVar p2mm_discord_rpc("p2mm_discord_rpc", "1", FCVAR_NONE, "Enable or disable Discord RPC with P2:MM.", true, 0, true, 1, RPCState);
252253

253-
static DiscordRichPresence RPC;
254-
CDiscordIntegration::CDiscordIntegration()
254+
bool CDiscordIntegration::StartDiscordWebHooks()
255255
{
256-
// Initialize RPC parameters to defaults.
257-
RPC.state = "";
258-
RPC.details = "Starting up...";
259-
RPC.startTimestamp = time(nullptr);
260-
RPC.endTimestamp = 0;
261-
RPC.largeImageKey = "p2mmlogo";
262-
RPC.largeImageText = "Portal 2";
263-
RPC.smallImageKey = "wave";
264-
RPC.smallImageText = "Welcome to P2:MM!";
265-
RPC.partyId = "";
266-
RPC.partySize = 0;
267-
RPC.partyMax = 0;
268-
RPC.matchSecret = "";
269-
RPC.joinSecret = "";
270-
RPC.spectateSecret = "";
271-
RPC.instance = 0;
272-
273-
this->m_bRPCRunning = false; // Flag bool for whether the RPC is running.
274-
275256
// Initialize curl requests for the webhook embeds.
276257
curl = curl_easy_init();
277258
curl_global_init(CURL_GLOBAL_DEFAULT);
278259
if (!curl)
279260
{
280261
DiscordLog(WARNING, false, "Failed to initialize curl request!");
281-
return;
262+
curl = nullptr;
263+
return false;
282264
}
283265

284266
// Set options for curl requests.
285267
curl_easy_setopt(curl, CURLOPT_URL, p2mm_discord_webhooks_url.GetString());
286268
curl_easy_setopt(curl, CURLOPT_POST, 1L);
269+
270+
return true;
287271
}
288272

289-
CDiscordIntegration::~CDiscordIntegration()
273+
void CDiscordIntegration::ShutdownDiscordWebHooks()
290274
{
291275
// Clean up and shut down using curl requests.
292276
curl_easy_cleanup(curl);
@@ -333,6 +317,23 @@ bool CDiscordIntegration::StartDiscordRPC()
333317
{
334318
DiscordLog(INFO, false, "Starting up Discord RPC...");
335319

320+
// Initialize RPC parameters to defaults.
321+
RPC.state = "";
322+
RPC.details = "Starting up...";
323+
RPC.startTimestamp = time(nullptr);
324+
RPC.endTimestamp = 0;
325+
RPC.largeImageKey = "p2mmlogo";
326+
RPC.largeImageText = "Portal 2";
327+
RPC.smallImageKey = "wave";
328+
RPC.smallImageText = "Welcome to P2:MM!";
329+
RPC.partyId = "";
330+
RPC.partySize = 0;
331+
RPC.partyMax = 0;
332+
RPC.matchSecret = "";
333+
RPC.joinSecret = "";
334+
RPC.spectateSecret = "";
335+
RPC.instance = 0;
336+
336337
DiscordLog(INFO, true, "Setting Discord event handlers...");
337338
const auto handlers = new DiscordEventHandlers;
338339
handlers->ready = HandleDiscordReady;
@@ -377,7 +378,7 @@ bool CDiscordIntegration::StartDiscordRPC()
377378
UpdateDiscordRPC();
378379

379380
DiscordLog(INFO, false, "Discord RPC activated!");
380-
this->m_bRPCRunning = true;
381+
bRPCRunning = true;
381382
return true;
382383
}
383384

@@ -389,7 +390,7 @@ void CDiscordIntegration::ShutdownDiscordRPC()
389390
DiscordLog(INFO, false, "Shutting down Discord RPC...");
390391
Discord_ClearPresence();
391392
Discord_Shutdown();
392-
this->m_bRPCRunning = false;
393+
bRPCRunning = false;
393394
DiscordLog(INFO, false, "Shutdown Discord RPC!");
394395
}
395396

@@ -602,4 +603,9 @@ void CDiscordIntegration::UpdateDiscordRPC()
602603
// Dump log data for developer logs and then update the RPC for Discord.
603604
DumpDiscordRPCValues(&RPC);
604605
Discord_UpdatePresence(&RPC);
605-
}
606+
}
607+
608+
bool CDiscordIntegration::DiscordRPCRunning()
609+
{
610+
return bRPCRunning;
611+
}

discordrpc.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515

1616
class CDiscordIntegration {
1717
public:
18-
CDiscordIntegration();
19-
~CDiscordIntegration();
20-
18+
static bool StartDiscordWebHooks();
19+
static void ShutdownDiscordWebHooks();
2120
static void SendWebHookEmbed(const std::string& title = "Unknown", const std::string& description = "*Insert Yapping Here*", int color = EMBED_COLOR_PLAYER, bool hasFooter = true);
22-
bool StartDiscordRPC();
23-
void ShutdownDiscordRPC();
24-
static void UpdateDiscordRPC();
2521

26-
bool m_bRPCRunning;
27-
};
28-
extern CDiscordIntegration* g_pDiscordIntegration;
22+
static bool StartDiscordRPC();
23+
static void ShutdownDiscordRPC();
24+
static void UpdateDiscordRPC();
25+
static bool DiscordRPCRunning();
26+
};

0 commit comments

Comments
 (0)