From be7c17c6b8ad8e28327d73f8188658d8cf0967ea Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 29 Jun 2015 09:25:39 +1000 Subject: [PATCH] Add public methods to send PlayerClick packets (code that calls them not yet implemented), simplify reading strings in FastNetReader and backspace key handler in TextInputWidget. --- 2D/Widgets/TextInputWidget.cs | 14 ++-- Network/FastNetReader.cs | 143 +++++++++------------------------- Network/NetworkProcessor.cs | 47 +++++++---- 3 files changed, 72 insertions(+), 132 deletions(-) diff --git a/2D/Widgets/TextInputWidget.cs b/2D/Widgets/TextInputWidget.cs index 4432301c9..dd7eb3cbf 100644 --- a/2D/Widgets/TextInputWidget.cs +++ b/2D/Widgets/TextInputWidget.cs @@ -6,7 +6,7 @@ using System.Windows.Forms; namespace ClassicalSharp { public sealed class TextInputWidget : Widget { - + public TextInputWidget( Game window, Font font, Font boldFont ) : base( window ) { HorizontalDocking = Docking.LeftOrTop; VerticalDocking = Docking.BottomOrRight; @@ -166,17 +166,15 @@ namespace ClassicalSharp { } void BackspaceKey() { - if( !chatInputText.Empty ) { + if( !chatInputText.Empty && caretPos != 0 ) { if( caretPos == -1 ) { - chatInputText.DeleteAt( chatInputText.Length - 1 ); - Dispose(); - Init(); - } else if( caretPos > 0 ) { + chatInputText.DeleteAt( chatInputText.Length - 1 ); + } else { caretPos--; chatInputText.DeleteAt( caretPos ); - Dispose(); - Init(); } + Dispose(); + Init(); } } diff --git a/Network/FastNetReader.cs b/Network/FastNetReader.cs index e2e353077..9f71604de 100644 --- a/Network/FastNetReader.cs +++ b/Network/FastNetReader.cs @@ -58,52 +58,39 @@ namespace ClassicalSharp { Remove( length ); return data; } - - /// 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 ); + + public string ReadCp437String() { + int length = GetString( buffer, false ); Remove( 64 ); - return value; - } - - internal string ReadWoMTextString( ref byte messageType, bool useMessageTypes ) { - if( useMessageTypes ) return ReadTextString(); - - string value = GetWoMTextString( buffer, ref messageType ); - Remove( 64 ); - return value; - } - - public string ReadString() { - string value = GetAsciiString( buffer ); - Remove( 64 ); - return value; - } - - static char[] characters = new char[64]; - const string womDetail = "^detail.user="; - static string GetTextString( byte[] data ) { - int length = CopyTextStringToBuffer( data ); return new String( characters, 0, length ); } - static string GetWoMTextString( byte[] data, ref byte messageType ) { + public string ReadAsciiString() { + int length = GetString( buffer, true ); + Remove( 64 ); + return new String( characters, 0, length ); + } + + internal string ReadChatString( ref byte messageType, bool useMessageTypes ) { + if( useMessageTypes ) + return ReadCp437String(); + messageType = (byte)CpeMessageType.Normal; - int length = CopyTextStringToBuffer( data ); + int length = GetString( buffer, false ); + Remove( 64 ); + int offset = 0; - if( IsWomDetailString( length ) ) { + if( length >= womDetail.Length && IsWomDetailString() ) { length -= womDetail.Length; - offset += womDetail.Length; + offset = womDetail.Length; messageType = (byte)CpeMessageType.Status3; } return new String( characters, offset, length ); } - static bool IsWomDetailString( int length ) { - if( length < womDetail.Length ) - return false; - + static char[] characters = new char[64]; + const string womDetail = "^detail.user="; + static bool IsWomDetailString() { for( int i = 0; i < womDetail.Length; i++ ) { if( characters[i] != womDetail[i] ) return false; @@ -111,91 +98,33 @@ namespace ClassicalSharp { return true; } - static string GetAsciiString( byte[] data ) { - int length = 0; - for( int i = 63; i >= 0; i-- ) { - byte code = data[i]; - if( length == 0 && !( code == 0 || code == 0x20 ) ) - length = i + 1; - - characters[i] = code >= 0x80 ? '?' : (char)code; - } - return new String( characters, 0, length ); - } - - static int CopyTextStringToBuffer( byte[] data ) { - // code page 437 indices --> actual unicode characters + static int GetString( byte[] data, bool ascii ) { int length = 0; for( int i = 63; i >= 0; i-- ) { byte code = data[i]; if( length == 0 && !( code == 0 || code == 0x20 ) ) length = i + 1; - if( code < 0x20 ) { // general control characters + if( ascii ) { + characters[i] = code >= 0x7F ? '?' : (char)code; + continue; + } + + // Treat code as an index in code page 437 + if( code < 0x20 ) { characters[i] = controlCharReplacements[code]; - } else if( code < 0x7F ) { // normal ascii character + } else if( code < 0x7F ) { characters[i] = (char)code; - } else if( code == 0x7F ) { // delete control character - characters[i] = '\u2302'; - } else if( code >= 0x80 ){ // extended ascii character - characters[i] = extendedCharReplacements[code - 0x80]; + } else { + characters[i] = extendedCharReplacements[code - 0x7F]; } } return length; } - static char[] controlCharReplacements = new char[] { // 00 -> 1F - '\u0000', '\u263A', '\u263B', '\u2665', - '\u2666', '\u2663', '\u2660', '\u2022', - '\u25D8', '\u25CB', '\u25D9', '\u2642', - '\u2640', '\u266A', '\u266B', '\u263C', - - '\u25BA', '\u25C4', '\u2195', '\u203C', - '\u00B6', '\u00A7', '\u25AC', '\u21A8', - '\u2191', '\u2193', '\u2192', '\u2190', - '\u221F', '\u2194', '\u25B2', '\u25BC', - }; - - static char[] extendedCharReplacements = new char[] { // 80 -> FF - '\u00C7', '\u00FC', '\u00E9', '\u00E2', - '\u00E4', '\u00E0', '\u00E5', '\u00E7', - '\u00EA', '\u00EB', '\u00E8', '\u00EF', - '\u00EE', '\u00EC', '\u00C4', '\u00C5', - - '\u00C9', '\u00E6', '\u00C6', '\u00F4', - '\u00F6', '\u00F2', '\u00FB', '\u00F9', - '\u00FF', '\u00D6', '\u00DC', '\u00A2', - '\u00A3', '\u00A5', '\u20A7', '\u0192', - - '\u00E1', '\u00ED', '\u00F3', '\u00FA', - '\u00F1', '\u00D1', '\u00AA', '\u00BA', - '\u00BF', '\u2310', '\u00AC', '\u00BD', - '\u00BC', '\u00A1', '\u00AB', '\u00BB', - - '\u2591', '\u2592', '\u2593', '\u2502', - '\u2524', '\u2561', '\u2562', '\u2556', - '\u2555', '\u2563', '\u2551', '\u2557', - '\u255D', '\u255C', '\u255B', '\u2510', - - '\u2514', '\u2534', '\u252C', '\u251C', - '\u2500', '\u253C', '\u255E', '\u255F', - '\u255A', '\u2554', '\u2569', '\u2566', - '\u2560', '\u2550', '\u256C', '\u2567', - - '\u2568', '\u2564', '\u2565', '\u2559', - '\u2558', '\u2552', '\u2553', '\u256B', - '\u256A', '\u2518', '\u250C', '\u2588', - '\u2584', '\u258C', '\u2590', '\u2580', - - '\u03B1', '\u00DF', '\u0393', '\u03C0', - '\u03A3', '\u03C3', '\u00B5', '\u03C4', - '\u03A6', '\u0398', '\u03A9', '\u03B4', - '\u221E', '\u03C6', '\u03B5', '\u2229', - - '\u2261', '\u00B1', '\u2265', '\u2264', - '\u2320', '\u2321', '\u00F7', '\u2248', - '\u00B0', '\u2219', '\u00B7', '\u221A', - '\u207F', '\u00B2', '\u25A0', '\u00A0', - }; + const string controlCharReplacements = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼"; + static string extendedCharReplacements = "⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»" + + "░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌" + + "█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\u00a0"; } } \ No newline at end of file diff --git a/Network/NetworkProcessor.cs b/Network/NetworkProcessor.cs index 7c410df99..302c38e44 100644 --- a/Network/NetworkProcessor.cs +++ b/Network/NetworkProcessor.cs @@ -10,6 +10,7 @@ using System.Net; using System.Net.Sockets; using ClassicalSharp.Network; using OpenTK; +using OpenTK.Input; namespace ClassicalSharp { @@ -67,6 +68,18 @@ namespace ClassicalSharp { SendPacket(); } + public void SendPlayerClick( MouseButton button, bool buttonDown, PickedPos pos ) { + Player p = Window.LocalPlayer; + MakePlayerClick( (byte)button, buttonDown, p.YawDegrees, p.PitchDegrees, 255, + pos.BlockPos, pos.BlockFace ); + } + + public void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId ) { + Player p = Window.LocalPlayer; + MakePlayerClick( (byte)button, buttonDown, p.YawDegrees, p.PitchDegrees, targetId, + new Vector3I( -100, -100, -100 ), 0 ); + } + public void Dispose() { socket.Close(); Disconnected = true; @@ -251,8 +264,8 @@ namespace ClassicalSharp { case PacketId.Handshake: { byte protocolVer = reader.ReadUInt8(); - ServerName = reader.ReadString(); - ServerMotd = reader.ReadString(); + ServerName = reader.ReadAsciiString(); + ServerMotd = reader.ReadAsciiString(); byte userType = reader.ReadUInt8(); if( !useBlockPermissions ) { Window.CanDelete[(int)Block.Bedrock] = userType == 0x64; @@ -351,7 +364,7 @@ namespace ClassicalSharp { case PacketId.AddEntity: { byte entityId = reader.ReadUInt8(); - string name = reader.ReadString(); + string name = reader.ReadAsciiString(); AddEntity( entityId, name, name, true ); } break; @@ -387,13 +400,13 @@ namespace ClassicalSharp { case PacketId.Message: { byte messageType = reader.ReadUInt8(); - string text = reader.ReadWoMTextString( ref messageType, useMessageTypes ); + string text = reader.ReadChatString( ref messageType, useMessageTypes ); Window.AddChat( text, messageType ); } break; case PacketId.Kick: { - string reason = reader.ReadString(); + string reason = reader.ReadAsciiString(); Window.Disconnect( "&eLost connection to the server", reason ); Dispose(); } break; @@ -409,14 +422,14 @@ namespace ClassicalSharp { case PacketId.CpeExtInfo: { - string appName = reader.ReadString(); + string appName = reader.ReadAsciiString(); Utils.LogDebug( "Server identified itself as: " + appName ); cpeServerExtensionsCount = reader.ReadInt16(); } break; case PacketId.CpeExtEntry: { - string extensionName = reader.ReadString(); + string extensionName = reader.ReadAsciiString(); int extensionVersion = reader.ReadInt32(); Utils.LogDebug( "cpe ext: " + extensionName + "," + extensionVersion ); if( extensionName == "HeldBlock" ) { @@ -475,9 +488,9 @@ namespace ClassicalSharp { case PacketId.CpeExtAddPlayerName: { short nameId = reader.ReadInt16(); - string playerName = Utils.StripColours( reader.ReadString() ); - string listName = reader.ReadString(); - string groupName = reader.ReadString(); + string playerName = Utils.StripColours( reader.ReadAsciiString() ); + string listName = reader.ReadAsciiString(); + string groupName = reader.ReadAsciiString(); byte groupRank = reader.ReadUInt8(); if( nameId >= 0 && nameId <= 255 ) { CpeListInfo oldInfo = Window.CpePlayersList[nameId]; @@ -495,8 +508,8 @@ namespace ClassicalSharp { case PacketId.CpeExtAddEntity: { byte entityId = reader.ReadUInt8(); - string displayName = reader.ReadString(); - string skinName = reader.ReadString(); + string displayName = reader.ReadAsciiString(); + string skinName = reader.ReadAsciiString(); AddEntity( entityId, displayName, skinName, false ); } break; @@ -511,7 +524,7 @@ namespace ClassicalSharp { case PacketId.CpeMakeSelection: { byte selectionId = reader.ReadUInt8(); - string label = reader.ReadString(); + string label = reader.ReadAsciiString(); short startX = reader.ReadInt16(); short startY = reader.ReadInt16(); short startZ = reader.ReadInt16(); @@ -578,7 +591,7 @@ namespace ClassicalSharp { case PacketId.CpeChangeModel: { byte playerId = reader.ReadUInt8(); - string modelName = reader.ReadString().ToLowerInvariant(); + string modelName = reader.ReadAsciiString().ToLowerInvariant(); Player player = playerId == 0xFF ? Window.LocalPlayer : Window.NetPlayers[playerId]; if( player != null ) { player.SetModel( modelName ); @@ -587,7 +600,7 @@ namespace ClassicalSharp { case PacketId.CpeEnvSetMapApperance: { - string url = reader.ReadString(); + string url = reader.ReadAsciiString(); byte sideBlock = reader.ReadUInt8(); byte edgeBlock = reader.ReadUInt8(); short waterLevel = reader.ReadInt16(); @@ -626,8 +639,8 @@ namespace ClassicalSharp { case PacketId.CpeExtAddEntity2: { byte entityId = reader.ReadUInt8(); - string displayName = reader.ReadString(); - string skinName = reader.ReadString(); + string displayName = reader.ReadAsciiString(); + string skinName = reader.ReadAsciiString(); AddEntity( entityId, displayName, skinName, true ); } break;