2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2013-02-01 11:20:46 +08:00
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-02-01 11:20:46 +08:00
|
|
|
#ifndef __CC_FILEUTILS_H__
|
|
|
|
#define __CC_FILEUTILS_H__
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccTypes.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCValue.h"
|
|
|
|
#include "base/CCData.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-06-01 13:43:56 +08:00
|
|
|
* @addtogroup platform
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-03-24 10:34:44 +08:00
|
|
|
/** Helper class to handle file operations. */
|
2013-08-22 18:16:50 +08:00
|
|
|
class CC_DLL FileUtils
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-01-26 22:31:57 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* Gets the instance of FileUtils.
|
2013-01-26 22:31:57 +08:00
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
static FileUtils* getInstance();
|
|
|
|
|
2013-01-26 22:31:57 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* Destroys the instance of FileUtils.
|
2013-01-26 22:31:57 +08:00
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
static void destroyInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-12-26 11:34:31 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
2015-07-07 14:06:59 +08:00
|
|
|
* Fox example, your resources are encrypted, so you need to decrypt it after reading data from
|
2014-12-26 11:34:31 +08:00
|
|
|
* resources, then you can implement all getXXX functions, and engine will invoke your own getXX
|
|
|
|
* functions when reading data of resources.
|
2014-12-26 14:25:55 +08:00
|
|
|
*
|
|
|
|
* If you don't want to system default implementation after setting delegate, you can just pass nullptr
|
|
|
|
* to this function.
|
|
|
|
*
|
2015-03-27 17:09:54 +08:00
|
|
|
* @warning It will delete previous delegate
|
2015-03-24 10:34:44 +08:00
|
|
|
* @lua NA
|
2014-12-26 11:34:31 +08:00
|
|
|
*/
|
2014-12-26 14:25:55 +08:00
|
|
|
static void setDelegate(FileUtils *delegate);
|
2013-07-12 06:24:23 +08:00
|
|
|
|
|
|
|
/** @deprecated Use getInstance() instead */
|
2013-09-07 06:33:28 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static FileUtils* sharedFileUtils() { return getInstance(); }
|
2013-07-12 06:24:23 +08:00
|
|
|
|
|
|
|
/** @deprecated Use destroyInstance() instead */
|
2013-09-07 06:33:28 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeFileUtils() { destroyInstance(); }
|
2013-07-12 06:24:23 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* The destructor of FileUtils.
|
2013-09-13 13:52:42 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-02-01 11:20:46 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual ~FileUtils();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-26 22:31:57 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Purges full path caches.
|
2013-01-26 22:31:57 +08:00
|
|
|
*/
|
2013-02-01 11:20:46 +08:00
|
|
|
virtual void purgeCachedEntries();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
/**
|
|
|
|
* Gets string from a file.
|
|
|
|
*/
|
|
|
|
virtual std::string getStringFromFile(const std::string& filename);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
/**
|
|
|
|
* Creates binary data from a file.
|
|
|
|
* @return A data object.
|
|
|
|
*/
|
|
|
|
virtual Data getDataFromFile(const std::string& filename);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-02-01 15:41:41 +08:00
|
|
|
* Gets resource file data
|
|
|
|
*
|
2013-07-26 06:53:24 +08:00
|
|
|
* @param[in] filename The resource file name which contains the path.
|
2015-03-27 17:09:54 +08:00
|
|
|
* @param[in] mode The read mode of the file.
|
|
|
|
* @param[out] size If the file read operation succeeds, it will be the data size, otherwise 0.
|
2013-02-01 15:41:41 +08:00
|
|
|
* @return Upon success, a pointer to the data is returned, otherwise NULL.
|
2013-11-12 10:09:47 +08:00
|
|
|
* @warning Recall: you are responsible for calling free() on any Non-NULL pointer returned.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-12-25 10:41:37 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t *size);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-02-01 15:41:41 +08:00
|
|
|
* Gets resource file data from a zip file.
|
|
|
|
*
|
2013-07-26 06:53:24 +08:00
|
|
|
* @param[in] filename The resource file name which contains the relative path of the zip file.
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param[out] size If the file read operation succeeds, it will be the data size, otherwise 0.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @return Upon success, a pointer to the data is returned, otherwise nullptr.
|
|
|
|
* @warning Recall: you are responsible for calling free() on any Non-nullptr pointer returned.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-12-24 10:51:47 +08:00
|
|
|
virtual unsigned char* getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
/** Returns the fullpath for a given filename.
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-29 16:28:59 +08:00
|
|
|
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.
|
2013-06-20 14:13:12 +08:00
|
|
|
Then it will try to obtain the full path of the filename using the FileUtils search rules: resolutions, and search paths.
|
2013-01-29 15:50:57 +08:00
|
|
|
The file search is based on the array element order of search paths and resolution directories.
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-29 15:50:57 +08:00
|
|
|
For instance:
|
2013-01-29 16:28:59 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
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/".
|
2013-01-29 15:50:57 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
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:
|
2013-01-29 16:28:59 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
/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")
|
2013-01-29 16:28:59 +08:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
/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")
|
2013-01-29 16:28:59 +08:00
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
If the new file can't be found on the file system, it will return the parameter filename directly.
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
This method was added to simplify multiplatform support. Whether you are using cocos2d-js or any cross-compilation toolchain like StellaSDK or Apportable,
|
2013-01-29 16:28:59 +08:00
|
|
|
you might need to load different resources for a given file in the different platforms.
|
2013-01-29 15:50:57 +08:00
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
@since v2.1
|
|
|
|
*/
|
2015-04-07 22:15:15 +08:00
|
|
|
virtual std::string fullPathForFilename(const std::string &filename) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
/**
|
|
|
|
* Loads the filenameLookup dictionary from the contents of a filename.
|
2015-07-07 14:06:59 +08:00
|
|
|
*
|
2013-01-18 18:05:32 +08:00
|
|
|
* @note The plist file name should follow the format below:
|
2015-07-07 14:06:59 +08:00
|
|
|
*
|
2013-02-01 15:41:41 +08:00
|
|
|
* @code
|
2013-01-18 18:05:32 +08:00
|
|
|
* <?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
* <plist version="1.0">
|
|
|
|
* <dict>
|
2013-01-25 20:51:52 +08:00
|
|
|
* <key>filenames</key>
|
2013-01-18 18:05:32 +08:00
|
|
|
* <dict>
|
|
|
|
* <key>sounds/click.wav</key>
|
|
|
|
* <string>sounds/click.caf</string>
|
|
|
|
* <key>sounds/endgame.wav</key>
|
|
|
|
* <string>sounds/endgame.caf</string>
|
|
|
|
* <key>sounds/gem-0.wav</key>
|
|
|
|
* <string>sounds/gem-0.caf</string>
|
|
|
|
* </dict>
|
|
|
|
* <key>metadata</key>
|
|
|
|
* <dict>
|
|
|
|
* <key>version</key>
|
|
|
|
* <integer>1</integer>
|
|
|
|
* </dict>
|
|
|
|
* </dict>
|
|
|
|
* </plist>
|
2013-02-01 15:41:41 +08:00
|
|
|
* @endcode
|
2013-01-18 18:05:32 +08:00
|
|
|
* @param filename The plist file name.
|
|
|
|
*
|
|
|
|
@since v2.1
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js loadFilenameLookup
|
|
|
|
* @lua loadFilenameLookup
|
2013-01-18 18:05:32 +08:00
|
|
|
*/
|
2013-09-07 13:54:08 +08:00
|
|
|
virtual void loadFilenameLookupDictionaryFromFile(const std::string &filename);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
/**
|
2013-01-28 23:28:14 +08:00
|
|
|
* Sets the filenameLookup dictionary.
|
|
|
|
*
|
|
|
|
* @param pFilenameLookupDict The dictionary for replacing filename.
|
|
|
|
* @since v2.1
|
2013-01-18 18:05:32 +08:00
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
virtual void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 23:28:14 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Gets full path from a file name and the path of the relative file.
|
2013-07-26 06:53:24 +08:00
|
|
|
* @param filename The file name.
|
2013-01-28 23:28:14 +08:00
|
|
|
* @param pszRelativeFile The path of the relative file.
|
|
|
|
* @return The full path.
|
2013-07-26 06:53:24 +08:00
|
|
|
* e.g. filename: hello.png, pszRelativeFile: /User/path1/path2/hello.plist
|
2013-01-28 23:40:56 +08:00
|
|
|
* Return: /User/path1/path2/hello.pvr (If there a a key(hello.png)-value(hello.pvr) in FilenameLookup dictionary. )
|
2013-01-28 23:28:14 +08:00
|
|
|
*
|
|
|
|
*/
|
2013-09-07 13:54:08 +08:00
|
|
|
virtual std::string fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile);
|
2013-01-23 22:29:00 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
/**
|
2013-01-28 23:28:14 +08:00
|
|
|
* Sets the array that contains the search order of the resources.
|
2013-01-26 22:31:57 +08:00
|
|
|
*
|
2013-01-28 23:28:14 +08:00
|
|
|
* @param searchResolutionsOrder The source array that contains the search order of the resources.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @see getSearchResolutionsOrder(), fullPathForFilename(const char*).
|
2013-01-26 22:31:57 +08:00
|
|
|
* @since v2.1
|
2013-09-13 11:41:20 +08:00
|
|
|
* In js:var setSearchResolutionsOrder(var jsval)
|
|
|
|
* @lua NA
|
2013-01-23 22:29:00 +08:00
|
|
|
*/
|
2013-02-01 11:20:46 +08:00
|
|
|
virtual void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);
|
2013-02-04 12:41:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append search order of the resources.
|
|
|
|
*
|
|
|
|
* @see setSearchResolutionsOrder(), fullPathForFilename().
|
|
|
|
* @since v2.1
|
|
|
|
*/
|
2014-07-02 10:29:09 +08:00
|
|
|
virtual void addSearchResolutionsOrder(const std::string &order,const bool front=false);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 23:28:14 +08:00
|
|
|
/**
|
|
|
|
* Gets the array that contains the search order of the resources.
|
|
|
|
*
|
2013-02-01 15:41:41 +08:00
|
|
|
* @see setSearchResolutionsOrder(const std::vector<std::string>&), fullPathForFilename(const char*).
|
2013-01-28 23:28:14 +08:00
|
|
|
* @since v2.1
|
2013-09-13 11:41:20 +08:00
|
|
|
* @lua NA
|
2013-01-28 23:28:14 +08:00
|
|
|
*/
|
2014-11-22 03:12:55 +08:00
|
|
|
virtual const std::vector<std::string>& getSearchResolutionsOrder() const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
/**
|
2013-01-26 22:31:57 +08:00
|
|
|
* Sets the array of search paths.
|
2015-07-07 14:06:59 +08:00
|
|
|
*
|
2013-01-26 22:31:57 +08:00
|
|
|
* 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.
|
2013-01-29 15:50:57 +08:00
|
|
|
*
|
2013-01-29 16:45:11 +08:00
|
|
|
* @note This method could access relative path and absolute path.
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the relative path was passed to the vector, FileUtils will add the default resource directory before the relative path.
|
2013-01-29 16:45:11 +08:00
|
|
|
* For instance:
|
2015-07-07 14:06:59 +08:00
|
|
|
* On Android, the default resource root path is "assets/".
|
|
|
|
* If "/mnt/sdcard/" and "resources-large" were set to the search paths vector,
|
|
|
|
* "resources-large" will be converted to "assets/resources-large" since it was a relative path.
|
2013-01-29 16:45:11 +08:00
|
|
|
*
|
2013-01-29 15:50:57 +08:00
|
|
|
* @param searchPaths The array contains search paths.
|
2013-02-01 15:41:41 +08:00
|
|
|
* @see fullPathForFilename(const char*)
|
2013-01-26 22:31:57 +08:00
|
|
|
* @since v2.1
|
2013-09-13 11:41:20 +08:00
|
|
|
* In js:var setSearchPaths(var jsval);
|
|
|
|
* @lua NA
|
2013-01-23 22:29:00 +08:00
|
|
|
*/
|
2013-02-01 11:20:46 +08:00
|
|
|
virtual void setSearchPaths(const std::vector<std::string>& searchPaths);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-12-25 20:33:47 +08:00
|
|
|
/**
|
|
|
|
* Set default resource root path.
|
|
|
|
*/
|
|
|
|
void setDefaultResourceRootPath(const std::string& path);
|
|
|
|
|
2013-02-04 12:41:24 +08:00
|
|
|
/**
|
|
|
|
* Add search path.
|
|
|
|
*
|
|
|
|
* @since v2.1
|
|
|
|
*/
|
2014-07-02 10:29:09 +08:00
|
|
|
void addSearchPath(const std::string & path, const bool front=false);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 23:28:14 +08:00
|
|
|
/**
|
|
|
|
* Gets the array of search paths.
|
2015-07-07 14:06:59 +08:00
|
|
|
*
|
2013-01-29 15:50:57 +08:00
|
|
|
* @return The array of search paths.
|
2013-02-01 15:41:41 +08:00
|
|
|
* @see fullPathForFilename(const char*).
|
2013-09-13 11:41:20 +08:00
|
|
|
* @lua NA
|
2013-01-28 23:28:14 +08:00
|
|
|
*/
|
2013-09-07 13:54:08 +08:00
|
|
|
virtual const std::vector<std::string>& getSearchPaths() const;
|
2012-08-09 12:49:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-02-06 18:04:40 +08:00
|
|
|
* Gets the writable path.
|
2013-02-01 18:48:44 +08:00
|
|
|
* @return The path that can be write/read a file in
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-09-07 13:54:08 +08:00
|
|
|
virtual std::string getWritablePath() const = 0;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-12-25 20:33:47 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Sets writable path.
|
2014-12-25 20:33:47 +08:00
|
|
|
*/
|
|
|
|
virtual void setWritablePath(const std::string& writablePath);
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Sets whether to pop-up a message box when failed to load an image.
|
2014-06-20 18:01:34 +08:00
|
|
|
*/
|
|
|
|
virtual void setPopupNotify(bool notify);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
/** Checks whether to pop up a message box when failed to load an image.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @return True if pop up a message box when failed to load an image, false if not.
|
|
|
|
*/
|
2015-04-07 22:15:15 +08:00
|
|
|
virtual bool isPopupNotify() const;
|
2014-06-20 18:01:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the contents of a file to a ValueMap.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @param filename The filename of the file to gets content.
|
|
|
|
* @return ValueMap of the file contents.
|
2014-06-20 18:01:34 +08:00
|
|
|
* @note This method is used internally.
|
|
|
|
*/
|
|
|
|
virtual ValueMap getValueMapFromFile(const std::string& filename);
|
2014-07-29 17:06:43 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
/** Converts the contents of a file to a ValueMap.
|
|
|
|
* This method is used internally.
|
|
|
|
*/
|
2014-07-29 17:06:43 +08:00
|
|
|
virtual ValueMap getValueMapFromData(const char* filedata, int filesize);
|
2015-03-24 10:34:44 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
/**
|
|
|
|
* write a ValueMap into a plist file
|
|
|
|
*
|
|
|
|
*@param dict the ValueMap want to save
|
|
|
|
*@param fullPath The full path to the file you want to save a string
|
|
|
|
*@return bool
|
|
|
|
*/
|
2014-06-20 18:01:34 +08:00
|
|
|
virtual bool writeToFile(ValueMap& dict, const std::string& fullPath);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* write a string into a file
|
|
|
|
*
|
|
|
|
* @param dataStr the string want to save
|
|
|
|
* @param fullPath The full path to the file you want to save a string
|
|
|
|
* @return bool True if write success
|
|
|
|
*/
|
|
|
|
virtual bool writeStringToFile(std::string dataStr, const std::string& fullPath);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write Data into a file
|
|
|
|
*
|
|
|
|
*@param retData the data want to save
|
|
|
|
*@param fullPath The full path to the file you want to save a string
|
|
|
|
*@return bool
|
|
|
|
*/
|
|
|
|
virtual bool writeDataToFile(Data retData, const std::string& fullPath);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write ValueMap into a plist file
|
|
|
|
*
|
|
|
|
*@param dict the ValueMap want to save
|
|
|
|
*@param fullPath The full path to the file you want to save a string
|
|
|
|
*@return bool
|
|
|
|
*/
|
|
|
|
virtual bool writeValueMapToFile(ValueMap& dict, const std::string& fullPath);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write ValueVector into a plist file
|
|
|
|
*
|
|
|
|
*@param vecData the ValueVector want to save
|
|
|
|
*@param fullPath The full path to the file you want to save a string
|
|
|
|
*@return bool
|
|
|
|
*/
|
|
|
|
virtual bool writeValueVectorToFile(ValueVector vecData, const std::string& fullPath);
|
|
|
|
|
2015-04-19 19:00:27 +08:00
|
|
|
/**
|
|
|
|
* Windows fopen can't support UTF-8 filename
|
|
|
|
* Need convert all parameters fopen and other 3rd-party libs
|
|
|
|
*
|
2015-09-22 16:08:23 +08:00
|
|
|
* @param filename std::string name file for conversion from utf-8
|
2015-04-19 19:00:27 +08:00
|
|
|
* @return std::string ansi filename in current locale
|
|
|
|
*/
|
|
|
|
virtual std::string getSuitableFOpen(const std::string& filenameUtf8) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-03-24 10:34:44 +08:00
|
|
|
// Converts the contents of a file to a ValueVector.
|
|
|
|
// This method is used internally.
|
2014-06-20 18:01:34 +08:00
|
|
|
virtual ValueVector getValueVectorFromFile(const std::string& filename);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
/**
|
2013-02-01 18:48:44 +08:00
|
|
|
* Checks whether a file exists.
|
2013-02-01 11:20:46 +08:00
|
|
|
*
|
2013-02-01 22:19:58 +08:00
|
|
|
* @note If a relative path was passed in, it will be inserted a default root path at the beginning.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @param filename The path of the file, it could be a relative or absolute path.
|
|
|
|
* @return True if the file exists, false if not.
|
2013-02-01 11:20:46 +08:00
|
|
|
*/
|
2014-04-02 16:33:05 +08:00
|
|
|
virtual bool isFileExist(const std::string& filename) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-08-06 03:21:16 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param filePath The path of the file, it could be a relative or absolute path.
|
|
|
|
* @return suffix for filename in lower case or empty if a dot not found.
|
|
|
|
*/
|
|
|
|
virtual std::string getFileExtension(const std::string& filePath) const;
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
/**
|
|
|
|
* Checks whether the path is an absolute path.
|
2013-02-01 22:19:58 +08:00
|
|
|
*
|
2013-02-01 16:46:15 +08:00
|
|
|
* @note On Android, if the parameter passed in is relative to "assets/", this method will treat it as an absolute path.
|
|
|
|
* Also on Blackberry, path starts with "app/native/Resources/" is treated as an absolute path.
|
2013-02-01 11:20:46 +08:00
|
|
|
*
|
2015-03-24 10:34:44 +08:00
|
|
|
* @param path The path that needs to be checked.
|
|
|
|
* @return True if it's an absolute path, false if not.
|
2013-02-01 11:20:46 +08:00
|
|
|
*/
|
2013-09-07 13:54:08 +08:00
|
|
|
virtual bool isAbsolutePath(const std::string& path) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Checks whether the path is a directory.
|
2014-06-20 18:01:34 +08:00
|
|
|
*
|
2014-07-08 18:26:11 +08:00
|
|
|
* @param dirPath The path of the directory, it could be a relative or an absolute path.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @return True if the directory exists, false if not.
|
2014-06-20 18:01:34 +08:00
|
|
|
*/
|
2015-04-07 22:15:15 +08:00
|
|
|
virtual bool isDirectoryExist(const std::string& dirPath) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Creates a directory.
|
2014-06-20 18:01:34 +08:00
|
|
|
*
|
2014-07-08 18:26:11 +08:00
|
|
|
* @param dirPath The path of the directory, it must be an absolute path.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @return True if the directory have been created successfully, false if not.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2014-06-20 18:01:34 +08:00
|
|
|
virtual bool createDirectory(const std::string& dirPath);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Removes a directory.
|
2014-06-20 18:01:34 +08:00
|
|
|
*
|
|
|
|
* @param dirPath The full path of the directory, it must be an absolute path.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @return True if the directory have been removed successfully, false if not.
|
2013-12-03 14:47:35 +08:00
|
|
|
*/
|
2014-06-20 18:01:34 +08:00
|
|
|
virtual bool removeDirectory(const std::string& dirPath);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Removes a file.
|
2014-06-20 18:01:34 +08:00
|
|
|
*
|
|
|
|
* @param filepath The full path of the file, it must be an absolute path.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @return True if the file have been removed successfully, false if not.
|
2013-12-03 14:47:35 +08:00
|
|
|
*/
|
2014-06-20 18:01:34 +08:00
|
|
|
virtual bool removeFile(const std::string &filepath);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Renames a file under the given directory.
|
2014-06-20 18:01:34 +08:00
|
|
|
*
|
|
|
|
* @param path The parent directory path of the file, it must be an absolute path.
|
|
|
|
* @param oldname The current name of the file.
|
|
|
|
* @param name The new name of the file.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @return True if the file have been renamed successfully, false if not.
|
2013-12-03 14:47:35 +08:00
|
|
|
*/
|
2014-06-20 18:01:34 +08:00
|
|
|
virtual bool renameFile(const std::string &path, const std::string &oldname, const std::string &name);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
/**
|
|
|
|
* Renames a file under the given directory.
|
|
|
|
*
|
|
|
|
* @param oldfullpath The current fullpath of the file. Includes path and name.
|
|
|
|
* @param newfullpath The new fullpath of the file. Includes path and name.
|
|
|
|
* @return True if the file have been renamed successfully, false if not.
|
|
|
|
*/
|
|
|
|
virtual bool renameFile(const std::string &oldfullpath, const std::string &newfullpath);
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
/**
|
2015-03-24 10:34:44 +08:00
|
|
|
* Retrieve the file size.
|
2014-06-20 18:01:34 +08:00
|
|
|
*
|
|
|
|
* @note If a relative path was passed in, it will be inserted a default root path at the beginning.
|
|
|
|
* @param filepath The path of the file, it could be a relative or absolute path.
|
|
|
|
* @return The file size.
|
|
|
|
*/
|
|
|
|
virtual long getFileSize(const std::string &filepath);
|
2014-01-15 09:22:45 +08:00
|
|
|
|
2015-03-24 10:34:44 +08:00
|
|
|
/** Returns the full path cache. */
|
2014-01-15 09:22:45 +08:00
|
|
|
const std::unordered_map<std::string, std::string>& getFullPathCache() const { return _fullPathCache; }
|
|
|
|
|
2012-08-08 17:42:04 +08:00
|
|
|
protected:
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
|
|
|
* The default constructor.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* Initializes the instance of FileUtils. It will set _searchPathArray and _searchResolutionsOrderArray to default values.
|
2013-02-01 15:41:41 +08:00
|
|
|
*
|
|
|
|
* @note When you are porting Cocos2d-x to a new platform, you may need to take care of this method.
|
2013-06-20 14:13:12 +08:00
|
|
|
* You could assign a default value to _defaultResRootPath in the subclass of FileUtils(e.g. FileUtilsAndroid). Then invoke the FileUtils::init().
|
2015-09-22 16:08:23 +08:00
|
|
|
* @return true if succeed, otherwise it returns false.
|
2013-02-01 15:41:41 +08:00
|
|
|
*
|
|
|
|
*/
|
2013-02-01 11:20:46 +08:00
|
|
|
virtual bool init();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
|
|
|
* Gets the new filename from the filename lookup dictionary.
|
2013-09-07 06:33:28 +08:00
|
|
|
* It is possible to have a override names.
|
2013-07-26 06:53:24 +08:00
|
|
|
* @param filename The original filename.
|
2013-02-01 15:41:41 +08:00
|
|
|
* @return The new filename after searching in the filename lookup dictionary.
|
2013-02-01 18:48:44 +08:00
|
|
|
* If the original filename wasn't in the dictionary, it will return the original filename.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2014-04-02 15:35:09 +08:00
|
|
|
virtual std::string getNewFilename(const std::string &filename) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
/**
|
2014-07-28 11:14:11 +08:00
|
|
|
* Checks whether a file exists without considering search paths and resolution orders.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @param filename The file (with absolute path) to look up for
|
2014-07-28 11:14:11 +08:00
|
|
|
* @return Returns true if the file found at the given absolute path, otherwise returns false
|
2014-04-02 15:35:09 +08:00
|
|
|
*/
|
|
|
|
virtual bool isFileExistInternal(const std::string& filename) const = 0;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-08 18:26:11 +08:00
|
|
|
/**
|
2014-07-28 11:14:11 +08:00
|
|
|
* Checks whether a directory exists without considering search paths and resolution orders.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @param dirPath The directory (with absolute path) to look up for
|
2014-07-28 11:14:11 +08:00
|
|
|
* @return Returns true if the directory found at the given absolute path, otherwise returns false
|
2014-07-08 18:26:11 +08:00
|
|
|
*/
|
2015-07-13 17:06:01 +08:00
|
|
|
virtual bool isDirectoryExistInternal(const std::string& dirPath) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
/**
|
2013-02-01 15:41:41 +08:00
|
|
|
* Gets full path for filename, resolution directory and search path.
|
2013-02-01 11:20:46 +08:00
|
|
|
*
|
|
|
|
* @param filename The file name.
|
|
|
|
* @param resolutionDirectory The resolution directory.
|
|
|
|
* @param searchPath The search path.
|
2013-02-01 22:19:58 +08:00
|
|
|
* @return The full path of the file. It will return an empty string if the full path of the file doesn't exist.
|
2013-02-01 11:20:46 +08:00
|
|
|
*/
|
2015-04-07 22:15:15 +08:00
|
|
|
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
|
|
|
* Gets full path for the directory and the filename.
|
|
|
|
*
|
|
|
|
* @note Only iOS and Mac need to override this method since they are using
|
|
|
|
* `[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]` to make a full path.
|
|
|
|
* Other platforms will use the default implementation of this method.
|
2015-03-24 10:34:44 +08:00
|
|
|
* @param directory The directory contains the file we are looking for.
|
|
|
|
* @param filename The name of the file.
|
2013-02-01 15:41:41 +08:00
|
|
|
* @return The full path of the file, if the file can't be found, it will return an empty string.
|
|
|
|
*/
|
2015-04-07 22:15:15 +08:00
|
|
|
virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
/** Dictionary used to lookup filenames based on a key.
|
2013-02-01 22:19:58 +08:00
|
|
|
* It is used internally by the following methods:
|
|
|
|
*
|
|
|
|
* std::string fullPathForFilename(const char*);
|
|
|
|
*
|
|
|
|
* @since v2.1
|
2013-01-18 18:05:32 +08:00
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap _filenameLookupDict;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
/**
|
2013-02-01 15:41:41 +08:00
|
|
|
* The vector contains resolution folders.
|
2013-02-01 17:16:33 +08:00
|
|
|
* The lower index of the element in this vector, the higher priority for this resolution directory.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
std::vector<std::string> _searchResolutionsOrderArray;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
|
|
|
* The vector contains search paths.
|
2013-02-01 17:16:33 +08:00
|
|
|
* The lower index of the element in this vector, the higher priority for this search path.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
std::vector<std::string> _searchPathArray;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
|
|
|
* The default root path of resources.
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the default root path of resources needs to be changed, do it in the `init` method of FileUtils's subclass.
|
2013-02-01 17:16:33 +08:00
|
|
|
* For instance:
|
2013-06-20 14:13:12 +08:00
|
|
|
* On Android, the default root path of resources will be assigned with "assets/" in FileUtilsAndroid::init().
|
|
|
|
* Similarly on Blackberry, we assign "app/native/Resources/" to this variable in FileUtilsBlackberry::init().
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _defaultResRootPath;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 16:46:15 +08:00
|
|
|
/**
|
2015-07-07 14:06:59 +08:00
|
|
|
* The full path cache. When a file is found, it will be added into this cache.
|
2013-02-01 18:48:44 +08:00
|
|
|
* This variable is used for improving the performance of file search.
|
2013-02-01 16:46:15 +08:00
|
|
|
*/
|
2015-04-07 22:15:15 +08:00
|
|
|
mutable std::unordered_map<std::string, std::string> _fullPathCache;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-12-25 20:33:47 +08:00
|
|
|
/**
|
|
|
|
* Writable path.
|
|
|
|
*/
|
|
|
|
std::string _writablePath;
|
|
|
|
|
2013-02-01 15:41:41 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* The singleton pointer of FileUtils.
|
2013-02-01 15:41:41 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static FileUtils* s_sharedFileUtils;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2015-03-24 11:15:40 +08:00
|
|
|
// end of support group
|
|
|
|
/** @} */
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
#endif // __CC_FILEUTILS_H__
|