sll552 / DiscordBee

MusicBee plugin that updates your Discord status with the currently playing track
Apache License 2.0
421 stars 31 forks source link

Rich Presence stops if it hits character limit #31

Closed Kirdock closed 4 years ago

Kirdock commented 4 years ago

Describe the bug Rich presence stops working (displayed as "stopped" or it is closed) when a certain character limit is reached (about 128) for "details" or "state" field.

Expected behavior

Environment

sll552 commented 4 years ago

Yeah I guess I expected too much from the new lib to handle such cases...

I will fix this asap, thanks for reporting

Kirdock commented 4 years ago

Still bugged. This does not work, for special characters.

example text (too long): Makayla Phillips - 「僕のヒーローアカデミア THE MOVIE ヒーローズ:ライジング」オリジナルサウンドトラック

result text (130 characters; still too long): Makayla Phillips - 「僕のヒーローアカデミア THE MOVIE ヒーローズ:ライジング」オリジナルサウンド�

sll552 commented 4 years ago

Well, seems I was a little too fast with the release, I will look into that. But its not crashing anymore, right?

Kirdock commented 4 years ago

It is as the same as before when using special characters. Unhandled Exception at line 250 in DiscordBee.cs, string too long. So yeah it crashes.

My temporary solution:

private string LimitByteLength(string input, int maxLength)
{
    return new string(input
        .TakeWhile((c, i) =>
            Encoding.UTF8.GetByteCount(input.Substring(0, i + 1)) <= maxLength)
        .ToArray());
}

...
return LimitByteLength(input, 128);

EDIT (probably prettier solution):

private string LimitByteLength(string input, int maxLength)
{
    char[] array = input.ToArray();
    int byteCount = 0;
    int length = 0;
    for(int i = 0; i < array.Length; i++)
    {
        byteCount += Encoding.UTF8.GetByteCount(new char[] { array[i] });
        if(byteCount > maxLength)
        {
            break;
        }
        length++;
    }
    return input.Substring(0, length);
}
sll552 commented 4 years ago

I guess the solution is much harder because of how unicode works ... there are unicode chars which are actually two elements in the byte[] and I don't know if toArray handles that. I guess thats the problem of the solution right now.

I will try to figure out a way to fix this for all of those cases. But your 2nd suggestion looks like a good starting point, thanks for that.

sll552 commented 4 years ago

Ok so I got something I guess, please test this: https://ci.appveyor.com/api/buildjobs/f55p5uek6vj7k3s4/artifacts/DiscordBee-Release--86.zip

Kirdock commented 4 years ago

Seems like it works now.

sll552 commented 4 years ago

Thanks, released with https://github.com/sll552/DiscordBee/releases/tag/v1.3.2