ClassiCube-Client icon indicating copy to clipboard operation
ClassiCube-Client copied to clipboard

Fix for characters >= 128.

Open UnknownShadow200 opened this issue 11 years ago • 5 comments

When trying to render characters with values >= 128, the client crashes with (for example) 'IndexOutOfBoundsException: 65531'. This is (almost certainly) because Java is treating them as signed bytes and casting them to unsigned chars.

By changing NetHandler.readObject() from

return new String(stringBytes, "UTF-8").trim();

to

char[] chars = new char[64];
for (int i = 0; i < chars.length; i++) {
   chars[i] = (char) (stringBytes[i] & 0xFF);
}
 return new String(chars).trim();

I was able to fix the problem. (no changes were necessary for rendering)

UnknownShadow200 avatar Aug 30 '14 22:08 UnknownShadow200

The current protocol does not use characters above 128, but we've got plans to implement it as an extension, with a more useful charset (Win-1252 derivative, rather than CP437).

mstefarov avatar Aug 30 '14 22:08 mstefarov

Fair enough. But this is more of a fix for reading all characters above 127, so it would still be useful for that future extension.

UnknownShadow200 avatar Aug 30 '14 22:08 UnknownShadow200

Don't worry, the whole font renderer and chat subsystem is to be rewritten cleanly.

mstefarov avatar Aug 30 '14 22:08 mstefarov

Yes, a rewrite would definitely be nice.

Slightly off topic, but this should fix the issue of an emote at the end of a message being hidden. (it appears Java's string.trim() was the problem, as it trims all characters <= 32)

int length = 64;
for (int i = 63; i >= 0; i--) {
   if (chars[i] == 0 || chars[i] == 32) {                       
      length--;
    } else {
      length = i + 1;
      break;
   }
 }
return new String(chars).substring(0, length);

UnknownShadow200 avatar Aug 30 '14 22:08 UnknownShadow200

Now that's definitely worth fixing for everyone. Thanks!

mstefarov avatar Aug 30 '14 22:08 mstefarov