COCOS2D-3.9 FileUtils 分析(一)

版本 3.9


FileUtils 类成员变量、方法大概分析。


搜索路径类型:搜索路径 > 相对路径 > 适配路径

静态方法:

static FileUtils* getInstance();
获取 FileUtils 单实例对象 s_sharedFileUtils,并调用init进行初始化。平台相关,在不同的平台CPP文件中定义。
static void destroyInstance();
销毁当前实例
static void setDelegate(FileUtils *delegate);
当继承平台相关 FileUtils 实现自己功能时,通过设置自定义实例对象来调用重写的功能

变量定义整理:

static FileUtils* s_sharedFileUtils;
这是一个当前 FileUtils 静态实例。整个程序中同一时间中只会存在一个。平台相关。
std::string _writablePath;
获取当前可写路径,用于保存游戏更新的数据。平台相关。
mutable std::unordered_map<std::string, std::string> _fullPathCache;
该变量保持可变,用于保存查找文件的路径,当查找一个文件时若在某个路径找到该文件则该路径会被保存,文件名作为 KEY。保证高效获取文件。
std::string _defaultResRootPath;
设置默认的资源存放的文件夹路径。平台相关
std::vector<std::string> _searchPathArray;
设置查找文件时的搜索路径,可以为绝对路径或者为相对于 _defaultResRootPath 的路径。索引值小的优先搜索。
std::vector<std::string> _searchResolutionsOrderArray;
适配文件存放的适配路径
ValueMap _filenameLookupDict;
这是一个 KEY - FILE 的字典,用于通过 KEY 查找文件

protected 方法整理:

virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const;
通过目录和文件名获取绝对路径,若文件不存在则返回一个空字符串。 IOS/MAC 需要重写。
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const;
通过 filename,resolutionDirectory,searchPath 获取绝对路径,若文件不存在则返回一个空字符串。内部调用 getFullPathForDirectoryAndFilename
virtual bool isDirectoryExistInternal(const std::string& dirPath) const;
判断路径是否存在,不考虑搜索路径和适配路径。WIN 没实现,UNIX 实现。
virtual bool isFileExistInternal(const std::string& filename) const = 0;
判断某个文件是否存在,平台相关。
virtual std::string getNewFilename(const std::string &filename) const;
通过 filename 获取文件路径,filename 可以为路径和KEY,FileUtils 中首先在 _filenameLookupDict 查找文件,获取文件路径。函数名字真够吐槽,NEW 表现在哪儿。
virtual bool init();
在获取实例时调用进行初始化,平台相关初始化 _defaultResRootPath 再初始化 _searchPathArray(默认包含_defaultResRootPath) _searchResolutionsOrderArray("")
FileUtils();
保证不能使用构造函数产生实例,初始化_writablePath。


public 方法整理:

virtual void purgeCachedEntries();
清除 _fullPathCache 中保存的所有内容。
virtual std::string getStringFromFile(const std::string& filename);
将整个文件的内容读入 string。
virtual Data getDataFromFile(const std::string& filename);
读取整个文件的二进制数据。
virtual unsigned char* getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size);
读取ZIP文件中的数据。
virtual std::string fullPathForFilename(const std::string &filename) const;
返回 filename 绝对路径,否则 “”
查找顺序:
1、判断是否为绝对路径。是则返回。
2、_fullPathCache 查找,是否曾经使用过被保存在该 MAP 中,是则返回。
3、通过 getNewFilename 获取实际的文件名,通过搜索路径、相对路径、适配路径获取绝对路径。是则返回。
4、最终返回空字符串。
virtual void loadFilenameLookupDictionaryFromFile(const std::string &filename);
通过字典文件,加载设置 _filenameLookupDict 并清空所有的 _fullPathCache
virtual void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);
设置 _filenameLookupDict并清空所有的 _fullPathCache
virtual std::string fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile);
搞不懂、
virtual void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);
设置查找文件的适配路径
virtual void addSearchResolutionsOrder(const std::string &order,const bool front=false);
添加文件的适配搜索路径
virtual const std::vector<std::string>& getSearchResolutionsOrder() const;
获取适配搜索路径数组
virtual void setSearchPaths(const std::vector<std::string>& searchPaths);
设置资源的搜索路径,若为相对路径则追加到默认资源文件夹之后。
void setDefaultResourceRootPath(const std::string& path);
设置默认资源根目录,需要重新初始化路径参数。
void addSearchPath(const std::string & path, const bool front=false);
添加文件的搜索路径。
virtual const std::vector<std::string>& getSearchPaths() const;
获取搜索路径的数组。
virtual std::string getWritablePath() const = 0;
获取可编辑路径
virtual void setWritablePath(const std::string& writablePath);
设置可编辑路径
virtual void setPopupNotify(bool notify);
设置是否在加载错误时提示
virtual bool isPopupNotify() const;
是否在加载错误时提示
virtual ValueMap getValueMapFromFile(const std::string& filename);
从文件中加载字典
virtual ValueMap getValueMapFromData(const char* filedata, int filesize);
从数据流中加载字典
virtual bool writeToFile(ValueMap& dict, const std::string& fullPath);
将一个字典写入文件、
virtual bool writeStringToFile(std::string dataStr, const std::string& fullPath);
将字符串写入文件
virtual bool writeDataToFile(Data retData, const std::string& fullPath);
将数据流写入文件
virtual bool writeValueMapToFile(ValueMap& dict, const std::string& fullPath);
将字典写入文件
virtual bool writeValueVectorToFile(ValueVector vecData, const std::string& fullPath);
将ValueVector写入文件
virtual std::string getSuitableFOpen(const std::string& filenameUtf8) const;
转换平台相关的路径编码
virtual ValueVector getValueVectorFromFile(const std::string& filename);
从一个文件中读取 ValueVector
virtual bool isFileExist(const std::string& filename) const;
判断文件是否存在
virtual std::string getFileExtension(const std::string& filePath) const;
获取文件后缀
virtual bool isAbsolutePath(const std::string& path) const;
判断是否为绝对路径
virtual bool isDirectoryExist(const std::string& dirPath) const;
判断路径是否存在
virtual bool createDirectory(const std::string& dirPath);
创建目录
virtual bool removeDirectory(const std::string& dirPath);
删除目录
virtual bool removeFile(const std::string &filepath);
删除文件
virtual bool renameFile(const std::string &path, const std::string &oldname, const std::string &name);
virtual bool renameFile(const std::string &oldfullpath, const std::string &newfullpath);
从命名文件
virtual long getFileSize(const std::string &filepath);
获取文件大小
const std::unordered_map<std::string, std::string>& getFullPathCache() const { return _fullPathCache; }
获取所有绝对路径的文件路径缓存。当查找到新文件时都会加入该 MAP 中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值