From edd321762ebe2bc9b889edbe349fd8a598cf3e8a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 5 Jan 2016 13:33:31 +1100 Subject: [PATCH] Fix clickable chat not properly checking input for invalid characters on non-FullCP437 servers. (Thanks WhatIDoHere) --- ClassicalSharp/2D/GuiElement.cs | 9 +++++++++ ClassicalSharp/2D/Screens/ChatScreen.cs | 11 +++++++++-- .../2D/Widgets/Chat/TextInputWidget.Handlers.cs | 6 +++--- ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs | 10 ---------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ClassicalSharp/2D/GuiElement.cs b/ClassicalSharp/2D/GuiElement.cs index fdf7842a4..880360522 100644 --- a/ClassicalSharp/2D/GuiElement.cs +++ b/ClassicalSharp/2D/GuiElement.cs @@ -63,6 +63,15 @@ namespace ClassicalSharp { if( mode == Anchor.BottomOrRight) return axisSize - elemSize - offset; return (axisSize - elemSize) / 2 + offset; } + + protected bool IsValidInputChar( char c ) { + if( c >= ' ' && c <= '~' ) return true; // ascii + + bool isCP437 = Utils.ControlCharReplacements.IndexOf( c ) >= 0 || + Utils.ExtendedCharReplacements.IndexOf( c ) >= 0; + bool supportsCP437 = game.Network.ServerSupportsFullCP437; + return supportsCP437 && isCP437; + } } public enum Anchor { diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index 4fdfe78df..77df4cab3 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -80,7 +80,7 @@ namespace ClassicalSharp { Font chatFont, chatInputFont, chatUnderlineFont, announcementFont; public override void Init() { int fontSize = (int)(12 * game.GuiChatScale); - Utils.Clamp( ref fontSize, 8, 60 ); + Utils.Clamp( ref fontSize, 8, 60 ); chatFont = new Font( "Arial", fontSize ); chatInputFont = new Font( "Arial", fontSize, FontStyle.Bold ); chatUnderlineFont = new Font( "Arial", fontSize, FontStyle.Underline ); @@ -151,7 +151,7 @@ namespace ClassicalSharp { } } - public override void Dispose() { + public override void Dispose() { if( HandlesAllInput ) { game.chatInInputBuffer = textInput.chatInputText.ToString(); if( game.CursorVisible ) @@ -307,6 +307,13 @@ namespace ClassicalSharp { " may have viruses, or things you may not want to open/see." ) ); } else if( game.ClickableChat ) { + for( int i = 0; i < text.Length; i++ ) { + if( !IsValidInputChar( text[i] ) ) { + Console.WriteLine( i + "," + text[i] ); + game.Chat.Add( "&eChatline contained characters that can't be sent on this server." ); + return true; + } + } textInput.AppendText( text ); } return true; diff --git a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs index 2adb5defc..2287b176a 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs @@ -8,7 +8,7 @@ namespace ClassicalSharp { public sealed partial class TextInputWidget : Widget { public override bool HandlesKeyPress( char key ) { - if( chatInputText.Length < len && IsValidChar( key ) ) { + if( chatInputText.Length < len && IsValidInputChar( key ) && key != '&' ) { if( caretPos == -1 ) { chatInputText.Append( chatInputText.Length, key ); } else { @@ -141,8 +141,8 @@ namespace ClassicalSharp { if( String.IsNullOrEmpty( text ) ) return true; for( int i = 0; i < text.Length; i++ ) { - if( !IsValidChar( text[i] ) ) { - game.Chat.Add( "&eClipboard contained characters that can't be sent." ); + if( !IsValidInputChar( text[i] ) ) { + game.Chat.Add( "&eClipboard contained characters that can't be sent on this server." ); return true; } } diff --git a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs index c5be6265a..31dd56750 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs @@ -188,16 +188,6 @@ namespace ClassicalSharp { altText.Y = altText.texture.Y1; } - bool IsValidChar( char c ) { - if( c == '&' ) return false; - if( c >= ' ' && c <= '~' ) return true; // ascii - - bool isCP437 = Utils.ControlCharReplacements.IndexOf( c ) >= 0 || - Utils.ExtendedCharReplacements.IndexOf( c ) >= 0; - bool supportsCP437 = game.Network.ServerSupportsFullCP437; - return supportsCP437 && isCP437; - } - public void SendTextInBufferAndReset() { SendInBuffer(); typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.