Helper class to handle file operations. More...
Public Member Functions | |
virtual | ~FileUtils () |
The destructor of FileUtils. More... | |
virtual void | purgeCachedEntries () |
Purges full path caches. | |
virtual std::string | getStringFromFile (const std::string &filename) const |
Gets string from a file. | |
virtual void | getStringFromFile (const std::string &path, std::function< void(std::string)> callback) const |
Gets string from a file, async off the main cocos thread. More... | |
virtual Data | getDataFromFile (const std::string &filename) const |
Creates binary data from a file. More... | |
virtual void | getDataFromFile (const std::string &filename, std::function< void(Data)> callback) const |
Gets a binary data object from a file, async off the main cocos thread. More... | |
template<typename T , typename Enable = typename std::enable_if< std::is_base_of< ResizableBuffer, ResizableBufferAdapter<T> >::value >::type> | |
Status | getContents (const std::string &filename, T *buffer) const |
Gets whole file contents as string from a file. More... | |
virtual unsigned char * | getFileDataFromZip (const std::string &zipFilePath, const std::string &filename, ssize_t *size) const |
Gets resource file data from a zip file. More... | |
virtual std::string | fullPathForFilename (const std::string &filename) const |
Returns the fullpath for a given filename. More... | |
virtual void | loadFilenameLookupDictionaryFromFile (const std::string &filename) |
Loads the filenameLookup dictionary from the contents of a filename. More... | |
virtual void | setFilenameLookupDictionary (const ValueMap &filenameLookupDict) |
Sets the filenameLookup dictionary. More... | |
virtual std::string | fullPathFromRelativeFile (const std::string &filename, const std::string &relativeFile) const |
Gets full path from a file name and the path of the relative file. More... | |
virtual void | setSearchResolutionsOrder (const std::vector< std::string > &searchResolutionsOrder) |
Sets the array that contains the search order of the resources. More... | |
virtual void | addSearchResolutionsOrder (const std::string &order, const bool front=false) |
Append search order of the resources. More... | |
virtual const std::vector< std::string > | getSearchResolutionsOrder () const |
Gets the array that contains the search order of the resources. More... | |
virtual void | setSearchPaths (const std::vector< std::string > &searchPaths) |
Sets the array of search paths. More... | |
const std::string | getDefaultResourceRootPath () const |
Get default resource root path. | |
void | setDefaultResourceRootPath (const std::string &path) |
Set default resource root path. | |
void | addSearchPath (const std::string &path, const bool front=false) |
Add search path. More... | |
virtual const std::vector< std::string > | getSearchPaths () const |
Gets the array of search paths. More... | |
virtual const std::vector< std::string > | getOriginalSearchPaths () const |
Gets the original search path array set by 'setSearchPaths' or 'addSearchPath'. More... | |
virtual std::string | getWritablePath () const =0 |
Gets the writable path. More... | |
virtual void | setWritablePath (const std::string &writablePath) |
Sets writable path. | |
virtual void | setPopupNotify (bool notify) |
Sets whether to pop-up a message box when failed to load an image. | |
virtual bool | isPopupNotify () const |
Checks whether to pop up a message box when failed to load an image. More... | |
virtual ValueMap | getValueMapFromFile (const std::string &filename) const |
Converts the contents of a file to a ValueMap. More... | |
virtual ValueMap | getValueMapFromData (const char *filedata, int filesize) const |
Converts the contents of a file to a ValueMap. More... | |
virtual bool | writeToFile (const ValueMap &dict, const std::string &fullPath) const |
write a ValueMap into a plist file More... | |
virtual bool | writeStringToFile (const std::string &dataStr, const std::string &fullPath) const |
write a string into a file More... | |
virtual void | writeStringToFile (std::string dataStr, const std::string &fullPath, std::function< void(bool)> callback) const |
Write a string to a file, done async off the main cocos thread Use this function if you need file access without blocking the main thread. More... | |
virtual bool | writeDataToFile (const Data &data, const std::string &fullPath) const |
write Data into a file More... | |
virtual void | writeDataToFile (Data data, const std::string &fullPath, std::function< void(bool)> callback) const |
Write Data into a file, done async off the main cocos thread. More... | |
virtual bool | writeValueMapToFile (const ValueMap &dict, const std::string &fullPath) const |
write ValueMap into a plist file More... | |
virtual void | writeValueMapToFile (ValueMap dict, const std::string &fullPath, std::function< void(bool)> callback) const |
Write a ValueMap into a file, done async off the main cocos thread. More... | |
virtual bool | writeValueVectorToFile (const ValueVector &vecData, const std::string &fullPath) const |
write ValueVector into a plist file More... | |
virtual void | writeValueVectorToFile (ValueVector vecData, const std::string &fullPath, std::function< void(bool)> callback) const |
Write a ValueVector into a file, done async off the main cocos thread. More... | |
virtual std::string | getSuitableFOpen (const std::string &filenameUtf8) const |
Windows fopen can't support UTF-8 filename Need convert all parameters fopen and other 3rd-party libs. More... | |
virtual bool | isFileExist (const std::string &filename) const |
Checks whether a file exists. More... | |
virtual void | isFileExist (const std::string &filename, std::function< void(bool)> callback) const |
Checks if a file exists, done async off the main cocos thread. More... | |
virtual std::string | getFileExtension (const std::string &filePath) const |
Gets filename extension is a suffix (separated from the base filename by a dot) in lower case. More... | |
virtual bool | isAbsolutePath (const std::string &path) const |
Checks whether the path is an absolute path. More... | |
virtual bool | isDirectoryExist (const std::string &dirPath) const |
Checks whether the path is a directory. More... | |
virtual void | isDirectoryExist (const std::string &fullPath, std::function< void(bool)> callback) const |
Checks whether the absoulate path is a directory, async off of the main cocos thread. More... | |
virtual bool | createDirectory (const std::string &dirPath) const |
Creates a directory. More... | |
virtual void | createDirectory (const std::string &dirPath, std::function< void(bool)> callback) const |
Create a directory, async off the main cocos thread. More... | |
virtual bool | removeDirectory (const std::string &dirPath) const |
Removes a directory. More... | |
virtual void | removeDirectory (const std::string &dirPath, std::function< void(bool)> callback) const |
Removes a directory, async off the main cocos thread. More... | |
virtual bool | removeFile (const std::string &filepath) const |
Removes a file. More... | |
virtual void | removeFile (const std::string &filepath, std::function< void(bool)> callback) const |
Removes a file, async off the main cocos thread. More... | |
virtual bool | renameFile (const std::string &path, const std::string &oldname, const std::string &name) const |
Renames a file under the given directory. More... | |
virtual void | renameFile (const std::string &path, const std::string &oldname, const std::string &name, std::function< void(bool)> callback) const |
Renames a file under the given directory, async off the main cocos thread. More... | |
virtual bool | renameFile (const std::string &oldfullpath, const std::string &newfullpath) const |
Renames a file under the given directory. More... | |
virtual void | renameFile (const std::string &oldfullpath, const std::string &newfullpath, std::function< void(bool)> callback) const |
Renames a file under the given directory, async off the main cocos thread. More... | |
virtual long | getFileSize (const std::string &filepath) const |
Retrieve the file size. More... | |
virtual void | getFileSize (const std::string &filepath, std::function< void(long)> callback) const |
Retrieve the file size, async off the main cocos thread. More... | |
virtual std::vector< std::string > | listFiles (const std::string &dirPath) const |
List all files in a directory. More... | |
virtual void | listFilesAsync (const std::string &dirPath, std::function< void(std::vector< std::string >)> callback) const |
List all files in a directory async, off of the main cocos thread. More... | |
virtual void | listFilesRecursively (const std::string &dirPath, std::vector< std::string > *files) const |
List all files recursively in a directory. More... | |
virtual void | listFilesRecursivelyAsync (const std::string &dirPath, std::function< void(std::vector< std::string >)> callback) const |
List all files recursively in a directory, async off the main cocos thread. More... | |
const std::unordered_map< std::string, std::string > | getFullPathCache () const |
Returns the full path cache. | |
virtual std::string | getNewFilename (const std::string &filename) const |
Gets the new filename from the filename lookup dictionary. More... | |
Static Public Member Functions | |
static FileUtils * | getInstance () |
Gets the instance of FileUtils. | |
static void | destroyInstance () |
Destroys the instance of FileUtils. | |
static void | setDelegate (FileUtils *delegate) |
You can inherit from platform dependent implementation of FileUtils, such as FileUtilsAndroid, and use this function to set delegate, then FileUtils will invoke delegate's implementation. More... | |
Helper class to handle file operations.
|
static |
You can inherit from platform dependent implementation of FileUtils, such as FileUtilsAndroid, and use this function to set delegate, then FileUtils will invoke delegate's implementation.
For example, your resources are encrypted, so you need to decrypt it after reading data from resources, then you can implement all getXXX functions, and engine will invoke your own getXX functions when reading data of resources.
If you don't want to system default implementation after setting delegate, you can just pass nullptr to this function.
|
virtual |
Gets string from a file, async off the main cocos thread.
path | filepath for the string to be read. Can be relative or absolute path |
callback | Function that will be called when file is read. Will be called on the main cocos thread. |
|
virtual |
Creates binary data from a file.
|
virtual |
Gets a binary data object from a file, async off the main cocos thread.
filename | filepath for the data to be read. Can be relative or absolute path |
callback | Function that will be called when file is read. Will be called on the main cocos thread. |
|
inline |
Gets whole file contents as string from a file.
Unlike getStringFromFile, these getContents methods:
The template version of can accept cocos2d::Data, std::basic_string and std::vector.
Note: if you read to std::vector<T> and std::basic_string<T> where T is not 8 bit type, you may get 0 ~ sizeof(T)-1 bytes padding.
[in] | filename | The resource file name which contains the path. |
[out] | buffer | The buffer where the file contents are store to. |
|
virtual |
Gets resource file data from a zip file.
[in] | filename | The resource file name which contains the relative path of the zip file. |
[out] | size | If the file read operation succeeds, it will be the data size, otherwise 0. |
|
virtual |
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 FileUtils 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 filename directly.
This method was added to simplify multiplatform support. Whether you are using cocos2d-js or any cross-compilation toolchain like StellaSDK or Apportable, you might need to load different resources for a given file in the different platforms.
|
virtual |
Loads the filenameLookup dictionary from the contents of a filename.
filename | The plist file name. |
|
virtual |
Sets the filenameLookup dictionary.
filenameLookupDict | The dictionary for replacing filename. |
|
virtual |
Gets full path from a file name and the path of the relative file.
filename | The file name. |
relativeFile | The path of the relative file. |
|
virtual |
Sets the array that contains the search order of the resources.
searchResolutionsOrder | The source array that contains the search order of the resources. |
|
virtual |
Append search order of the resources.
|
virtual |
Gets the array that contains the search order of the resources.
|
virtual |
Sets the array of search paths.
You can use this array to modify the search path of the resources. If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array.
searchPaths | The array contains search paths. |
void addSearchPath | ( | const std::string & | path, |
const bool | front = false |
||
) |
Add search path.
|
virtual |
Gets the array of search paths.
|
virtual |
Gets the original search path array set by 'setSearchPaths' or 'addSearchPath'.
|
pure virtual |
Gets the writable path.
|
virtual |
Checks whether to pop up a message box when failed to load an image.
|
virtual |
Converts the contents of a file to a ValueMap.
filename | The filename of the file to gets content. |
|
virtual |
Converts the contents of a file to a ValueMap.
This method is used internally.
|
virtual |
write a ValueMap into a plist file
dict | the ValueMap want to save |
fullPath | The full path to the file you want to save a string |
|
virtual |
write a string into a file
dataStr | the string want to save |
fullPath | The full path to the file you want to save a string |
|
virtual |
Write a string to a file, done async off the main cocos thread Use this function if you need file access without blocking the main thread.
This function takes a std::string by value on purpose, to leverage move sematics. If you want to avoid a copy of your datastr, use std::move/std::forward if appropriate
dataStr | the string want to save |
fullPath | The full path to the file you want to save a string |
callback | The function called once the string has been written to a file. This function will be executed on the main cocos thread. It will have on boolean argument signifying if the write was successful. |
|
virtual |
write Data into a file
data | the data want to save |
fullPath | The full path to the file you want to save a string |
|
virtual |
Write Data into a file, done async off the main cocos thread.
Use this function if you need to write Data while not blocking the main cocos thread.
This function takes Data by value on purpose, to leverage move sematics. If you want to avoid a copy of your data, use std::move/std::forward if appropriate
data | The data that will be written to disk |
fullPath | The absolute file path that the data will be written to |
callback | The function that will be called when data is written to disk. This function will be executed on the main cocos thread. It will have on boolean argument signifying if the write was successful. |
|
virtual |
write ValueMap into a plist file
dict | the ValueMap want to save |
fullPath | The full path to the file you want to save a string |
|
virtual |
Write a ValueMap into a file, done async off the main cocos thread.
Use this function if you need to write a ValueMap while not blocking the main cocos thread.
This function takes ValueMap by value on purpose, to leverage move sematics. If you want to avoid a copy of your dict, use std::move/std::forward if appropriate
dict | The ValueMap that will be written to disk |
fullPath | The absolute file path that the data will be written to |
callback | The function that will be called when dict is written to disk. This function will be executed on the main cocos thread. It will have on boolean argument signifying if the write was successful. |
|
virtual |
write ValueVector into a plist file
vecData | the ValueVector want to save |
fullPath | The full path to the file you want to save a string |
|
virtual |
Write a ValueVector into a file, done async off the main cocos thread.
Use this function if you need to write a ValueVector while not blocking the main cocos thread.
This function takes ValueVector by value on purpose, to leverage move sematics. If you want to avoid a copy of your dict, use std::move/std::forward if appropriate
vecData | The ValueVector that will be written to disk |
fullPath | The absolute file path that the data will be written to |
callback | The function that will be called when vecData is written to disk. This function will be executed on the main cocos thread. It will have on boolean argument signifying if the write was successful. |
|
virtual |
Windows fopen can't support UTF-8 filename Need convert all parameters fopen and other 3rd-party libs.
filenameUtf8 | std::string name file for conversion from utf-8 |
|
virtual |
Checks whether a file exists.
filename | The path of the file, it could be a relative or absolute path. |
|
virtual |
Checks if a file exists, done async off the main cocos thread.
Use this function if you need to check if a file exists while not blocking the main cocos thread.
filename | The path of the file, it could be a relative or absolute path. |
callback | The function that will be called when the operation is complete. Will have one boolean argument, true if the file exists, false otherwise. |
|
virtual |
Gets filename extension is a suffix (separated from the base filename by a dot) in lower case.
Examples of filename extensions are .png, .jpeg, .exe, .dmg and .txt.
filePath | The path of the file, it could be a relative or absolute path. |
|
virtual |
Checks whether the path is an absolute path.
path | The path that needs to be checked. |
|
virtual |
Checks whether the path is a directory.
dirPath | The path of the directory, it could be a relative or an absolute path. |
|
virtual |
Checks whether the absoulate path is a directory, async off of the main cocos thread.
dirPath | The path of the directory, it must be an absolute path |
callback | that will accept a boolean, true if the file exists, false otherwise. Callback will happen on the main cocos thread. |
|
virtual |
Creates a directory.
dirPath | The path of the directory, it must be an absolute path. |
|
virtual |
Create a directory, async off the main cocos thread.
dirPath | the path of the directory, it must be an absolute path |
callback | The function that will be called when the operation is complete. Will have one boolean argument, true if the directory was successfully, false otherwise. |
|
virtual |
Removes a directory.
dirPath | The full path of the directory, it must be an absolute path. |
|
virtual |
Removes a directory, async off the main cocos thread.
dirPath | the path of the directory, it must be an absolute path |
callback | The function that will be called when the operation is complete. Will have one boolean argument, true if the directory was successfully removed, false otherwise. |
|
virtual |
Removes a file.
filepath | The full path of the file, it must be an absolute path. |
|
virtual |
Removes a file, async off the main cocos thread.
filepath | the path of the file to remove, it must be an absolute path |
callback | The function that will be called when the operation is complete. Will have one boolean argument, true if the file was successfully removed, false otherwise. |
|
virtual |
Renames a file under the given directory.
path | The parent directory path of the file, it must be an absolute path. |
oldname | The current name of the file. |
name | The new name of the file. |
|
virtual |
Renames a file under the given directory, async off the main cocos thread.
path | The parent directory path of the file, it must be an absolute path. |
oldname | The current name of the file. |
name | The new name of the file. |
callback | The function that will be called when the operation is complete. Will have one boolean argument, true if the file was successfully renamed, false otherwise. |
|
virtual |
Renames a file under the given directory.
oldfullpath | The current fullpath of the file. Includes path and name. |
newfullpath | The new fullpath of the file. Includes path and name. |
|
virtual |
Renames a file under the given directory, async off the main cocos thread.
oldfullpath | The current fullpath of the file. Includes path and name. |
newfullpath | The new fullpath of the file. Includes path and name. |
callback | The function that will be called when the operation is complete. Will have one boolean argument, true if the file was successfully renamed, false otherwise. |
|
virtual |
Retrieve the file size.
filepath | The path of the file, it could be a relative or absolute path. |
|
virtual |
Retrieve the file size, async off the main cocos thread.
filepath | The path of the file, it could be a relative or absolute path. |
callback | The function that will be called when the operation is complete. Will have one long argument, the file size. |
|
virtual |
List all files in a directory.
dirPath | The path of the directory, it could be a relative or an absolute path. |
|
virtual |
List all files in a directory async, off of the main cocos thread.
dirPath | The path of the directory, it could be a relative or an absolute path. |
callback | The callback to be called once the list operation is complete. Will be called on the main cocos thread. @js NA @lua NA |
|
virtual |
List all files recursively in a directory.
dirPath | The path of the directory, it could be a relative or an absolute path. |
|
virtual |
List all files recursively in a directory, async off the main cocos thread.
dirPath | The path of the directory, it could be a relative or an absolute path. |
callback | The callback to be called once the list operation is complete. Will be called on the main cocos thread. @js NA @lua NA |
|
virtual |
Gets the new filename from the filename lookup dictionary.
It is possible to have a override names.
filename | The original filename. |