cocos2dx-2.x CCFileUtils文件管理分析(2)

cocos2dx-2.x CCFileUtils文件管理分析(2)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

于1于,我只是对整体结构进行了分析,然后,2于,我会在一些我们经常使用的分析功能。

//获取给定文件名称的全路径 //以下这非常长一段凝视。通过举样例,像我们说明cocos2dx获取文件全路径的规则。 //这段我就不翻译了,直接通过代码来看。 /** Returns the fullpath for a given filename. First it will try to get a new filename from the "filenameLookup" dictionary. If a new filename can't be found on the dictionary, it will use the original filename. Then it will try to obtain the full path of the filename using the CCFileUtils search rules: resolutions, and search paths. The file search is based on the array element order of search paths and resolution directories. For instance: We set two elements("/mnt/sdcard/", "internal_dir/") to search paths vector by setSearchPaths, and set three elements("resources-ipadhd/", "resources-ipad/", "resources-iphonehd") to resolutions vector by setSearchResolutionsOrder. The "internal_dir" is relative to "Resources/". If we have a file named 'sprite.png', the mapping in fileLookup dictionary contains `key: sprite.png -> value: sprite.pvr.gz`. Firstly, it will replace 'sprite.png' with 'sprite.pvr.gz', then searching the file sprite.pvr.gz as follows: /mnt/sdcard/resources-ipadhd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/resources-ipad/sprite.pvr.gz (if not found, search next) /mnt/sdcard/resources-iphonehd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/sprite.pvr.gz (if not found, search next) internal_dir/resources-ipadhd/sprite.pvr.gz (if not found, search next) internal_dir/resources-ipad/sprite.pvr.gz (if not found, search next) internal_dir/resources-iphonehd/sprite.pvr.gz (if not found, search next) internal_dir/sprite.pvr.gz (if not found, return "sprite.png") If the filename contains relative path like "gamescene/uilayer/sprite.png", and the mapping in fileLookup dictionary contains `key: gamescene/uilayer/sprite.png -> value: gamescene/uilayer/sprite.pvr.gz`. The file search order will be: /mnt/sdcard/gamescene/uilayer/resources-ipadhd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/gamescene/uilayer/resources-ipad/sprite.pvr.gz (if not found, search next) /mnt/sdcard/gamescene/uilayer/resources-iphonehd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/gamescene/uilayer/sprite.pvr.gz (if not found, search next) internal_dir/gamescene/uilayer/resources-ipadhd/sprite.pvr.gz (if not found, search next) internal_dir/gamescene/uilayer/resources-ipad/sprite.pvr.gz (if not found, search next) internal_dir/gamescene/uilayer/resources-iphonehd/sprite.pvr.gz (if not found, search next) internal_dir/gamescene/uilayer/sprite.pvr.gz (if not found, return "gamescene/uilayer/sprite.png") If the new file can't be found on the file system, it will return the parameter pszFileName directly. @since v2.1 */ virtual std::string fullPathForFilename(const char* pszFileName); -->> std::string CCFileUtils::fullPathForFilename(const char* pszFileName){ CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path"); //推断是否是绝对路径,假设是绝对路径就直接返回。 /* //android下 推断根据就是是否以'/'开头或者以assets/开头。

以下这个函数。凝视的非常清楚。 //你能够做个实验: //例: Get data from file(/second_bg.png) failed! 我在创建精灵时传递/second_bg.png路径 bool CCFileUtilsAndroid::isAbsolutePath(const std::string& strPath) { // On Android, there are two situations for full path. // 1) Files in APK, e.g. assets/path/path/file.png // 2) Files not in APK, e.g. /data/data/org.cocos2dx.hellocpp/cache/path/path/file.png, or /sdcard/path/path/file.png. // So these two situations need to be checked on Android. if (strPath[0] == '/' || strPath.find(m_strDefaultResRootPath) == 0) { return true; } return false; } */ std::string strFileName = pszFileName; if (isAbsolutePath(pszFileName)) { //CCLOG("Return absolute path( %s ) directly.", pszFileName); return pszFileName; } // Already Cached ? //是否已经缓存。假设缓存过,直接返回 std::map<std::string, std::string>::iterator cacheIter = m_fullPathCache.find(pszFileName); if (cacheIter != m_fullPathCache.end()) { //CCLOG("Return full path from cache: %s", cacheIter->second.c_str()); return cacheIter->second; } /* std::string CCFileUtils::getNewFilename(const char* pszFileName) { const char* pszNewFileName = NULL; // in Lookup Filename dictionary ?

