Skip to content

Commit

Permalink
Merge pull request ppy#28193 from bdach/what-is-discord-doing
Browse files Browse the repository at this point in the history
Fix discord arbitrarily refusing to work on "too short" strings
  • Loading branch information
smoogipoo authored May 17, 2024
2 parents f9fd1b9 + a3dfd99 commit a2e67ee
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions osu.Desktop/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ private void updatePresence(bool hideIdentifiableInformation)
// user activity
if (activity.Value != null)
{
presence.State = truncate(activity.Value.GetStatus(hideIdentifiableInformation));
presence.Details = truncate(activity.Value.GetDetails(hideIdentifiableInformation) ?? string.Empty);
presence.State = clampLength(activity.Value.GetStatus(hideIdentifiableInformation));
presence.Details = clampLength(activity.Value.GetDetails(hideIdentifiableInformation) ?? string.Empty);

if (getBeatmapID(activity.Value) is int beatmapId && beatmapId > 0)
{
Expand Down Expand Up @@ -271,8 +271,15 @@ private void onJoin(object sender, JoinMessage args) => Scheduler.AddOnce(() =>

private static readonly int ellipsis_length = Encoding.UTF8.GetByteCount(new[] { '…' });

private static string truncate(string str)
private static string clampLength(string str)
{
// For whatever reason, discord decides that strings shorter than 2 characters cannot possibly be valid input, because... reasons?
// And yes, that is two *characters*, or *codepoints*, not *bytes* as further down below (as determined by empirical testing).
// That seems very questionable, and isn't even documented anywhere. So to *make it* accept such valid input,
// just tack on enough of U+200B ZERO WIDTH SPACEs at the end.
if (str.Length < 2)
return str.PadRight(2, '\u200B');

if (Encoding.UTF8.GetByteCount(str) <= 128)
return str;

Expand Down

0 comments on commit a2e67ee

Please sign in to comment.