COCOS2D-3.9 FileUtils 分析(三) 文件查找深入

来源:互联网 发布:做微信推送的软件 编辑:程序博客网 时间:2024/05/16 12:16

文件读取过程:

以该函数为例:
static Data getData(const std::string& filename, bool forString)
判断 filename 是否为空,是空则返回空DATA,否则继续。
std::string FileUtils::fullPathForFilename(const std::string &filename) const
通过该函数获取文件的绝对路径。然后进行常规的文件读取。

fullPathForFilename 在整个文件查找中的逻辑:
首先判断是否为绝对路径,若为绝对路径直接返回,否则继续。 isAbsolutePath 这是一个平台相关的函数,每个平台不一样。

_fullPathCache 开始用来增加效率。
若不为绝对路径,则在 _fullPathCache 中查找,是否曾经查找过该文件,若查找过就返回,否则继续。

既然 _fullPathCache 中找不到文件绝对路径那么就应该是一个新的文件路径,开始调用
virtual std::string getNewFilename(const std::string &filename) const;
_filenameLookupDict 在该函数中用于查找配置了的文件路径,若找到则返回配置的路径,这个路径只是配置的路径,不是绝对路径,否则返回 filename

到目前文件还没找到,只有通过 _searchPathArray 和 _searchResolutionsOrderArray 以及返回的 newFilename 组合查找。

std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
这个函数名好长...
分别传入 newFilename _searchResolutionsOrderArray中的一个 _searchPathArray中的一个。这个顺序也是很有意义的。
newFilename需要查找的文件,可能带有相对路径。 file 文件名 相对路径 path
_searchResolutionsOrderArray 适配分辨率路径中的一个
_searchPathArray 搜索路径中的一个

最后查找文件的路径构成为 _searchPathArray/_searchResolutionsOrderArray/newFilename 或者 _searchPathArray/path/_searchResolutionsOrderArray/file

进入函数首先就将 newFilename 分解为 path 和 file 两部分。当然 path 可能为 ""
然后按照前面结构开始去找文件 : searchPath + file_path + resourceDirectory,判断文件是否存在。
virtual bool isFileExistInternal(const std::string& filename) const = 0;
在个平台中判断文件是否存在,只考虑绝对路径和相对于资源文件的路径查找,若是绝对路径这直接判断,否则 _defaultResRootPath 为根路径。


_fullPathCache.insert(std::make_pair(filename, fullpath)); 

最后将查找到问文件加入 _fullPathCache 缓存起来。

"" 路径的作用:

在 _searchPathArray 和 _searchResolutionsOrderArray 相对搜索路径设置中都包含该路径。
在路径搜索时使用该路径则表示使用根路径,用于根路径的匹配,不会跳过根路径的搜索
1 0