//能够把这个m_pFilenameLookupDict(默觉得NULL)字典理解为一种查找 //比方这个字典里存了一个"fish.png(key)" --> "big_fish.png(value)" //那么我们传入fish.png是,就会给我们转化为big_fish.png。

假设没有,则返回我们传入的。

CCString* fileNameFound = m_pFilenameLookupDict ?

(CCString*)m_pFilenameLookupDict->objectForKey(pszFileName) : NULL; if( NULL == fileNameFound || fileNameFound->length() == 0) { pszNewFileName = pszFileName; } else { pszNewFileName = fileNameFound->getCString(); //CCLOG("FOUND NEW FILE NAME: %s.", pszNewFileName); } return pszNewFileName; } */ // Get the new file name. std::string newFilename = getNewFilename(pszFileName); string fullpath = ""; //以下这一段非常关键: //m_searchPathArray 前面介绍过搜索路径数组,须要我们手动设置。

android的初始话会加入一个默认值为 //m_searchPathArray.push_back(m_strDefaultResRootPath)即,"assets/"。 /* m_searchResolutionsOrderArray 能够理解为分辨率搜索顺序,就按开头凝视说明的那样 //m_searchPathArray We set two elements("/mnt/sdcard/", "internal_dir/") to search paths vector by setSearchPaths, //m_searchResolutionsOrderArray and set three elements("resources-ipadhd/", "resources-ipad/", "resources-iphonehd") to resolutions vector by setSearchResolutionsOrder. //组合后的路径 /mnt/sdcard/resources-ipadhd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/resources-ipad/sprite.pvr.gz (if not found, search next) /mnt/sdcard/resources-iphonehd/sprite.pvr.gz (if not found, search next) 总结:从这里能够看出,m_searchPathArray在前面的路径,会优先搜索。m_searchResolutionsOrderArray也一样。 */ for (std::vector<std::string>::iterator searchPathsIter = m_searchPathArray.begin(); searchPathsIter != m_searchPathArray.end(); ++searchPathsIter) { for (std::vector<std::string>::iterator resOrderIter = m_searchResolutionsOrderArray.begin(); resOrderIter != m_searchResolutionsOrderArray.end(); ++resOrderIter) { //CCLOG("\n\nSEARCHING: %s, %s, %s", newFilename.c_str(), resOrderIter->c_str(), searchPathsIter->c_str()); //以下我分析一下这个函数:-->> 2 fullpath = this->getPathForFilename(newFilename, *resOrderIter, *searchPathsIter); //这里会对找到的路径,进行缓存 if (fullpath.length() > 0) { // Using the filename passed in as key. m_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath)); //CCLOG("Returning path: %s", fullpath.c_str()); return fullpath; } } } //CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", pszFileName); // The file wasn't found, return the file name passed in. return pszFileName;}--> 2//filename -- 传入的文件名称//searchPath -- 搜索路径//resolutionDirectory -- 资源分辨率路径std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath){ std::string file = filename; std::string file_path = ""; size_t pos = filename.find_last_of("/"); if (pos != std::string::npos) { file_path = filename.substr(0, pos+1); file = filename.substr(pos+1); } //假设传入的"gamescene/uilayer/sprite.png"是这种路径。那么进行一定的处理, //处理成:path = searchPath + gamescene/uilayer/ + resourceDirectory file = sprite.png ///mnt/sdcard/ gamescene/uilayer/ resources-ipadhd/sprite.pvr.gz // searchPath + file_path + resourceDirectory std::string path = searchPath; path += file_path; path += resolutionDirectory; path = getFullPathForDirectoryAndFilename(path, file); //CCLOG("getPathForFilename, fullPath = %s", path.c_str()); return path;}-->>std::string CCFileUtils::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename){ std::string ret = strDirectory+strFilename; //假设文件存在。就把文件的路径返回,这个路径可能是绝对路径,也可能是包里的路径 if (!isFileExist(ret)) { ret = ""; } return ret;}-->>//把上面合成的整个文件路径传进去,推断文件是否存在bool CCFileUtilsAndroid::isFileExist(const std::string& strFilePath){ if (0 == strFilePath.length()) { return false; } bool bFound = false; // Check whether file exists in apk. //假设不是以'/'开头,就在android包里查找 if (strFilePath[0] != '/') { //假设不是以"assets/"开头,则插入 std::string strPath = strFilePath; if (strPath.find(m_strDefaultResRootPath) != 0) {// Didn't find "assets/" at the beginning of the path, adding it. strPath.insert(0, m_strDefaultResRootPath); } //在安装包里查找,看是否存在 if (s_pZipFile->fileExists(strPath)) { bFound = true; } } else { //假设是绝对路径,看否打开成功。假设成功,则证明文件存在。

FILE *fp = fopen(strFilePath.c_str(), "r"); if(fp) { bFound = true; fclose(fp); } } return bFound;}总结:这里须要知道一点。就是先载入搜索路径的路径,会优先搜索。 例如热更新,我们只是想更新的路径在你面前的设置就可以。

