2019-12-18 13:07:53 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2019-12-23 18:34:12 +08:00
using System ;
2019-12-21 22:48:15 +08:00
using System.Text ;
2019-12-18 13:07:53 +08:00
using DiscordRPC ;
using DiscordRPC.Message ;
2024-03-06 07:15:53 +08:00
using Newtonsoft.Json ;
2019-12-18 13:07:53 +08:00
using osu.Framework.Allocation ;
using osu.Framework.Bindables ;
using osu.Framework.Graphics ;
using osu.Framework.Logging ;
2024-03-01 08:57:32 +08:00
using osu.Game ;
2020-12-30 13:29:51 +08:00
using osu.Game.Configuration ;
2022-03-03 13:15:25 +08:00
using osu.Game.Extensions ;
2019-12-18 13:07:53 +08:00
using osu.Game.Online.API ;
2021-11-04 17:02:44 +08:00
using osu.Game.Online.API.Requests.Responses ;
2024-03-01 08:57:32 +08:00
using osu.Game.Online.Multiplayer ;
using osu.Game.Online.Rooms ;
2019-12-18 13:07:53 +08:00
using osu.Game.Rulesets ;
using osu.Game.Users ;
using LogLevel = osu . Framework . Logging . LogLevel ;
namespace osu.Desktop
{
internal partial class DiscordRichPresence : Component
{
private const string client_id = "367827983903490050" ;
2022-08-02 22:23:54 +08:00
private DiscordRpcClient client = null ! ;
2019-12-18 13:07:53 +08:00
[Resolved]
2022-08-02 22:23:54 +08:00
private IBindable < RulesetInfo > ruleset { get ; set ; } = null ! ;
2019-12-18 13:07:53 +08:00
2022-08-02 22:23:54 +08:00
private IBindable < APIUser > user = null ! ;
2019-12-18 13:07:53 +08:00
2022-05-31 04:38:47 +08:00
[Resolved]
2022-08-02 22:23:54 +08:00
private IAPIProvider api { get ; set ; } = null ! ;
2022-05-31 04:38:47 +08:00
2024-03-01 08:57:32 +08:00
[Resolved]
private OsuGame game { get ; set ; } = null ! ;
[Resolved]
private MultiplayerClient multiplayerClient { get ; set ; } = null ! ;
2023-12-07 01:21:44 +08:00
private readonly IBindable < UserStatus ? > status = new Bindable < UserStatus ? > ( ) ;
2019-12-18 13:07:53 +08:00
private readonly IBindable < UserActivity > activity = new Bindable < UserActivity > ( ) ;
2021-01-06 23:05:12 +08:00
private readonly Bindable < DiscordRichPresenceMode > privacyMode = new Bindable < DiscordRichPresenceMode > ( ) ;
2019-12-18 13:07:53 +08:00
private readonly RichPresence presence = new RichPresence
{
2024-03-01 08:57:32 +08:00
Assets = new Assets { LargeImageKey = "osu_logo_lazer" } ,
Secrets = new Secrets
{
JoinSecret = null ,
SpectateSecret = null ,
} ,
2019-12-18 13:07:53 +08:00
} ;
[BackgroundDependencyLoader]
2022-05-31 05:32:55 +08:00
private void load ( OsuConfigManager config )
2019-12-18 13:07:53 +08:00
{
client = new DiscordRpcClient ( client_id )
{
SkipIdenticalPresence = false // handles better on discord IPC loss, see updateStatus call in onReady.
} ;
client . OnReady + = onReady ;
2024-03-01 08:57:32 +08:00
client . OnError + = ( _ , e ) = > Logger . Log ( $"An error occurred with Discord RPC Client: {e.Code} {e.Message}" , LoggingTarget . Network , LogLevel . Error ) ;
2019-12-23 18:50:35 +08:00
2024-03-02 03:32:44 +08:00
// A URI scheme is required to support game invitations, as well as informing Discord of the game executable path to support launching the game when a user clicks on join/spectate.
2024-03-01 08:57:32 +08:00
client . RegisterUriScheme ( ) ;
2024-03-02 03:32:44 +08:00
client . Subscribe ( EventType . Join ) ;
2024-03-01 08:57:32 +08:00
client . OnJoin + = onJoin ;
2019-12-18 13:07:53 +08:00
2021-01-06 23:05:12 +08:00
config . BindWith ( OsuSetting . DiscordRichPresence , privacyMode ) ;
2022-06-09 11:32:30 +08:00
user = api . LocalUser . GetBoundCopy ( ) ;
user . BindValueChanged ( u = >
2019-12-18 13:07:53 +08:00
{
status . UnbindBindings ( ) ;
status . BindTo ( u . NewValue . Status ) ;
activity . UnbindBindings ( ) ;
activity . BindTo ( u . NewValue . Activity ) ;
} , true ) ;
ruleset . BindValueChanged ( _ = > updateStatus ( ) ) ;
status . BindValueChanged ( _ = > updateStatus ( ) ) ;
activity . BindValueChanged ( _ = > updateStatus ( ) ) ;
2021-01-06 23:05:12 +08:00
privacyMode . BindValueChanged ( _ = > updateStatus ( ) ) ;
2019-12-18 13:07:53 +08:00
client . Initialize ( ) ;
}
private void onReady ( object _ , ReadyMessage __ )
{
Logger . Log ( "Discord RPC Client ready." , LoggingTarget . Network , LogLevel . Debug ) ;
updateStatus ( ) ;
}
private void updateStatus ( )
{
2020-01-11 23:03:00 +08:00
if ( ! client . IsInitialized )
return ;
2023-12-07 01:21:44 +08:00
if ( status . Value = = UserStatus . Offline | | privacyMode . Value = = DiscordRichPresenceMode . Off )
2019-12-18 13:07:53 +08:00
{
client . ClearPresence ( ) ;
return ;
}
2024-02-24 11:07:47 +08:00
if ( activity . Value ! = null )
2019-12-18 13:07:53 +08:00
{
2024-02-28 12:58:02 +08:00
bool hideIdentifiableInformation = privacyMode . Value = = DiscordRichPresenceMode . Limited | | status . Value = = UserStatus . DoNotDisturb ;
2024-02-28 05:23:36 +08:00
2023-12-07 01:16:45 +08:00
presence . State = truncate ( activity . Value . GetStatus ( hideIdentifiableInformation ) ) ;
2024-02-28 12:58:02 +08:00
presence . Details = truncate ( activity . Value . GetDetails ( hideIdentifiableInformation ) ? ? string . Empty ) ;
2021-11-20 20:41:01 +08:00
2023-12-07 01:16:45 +08:00
if ( getBeatmapID ( activity . Value ) is int beatmapId & & beatmapId > 0 )
2021-11-20 20:41:01 +08:00
{
2022-06-15 01:25:06 +08:00
presence . Buttons = new [ ]
2021-11-20 20:41:01 +08:00
{
2022-06-15 01:25:06 +08:00
new Button
{
Label = "View beatmap" ,
2023-12-07 01:16:45 +08:00
Url = $@"{api.WebsiteRootUrl}/beatmaps/{beatmapId}?mode={ruleset.Value.ShortName}"
2022-06-15 01:25:06 +08:00
}
2021-11-20 20:41:01 +08:00
} ;
}
else
{
presence . Buttons = null ;
}
2024-03-01 08:57:32 +08:00
if ( ! hideIdentifiableInformation & & multiplayerClient . Room ! = null )
{
MultiplayerRoom room = multiplayerClient . Room ;
presence . Party = new Party
{
Privacy = string . IsNullOrEmpty ( room . Settings . Password ) ? Party . PrivacySetting . Public : Party . PrivacySetting . Private ,
ID = room . RoomID . ToString ( ) ,
// technically lobbies can have infinite users, but Discord needs this to be set to something.
// 1024 just happens to look nice.
// https://discord.com/channels/188630481301012481/188630652340404224/1212967974793642034
Max = 1024 ,
Size = room . Users . Count ,
} ;
2024-03-06 07:15:53 +08:00
RoomSecret roomSecret = new RoomSecret
{
RoomID = room . RoomID ,
Password = room . Settings . Password ,
} ;
presence . Secrets . JoinSecret = JsonConvert . SerializeObject ( roomSecret ) ;
2024-03-01 08:57:32 +08:00
}
else
{
presence . Party = null ;
presence . Secrets . JoinSecret = null ;
}
2019-12-18 13:07:53 +08:00
}
else
{
presence . State = "Idle" ;
presence . Details = string . Empty ;
}
// update user information
2021-01-06 23:05:12 +08:00
if ( privacyMode . Value = = DiscordRichPresenceMode . Limited )
2020-12-30 13:29:51 +08:00
presence . Assets . LargeImageText = string . Empty ;
else
2022-05-31 04:38:47 +08:00
{
2022-08-02 22:23:54 +08:00
if ( user . Value . RulesetsStatistics ! = null & & user . Value . RulesetsStatistics . TryGetValue ( ruleset . Value . ShortName , out UserStatistics ? statistics ) )
2022-05-31 04:38:47 +08:00
presence . Assets . LargeImageText = $"{user.Value.Username}" + ( statistics . GlobalRank > 0 ? $" (rank #{statistics.GlobalRank:N0})" : string . Empty ) ;
else
presence . Assets . LargeImageText = $"{user.Value.Username}" + ( user . Value . Statistics ? . GlobalRank > 0 ? $" (rank #{user.Value.Statistics.GlobalRank:N0})" : string . Empty ) ;
}
2019-12-18 13:07:53 +08:00
// update ruleset
2022-03-03 13:15:25 +08:00
presence . Assets . SmallImageKey = ruleset . Value . IsLegacyRuleset ( ) ? $"mode_{ruleset.Value.OnlineID}" : "mode_custom" ;
2019-12-18 13:07:53 +08:00
presence . Assets . SmallImageText = ruleset . Value . Name ;
client . SetPresence ( presence ) ;
}
2024-03-01 08:57:32 +08:00
private void onJoin ( object sender , JoinMessage args )
{
2024-03-02 03:32:44 +08:00
game . Window ? . Raise ( ) ;
2024-03-06 07:15:53 +08:00
Logger . Log ( $"Received room secret from Discord RPC Client: {args.Secret}" , LoggingTarget . Network , LogLevel . Debug ) ;
2024-03-01 08:57:32 +08:00
if ( ! tryParseRoomSecret ( args . Secret , out long roomId , out string? password ) )
2024-03-06 07:15:53 +08:00
{
2024-03-06 07:22:39 +08:00
Logger . Log ( "Could not join multiplayer room." , LoggingTarget . Network , LogLevel . Important ) ;
2024-03-06 07:15:53 +08:00
return ;
}
2024-03-01 08:57:32 +08:00
var request = new GetRoomRequest ( roomId ) ;
request . Success + = room = > Schedule ( ( ) = >
{
game . PresentMultiplayerMatch ( room , password ) ;
} ) ;
2024-03-05 11:25:36 +08:00
request . Failure + = _ = > Logger . Log ( $"Could not find room {roomId} from Discord RPC Client" , LoggingTarget . Network , LogLevel . Important ) ;
2024-03-01 08:57:32 +08:00
api . Queue ( request ) ;
}
2019-12-25 10:14:40 +08:00
private static readonly int ellipsis_length = Encoding . UTF8 . GetByteCount ( new [ ] { '…' } ) ;
2024-03-01 13:02:20 +08:00
private static string truncate ( string str )
2019-12-23 17:55:44 +08:00
{
2019-12-23 18:56:05 +08:00
if ( Encoding . UTF8 . GetByteCount ( str ) < = 128 )
2019-12-25 11:04:28 +08:00
return str ;
ReadOnlyMemory < char > strMem = str . AsMemory ( ) ;
2019-12-23 17:55:44 +08:00
2019-12-23 18:34:12 +08:00
do
{
2019-12-25 11:04:28 +08:00
strMem = strMem [ . . ^ 1 ] ;
} while ( Encoding . UTF8 . GetByteCount ( strMem . Span ) + ellipsis_length > 128 ) ;
2019-12-23 18:34:12 +08:00
2019-12-25 11:04:28 +08:00
return string . Create ( strMem . Length + 1 , strMem , ( span , mem ) = >
{
mem . Span . CopyTo ( span ) ;
span [ ^ 1 ] = '…' ;
} ) ;
2019-12-23 17:55:44 +08:00
}
2019-12-21 22:48:15 +08:00
2024-03-06 07:15:53 +08:00
private static bool tryParseRoomSecret ( string secretJson , out long roomId , out string? password )
2024-03-01 08:57:32 +08:00
{
roomId = 0 ;
password = null ;
2024-03-06 07:15:53 +08:00
RoomSecret ? roomSecret ;
2024-03-01 08:57:32 +08:00
2024-03-06 07:15:53 +08:00
try
{
roomSecret = JsonConvert . DeserializeObject < RoomSecret > ( secretJson ) ;
}
catch
{
2024-03-01 08:57:32 +08:00
return false ;
2024-03-06 07:15:53 +08:00
}
2024-03-01 08:57:32 +08:00
2024-03-06 07:15:53 +08:00
if ( roomSecret = = null ) return false ;
2024-03-01 08:57:32 +08:00
2024-03-06 07:15:53 +08:00
roomId = roomSecret . RoomID ;
password = roomSecret . Password ;
2024-03-01 08:57:32 +08:00
return true ;
}
2024-03-01 13:02:20 +08:00
private static int? getBeatmapID ( UserActivity activity )
2022-06-15 01:25:06 +08:00
{
switch ( activity )
{
case UserActivity . InGame game :
2023-12-07 01:16:45 +08:00
return game . BeatmapID ;
2022-06-15 01:25:06 +08:00
2023-02-13 04:32:17 +08:00
case UserActivity . EditingBeatmap edit :
2023-12-07 01:16:45 +08:00
return edit . BeatmapID ;
2022-06-15 01:25:06 +08:00
}
return null ;
}
2019-12-18 13:07:53 +08:00
protected override void Dispose ( bool isDisposing )
{
client . Dispose ( ) ;
base . Dispose ( isDisposing ) ;
}
2024-03-06 07:15:53 +08:00
private class RoomSecret
{
[JsonProperty(@"roomId", Required = Required.Always)]
public long RoomID { get ; set ; }
[JsonProperty(@"password", Required = Required.AllowNull)]
public string? Password { get ; set ; }
}
2019-12-18 13:07:53 +08:00
}
}