Skip to content

Commit

Permalink
Fix possible crash due to race in DiscordRichPresence
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Apr 8, 2024
1 parent b226a05 commit 7d8fe51
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions osu.Desktop/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ internal partial class DiscordRichPresence : Component
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;

private IBindable<APIUser> user = null!;

[Resolved]
private IAPIProvider api { get; set; } = null!;

Expand All @@ -50,9 +48,11 @@ internal partial class DiscordRichPresence : Component
[Resolved]
private MultiplayerClient multiplayerClient { get; set; } = null!;

[Resolved]
private OsuConfigManager config { get; set; } = null!;

private readonly IBindable<UserStatus?> status = new Bindable<UserStatus?>();
private readonly IBindable<UserActivity> activity = new Bindable<UserActivity>();

private readonly Bindable<DiscordRichPresenceMode> privacyMode = new Bindable<DiscordRichPresenceMode>();

private readonly RichPresence presence = new RichPresence
Expand All @@ -65,8 +65,10 @@ internal partial class DiscordRichPresence : Component
},
};

private IBindable<APIUser>? user;

[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
private void load()
{
client = new DiscordRpcClient(client_id)
{
Expand All @@ -87,6 +89,13 @@ private void load(OsuConfigManager config)
client.OnJoin += onJoin;
}

client.Initialize();
}

protected override void LoadComplete()
{
base.LoadComplete();

config.BindWith(OsuSetting.DiscordRichPresence, privacyMode);

user = api.LocalUser.GetBoundCopy();
Expand All @@ -104,8 +113,6 @@ private void load(OsuConfigManager config)
activity.BindValueChanged(_ => schedulePresenceUpdate());
privacyMode.BindValueChanged(_ => schedulePresenceUpdate());
multiplayerClient.RoomUpdated += onRoomUpdated;

client.Initialize();
}

private void onReady(object _, ReadyMessage __)
Expand Down Expand Up @@ -146,6 +153,9 @@ private void schedulePresenceUpdate()

private void updatePresence(bool hideIdentifiableInformation)
{
if (user == null)
return;

// user activity
if (activity.Value != null)
{
Expand Down

0 comments on commit 7d8fe51

Please sign in to comment.