Cocos2dx-2.x CCFileUtils File Management Analysis (2)

Source: Internet
Author: User

Cocos2dx-2.x CCFileUtils File Management Analysis (2)

In 1, I only analyzed the overall structure, so in 2, I will analyze some of our commonly used functions. // Obtain the full path of the given file name. // The following long comment shows how to obtain the full path of the file by cocos2dx. // I will not translate this section, but I can see it directly through the code. /** 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 pa Ths 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 done', 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, se Arch next)/mnt/sdcard/gamescene/uilayer/else (if not found, search next) internal_dir/gamescene/uilayer/resources-ipadhd/sprite.pvr.gz (if not found, next search) internal_dir/gamescene/uilayer/resources-ipad/templates (if not found, search next) internal_dir/gamescene/uilayer/resources-iphonehd/sprite.pvr.gz (if not found, next search) internal_dir/gamescene/uilayer/sprite.pvr.gz (I F 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"); // determines whether it is an absolute path. If it is an absolute path, it is returned directly. /* // The android system determines whether to start with '/' or assets. Note the following function clearly. // You can make an experiment: // example: Get data from file (/second_bg.png) failed! When creating the Genie, I pass the/second_bg.png path bool CCFileUtilsAndroid: isAbsolutePath (const std: string & strPath) {// On Android, there are two situations for full path. // 1) Files in APK, e.g. assets/path/file.png // 2) Files not in APK, e.g. /data/org. cocos2dx. hellocpp/cache/path/file.png, or/sdcard/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? // Whether the database has been cached. If the database has been cached, std: map is returned directly.
 
  
: 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? // The m_pFilenameLookupDict (NULL by default) dictionary can be considered as a kind of query // For example, the dictionary contains a "fish.png (key)" --> "big_fish.png (value) "// if we pass it to fish.png, it will convert it to big_fish.png. If no, the input is returned. 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 = ""; // below This section is critical: // m_searchPathArray previously introduced the search path array, which needs to be set manually. The default value of "assets/" is // m_searchPathArray.push_back (m_strDefaultResRootPath /". /* M_searchResolutionsOrderArray can be understood as the resolution search order, as described in the first comment. // m_searchPathArrayWe 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. // path after combination/mnt/sdcard/resources-ipadhd/sprite.pvr.gz (if not f Ound, 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) Conclusion: From here we can see that m_searchPathArray will first search for the path above, and m_searchResolutionsOrderArray will also be the same. */For (std: vector
  
   
: Iterator searchPathsIter = m_searchPathArray.begin (); searchPathsIter! = M_searchPathArray.end (); ++ searchPathsIter) {for (std: vector
   
    
: 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 (); // I will analyze this function below: --> 2 fullpath = this-> getPathForFilename (newFilename, * resOrderIter, * searchPathsIter); // cache the found path if (fullpath. length ()> 0) {// Using the filename passed in as key. m_fullPathCache.insert (std: pair
    
     
(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 -- input file name // searchPath -- search path // resolutionDirectory -- Resource resolution path 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);} // If the input "gamescene/uilayer/sprite.png" is such a path, it must be processed. // process it: path = searchPath + gamescene/uilayer/+ resourceDirectory file = sprite.png // mnt/sdcard/gamescene/uilayer/resources-ipadhd/issue // 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 the file exists, return the path of the file. This path may be an absolute path or the path in the package if (! IsFileExist (ret) {ret = "";} return ret;} --> // upload the entire file path synthesized above to determine whether the file exists with bool CCFileUtilsAndroid :: isFileExist (const std: string & strFilePath) {if (0 = strFilePath. length () {return false;} bool bFound = false; // Check whether file exists in apk. // if it does not start with '/', search for if (strFilePath [0] In the android package. = '/') {// If it does not start with "assets/", insert 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);} // In the installation package, check whether if (s_pZipFile-> fileExists (strPath) {bFound = true ;}} else {// if it is an absolute path, check whether it is successfully opened. If it is successful, it indicates that the file exists. FILE * fp = fopen (strFilePath. c_str (), "r"); if (fp) {bFound = true; fclose (fp) ;}} return bFound ;}summary: here you need to know a bit, that is, first load the path of the search path, and the search will be given priority. For example, for hot update, we only need to set the update path to the front.
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.