Rise
The Vieneo Province
Loading...
Searching...
No Matches
DiscordSocialPresence.cpp
Go to the documentation of this file.
2#include <cstdio>
3#include <utility>
4
5namespace RiseDiscord
6{
7 namespace
8 {
9 constexpr std::uint64_t RetryMilliseconds = 15000;
10 }
11
21
22 PresenceSession::PresenceSession(std::unique_ptr<SocialSdk> backend, Log log, Identity identity)
23 : state(std::make_shared<State>()), sdk(std::move(backend))
24 {
25 state->log = std::move(log);
26 state->identity = std::move(identity);
27 }
28
30
32 {
33 if (state->stopped) return;
34 state->latest = std::move(activity);
35 state->dirty = true;
36 }
37
38 void PresenceSession::FrameMove(std::uint64_t monotonicMilliseconds)
39 {
40 if (!sdk || state->stopped) return;
41 state->now = monotonicMilliseconds;
42 sdk->Pump();
43 const std::weak_ptr<State> weak = state;
44
45 // RPC is independent of the authenticated Client::Status state machine.
46 // Poll for desktop startup/restart/account changes without starting OAuth.
47 if (!state->userPending && state->now >= state->nextUserQuery)
48 {
49 state->userPending = true;
50 state->nextUserQuery = state->now + RetryMilliseconds;
51 sdk->QueryUser([weak](Result result, std::uint64_t userId) {
52 const auto current = weak.lock();
53 if (!current || current->stopped) return;
54 current->userPending = false;
55 if (!result.successful || !userId)
56 {
57 current->userId = 0;
58 return;
59 }
60 if (userId != current->userId)
61 {
62 current->userId = userId;
63 current->dirty = true;
64 if (current->identity) current->identity(userId);
65 }
66 });
67 }
68
69 if (!state->dirty || state->updatePending || state->now < state->nextUpdate) return;
70 state->dirty = false;
71 state->updatePending = true;
72 sdk->Update(state->latest, [weak](Result result) {
73 const auto current = weak.lock();
74 if (!current || current->stopped) return;
75 current->updatePending = false;
76 if (!result.successful)
77 {
78 current->dirty = true; // Retain the latest screen/telemetry on failure.
79 current->nextUpdate = current->now + RetryMilliseconds;
80 }
81 if (!current->hasResult ||
82 result.successful != current->lastResult.successful ||
83 result.errorType != current->lastResult.errorType ||
84 result.errorCode != current->lastResult.errorCode)
85 {
86 if (current->log)
87 {
88 if (result.successful)
89 current->log(current->hasResult ? "Rich Presence updates restored" :
90 "Rich Presence activity accepted", false);
91 else
92 {
93 char message[128];
94 sprintf_s(message, "Social SDK presence unavailable (type %i, code %i); will retry",
95 result.errorType, result.errorCode);
96 current->log(message, true);
97 }
98 }
99 current->lastResult = result;
100 current->hasResult = true;
101 }
102 });
103 }
104
106 {
107 if (state->stopped) return;
108 // Callbacks can outlive the SDK and this object. They only hold weak state,
109 // and must never reach the game's network/logger after shutdown begins.
110 state->stopped = true;
111 state->dirty = false;
112 state->identity = {};
113 state->log = {};
114 if (sdk)
115 {
116 sdk->Clear();
117 sdk->Pump();
118 sdk.reset();
119 }
120 }
121}
std::shared_ptr< State > state
void FrameMove(std::uint64_t monotonicMilliseconds)
std::unique_ptr< SocialSdk > sdk
PresenceSession(std::unique_ptr< SocialSdk >, Log, Identity)
std::function< void(std::uint64_t)> Identity
std::function< void(const char *, bool warning)> Log