当前位置: 首页 > news >正文

cocos2d 中UserDefault在windows平台下的路径问题

在使用cocos2dx c++开发项目时,通常使用cocos自带的UserDefault来存储一些项目所用到的一些配置信息:如游戏的音量,游戏的闯关数等...

但是windows平台下,测试发现如果用户的帐户名使用是中文,在启动程序时会报错,导致程序无法运行。经过排查,把问题定位到CCFileUtils-win32.cpp的FileUtilsWin32::getWritablePath函数中:string FileUtilsWin32::getWritablePath() const
{
    // 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;
}

这里可以看到作者在使用
SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, app_data_path))
这个函数时,目的是想通过
CSIDL_LOCAL_APPDATA
来把路径定位到帐户下的APPData目录,而CSIDL_LOCAL_APPDATA这个宏的解释是:

#define CSIDL_LOCAL_APPDATA             0x001c        // <user name>\Local Settings\Applicaiton Data (non roaming)

微软在解释SHGetFolderPath时,说明了用法:

SHGetFolderPathW (Unicode) and SHGetFolderPathA (ANSI)
由于ANSI的兼容性不好,很多情况会导致中文乱码,所以这里需要修改SHGetFolderPathW来将字符集转成unicode,这样比较好用。

修改过后:


string FileUtilsWin32::getWritablePath() const
{
    // 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)
    {
     // Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
    WCHAR utf16Path[CC_MAX_PATH + 1] = { 0 };
    if(SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, utf16Path)))
    {
            char utf8_path[1024 + 2] = { 0 };
        WideCharToMultiByte(CP_UTF8, 0, utf16Path, CC_MAX_PATH + 1, utf8_path, 1024 + 2, NULL, NULL);
        string ret((char*)utf8_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;
}

这样就解决了中文目录下使用userDefault造成的程序崩溃问题。
 

http://www.lryc.cn/news/100821.html

相关文章:

  • ChatGPT与高等教育变革:价值、影响及未来发展
  • Matlab Image Processing toolbox 下载安装方法
  • 什么是消息键(Key)?如何使用消息键进行消息顺序性保证?
  • 慎思笃行,兴业致远:金融行业的数据之道
  • Git-分支管理
  • [Ubuntu 22.04] containerd配置HTTP方式拉取私仓Harbor
  • 入门指南:深入解析OpenCV的copyTo函数及其与rect的应用场景
  • 2018年全国硕士研究生入学统一考试管理类专业学位联考写作试题——解析版
  • 系统集成|第七章(笔记)
  • Qt —— Vs2017编译hiredis源码并测试调用(附调用hiredis库源码)
  • 深入理解设计模式:设计模式定义、设计原则以及组织编目
  • 鸿鹄协助管理华为云与炎凰Ichiban
  • Vite创建Vue+TS项目引入文件路径报错
  • 计算机里基本硬件的组成以及硬件协同
  • 2023软件设计师中级备考经验分享(文中有资料链接分享)
  • Windows 10 中无法最大化任务栏中的程序
  • 【iOS】KVOKVC原理
  • 当机器人变硬核:探索深度学习中的时间序列预测
  • C# Solidworks二次开发:自动创建虚拟零件及使用注意事项
  • vim工具 windows系统使用
  • Tesseract开源的OCR工具及python pytesseract安装使用
  • 【数理知识】自由度 degree of freedom 及自由度的计算方法
  • 苍穹外卖day09——历史订单模块(用户端)+订单管理模块(管理端)
  • 正则表达式 —— Grep
  • STC12C5A系列单片机片内看门狗的应用
  • C语言指针详解
  • RTPS规范v2.5(中文版)
  • LeetCode102.Binary-Tree-Level-Order-Traversal<二叉树的层序遍历>
  • yolov8系列[五]-项目实战-yolov8模型无人机检测
  • Redis 笔记,基本数据类型、持久化、主从、集群等等问题