From 30b0c09316637c328c9eb4b61c9cf65cf81be7c0 Mon Sep 17 00:00:00 2001 From: Ryzhehvost Date: Fri, 25 Jun 2021 20:38:53 +0300 Subject: [PATCH] implement split on newlines --- .../Steam/Integration/SteamChatMessage.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ArchiSteamFarm/Steam/Integration/SteamChatMessage.cs b/ArchiSteamFarm/Steam/Integration/SteamChatMessage.cs index be6b65527..54a900984 100644 --- a/ArchiSteamFarm/Steam/Integration/SteamChatMessage.cs +++ b/ArchiSteamFarm/Steam/Integration/SteamChatMessage.cs @@ -33,6 +33,7 @@ namespace ArchiSteamFarm.Steam.Integration { internal static class SteamChatMessage { internal const char ContinuationCharacter = '…'; // A character used for indicating that the next newline part is a continuation of the previous line internal const byte ContinuationCharacterBytes = 3; // The continuation character specified above uses 3 bytes in UTF-8 + internal const char ParagraphCharacter = '¶'; // A character used for indicating that this is not the last part of message (2 bytes, so it fits in ContinuationCharacterBytes) internal const ushort MaxMessageBytesForLimitedAccounts = 1945; // This is a limitation enforced by Steam internal const ushort MaxMessageBytesForUnlimitedAccounts = 6340; // This is a limitation enforced by Steam internal const ushort MaxMessagePrefixBytes = MaxMessageBytesForLimitedAccounts - ReservedContinuationMessageBytes - ReservedEscapeMessageBytes; // Simplified calculation, nobody should be using prefixes even close to that anyway @@ -86,6 +87,10 @@ namespace ArchiSteamFarm.Steam.Integration { // Check if we reached the limit for one message if (messagePartBytes + NewlineWeight + ReservedEscapeMessageBytes > maxMessageBytes) { + if (stringReader.Peek() != -1) { + messagePart.Append(ParagraphCharacter); + } + yield return messagePart.ToString(); messagePartBytes = prefixBytes; @@ -101,8 +106,17 @@ namespace ArchiSteamFarm.Steam.Integration { for (int lineBytesRead = 0; lineBytesRead < lineBytes.Length;) { if (messagePart.Length > prefixLength) { - messagePartBytes += NewlineWeight; - messagePart.AppendLine(); + if (messagePartBytes + NewlineWeight + lineBytes.Length > maxMessageBytes) { + messagePart.Append(ParagraphCharacter); + yield return messagePart.ToString(); + + messagePartBytes = prefixBytes; + messagePart.Clear(); + messagePart.Append(steamMessagePrefix); + } else { + messagePartBytes += NewlineWeight; + messagePart.AppendLine(); + } } int bytesToTake = Math.Min(maxMessageBytes - messagePartBytes, lineBytes.Length - lineBytesRead); @@ -152,6 +166,10 @@ namespace ArchiSteamFarm.Steam.Integration { continue; } + if (stringReader.Peek() != -1 && messagePart[messagePart.Length-1] != ContinuationCharacter) { + messagePart.Append(ParagraphCharacter); + } + yield return messagePart.ToString(); messagePartBytes = prefixBytes;