版权声明:原创文章,转载请注明出处。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116935.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 11种经典滤波算法「建议收藏」

    11种经典滤波算法「建议收藏」1、限幅滤波法(又称程序判断滤波法)  A、方法:      根据经验判断,确定两次采样允许的最大偏差值(设为A)      每次检测到新值时判断:      如果本次值与上次值之差&lt;=A,则本次值有效      如果本次值与上次值之差&gt;A,则本次值无效,放弃本次值,用上次值代替本次值  B、优点:      能有效克服因偶然因素引起的脉冲干扰…

    2022年5月3日
    70
  • eagleeye_EagleEye简介:户外视频监控分析和面部识别软件

    eagleeye_EagleEye简介:户外视频监控分析和面部识别软件eagleeye我还进行了大量研究,并使用各种机器学习方法开发了该软件系统。我已经在这个项目上花费了大约一年的时间,以为当地的州政府实施这项技术。不幸的是它没有实现。但是我有兴趣为开源社区做出贡献。它可以准确地识别,分割和识别视频源中的对象(视频源中人的92种语义属性)。最有趣的部分是我们对来自街头闭路电视摄像机的野外镜头的面部识别的准确性。EagleEye是基于真实人工智能的…

    2022年8月16日
    6
  • intellijidea激活码2022[最新免费获取][通俗易懂]

    (intellijidea激活码2022)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月31日
    211
  • smt贴片元件封装型号及名称_贴片封装的电子元件图片

    smt贴片元件封装型号及名称_贴片封装的电子元件图片SMT贴片元器件封装类型的识别#学习目标:封装类型是元件的外观尺寸和形状的集合,它是元件的重要属性之一。相同电子参数的元件可能有不同的封装类型。厂家按照相应封装标准生产元件以保证元件的装配使用和特殊用途。由于封装技术日新月异且封装代码暂无唯一标准,本指导只给出通用的电子元件封装类型和图示,与SMT工序无关的封装暂不涉及。1、常见SMT封装以公司内部产品所用元件为例,如下表:名称 缩写含义 备注Chip Chip 片式元件MLD MoldedBody 模制本体元件CAE Aluminum

    2022年8月21日
    7
  • PHP审计之Duomicms审计

    PHP审计之Duomicms审计前言废话不多说,开冲代码审计任意文件写入定位漏洞代码admin/admin_ping.phpif($action=="set"){$

    2021年12月13日
    47
  • 【已解决】MySQL Connector Net 卸载不了问题?

    【已解决】MySQL Connector Net 卸载不了问题?今天mysql出现了一些问题,想要全部卸载重新安装,控制面板中右键卸载,发现MySQLConnectorNet无法卸载。百度上搜索发现回答都是复制粘贴,千篇一律,都是检查C盘文件是否删除干净,还有就是注册表是否删除干净;使用这些方法均不能完成卸载,重装mysql。不断搜索发现一方法可行进行分享:1.微软的支持里面有一个Fixproblemsthatblockprogramsfrombeinginstalledorremoved,链接https://support.micros

    2022年7月25日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号