mirror of
https://github.com/osukey/osukey.git
synced 2025-08-07 00:23:59 +09:00
Truncate strings passed to the Discord RPC client to a maximum of 128 bytes
This commit is contained in:
@ -1,7 +1,10 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using DiscordRPC;
|
using DiscordRPC;
|
||||||
|
using DiscordRPC.Helper;
|
||||||
using DiscordRPC.Message;
|
using DiscordRPC.Message;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
@ -78,8 +81,8 @@ namespace osu.Desktop
|
|||||||
|
|
||||||
if (status.Value is UserStatusOnline && activity.Value != null)
|
if (status.Value is UserStatusOnline && activity.Value != null)
|
||||||
{
|
{
|
||||||
presence.State = activity.Value.Status;
|
presence.State = truncate(activity.Value.Status);
|
||||||
presence.Details = getDetails(activity.Value);
|
presence.Details = truncate(getDetails(activity.Value));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -97,6 +100,8 @@ namespace osu.Desktop
|
|||||||
client.SetPresence(presence);
|
client.SetPresence(presence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string truncate(string str) => str.WithinLength(128) ? str : new string (str.TakeWhile((c, i) => Encoding.UTF8.GetByteCount(str.Substring(0, i + 1)) <= 125).ToArray()) + '…'; //the ellipsis char is 3 bytes long in UTF8
|
||||||
|
|
||||||
private string getDetails(UserActivity activity)
|
private string getDetails(UserActivity activity)
|
||||||
{
|
{
|
||||||
switch (activity)
|
switch (activity)
|
||||||
|
Reference in New Issue
Block a user