2019-12-18 05:07:53 +00: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 10:34:12 +00:00
|
|
|
|
using System;
|
2019-12-21 14:48:15 +00:00
|
|
|
|
using System.Text;
|
2019-12-18 05:07:53 +00:00
|
|
|
|
using DiscordRPC;
|
|
|
|
|
using DiscordRPC.Message;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Logging;
|
2020-12-30 05:29:51 +00:00
|
|
|
|
using osu.Game.Configuration;
|
2022-03-03 05:15:25 +00:00
|
|
|
|
using osu.Game.Extensions;
|
2019-12-18 05:07:53 +00:00
|
|
|
|
using osu.Game.Online.API;
|
2021-11-04 09:02:44 +00:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2019-12-18 05:07:53 +00: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 14:23:54 +00:00
|
|
|
|
private DiscordRpcClient client = null!;
|
2019-12-18 05:07:53 +00:00
|
|
|
|
|
|
|
|
|
[Resolved]
|
2022-08-02 14:23:54 +00:00
|
|
|
|
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
|
2019-12-18 05:07:53 +00:00
|
|
|
|
|
2022-08-02 14:23:54 +00:00
|
|
|
|
private IBindable<APIUser> user = null!;
|
2019-12-18 05:07:53 +00:00
|
|
|
|
|
2022-05-30 20:38:47 +00:00
|
|
|
|
[Resolved]
|
2022-08-02 14:23:54 +00:00
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
2022-05-30 20:38:47 +00:00
|
|
|
|
|
2023-12-06 17:21:44 +00:00
|
|
|
|
private readonly IBindable<UserStatus?> status = new Bindable<UserStatus?>();
|
2019-12-18 05:07:53 +00:00
|
|
|
|
private readonly IBindable<UserActivity> activity = new Bindable<UserActivity>();
|
2021-01-06 15:05:12 +00:00
|
|
|
|
|
|
|
|
|
private readonly Bindable<DiscordRichPresenceMode> privacyMode = new Bindable<DiscordRichPresenceMode>();
|
2019-12-18 05:07:53 +00:00
|
|
|
|
|
|
|
|
|
private readonly RichPresence presence = new RichPresence
|
|
|
|
|
{
|
|
|
|
|
Assets = new Assets { LargeImageKey = "osu_logo_lazer", }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-05-30 21:32:55 +00:00
|
|
|
|
private void load(OsuConfigManager config)
|
2019-12-18 05:07:53 +00:00
|
|
|
|
{
|
|
|
|
|
client = new DiscordRpcClient(client_id)
|
|
|
|
|
{
|
|
|
|
|
SkipIdenticalPresence = false // handles better on discord IPC loss, see updateStatus call in onReady.
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
client.OnReady += onReady;
|
2019-12-23 10:50:35 +00:00
|
|
|
|
|
2019-12-18 13:56:00 +00:00
|
|
|
|
client.OnError += (_, e) => Logger.Log($"An error occurred with Discord RPC Client: {e.Code} {e.Message}", LoggingTarget.Network);
|
2019-12-18 05:07:53 +00:00
|
|
|
|
|
2021-01-06 15:05:12 +00:00
|
|
|
|
config.BindWith(OsuSetting.DiscordRichPresence, privacyMode);
|
|
|
|
|
|
2022-06-09 03:32:30 +00:00
|
|
|
|
user = api.LocalUser.GetBoundCopy();
|
|
|
|
|
user.BindValueChanged(u =>
|
2019-12-18 05:07:53 +00: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 15:05:12 +00:00
|
|
|
|
privacyMode.BindValueChanged(_ => updateStatus());
|
2019-12-18 05:07:53 +00: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 15:03:00 +00:00
|
|
|
|
if (!client.IsInitialized)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-12-06 17:21:44 +00:00
|
|
|
|
if (status.Value == UserStatus.Offline || privacyMode.Value == DiscordRichPresenceMode.Off)
|
2019-12-18 05:07:53 +00:00
|
|
|
|
{
|
|
|
|
|
client.ClearPresence();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 03:07:47 +00:00
|
|
|
|
if (activity.Value != null)
|
2019-12-18 05:07:53 +00:00
|
|
|
|
{
|
2023-12-06 17:16:45 +00:00
|
|
|
|
bool hideIdentifiableInformation = privacyMode.Value == DiscordRichPresenceMode.Limited;
|
2024-02-27 21:23:36 +00:00
|
|
|
|
bool hideInteractions = status.Value == UserStatus.DoNotDisturb && activity.Value is UserActivity.InLobby;
|
|
|
|
|
|
2023-12-06 17:16:45 +00:00
|
|
|
|
presence.State = truncate(activity.Value.GetStatus(hideIdentifiableInformation));
|
2024-02-27 21:23:36 +00:00
|
|
|
|
presence.Details = truncate(activity.Value.GetDetails(hideIdentifiableInformation || hideInteractions) ?? string.Empty);
|
2021-11-20 12:41:01 +00:00
|
|
|
|
|
2023-12-06 17:16:45 +00:00
|
|
|
|
if (getBeatmapID(activity.Value) is int beatmapId && beatmapId > 0)
|
2021-11-20 12:41:01 +00:00
|
|
|
|
{
|
2022-06-14 17:25:06 +00:00
|
|
|
|
presence.Buttons = new[]
|
2021-11-20 12:41:01 +00:00
|
|
|
|
{
|
2022-06-14 17:25:06 +00:00
|
|
|
|
new Button
|
|
|
|
|
{
|
|
|
|
|
Label = "View beatmap",
|
2023-12-06 17:16:45 +00:00
|
|
|
|
Url = $@"{api.WebsiteRootUrl}/beatmaps/{beatmapId}?mode={ruleset.Value.ShortName}"
|
2022-06-14 17:25:06 +00:00
|
|
|
|
}
|
2021-11-20 12:41:01 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
presence.Buttons = null;
|
|
|
|
|
}
|
2019-12-18 05:07:53 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
presence.State = "Idle";
|
|
|
|
|
presence.Details = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update user information
|
2021-01-06 15:05:12 +00:00
|
|
|
|
if (privacyMode.Value == DiscordRichPresenceMode.Limited)
|
2020-12-30 05:29:51 +00:00
|
|
|
|
presence.Assets.LargeImageText = string.Empty;
|
|
|
|
|
else
|
2022-05-30 20:38:47 +00:00
|
|
|
|
{
|
2022-08-02 14:23:54 +00:00
|
|
|
|
if (user.Value.RulesetsStatistics != null && user.Value.RulesetsStatistics.TryGetValue(ruleset.Value.ShortName, out UserStatistics? statistics))
|
2022-05-30 20:38:47 +00: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 05:07:53 +00:00
|
|
|
|
|
|
|
|
|
// update ruleset
|
2022-03-03 05:15:25 +00:00
|
|
|
|
presence.Assets.SmallImageKey = ruleset.Value.IsLegacyRuleset() ? $"mode_{ruleset.Value.OnlineID}" : "mode_custom";
|
2019-12-18 05:07:53 +00:00
|
|
|
|
presence.Assets.SmallImageText = ruleset.Value.Name;
|
|
|
|
|
|
|
|
|
|
client.SetPresence(presence);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 02:14:40 +00:00
|
|
|
|
private static readonly int ellipsis_length = Encoding.UTF8.GetByteCount(new[] { '…' });
|
|
|
|
|
|
2019-12-25 03:04:28 +00:00
|
|
|
|
private string truncate(string str)
|
2019-12-23 09:55:44 +00:00
|
|
|
|
{
|
2019-12-23 10:56:05 +00:00
|
|
|
|
if (Encoding.UTF8.GetByteCount(str) <= 128)
|
2019-12-25 03:04:28 +00:00
|
|
|
|
return str;
|
|
|
|
|
|
|
|
|
|
ReadOnlyMemory<char> strMem = str.AsMemory();
|
2019-12-23 09:55:44 +00:00
|
|
|
|
|
2019-12-23 10:34:12 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
2019-12-25 03:04:28 +00:00
|
|
|
|
strMem = strMem[..^1];
|
|
|
|
|
} while (Encoding.UTF8.GetByteCount(strMem.Span) + ellipsis_length > 128);
|
2019-12-23 10:34:12 +00:00
|
|
|
|
|
2019-12-25 03:04:28 +00:00
|
|
|
|
return string.Create(strMem.Length + 1, strMem, (span, mem) =>
|
|
|
|
|
{
|
|
|
|
|
mem.Span.CopyTo(span);
|
|
|
|
|
span[^1] = '…';
|
|
|
|
|
});
|
2019-12-23 09:55:44 +00:00
|
|
|
|
}
|
2019-12-21 14:48:15 +00:00
|
|
|
|
|
2023-12-06 17:16:45 +00:00
|
|
|
|
private int? getBeatmapID(UserActivity activity)
|
2022-06-14 17:25:06 +00:00
|
|
|
|
{
|
|
|
|
|
switch (activity)
|
|
|
|
|
{
|
|
|
|
|
case UserActivity.InGame game:
|
2023-12-06 17:16:45 +00:00
|
|
|
|
return game.BeatmapID;
|
2022-06-14 17:25:06 +00:00
|
|
|
|
|
2023-02-12 20:32:17 +00:00
|
|
|
|
case UserActivity.EditingBeatmap edit:
|
2023-12-06 17:16:45 +00:00
|
|
|
|
return edit.BeatmapID;
|
2022-06-14 17:25:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 05:07:53 +00:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
client.Dispose();
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|