Tencent / puerts

PUER(普洱) Typescript. Let's write your game in UE or Unity with TypeScript.
Other
4.99k stars 696 forks source link

[Unity]字符串中间含\0传递到js会被截断 #1732

Open chexiongsheng opened 4 months ago

chexiongsheng commented 4 months ago

detail | 详细描述

现在plugin api传递字符串是没传长度的,比如返回值: https://github.com/Tencent/puerts/blob/81b0f39a40c5ec9ce878c28bfbda6d88d250f03e/unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs#L304

但utf8,utf16编码都支持中间含\0,c#编码类型api调用可能会被截断:https://github.com/Tencent/puerts/issues/20

chexiongsheng commented 4 months ago

il2cpp pinvoke本身也有同样的bug,如果要修正,原生接口将不能直接声明为string,而是传byte[]和长度,这样会多一个Encoding.UTF8.GetBytes调用,增加一个byte[]数组的分配和gc 其次xil2cpp版本用到的il2cpp::utils::StringUtils::Utf16ToUtf8也有该bug,改为调用utf8::unchecked::utf16to8需要增加utils/utf8-cpp/source/utf8/unchecked.h的引用,这个修改不知道各unity版本的兼容性如何。

考虑到unity本身也有类似的bug,之前也甚少项目反馈这问题,所以本issue的修改先不合入到master,先放到这个分支:https://github.com/Tencent/puerts/commits/fix_null_terminator_inside_string_being_cut

mingxxming commented 3 months ago

如果 \0 不截断是不是也会有问题。。有遇到base64 的string末尾多几个字符以及在linux下读取js文件多几个字符的情况,都是加\0后得以解决的。

chexiongsheng commented 3 months ago

如果 \0 不截断是不是也会有问题。。有遇到base64 的string末尾多几个字符以及在linux下读取js文件多几个字符的情况,都是加\0后得以解决的。

通过长度识别结尾