This commit is contained in:
2026-02-13 17:40:07 +08:00
parent d951b3a7e1
commit 23ad728a35
61 changed files with 362 additions and 246 deletions

View File

@@ -1,13 +1,18 @@
#include "Utility.h"
#include "Utility.h"
#include "ui/CocosGUI.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "iconv.h"
#else
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#endif
#include "Helps.h"
//////////////////////////////////////////////////////////////////////////
/**
* 计算utf8字符串长度
* @param utf8 utf8字符串
* @return 字符串长度
* 获取utf8字符串长度
* @param utf8 utf8字符串
* @return 字符串长度
*/
int utf8_len(const char *utf8)
{
@@ -34,9 +39,9 @@ int utf8_cmp(const char* str1, const char* str2)
}
/**
* 计算ucs2字符串长度
* @param ucs2 ucs2字符串
* @return 字符串长度
* 获取ucs2字符串长度
* @param ucs2 ucs2字符串
* @return 字符串长度
*/
int ucs2_len(const unsigned short* ucs2)
{
@@ -70,15 +75,36 @@ int code_convert(const char *from_charset, const char *to_charset, const char *i
return 0;
#endif
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
iconv_t cd;
const char *temp = inbuf;
const char **pin = &temp;
char **pout = &outbuf;
memset(outbuf,0,outlen);
cd = iconv_open(to_charset,from_charset);
if(cd==0) return -1;
if(-1==iconv(cd, (char **)(pin),&inlen,pout,&outlen)) return -1;
iconv_close(cd);
JNIEnv* env = cocos2d::JniHelper::getEnv();
if (!env) return -1;
jbyteArray inputBytes = env->NewByteArray(inlen);
env->SetByteArrayRegion(inputBytes, 0, inlen, (const jbyte*)inbuf);
jstring fromCharset = env->NewStringUTF(from_charset);
jstring toCharset = env->NewStringUTF(to_charset);
jclass stringClass = env->FindClass("java/lang/String");
jmethodID ctor = env->GetMethodID(stringClass, "<init>", "([BLjava/lang/String;)V");
jobject jstr = env->NewObject(stringClass, ctor, inputBytes, fromCharset);
jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray outputBytes = (jbyteArray)env->CallObjectMethod(jstr, getBytes, toCharset);
if (outputBytes) {
jsize outputLen = env->GetArrayLength(outputBytes);
if ((size_t)outputLen < outlen) {
env->GetByteArrayRegion(outputBytes, 0, outputLen, (jbyte*)outbuf);
}
env->DeleteLocalRef(outputBytes);
}
env->DeleteLocalRef(jstr);
env->DeleteLocalRef(toCharset);
env->DeleteLocalRef(fromCharset);
env->DeleteLocalRef(inputBytes);
env->DeleteLocalRef(stringClass);
return 0;
#endif
return 0;
@@ -87,9 +113,9 @@ int code_convert(const char *from_charset, const char *to_charset, const char *i
namespace utility
{
/**
* 计算utf8字符串长度
* @param utf8 utf8字符串
* @return 字符串长度
* 获取utf8字符串长度
* @param utf8 utf8字符串
* @return 字符串长度
*/
int utf8_len(std::string utf8)
{
@@ -99,15 +125,15 @@ namespace utility
{
return 0;
unsigned int i;
unsigned long nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节
unsigned long nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节
unsigned char chr;
int bAllAscii=1; //如果全部都是ASCII, 说明不是UTF-8
int bAllAscii=1; //如果全部都是ASCII, 说明不是UTF-8
for(i=0;i<length;i++)
{
chr= *(str+i);
if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,用一个字节存,最高位标记为0,o0xxxxxxx
if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,用一个字节存,最高位标记为0,o0xxxxxxx
bAllAscii= 0;
if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数
if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数
{
if(chr>=0x80)
{
@@ -128,7 +154,7 @@ namespace utility
nBytes--;
}
}
else //多字节符的非首字节,应为 10xxxxxx
else //多字节符的非首字节,应为 10xxxxxx
{
if( (chr&0xC0) != 0x80 )
{
@@ -137,11 +163,11 @@ namespace utility
nBytes--;
}
}
if( nBytes > 0 ) //违规则
if( nBytes > 0 ) //违规则
{
return 0;
}
if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8
if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8
{
return 0;
}
@@ -184,7 +210,7 @@ namespace utility
return strRet;
}
/*gbk转为UTF16*/
/*gbk转为UTF16*/
std::string a2l(const char *inbuf)
{
size_t inlen = strlen(inbuf);
@@ -197,7 +223,7 @@ namespace utility
}
/*utf16转为UTF8*/
/*utf16转为UTF8*/
std::string l2u(const char *inbuf)
{
size_t inlen = ucs2_len((const unsigned short*)&inbuf[0]);
@@ -270,7 +296,7 @@ namespace utility
pSprFace->setAnchorPoint(Vec2(0, 0));
pSprFace->setFlippedY(true);
// 头像剪切
// 头像遮罩
auto textureCache = Director::getInstance()->getTextureCache();
auto texMask = textureCache->addImage("Platform/lobby/head_mask.png");
CC_ASSERT(texMask != nullptr);
@@ -407,7 +433,7 @@ namespace utility
}
}
//超出宽度用星号代替(参数:昵称字体文件字体大小,超出宽度)
//根据宽度截取昵号长度(参数:昵称,字体文件名,字体大小,限定宽度)
std::string getShortName(const std::string& strName, const std::string& strFont, float fFontSize, float fOverWidth)
{
size_t nCount = strName.size();
@@ -435,7 +461,7 @@ namespace utility
if (fTotalWidth >= fOverWidth)
{
return strName.substr(0, i - (bChinese ? 3 : 1)) + a_u8("");
return strName.substr(0, i - (bChinese ? 3 : 1)) + a_u8("...");
}
}
@@ -466,7 +492,7 @@ namespace utility
if (nTempNum > nFontNum)
{
return strName.substr(0, i - (bChinese ? 3 : 1)) + a_u8("");
return strName.substr(0, i - (bChinese ? 3 : 1)) + a_u8("...");
}
}
@@ -486,17 +512,17 @@ namespace utility
std::string GetUserLevel(SCORE lGrade)
{
static std::string Levels[] = {
"等级:雀",
"等级:雀",
"等级:雀头",
"等级:铜雀",
"等级:银雀",
"等级:金雀",
"等级:雀",
"等级:雀",
"等级:雀",
"等级:雀圣",
"等级:雀神"
"等级:雀",
"等级:雀",
"等级:雀头",
"等级:铜雀",
"等级:银雀",
"等级:金雀",
"等级:雀",
"等级:雀",
"等级:雀",
"等级:雀圣",
"等级:雀神"
};
static SCORE lLevels[] = { 200, 500, 1000, 2000, 4000, 6000, 10000, 15000, 20000, 30000 };
@@ -527,4 +553,4 @@ namespace utility
return Helps::MD5Encrypt(timeString);
}
}
}