English [EngiN33R] UTF-8 Wrapper Library icon

22 comments
05.12.17 09:09:04 pm
like 11 like it!
2 kb, 525 Downloads
EngiN33R
Moderator
Offline Off
This is an official tiny wrapper library that helps you forget about UTF-8 conversions in the new CS2D versions. The library replaces the msg, msg2, print and parse functions to always use UTF-8. The library has a safeguard against being loaded multiple times, so it should be safe to include in your mod or script without additional considerations.

When using this library for UTF-8 strings in your code, make sure to save your .lua files as UTF-8 without BOM!

In case you need raw access to UTF-8 helper functions, the library provides the following:

• utf8.codepoint(char) [number] - get the Unicode codepoint of the given character.
• utf8.encode(char) [string] - get the UTF-8 representation of a single character.
• utf8.convert(str) [string] - get a CS2D-ready UTF-8 representation of the given string.

After loading this library, UTF-8 conversion works as follows:

• utf8.convert("欢迎来到CS2D") gives
UTF-8:xE6xACxA2xE8xBFx8ExE6x9DxA5xE5x88xB0x43x53x32x44

• utf8.convert("Привет CS2D!") gives
UTF-8:xD0x9FxD1x80xD0xB8xD0xB2xD0xB5xD1x82x20x43x53x32x44x21
edited 1×, last 05.12.17 09:17:54 pm
ok This file has been reviewed and approved by SQ (05.12.17 09:09:52 pm)

Comments

22 comments
Goto Page
To the start Previous 1 2 Next To the start

Log in!

You need to log in to be able to write comments!Log in
01.04.19 01:08:41 pm
like I like it!
Up
TobyInChina
User
Offline Off
Hello
And I find another Bug,
If you want say something by using addhook('say','_say')
it's like dont work.
IMG:https://s2.ax1x.com/2019/04/01/AySwxU.png

IMG:https://s2.ax1x.com/2019/04/01/AySaGV.png

IMG:https://s2.ax1x.com/2019/04/01/AySd2T.png
edited 1×, last 01.04.19 01:30:04 pm
15.12.17 07:24:42 pm
like I like it!
Up
Vehk
User
Offline Off
user Gaios Here's a patch for that.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
--- utf8-original.lua
+++ utf8.lua
@@ -26,6 +26,9 @@
                -- UTF8-1
                return 1

+       elseif c == 0xA9 then           -- ASCII Copyright/CS2D color code
+               return 1
+
        elseif c >= 194 and c <= 223 then
                -- UTF8-2
                local c2 = string.byte(s, i + 1)


To anyone using this script I'd recommend using an alias for utf8.convert

Code:
1
2
3
4
U = utf8.convert

-- simplifies usage
U"欢迎来到CS2D"
edited 1×, last 29.12.18 02:06:26 am
14.12.17 02:45:25 pm
like I like it!
Up
Gaios
Reviewer
Offline Off
You may add exception for
\169
14.12.17 10:33:08 am
like I like it!
Up
TobyInChina
User
Offline Off
@user SQ: @user Gaios:
Thank you
Finally I success
10.12.17 01:55:07 pm
like I like it!
Up
Gaios
Reviewer
Offline Off
@user TobyInChina: Autorun folder is executed after all scripts. You do it wrong.
10.12.17 11:38:17 am
like I like it!
Up
TobyInChina
User
Offline Off
@user SQ:
I've run UTF in the AutoRun folder
08.12.17 10:15:48 pm
like I like it!
Up
Vehk
User
Offline Off
Good script! Fixes another design flaw in the cs2d Lua API.

Why are utf8len and utf8sub private?

Note to scripters
If you use code similar to this
Code:
1
2
3
local colorcode=string.char(0xA9)
...
msg(colorcode.."255000000hello")

Using this script will break it. You can add a fix by doing this
Code:
1
2
3
4
local colorcode=string.char(0xA9)
if utf8 then
     colorcode=string.char(0xC2)..string.char(0xA9)
end


This will cause problems with existing scripts, especially if this file becomes included by default.

Any script using code similar to the above snippet or using the © symbol and saving as ASCII will be broken.

One fix is for scripters to always use code like the second snippet above. Another would be for this library to return invalid utf-8 as byte(s) or to provide new functions instead of overwriting existing ones.

This should be considered before possibly including this with CS2D and for anyone wanting to use this script on their servers (you might need to make changes to existing scripts).
edited 4×, last 10.12.17 04:59:16 am
08.12.17 04:17:31 am
like I like it!
Up
SQ
Moderator
Offline Off
@user TobyInChina:
Quote:
When using this library for UTF-8 strings in your code, make sure to save your .lua files as UTF-8 without BOM!

Also you have loaded UTF after printing your line.
08.12.17 04:01:08 am
like I like it!
Up
TobyInChina
User
Offline Off
I use you lua.but It maybe dont work!
Help!!!
IMG:http://ww4.sinaimg.cn/large/0060lm7Tly1fm96hg3pj2j307202nglr.jpg

IMG:http://ww2.sinaimg.cn/large/0060lm7Tly1fm96hg1jc4j30ei021mx2.jpg

IMG:http://ww1.sinaimg.cn/large/0060lm7Tly1fm96hg1hyuj308m02xt9f.jpg
06.12.17 07:52:08 pm
like I like it!
Up
Nekomata
User
Offline Off
Thank God.
06.12.17 02:00:42 pm
like I like it!
Up
2Gen
User
Offline Off
@user SQ:
Yea, it working, thanks. I just made a mistake in my code and wrote
Code:
1
parse("sv_msg2 "..id.."©255000000 Привет CS2D@C")

instead
Code:
1
parse("sv_msg2 "..id.." ©255000000 Привет CS2D@C")


well, now everything is alright
06.12.17 01:37:22 pm
like I like it!
Up
Devil-Thanh
GAME BANNED
Offline Off
Great work here
06.12.17 12:29:50 pm
like I like it!
Up
Gaios
Reviewer
Offline Off
@user SQ: I didn't even download it
06.12.17 12:05:13 pm
like I like it!
Up
SQ
Moderator
Offline Off
@user 2Gen: @user Gaios:
There is nothing you need to additionally do. It's a wrapper.
Code:
1
msg("©255100100Привет CS2D!!")


Gaiyos code won't even work because it's double converting.
06.12.17 08:50:35 am
like I like it!
Up
2Gen
User
Offline Off
Oh, this is exactly what I was looking for, thanks. But can you explain how to get the colored text using this script, since I always have it yellow.
06.12.17 07:50:50 am
like I like it!
Up
Gaios
Reviewer
Offline Off
Thank you
06.12.17 06:05:33 am
like I like it!
Up
VADemon
User
Offline Off
Must be included by default...
but then nobody uses wrapper.lua because of unclear up-to-date state.
06.12.17 03:36:54 am
like I like it!
Up
Cure Pikachu
User
Offline Off
Thumbs up.
05.12.17 11:25:40 pm
like I like it!
Up
Yates
Reviewer
Offline Off
                                                                             
05.12.17 09:30:38 pm
Up
EngiN33R
Moderator
Offline Off
@user Infinite Rain: The intricacies of editing
package.path
to be able to painlessly include files, as well as the question of the library being potentially redistributed with mods and required multiple times, means that an old-fashioned safeguard is easier to do. If you want to use the require toolchain, just use
require "utf8"
, provided your
package.path
is modified accordingly.
To the start Previous 1 2 Next To the start