[EngiN33R] UTF-8 Wrapper Library 
22 comments 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
utf8.convert("Привет CS2D!") gives
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:



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

UTF-8:xE6xACxA2xE8xBFx8ExE6x9DxA5xE5x88xB0x43x53x32x44

UTF-8:xD0x9FxD1x80xD0xB8xD0xB2xD0xB5xD1x82x20x43x53x32x44x21
edited 1×, last 05.12.17 09:17:54 pm

Comments
22 commentsLog in!
You need to log in to be able to write comments!Log in

Hello
And I find another Bug,
If you want say something by using addhook('say','_say')
it's like dont work.


And I find another Bug,
If you want say something by using addhook('say','_say')
it's like dont work.



edited 1×, last 01.04.19 01:30:04 pm


Code:
1
2
3
4
5
6
7
8
9
10
11
12
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)
+++ 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
2
3
4
U = utf8.convert
-- simplifies usage
U"欢迎来到CS2D"
-- simplifies usage
U"欢迎来到CS2D"
edited 1×, last 29.12.18 02:06:26 am

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
Using this script will break it. You can add a fix by doing this
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).
Why are utf8len and utf8sub private?
Note to scripters
If you use code similar to this
Code:
1
2
3
2
3
local colorcode=string.char(0xA9)
...
msg(colorcode.."255000000hello")
...
msg(colorcode.."255000000hello")
Using this script will break it. You can add a fix by doing this
Code:
1
2
3
4
2
3
4
local colorcode=string.char(0xA9)
if utf8 then
colorcode=string.char(0xC2)..string.char(0xA9)
end
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

@
TobyInChina:
Also you have loaded UTF after printing your line.

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.
06.12.17 02:00:42 pm

@
SQ:
Yea, it working, thanks. I just made a mistake in my code and wrote
instead
well, now everything is alright

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 08:50:35 am

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.

Must be included by default...
but then nobody uses wrapper.lua because of unclear up-to-date state.
but then nobody uses wrapper.lua because of unclear up-to-date state.
@
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.