Skip to content

Commit 0c75ee4

Browse files
committed
[APP] Improved updating discord invitation info
1 parent e88dca8 commit 0c75ee4

File tree

8 files changed

+69
-35
lines changed

8 files changed

+69
-35
lines changed

src/xenia/app/discord/discord_presence.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,12 @@ void DiscordPresence::PlayingTitle(const std::string_view game_title,
6969
current_state_ =
7070
std::regex_replace(std::string(state), std::regex("\\n"), ", ");
7171
current_details_ = std::string(game_title);
72-
join_secret_.clear();
73-
party_id_.clear();
74-
party_size_ = 0;
75-
party_max_ = 0;
7672
UpdatePresence();
7773
}
7874

7975
void DiscordPresence::UpdateSession(uint32_t title_id,
8076
const kernel::XSESSION_INFO* session_info,
81-
int party_size, int party_max,
77+
uint32_t party_size, uint32_t party_max,
8278
uint64_t host_xuid) {
8379
bool reset = false;
8480

@@ -129,6 +125,7 @@ void DiscordPresence::UpdatePresence() {
129125
if (current_details_.empty()) {
130126
return;
131127
}
128+
132129
DiscordRichPresence discordPresence = {};
133130
discordPresence.state = current_state_.c_str();
134131
discordPresence.details = current_details_.c_str();

src/xenia/app/discord/discord_presence.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class DiscordPresence {
2929
const std::string_view state);
3030
static void UpdateSession(uint32_t title_id,
3131
const kernel::XSESSION_INFO* session_info,
32-
int party_size, int party_max, uint64_t host_xuid);
32+
uint32_t party_size, uint32_t party_max,
33+
uint64_t host_xuid);
3334
static std::optional<kernel::X_INVITE_INFO> DecodeJoinSecret(
3435
const std::string join_secret);
3536
static void SetJoinRequestHandler(

src/xenia/app/xenia_main.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,6 @@ void EmulatorApp::EmulatorThread() {
748748
return;
749749
}
750750

751-
// Issue: It's not guaranteed discord_presence_user_index is host of
752-
// session.
753751
const uint32_t user_index = cvars::discord_presence_user_index;
754752
kernel::xam::UserProfile* profile =
755753
emulator_->kernel_state()->xam_state()->GetUserProfile(

src/xenia/kernel/xam/user_profile.cc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,33 +522,38 @@ bool UserProfile::IsPresenceStringUpdateAvailable() {
522522
return current_presence != updated_presence;
523523
}
524524

525-
std::optional<object_ref<XSession>> UserProfile::IsSessionUpdateAvailable() {
526-
XSESSION_INFO current_session_info = {};
527-
object_ref<XSession> updated_session;
525+
std::optional<object_ref<XSession>> UserProfile::FindValidSession() {
526+
object_ref<XSession> valid_session;
528527

529528
for (const auto& session : GetOwnedSessions()) {
530529
if (session->IsHost() && session->IsCreated() &&
531530
session->HasXboxLiveFeatureFlags() &&
532531
session->IsJoinViaPresenceEnabled() &&
533532
!session->IsJoinViaPresenceFriendsOnly()) {
534-
current_session_info = session->GetSessionInfo();
535-
updated_session = session;
533+
valid_session = session;
536534
break;
537535
}
538536
}
539537

540-
if (std::memcmp(&current_session_info, &last_session_info_,
541-
sizeof(XSESSION_INFO)) == 0) {
538+
if (!valid_session) {
542539
return std::nullopt;
543540
}
544-
if (!updated_session) {
545-
return std::nullopt;
546-
}
547-
return updated_session;
541+
542+
return valid_session;
548543
}
549544

550-
void UserProfile::SetLastSessionState(const XSESSION_INFO& session_info) {
545+
void UserProfile::SetLastSessionState(
546+
const XSESSION_INFO& session_info,
547+
const XSESSION_LOCAL_DETAILS& session_details) {
551548
last_session_info_ = session_info;
549+
last_session_details_ = session_details;
550+
}
551+
552+
void UserProfile::GetLastSessionState(
553+
XSESSION_INFO& session_info,
554+
XSESSION_LOCAL_DETAILS& session_details) const {
555+
session_info = last_session_info_;
556+
session_details = last_session_details_;
552557
}
553558

554559
bool UserProfile::BuildPresenceString(bool update,

src/xenia/kernel/xam/user_profile.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,12 @@ class UserProfile {
213213
bool IsPresenceStringUpdateAvailable();
214214
bool BuildPresenceString(bool update, std::u16string* presence_string);
215215

216-
std::optional<object_ref<XSession>> IsSessionUpdateAvailable();
217-
void SetLastSessionState(const XSESSION_INFO& session_info);
216+
std::optional<object_ref<XSession>> FindValidSession();
217+
void SetLastSessionState(const XSESSION_INFO& session_info,
218+
const XSESSION_LOCAL_DETAILS& session_details);
219+
220+
void GetLastSessionState(XSESSION_INFO& session_info,
221+
XSESSION_LOCAL_DETAILS& session_details) const;
218222

219223
void AddOwnedSession(object_ref<XSession> owned_session) {
220224
const auto& session_obj_ref =
@@ -274,6 +278,7 @@ class UserProfile {
274278
std::u16string online_presence_desc_ = u"";
275279

276280
XSESSION_INFO last_session_info_ = {};
281+
XSESSION_LOCAL_DETAILS last_session_details_ = {};
277282

278283
GpdInfo* GetGpd(const uint32_t title_id);
279284
const GpdInfo* GetGpd(const uint32_t title_id) const;

src/xenia/kernel/xam/user_tracker.cc

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,28 +1438,46 @@ void UserTracker::PeriodicMaintenance(uint64_t xuid,
14381438
kernel_state()->xam_state()->GetUserIndexAssignedToProfileFromXUID(xuid);
14391439

14401440
if (cvars::discord_presence_user_index == user_index) {
1441-
bool found = false;
1441+
const auto valid_session = user->FindValidSession();
14421442

1443-
const auto updated_session = user->IsSessionUpdateAvailable();
1444-
1445-
if (updated_session.has_value() && updated_session.value()) {
1446-
const auto& session = updated_session.value();
1443+
if (valid_session.has_value()) {
1444+
const auto& session = valid_session.value();
14471445

14481446
const XSESSION_INFO session_info = session->GetSessionInfo();
1447+
const XSESSION_LOCAL_DETAILS session_details =
1448+
session->GetSessionDetails();
14491449
const uint32_t party_size = session->GetMembersCount();
14501450
const uint32_t party_max = session->GetMaxPublicSlots();
14511451

1452-
kernel_state()->emulator()->on_session_change(
1453-
&session_info, party_size, party_max, user->GetOnlineXUID());
1454-
user->SetLastSessionState(session_info);
1452+
XSESSION_INFO last_session_info = {};
1453+
XSESSION_LOCAL_DETAILS last_session_details = {};
1454+
user->GetLastSessionState(last_session_info, last_session_details);
14551455

1456-
found = true;
1457-
}
1456+
bool session_info_changed = session_info != last_session_info;
1457+
bool session_details_changed = session_details != last_session_details;
1458+
1459+
if (session_info_changed || session_details_changed) {
1460+
kernel_state()->emulator()->on_session_change(
1461+
&session_info, party_size, party_max, user->GetOnlineXUID());
1462+
user->SetLastSessionState(session_info, session_details);
1463+
}
1464+
} else {
1465+
const XSESSION_INFO session_info = {};
1466+
const XSESSION_LOCAL_DETAILS session_details = {};
14581467

1459-
if (!found) {
1460-
kernel_state()->emulator()->on_session_change(nullptr, 0, 0, 0);
1461-
user->SetLastSessionState({});
1468+
XSESSION_INFO last_session_info = {};
1469+
XSESSION_LOCAL_DETAILS last_session_details = {};
1470+
user->GetLastSessionState(last_session_info, last_session_details);
1471+
1472+
bool session_info_changed = session_info != last_session_info;
1473+
bool session_details_changed = session_details != last_session_details;
1474+
1475+
if (session_info_changed || session_details_changed) {
1476+
kernel_state()->emulator()->on_session_change(nullptr, 0, 0, 0);
1477+
user->SetLastSessionState({}, {});
1478+
}
14621479
}
1480+
14631481
xe::discord::DiscordPresence::Update();
14641482
}
14651483

src/xenia/kernel/xnet.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ struct XSESSION_INFO {
457457
XNKID sessionID;
458458
XNADDR hostAddress;
459459
XNKEY keyExchangeKey;
460+
461+
bool operator==(const XSESSION_INFO& other) const {
462+
return std::memcmp(this, &other, sizeof(XSESSION_INFO)) == 0;
463+
}
460464
};
461465
static_assert_size(XSESSION_INFO, 0x3C);
462466

@@ -517,6 +521,10 @@ struct XSESSION_LOCAL_DETAILS {
517521
XSESSION_INFO sessionInfo;
518522
XNKID xnkidArbitration;
519523
xe::be<uint32_t> SessionMembers_ptr;
524+
525+
bool operator==(const XSESSION_LOCAL_DETAILS& other) const {
526+
return std::memcmp(this, &other, sizeof(XSESSION_LOCAL_DETAILS)) == 0;
527+
}
520528
};
521529
static_assert_size(XSESSION_LOCAL_DETAILS, 0x80);
522530

src/xenia/kernel/xsession.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ class XSession : public XObject {
356356
// Host session info for presence/join; valid when IsHost() && IsCreated().
357357
XSESSION_INFO GetSessionInfo() const { return local_details_.sessionInfo; };
358358

359+
XSESSION_LOCAL_DETAILS GetSessionDetails() const { return local_details_; };
360+
359361
// Gets XUID of the owner managing the local session
360362
uint64_t GetOwnerXUID() const { return owner_xuid_; };
361363

0 commit comments

Comments
 (0)