当前位置 : 主页 > 手机开发 > 其它 >

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

来源:互联网 收集:自由互联 发布时间:2021-06-13
在使用cocos2dx开发的游戏 在手机上面运行的游戏安装目录出现中文的可能性极低,但是有些公司也会将自己的游戏发布到PC上面 楼主的公司就有这个需求 这个时候用户在选择安装路径的

在使用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, 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
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, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, 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, ret.c_str(), 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, 0, (LPCWSTR)retPath.c_str(), nLen, (LPSTR)str.c_str(), nLen, 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报错的问题


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

主要有两处地方

网友评论