Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit a7914d6

Browse files
authored
Discord Rich Presence (#20)
1 parent 550284c commit a7914d6

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/HoloCure.Launcher.Base/Games/GameProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class GameProvider
1515

1616
public virtual Bindable<Game?> SelectedGame { get; } = new();
1717

18+
// THIS IS TEMPORARY; EXISTS FOR DRPC IN DESKTOP IMPL UNTIL WE REWRITE LAUNCHING
19+
public virtual Bindable<Game?> PlayingGame { get; } = new();
20+
1821
public GameProvider()
1922
{
2023
Games = new Lazy<List<Game>>(() => GetGames().ToList());

src/HoloCure.Launcher.Base/Games/HoloCure/HoloCureGame.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public override async Task InstallOrPlayGameAsync(Action<GameAlert> onAlert, Sto
5858
RedirectStandardError = true,
5959
WorkingDirectory = storage.GetFullPath(hcDir)
6060
});
61+
onAlert(GameAlert.GameStarted);
6162
proc?.WaitForExit();
6263
onAlert(GameAlert.GameExited);
6364
}

src/HoloCure.Launcher.Base/Rendering/Graphics/UserInterface/Screens/GameLauncherScreen.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public GameLauncherScreen(Game game)
3232
}
3333

3434
[BackgroundDependencyLoader]
35-
private void load(TextureStore textures, Storage storage)
35+
private void load(TextureStore textures, Storage storage, GameProvider gameProvider)
3636
{
3737
InternalChildren = new Drawable[]
3838
{
@@ -77,12 +77,14 @@ void handleAlert(GameAlert alert)
7777

7878
case GameAlert.GameStarted:
7979
playButton.UpdateText("Game started!");
80+
gameProvider.PlayingGame.Value = game; /* TEMPORARY */
8081
break;
8182

8283
case GameAlert.GameExited:
8384
playButton.UpdateText("Play Game");
8485
playButton.Enabled.Value = true;
8586
updateButton.Enabled.Value = true;
87+
gameProvider.PlayingGame.Value = null; /* TEMPORARY */
8688
break;
8789

8890
case GameAlert.CheckingForUpdates:
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using DiscordRPC;
1+
using System;
2+
using DiscordRPC;
23
using DiscordRPC.Message;
4+
using HoloCure.Launcher.Base.Games;
35
using osu.Framework.Allocation;
46
using osu.Framework.Graphics;
57
using osu.Framework.Logging;
@@ -13,28 +15,28 @@ internal class DRPComponent : Component
1315

1416
private DiscordRpcClient client = null!;
1517

16-
private readonly RichPresence presence = new RichPresence
18+
private readonly RichPresence presence = new()
1719
{
1820
Assets = new Assets { LargeImageKey = large_image_key }
1921
};
2022

2123
// see: https://github.com/ppy/osu/blob/master/osu.Desktop/DiscordRichPresence.cs#L48
2224
[BackgroundDependencyLoader]
23-
private void load()
25+
private void load(GameProvider gameProvider)
2426
{
2527
client = new DiscordRpcClient(client_id)
2628
{
2729
SkipIdenticalPresence = false // https://github.com/ppy/osu/blob/master/osu.Desktop/DiscordRichPresence.cs#L52
2830
};
2931

3032
client.OnReady += onReady;
31-
32-
// https://github.com/ppy/osu/blob/master/osu.Desktop/DiscordRichPresence.cs#L57
33-
client.OnConnectionFailed += (_, _) => client.Deinitialize();
34-
33+
client.OnConnectionFailed += (_, _) => client.Deinitialize(); // https://github.com/ppy/osu/blob/master/osu.Desktop/DiscordRichPresence.cs#L57
3534
client.OnError += (_, e) => Logger.Log($"An error occurred with Discord RPC Client: {e.Code} {e.Message}", LoggingTarget.Network);
36-
3735
client.Initialize();
36+
37+
gameProvider.SelectedGame.ValueChanged += e => { updatePresence(e.NewValue, gameProvider.PlayingGame.Value); };
38+
gameProvider.PlayingGame.ValueChanged += e => { updatePresence(gameProvider.SelectedGame.Value, e.NewValue); };
39+
updatePresence(gameProvider.SelectedGame.Value, gameProvider.PlayingGame.Value); // ensure current selected game when this component is loaded is displayed
3840
}
3941

4042
private void onReady(object _, ReadyMessage __)
@@ -43,6 +45,19 @@ private void onReady(object _, ReadyMessage __)
4345
updateStatus();
4446
}
4547

48+
private void updatePresence(Base.Games.Game? browsingGame, Base.Games.Game? playingGame)
49+
{
50+
presence.Details = playingGame is null ? "Browsing games..." : "Playing game!";
51+
presence.State = playingGame is null ? browsingGame is null ? "" : $"Looking at {browsingGame.GameTitle}" : $"Playing {playingGame.GameTitle}";
52+
53+
presence.Timestamps = new Timestamps
54+
{
55+
Start = playingGame is not null ? DateTime.UtcNow : null
56+
};
57+
58+
updateStatus();
59+
}
60+
4661
private void updateStatus()
4762
{
4863
if (!client.IsInitialized) return;
@@ -52,7 +67,7 @@ private void updateStatus()
5267

5368
protected override void Dispose(bool isDisposing)
5469
{
55-
client.Dispose();
5670
base.Dispose(isDisposing);
71+
client.Dispose();
5772
}
5873
}

0 commit comments

Comments
 (0)