Closed Kirdock closed 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
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 ヒーローズ:ライジング」オリジナルサウンド�
Well, seems I was a little too fast with the release, I will look into that. But its not crashing anymore, right?
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);
}
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.
Ok so I got something I guess, please test this: https://ci.appveyor.com/api/buildjobs/f55p5uek6vj7k3s4/artifacts/DiscordBee-Release--86.zip
Seems like it works now.
Thanks, released with https://github.com/sll552/DiscordBee/releases/tag/v1.3.2
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