cocos2dx windows安装有中文路径的解决办法

cocos2dx windows安装有中文路径的解决办法,第1张

概述在使用cocos2dx开发的游戏  在手机上面运行的游戏安装目录出现中文的可能性极低,但是有些公司也会将自己的游戏发布到PC上面  楼主的公司就有这个需求    这个时候用户在选择安装路径的时候可能会出现中文 (当然你也可以不让用户选择安装路径,将安装路径固定死  安装在c盘,不过用户体验不好),这个时候游戏很可能会出错,  例如资源找不到  。 这个时候可能会出现一个头疼的问题,就是你的开发环境

在使用cocos2dx开发的游戏 在手机上面运行的游戏安装目录出现中文的可能性极低,但是有些公司也会将自己的游戏发布到PC上面 楼主的公司就有这个需求 这个时候用户在选择安装路径的时候可能会出现中文 (当然你也可以不让用户选择安装路径,将安装路径固定死 安装在c盘,不过用户体验不好),这个时候游戏很可能会出错, 例如资源找不到 。

这个时候可能会出现一个头疼的问题,就是你的开发环境的目录是不包含中文路径的,只有用户自定义安装路径的时候才会出现中文,这个时候你只需要将自己的工程路径中包含一个中文就行了,这样你就可以调试了,注意如果你的路径中包含中文 vs编译器可能会出错不让运行 具体出现的问题我不贴出来了 说一下我的解决办法吧 选择工程->属性->生成事件 将命令行全部删除,下面有一个后期生成事件中的命令行也全部删除(注意 这个时候你只是将你游戏工程里面的这个属性去掉了 但是依赖工程并没有,所以你在按照上面的步骤将依赖工程(libcocos2d工程或者你工程依赖的其他工程)里面的生成事件中的命令行中的内容全部删除)这样你就可以有一个调试环境了



首先来看一下UserDefault 这个类把 因为他会将你的xml文件保存到C:\Users\用户名\AppData\Local 下面 这个时候如果你的用户名是中文这个时候就会出错

通过查看源码 发现在userDefault初始化的时候会有一个路径 来指定 文件 这个时候你要是用户名是中文的话就会发现这个_filePath会有乱码


因为值是从fileUtils::getInstance()->getWritablePath()之中传出来的 所以我们只要进去修改这里面的方法就可以了


string fileUtilsWin32::getWritablePath() const
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
char dir[MAX_PATH];
Getworkdirectory(dir);
return dir;
#endif


//cclOG("dir------------%s",dir);
if (_writablePath.length())
{
return _writablePath;
}


// Get full path of executable,e.g. c:\Program files (x86)\My Game Folder\MyGame.exe
char full_path[CC_MAX_PATH + 1];
::GetmodulefilenameA(nullptr,full_path,CC_MAX_PATH + 1);


// DeBUG app uses executable directory; Non-deBUG app uses local app data directory
//#ifndef _DEBUG
// Get filename of executable only,e.g. MyGame.exe
char *base_name = strrchr(full_path,'\\');


if(base_name)
{
char app_data_path[CC_MAX_PATH + 1];


// Get local app data directory,e.g. C:\documents and Settings\username\Local Settings\Application Data
if (SUCCEEDED(SHGetFolderPathA(nullptr,CSIDL_LOCAL_APPDATA,nullptr,SHGFP_TYPE_CURRENT,app_data_path)))
{
string ret((char*)app_data_path);


// Adding executable filename,e.g. C:\documents and Settings\username\Local Settings\Application Data\MyGame.exe
ret += base_name;


// Remove ".exe" extension,e.g. C:\documents and Settings\username\Local Settings\Application Data\MyGame
ret = ret.substr(0,ret.rfind("."));


ret += "\\";


// Create directory
if (SUCCEEDED(SHCreateDirectoryExA(nullptr,ret.c_str(),nullptr)))
{
return convertPathFormatToUnixStyle(ret);
}
}
}
//#endif // not defined _DEBUG


// If fetching of local app data directory fails,use the executable one
string ret((char*)full_path);


// remove xxx.exe
ret = ret.substr(0,ret.rfind("\\") + 1);


ret = convertPathFormatToUnixStyle(ret);


return ret;
}

以上是原来的引擎的代码


下面贴一下我的代码吧


string fileUtilsWin32::getWritablePath() const
{

if (_writablePath.length())
{
return _writablePath;
}


// Get full path of executable,e.g. c:\Program files (x86)\My Game Folder\MyGame.exe
WCHAR full_path[CC_MAX_PATH + 1] = { 0 };
::Getmodulefilename(nullptr,CC_MAX_PATH + 1);


// DeBUG app uses executable directory; Non-deBUG app uses local app data directory
//#ifndef _DEBUG
// Get filename of executable only,e.g. MyGame.exe
WCHAR *base_name = wcsrchr(full_path,'\\');
wstring retPath;
if (base_name)
{
WCHAR app_data_path[CC_MAX_PATH + 1];


// Get local app data directory,e.g. C:\documents and Settings\username\Local Settings\Application Data
if (SUCCEEDED(SHGetFolderPath(nullptr,app_data_path)))
{
wstring ret(app_data_path);


// Adding executable filename,e.g. C:\documents and Settings\username\Local Settings\Application Data\MyGame.exe
ret += base_name;


// Remove ".exe" extension,e.g. C:\documents and Settings\username\Local Settings\Application Data\MyGame
ret = ret.substr(0,ret.rfind(L"."));


ret += L"\\";


// Create directory
if (SUCCEEDED(SHCreateDirectoryEx(nullptr,nullptr)))
{
retPath = ret;
}
}
}
if (retPath.empty())
//#endif // not defined _DEBUG
{
// If fetching of local app data directory fails,use the executable one
retPath = full_path;


// remove xxx.exe
retPath = retPath.substr(0,retPath.rfind(L"\\") + 1);
}
std::string str = WstringToString(retPath);
//int nLen = (int)retPath.length();
//str.resize(nLen+5,' ');
//int nResult = WIDeCharToMultiByte(CP_ACP,(LPCWSTR)retPath.c_str(),nLen,(LPSTR)str.c_str(),NulL,NulL);

// return convertPathFormatToUnixStyle(StringWIDeCharToUtf8(retPath));
return str;
}

std::string WstringToString(const std::wstring str)
{// wstring转string
unsigned len = str.size() * 4;
setlocale(LC_CTYPE,"");
char *p = new char[len];
wcstombs(p,str.c_str(),len);
std::string str1(p);
delete[] p;
return str1;
}

这里面其实就是转换的时候出现乱码。

这样就解决了在windows中如果用户名是中文 使用UserDefault报错的问题


接下来再来看看如果安装目录出现中文的解决办法

主要有两处地方

总结

以上是内存溢出为你收集整理的cocos2dx windows安装有中文路径的解决办法全部内容,希望文章能够帮你解决cocos2dx windows安装有中文路径的解决办法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存