Page 1 of 1

Character limit for 255 bytes

Posted: Wed Oct 19, 2016 12:23 pm
by decompile
Hey,

I have a short question regarding how much characters 255 bytes are.

I googled it, and they said 1 character is 1 byte, so I added a security check for my message function:

Syntax: Select all

message = message[:255]


I tried to send a message with 252 characters, but ingame im receiving:

Code: Select all

DLL_MessageEnd:  Refusing to send user message SayText2 of 256 bytes to client, user message size limit is 255 bytes

Re: Character limit for 255 bytes

Posted: Wed Oct 19, 2016 1:26 pm
by satoon101
Are there any non-ascii characters in the string? Each of those would take up more than 1 byte. As an example:

Syntax: Select all

>>> x = chr(258)
>>> x
'Ă'
>>> x.encode('utf-8')
b'\xc4\x82'
>>> len(x.encode('utf-8'))
2
>>> 's'.encode('utf-8')
b's'
>>> len('s'.encode('utf-8'))
1

Re: Character limit for 255 bytes

Posted: Wed Oct 19, 2016 1:53 pm
by decompile
Oh ye, could be possible due to color codes. Is there any other way to limit the message to 255 bytes?

Re: Character limit for 255 bytes

Posted: Wed Oct 19, 2016 9:21 pm
by Ayuto
You could use something like this:

Syntax: Select all

def limit_msg(msg, size):
# Use 'ignore' for the same reason like we used it here:
# https://github.com/Source-Python-Dev-Te ... /issues/27
return msg.encode('utf-8')[:size].decode('utf-8', 'ignore')

# Should print ab
print(limit_msg('abc', 2))

# Should print a
print(limit_msg('aä', 2))

Re: Character limit for 255 bytes

Posted: Wed Mar 03, 2021 6:27 pm
by decompile
Sorry to bump an older thread of mine.

But how can I apply these to a TranslationStrings instance?

It throws me an:

Code: Select all

AttributeError: 'TranslationStrings' object has no attribute 'encode'


I'm using TranslationStrings to tokenize player names and other stuff, so its very dynamic in length.

The best would be if it could split the message in 2 parts if its too long, without cutting through a word. Like: "This is too lo" "ng" to "This is too " "long"

Re: Character limit for 255 bytes

Posted: Fri Mar 05, 2021 11:10 am
by L'In20Cible
decompile wrote:Sorry to bump an older thread of mine.

But how can I apply these to a TranslationStrings instance?

It throws me an:

Code: Select all

AttributeError: 'TranslationStrings' object has no attribute 'encode'


I'm using TranslationStrings to tokenize player names and other stuff, so its very dynamic in length.

The best would be if it could split the message in 2 parts if its too long, without cutting through a word. Like: "This is too lo" "ng" to "This is too " "long"

The easiest way would probably be to subclass SayText2 and truncate the localized string in a _send override. Something like that:

Syntax: Select all

class MySayText2(SayText2):
def _send(self, player_indexes, translated_kwargs):
translated_kwargs.message = truncate_or_whatever(translated_kwargs.message)
super()._send(player_indexes, translated_kwargs)


And then use that class to send your messages as normal. Another way without subclassing would be to hook that message and truncate the string in there before sending it.

Re: Character limit for 255 bytes

Posted: Fri Mar 05, 2021 3:04 pm
by decompile
Thank you.

I will try to play around with it. I was trying to find how SayText2 gets the string out of a TranslationStrings class instance but didn't had a success, but looks like its just TranslationStrings.message to return the string.