[✔️]cocos2dx的CCFileUtils深度理解

简介: [✔️]cocos2dx的CCFileUtils深度理解

重点看在Android上的实现


网络异常,图片无法展示
|


  • CCFileUtils-android.cpp


FileUtils* FileUtils::getInstance()
{
    if (s_sharedFileUtils == nullptr)
    {
        // Android的类
        s_sharedFileUtils = new FileUtilsAndroid();
    }
    return s_sharedFileUtils;
}


std::string FileUtils::fullPathForFilename(const std::string &filename) const{
    std::string fullpath;
    // 这个_searchPathArray里面就有assets这个很特殊的目录
    for (const auto& searchIt : _searchPathArray) {
        for (const auto& resolutionIt : _searchResolutionsOrderArray) {
            // 里面有平台代码,换取fullpath
            fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);
            if (!fullpath.empty()) {
                // Using the filename passed in as key.
                _fullPathCache.emplace(name, fullpath);
                return fullpath;
            }
        }
    }
}
// 最终会来到这个函数
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const {
    // get directory+filename, safely adding '/' as necessary
    std::string ret = directory;
    if (directory.size() && directory[directory.size()-1] != '/'){
        ret += '/';
    }
    ret += filename;
    // if the file doesn't exist, return an empty string
    if (!isFileExistInternal(ret)) {
        ret = "";
    }
    return ret;
}


// 安卓平台判断文件是否是内置的资源
bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const
{
    if (strFilePath.empty()) {
        return false;
    }
    bool bFound = false;
    // Check whether file exists in apk.
    if (strFilePath[0] != '/') {
        const char* s = strFilePath.c_str();
        // Found "assets/" at the beginning of the path and we don't want it
        // 注意这个_defaultResRootPath,它是一个protected变量,在init有赋值
        if (strFilePath.find(_defaultResRootPath) == 0) s += _defaultResRootPath.length();
        if (obbfile && obbfile->fileExists(s)) {
            bFound = true;
        }else if (FileUtilsAndroid::assetmanager){
            // 看到了Android平台的jni代码
            AAsset* aa = AAssetManager_open(FileUtilsAndroid::assetmanager, s, AASSET_MODE_UNKNOWN);
            if (aa){
                bFound = true;
                AAsset_close(aa);
            } else {
                // CCLOG("[AssetManager] ... in APK %s, found = false!", strFilePath.c_str());
            }
        }
    }
    else {
        FILE *fp = fopen(strFilePath.c_str(), "r");
        if (fp) {
            bFound = true;
            fclose(fp);
        }
    }
    return bFound;
}
#define  ASSETS_FOLDER_NAME          "assets/"
bool FileUtilsAndroid::init()
{
    _defaultResRootPath = ASSETS_FOLDER_NAME;
    // ...
}
void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
{
    // 每一次添加searchpaths的时候,都会检查rootPath的存在
    bool existDefaultRootPath = false;
    // ...
    if (!existDefaultRootPath) {
        _searchPathArray.push_back(_defaultResRootPath);
    }
}


安卓平台的搜索路径:


  • assets/src/


  • assets/res/


  • assets/


至此也就搞明白了,fullpath在Android上是一个假的路径


由于assets文件夹中的文件在APK文件中被打包,因此它们没有绝对路径。


string FileUtilsAndroid::getWritablePath() const
{
    // Fix for Nexus 10 (Android 4.2 multi-user environment)
    // the path is retrieved through Java Context.getCacheDir() method
    string dir("");
    string tmp = JniHelper::callStaticStringMethod("org/cocos2dx/lib/Cocos2dxHelper", "getCocos2dxWritablePath");
    if (tmp.length() > 0)
    {
        dir.append(tmp).append("/");
        return dir;
    }
    else
    {
        return "";
    }
}


public static String getCocos2dxWritablePath() {
    //赋值的逻辑: Cocos2dxHelper.sFileDirectory = activity.getFilesDir().getAbsolutePath();
    return Cocos2dxHelper.sFileDirectory;
}


Android的各种dir


目录方法 路径 特点和用途 卸载应用程序时是否会被删除
getDir() /data/data/包名/app_data/自定义目录名 应用程序的私有数据目录,通常用于存储应用程序的数据库文件或其他私有数据。可以指定自定义目录 是
getFilesDir() /data/data/包名/files 应用程序的私有文件目录,通常用于存储应用程序的数据文件。 固定目录 是
getCacheDir() /data/data/包名/cache 应用程序的私有缓存目录,通常用于存储临时文件或缓存数据。 是
getExternalFilesDir() /storage/emulated/0/Android/data/包名/files 应用程序在外部存储设备上的私有文件目录,通常用于存储需要与其他应用程序共享的数据文件。 否
getExternalCacheDir() /storage/emulated/0/Android/data/包名/cache 应用程序在外部存储设备上的缓存目录,通常用于存储临时文件或缓存数据。 否


目录
相关文章
|
数据格式 JSON 存储
Lua模块的加载与内存释放
今天早上听说一件事情让我觉得很诡异的事情:公司线上的一款游戏,加载一份配置资源后,内存涨了几十M,然后内存再也下不来了。因为好奇,所以要来了最大的一个配置文件(4.5M,去除空格与换行后的大小),进行测试。
2625 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8045 0
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17953 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
1天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
755 0
|
7天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1445 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2546 8