1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:47:26 +08:00
osu-lazer/osu.Desktop/DiscordRichPresence.cs

205 lines
6.8 KiB
C#
Raw Normal View History

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.
2022-06-17 15:37:17 +08:00
#nullable disable
2019-12-23 18:34:12 +08:00
using System;
using System.Text;
2019-12-18 13:07:53 +08:00
using DiscordRPC;
using DiscordRPC.Message;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Extensions;
2019-12-18 13:07:53 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
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 class DiscordRichPresence : Component
{
private const string client_id = "367827983903490050";
private DiscordRpcClient client;
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
private IBindable<APIUser> user;
2019-12-18 13:07:53 +08:00
[Resolved]
2022-06-09 11:32:30 +08:00
private IAPIProvider api { get; set; }
2019-12-18 13:07:53 +08:00
private readonly IBindable<UserStatus> status = new Bindable<UserStatus>();
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
{
Assets = new Assets { LargeImageKey = "osu_logo_lazer", }
};
[BackgroundDependencyLoader]
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;
// safety measure for now, until we performance test / improve backoff for failed connections.
2022-06-24 20:25:23 +08:00
client.OnConnectionFailed += (_, _) => client.Deinitialize();
2019-12-18 21:56:00 +08:00
client.OnError += (_, e) => Logger.Log($"An error occurred with Discord RPC Client: {e.Code} {e.Message}", LoggingTarget.Network);
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()
{
if (!client.IsInitialized)
return;
2021-01-06 23:05:12 +08:00
if (status.Value is UserStatusOffline || privacyMode.Value == DiscordRichPresenceMode.Off)
2019-12-18 13:07:53 +08:00
{
client.ClearPresence();
return;
}
2021-11-20 20:41:31 +08:00
if (status.Value is UserStatusOnline && activity.Value != null)
2019-12-18 13:07:53 +08:00
{
presence.State = truncate(activity.Value.Status);
presence.Details = truncate(getDetails(activity.Value));
if (getBeatmap(activity.Value) is IBeatmapInfo beatmap && beatmap.OnlineID > 0)
{
presence.Buttons = new[]
{
new Button
{
Label = "View beatmap",
Url = $@"{api.WebsiteRootUrl}/beatmapsets/{beatmap.BeatmapSet?.OnlineID}#{ruleset.Value.ShortName}/{beatmap.OnlineID}"
}
};
}
else
{
presence.Buttons = 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)
presence.Assets.LargeImageText = string.Empty;
else
{
if (user.Value.RulesetsStatistics != null && user.Value.RulesetsStatistics.TryGetValue(ruleset.Value.ShortName, out UserStatistics statistics))
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
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);
}
2019-12-25 10:14:40 +08:00
private static readonly int ellipsis_length = Encoding.UTF8.GetByteCount(new[] { '…' });
2019-12-25 11:04:28 +08:00
private 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
}
private IBeatmapInfo getBeatmap(UserActivity activity)
{
switch (activity)
{
case UserActivity.InGame game:
return game.BeatmapInfo;
case UserActivity.Editing edit:
return edit.BeatmapInfo;
}
return null;
}
2019-12-18 13:07:53 +08:00
private string getDetails(UserActivity activity)
{
switch (activity)
{
case UserActivity.InGame game:
2021-10-02 23:55:29 +08:00
return game.BeatmapInfo.ToString();
2019-12-18 13:07:53 +08:00
case UserActivity.Editing edit:
2021-10-02 23:55:29 +08:00
return edit.BeatmapInfo.ToString();
case UserActivity.InLobby lobby:
2021-01-06 23:05:12 +08:00
return privacyMode.Value == DiscordRichPresenceMode.Limited ? string.Empty : lobby.Room.Name.Value;
2019-12-18 13:07:53 +08:00
}
return string.Empty;
}
protected override void Dispose(bool isDisposing)
{
client.Dispose();
base.Dispose(isDisposing);
}
}
}