COCOS2D-3.9 FileUtils 分析(二) 变量深入

本文详细分析了COCOS2D-3.9版本中关于文件系统的重要变量,包括_defaultResRootPath、_searchPathArray、_searchResolutionsOrderArray和_fullPathCache的使用和操作,揭示了资源查找和路径管理的内部机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

std::string _defaultResRootPath;

作用 默认的资源文件的第一级目录,资源打包时都放入这个目录。

virtual bool init();
在获取单实例对象时,会先调用子类该方法,初始化 _defaultResRootPath ,然后调用父类该方法完成 _searchPathArray和_searchResolutionsOrderArray初始化
_searchPathArray 中添加 _defaultResRootPath 用A于基本的资源搜索路径。
_searchResolutionsOrderArray 初始化为空字符串。(这个空字符串相对来说在搜索时也是一个路径)

_searchPathArray的相关操作:

virtual void setSearchPaths(const std::vector<std::string>& searchPaths);
用于 _searchPathArray 的设置,传入一个绝对或者相对路径数组。
在调用该函数时,首先清空 _fullPathCache _searchPathArray 容器中的内容,保证不会出现旧数据。
然后将 searchPaths 中的类容添加到 _searchPathArray,添加时需要判断是否为绝对路径,
若为绝对路径则直接将类容添加到 _searchPathArray 中,否则添加以 _defaultResRootPath 目录的相对路径,
判断该添加的路径是否为 _defaultResRootPath,保证有且只有一个 _defaultResRootPath

void addSearchPath(const std::string & path, const bool front=false);
单个的添加搜索路径到 _searchPathArray , 并标记优先级。若 path 为空则会重复添加 _defaultResRootPath

_searchResolutionsOrderArray的相关操作:

virtual void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);
用于 _searchResolutionsOrderArray 的设置,传入一个相对的路径数组。
在调用该函数时,首先清空 _fullPathCache _searchResolutionsOrderArray 容器中的内容,保证不会出现旧数据。
然后将 searchResolutionsOrder 中的类容添加到 _searchResolutionsOrderArray,添加时需要判断是否添加了空路径,
保证空路径在 _searchResolutionsOrderArray 中

virtual void addSearchResolutionsOrder(const std::string &order,const bool front=false);
单个的添加资源适配路径到 _searchResolutionsOrderArray,并标记优先级。若 order 为空则会重复添加 空路径。

_filenameLookupDict 的相关操作:

这是一个通过文件名查找文件路径的集合, FILE-PATH 的对应关系。
virtual void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);
首先清空 _fullPathCache 保证数据及时更新, _filenameLookupDict 赋值为最新字典数据。
filenameLookupDict 则从字典数据文件中解析获取代码设置。

_fullPathCache 相关调用

作用:用于保存相关文件的绝对路径的 KEY-VALUE,通过文件名直接获取路径,加快文件搜索。

mutable std::unordered_map<std::string, std::string> _fullPathCache;
随时都可变的的 unordered_map 类型的数据。保证数据任意地方都能更新。

virtual void purgeCachedEntries();
该函数作用是清空所有缓存的绝对路径。 _fullPathCache 清空

virtual void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);
设置适配搜索路径,由于文件的绝对路径可能会变所以清空 _fullPathCache
virtual void setSearchPaths(const std::vector<std::string>& searchPaths);
设置资源搜索路径,由于文件的绝对路径可能会变所以清空 _fullPathCache
virtual void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);
设置文件的搜索字典,由于文件的绝对路径可能会变所以清空 _fullPathCache
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值