'Special characters' question

10 replies [Last post]
Posts: 1030

Any time someone tries to use special letters (ö, ü, ß, etc.), the server throws an assertion, what's the best way to handle this? I guess I could remove the assertion but it's there for a reason. Why can't the server process these characters anyway?

Posts: 263

Spodi hates foreigners!

Posts: 147

LOLS

Posts: 53

Racist!

Posts: 1691

Skye wrote:
Any time someone tries to use special letters (ö, ü, ß, etc.), the server throws an assertion

And what might this racist assertion say? Maybe it is just a highly misguided individual.

Posts: 1030

I typed this: ü

The server log printed the following:

Invalid Say string from User `Skye [8; User]`: here's a square

Failed to process received data from `IPSocket - 192.168.1.100:60019`. Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at NetGore.Network.MessageProcessorManager.GetInternalMessageProcessor(MessageProcessorID msgID) in C:\SVO_SVN\SVO_NET\NetGore\NetGore\Network\MessageProcessor\MessageProcessorManager.cs:line 154
at NetGore.Network.MessageProcessorManager.GetMessageProcessor(MessageProcessorID msgID) in C:\SVO_SVN\SVO_NET\NetGore\NetGore\Network\MessageProcessor\MessageProcessorManager.cs:line 166
at NetGore.Network.MessageProcessorManager.Process(IIPSocket socket, BitStream data) in C:\SVO_SVN\SVO_NET\NetGore\NetGore\Network\MessageProcessor\MessageProcessorManager.cs:line 277
at DemoGame.Server.ServerSockets.OnReceiveData(IIPSocket sender, BitStream data) in C:\SVO_SVN\SVO_NET\NetGore\DemoGame.Server\Networking\ServerSockets.cs:line 99

The assertion messages: http://min.us/mvnZi1L

Posts: 1691

It happens due to this method, which by default only accepts ASCII characters from 32 to 126.

        /// <summary>
        /// Checks if a string is a valid Say string.
        /// </summary>
        /// <param name="text">String to check.</param>
        /// <returns>True if a valid Say string, else false.</returns>
        protected virtual bool IsValidSayString(string text)
        {
            // Check if empty
            if (string.IsNullOrEmpty(text))
                return false;
 
            // Check each character in the string to make sure they are valid
            foreach (var letter in text)
            {
                var i = Convert.ToInt32(letter);
                if (i > 126 || i < 32)
                    return false;
            }
 
            return true;
        }

Override it in the server's SayHandler with something like:

        protected override bool IsValidSayString(string text)
        {
            if (string.IsNullOrEmpty(text))
                return false;
 
            /* put your check for legal characters here... */
 
            return true;
        }

Posts: 1030

Anyone know a decent algorithm to check for legal characters?
Can't I just restrict the allowed characters on the clientside so only characters between 32 & 126 can be entered in input fields? Laughing out loud

Edit: the : D emoticon is broken?

Edit2: I've been looking at some ascii tables but apparently there's a bunch of different standards, how do I know which one is the one used in C#?

For example this one http://www.csharp411.com/ascii-table/ says 153 is a TM sign while I typed a ü

Posts: 1691

Chars in unicode, not ascii. I only said ascii since characters 0 to 127 (the ascii set) are the same in unicode for backwards compatibility.

As for how you would go about it... this is why I just don't bother with foreign characters. Tongue First step is definitely to make yourself a unit test to test all valid characters you can think of. Then, I guess it depends on what you want to support. You could probably just reject anything that matches char.IsControl(), which should give you all printable unicode characters. Its way too many (probably more than your font sets even support), but its easy enough.

Unless I screwed up somewhere, NetGore should encode everything in UTF-8, so the whole unicode set should be supported as long as you have the fonts for it.

Skye wrote:
Edit: the : D emoticon is broken?

Thanks, didn't realize that since the emoticons were all still in my cache.

Posts: 1030

Well I don't really *need* the support. For a quick/lazy fix so the server doesn't freeze every time some silly person types in such a character, where is the assertion that is thrown exactly and can I just comment out the line?

Posts: 1691

Just search for the constant of the string: "Invalid Say string from"

And yeah, all assertions can be removed without problem. That is why they are assertions, not exceptions.