mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-07 06:56:04 +02:00
limit byte length (fixes #72)
This commit is contained in:
+17
-2
@@ -4,6 +4,7 @@
|
|||||||
// For a copy, see <https://opensource.org/licenses/MIT>.
|
// For a copy, see <https://opensource.org/licenses/MIT>.
|
||||||
|
|
||||||
using DiscordRPC;
|
using DiscordRPC;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace VRCX
|
namespace VRCX
|
||||||
@@ -88,13 +89,27 @@ namespace VRCX
|
|||||||
m_Active = active;
|
m_Active = active;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/1225052/best-way-to-shorten-utf8-string-based-on-byte-length
|
||||||
|
private static string LimitByteLength(string str, int maxBytesLength)
|
||||||
|
{
|
||||||
|
var bytesArr = Encoding.UTF8.GetBytes(str);
|
||||||
|
int bytesToRemove = 0;
|
||||||
|
int lastIndexInString = str.Length - 1;
|
||||||
|
while (bytesArr.Length - bytesToRemove > maxBytesLength)
|
||||||
|
{
|
||||||
|
bytesToRemove += Encoding.UTF8.GetByteCount(new char[] { str[lastIndexInString] });
|
||||||
|
--lastIndexInString;
|
||||||
|
}
|
||||||
|
return Encoding.UTF8.GetString(bytesArr, 0, bytesArr.Length - bytesToRemove);
|
||||||
|
}
|
||||||
|
|
||||||
public void SetText(string details, string state)
|
public void SetText(string details, string state)
|
||||||
{
|
{
|
||||||
m_Lock.EnterWriteLock();
|
m_Lock.EnterWriteLock();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
m_Presence.Details = details;
|
m_Presence.Details = LimitByteLength(details, 127);
|
||||||
m_Presence.State = state;
|
m_Presence.State = LimitByteLength(state, 127);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user