Files

> > CS2D > Lua Scripts > [EngiN33R] UTF-8 Wrapper Library
Files overviewCS2D overviewLua Scripts overview

English [EngiN33R] UTF-8 Wrapper Library >

22 comments2 kb, 579 Downloads

old [EngiN33R] UTF-8 Wrapper Library

EngiN33R
Moderator Off Offline

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
Approved by SQ

Download Download

2 kb, 579 Downloads

Comments

22 comments
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

old

TobyInChina
User Off Offline

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
I like it!
edited 1×, last 01.04.19 01:30:04 pm

old

Vehk
User Off Offline

user Gaios Here's a patch for that.

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

1
2
3
4
U = utf8.convert

-- simplifies usage
U"欢迎来到CS2D"
I like it!
edited 1×, last 29.12.18 02:06:26 am

old

Gaios
Reviewer Off Offline

You may add exception for
\169
I like it!

old

Gaios
Reviewer Off Offline

@user TobyInChina: Autorun folder is executed after all scripts. You do it wrong.
I like it!

old

TobyInChina
User Off Offline

@user SQ:
I've run UTF in the AutoRun folder
I like it!

old

Vehk
User Off Offline

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
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
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).
I like it!
edited 4×, last 10.12.17 04:59:16 am

old

SQ
Moderator Off Offline

@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.
I like it!

old

TobyInChina
User Off Offline

I use you lua.but It maybe dont work!
Help!!!
IMG:https://ww4.sinaimg.cn/large/0060lm7Tly1fm96hg3pj2j307202nglr.jpg

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

IMG:https://ww1.sinaimg.cn/large/0060lm7Tly1fm96hg1hyuj308m02xt9f.jpg
I like it!

old

Nekomata
User Off Offline

Thank God.
I like it!

old colored text working

2Gen
User Off Offline

@user SQ:
Yea, it working, thanks. I just made a mistake in my code and wrote
1
parse("sv_msg2 "..id.."©255000000 Привет CS2D@C")
instead
1
parse("sv_msg2 "..id.." ©255000000 Привет CS2D@C")

well, now everything is alright
I like it!

old

Devil-Thanh
GAME BANNED Off Offline

Great work here
I like it!

old

Gaios
Reviewer Off Offline

@user SQ: I didn't even download it
I like it!

old

SQ
Moderator Off Offline

@user 2Gen: @user Gaios:
There is nothing you need to additionally do. It's a wrapper.
1
msg("©255100100Привет CS2D!!")

Gaiyos code won't even work because it's double converting.
I like it!

old otlichnaya rabota

2Gen
User Off Offline

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.
I like it!

old

Gaios
Reviewer Off Offline

Thank you
I like it!

old

VADemon
User Off Offline

Must be included by default...
but then nobody uses wrapper.lua because of unclear up-to-date state.
I like it!

old

Yates
Reviewer Off Offline

                                                                             
I like it!

old

EngiN33R
Moderator Off Offline

@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