diff --git a/Network/FastNetReader.cs b/Network/FastNetReader.cs index 33e3690e6..f11c4fc66 100644 --- a/Network/FastNetReader.cs +++ b/Network/FastNetReader.cs @@ -62,30 +62,38 @@ namespace ClassicalSharp { /// Reads a string, then converts control characters into the /// unicode values of their equivalent code page 437 graphical representations. public string ReadTextString() { - string value = GetTextString( buffer ).TrimEnd( '\0', ' ' ); // servers can use either null or spaces for padding. + string value = GetTextString( buffer ); Remove( 64 ); return value; } public string ReadString() { - string value = GetAsciiString( buffer ).TrimEnd( '\0', ' ' ); + string value = GetAsciiString( buffer ); Remove( 64 ); return value; } static char[] characters = new char[64]; static string GetAsciiString( byte[] data ) { - for( int i = 0; i < 64; i++ ) { + int endIndex = 0; + for( int i = 63; i >= 0; i-- ) { byte code = data[i]; - characters[i] = code >= 0x80 ? '?' : (char)code; + if( endIndex == 0 && !( code == 0 || code == 0x20 ) ) + endIndex = i + 1; + + characters[i] = code >= 0x80 ? '?' : (char)code; } - return new String( characters ); + return new String( characters, 0, endIndex ); } static string GetTextString( byte[] data ) { // code page 437 indices --> actual unicode characters - for( int i = 0; i < 64; i++ ) { + int endIndex = 0; + for( int i = 63; i >= 0; i-- ) { byte code = data[i]; + if( endIndex == 0 && !( code == 0 || code == 0x20 ) ) + endIndex = i + 1; + if( code < 0x20 ) { // general control characters characters[i] = controlCharReplacements[code]; } else if( code < 0x7F ) { // normal ascii character @@ -96,7 +104,7 @@ namespace ClassicalSharp { characters[i] = extendedCharReplacements[code - 0x80]; } } - return new String( characters ); + return new String( characters, 0, endIndex ); } static char[] controlCharReplacements = new char[] { // 00 -> 1F