axmol/cocos/platform/CCFileUtils.h

995 lines
41 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2013-2017 Chukong Technologies Inc.
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.
****************************************************************************/
#ifndef __CC_FILEUTILS_H__
#define __CC_FILEUTILS_H__
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include <string>
#include <vector>
#include <unordered_map>
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
#include <type_traits>
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +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"
#include "base/CCAsyncTaskPool.h"
#include "base/CCScheduler.h"
#include "base/CCDirector.h"
NS_CC_BEGIN
2012-06-20 18:09:11 +08:00
/**
* @addtogroup platform
2012-06-20 18:09:11 +08:00
* @{
*/
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
class ResizableBuffer {
public:
virtual ~ResizableBuffer() {}
virtual void resize(size_t size) = 0;
virtual void* buffer() const = 0;
};
template<typename T>
class ResizableBufferAdapter { };
template<typename CharT, typename Traits, typename Allocator>
class ResizableBufferAdapter< std::basic_string<CharT, Traits, Allocator> > : public ResizableBuffer {
typedef std::basic_string<CharT, Traits, Allocator> BufferType;
BufferType* _buffer;
public:
explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {}
virtual void resize(size_t size) override {
_buffer->resize((size + sizeof(CharT) - 1) / sizeof(CharT));
}
virtual void* buffer() const override {
// can not invoke string::front() if it is empty
2016-07-05 14:28:30 +08:00
if (_buffer->empty())
return nullptr;
else
return &_buffer->front();
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
}
};
template<typename T, typename Allocator>
class ResizableBufferAdapter< std::vector<T, Allocator> > : public ResizableBuffer {
typedef std::vector<T, Allocator> BufferType;
BufferType* _buffer;
public:
explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {}
virtual void resize(size_t size) override {
_buffer->resize((size + sizeof(T) - 1) / sizeof(T));
}
virtual void* buffer() const override {
// can not invoke vector::front() if it is empty
2016-07-05 14:28:30 +08:00
if (_buffer->empty())
return nullptr;
else
return &_buffer->front();
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
}
};
template<>
class ResizableBufferAdapter<Data> : public ResizableBuffer {
typedef Data BufferType;
BufferType* _buffer;
public:
explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {}
virtual void resize(size_t size) override {
size_t oldSize = static_cast<size_t>(_buffer->getSize());
if (oldSize != size) {
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
auto old = _buffer->getBytes();
void* buffer = realloc(old, size);
if (buffer)
_buffer->fastSet((unsigned char*)buffer, size);
}
}
virtual void* buffer() const override {
return _buffer->getBytes();
}
};
2015-03-24 10:34:44 +08:00
/** Helper class to handle file operations. */
class CC_DLL FileUtils
{
public:
/**
* Gets the instance of FileUtils.
*/
static FileUtils* getInstance();
/**
* Destroys the instance of FileUtils.
*/
static void destroyInstance();
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-10-23 15:37:33 +08:00
* For 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);
/** @deprecated Use getInstance() instead */
CC_DEPRECATED_ATTRIBUTE static FileUtils* sharedFileUtils() { return getInstance(); }
/** @deprecated Use destroyInstance() instead */
CC_DEPRECATED_ATTRIBUTE static void purgeFileUtils() { destroyInstance(); }
/**
* The destructor of FileUtils.
* @js NA
* @lua NA
*/
virtual ~FileUtils();
/**
2015-03-24 10:34:44 +08:00
* Purges full path caches.
*/
virtual void purgeCachedEntries();
/**
* Gets string from a file.
*/
virtual std::string getStringFromFile(const std::string& filename);
/**
* Gets string from a file, async off the main cocos thread
*
* @param path filepath for the string to be read. Can be relative or absolute path
* @param callback Function that will be called when file is read. Will be called
* on the main cocos thread.
*/
virtual void getStringFromFile(const std::string& path, std::function<void(std::string)> callback);
/**
* Creates binary data from a file.
* @return A data object.
*/
virtual Data getDataFromFile(const std::string& filename);
/**
* Gets a binary data object from a file, async off the main cocos thread.
*
* @param filename filepath for the data to be read. Can be relative or absolute path
* @param callback Function that will be called when file is read. Will be called
* on the main cocos thread.
*/
virtual void getDataFromFile(const std::string& filename, std::function<void(Data)> callback);
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
enum class Status
{
OK = 0,
NotExists = 1, // File not exists
OpenFailed = 2, // Open file failed.
2016-06-25 11:58:55 +08:00
ReadFailed = 3, // Read failed
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
NotInitialized = 4, // FileUtils is not initializes
TooLarge = 5, // The file is too large (great than 2^32-1)
ObtainSizeFailed = 6 // Failed to obtain the file size.
};
/**
* Gets whole file contents as string from a file.
*
* Unlike getStringFromFile, these getContents methods:
* - read file in binary mode (does not convert CRLF to LF).
* - does not truncate the string when '\0' is found (returned string of getContents may have '\0' in the middle.).
*
* The template version of can accept cocos2d::Data, std::basic_string and std::vector.
*
* @code
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
* std::string sbuf;
* FileUtils::getInstance()->getContents("path/to/file", &sbuf);
*
* std::vector<int> vbuf;
* FileUtils::getInstance()->getContents("path/to/file", &vbuf);
*
* Data dbuf;
* FileUtils::getInstance()->getContents("path/to/file", &dbuf);
* @endcode
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
*
* 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.
*
* - To write a new buffer class works with getContents, just extend ResizableBuffer.
* - To write a adapter for existing class, write a specialized ResizableBufferAdapter for that class, see follow code.
*
* @code
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
* NS_CC_BEGIN // ResizableBufferAdapter needed in cocos2d namespace.
* template<>
* class ResizableBufferAdapter<AlreadyExistsBuffer> : public ResizableBuffer {
* public:
* ResizableBufferAdapter(AlreadyExistsBuffer* buffer) {
* // your code here
* }
* virtual void resize(size_t size) override {
* // your code here
* }
* virtual void* buffer() const override {
* // your code here
* }
* };
* NS_CC_END
* @endcode
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
*
* @param[in] filename The resource file name which contains the path.
* @param[out] buffer The buffer where the file contents are store to.
* @return Returns:
* - Status::OK when there is no error, the buffer is filled with the contents of file.
* - Status::NotExists when file not exists, the buffer will not changed.
* - Status::OpenFailed when cannot open file, the buffer will not changed.
2016-06-25 11:58:55 +08:00
* - Status::ReadFailed when read end up before read whole, the buffer will fill with already read bytes.
Add FileUtils::getContents(). (#15479) * Add FileUtils::getContents(). * skip FileUtils::getContents() in binding generator config. * use FileUtils::getContents in CCDataReaderHelper. * obey the cocos2d-x coding style. * Explicit constructor. * More docs. * More tests. * Move FileError to FileUtils::Error. * Fixes wrong buffer size for reading into string and vector. * Update tests. * Add note on padding for output buffers. * FileUtils: implements old methods by using `getContents()`. methods are: * FileUtils::getDataFromFile() * FileUtils::getStringFromFile() * FileUtils::getFileData() and follow Android methods are now just calls FileUtils' ones. * FileUtilsAndroid::getDataFromFile() * FileUtilsAndroid::getStringFromFile() * FileUtilsAndroid::getFileData() * Fixes build error. * FileUtils::getFileData: Return the size of data. * Remove old methods form FileUtilsAndroid they are now done in FileUtils. * Fixes for win32 code. * Fixes build error in test and add more test. * Better error message. * Make template type name more readable. * Update comments. * Move internal functions to anonymous namespace. * Refactor FileUtils test. * Fix warning about compare signed and unsigned. * Win32 and WinRT does not use text mode. That is we don't need simulate convert CRLF to LF. * Fixes for Win32 and WinRT. * Update for Win32 and WinRT. * Win32: return FileUtils:Error::TooLarge when file is large than 2^32-1. * Win32: remove checkFileName() which has no effect at all. * WinRT: add FileUtilsWinRT::getContents() using ::CreateFile2. * WinRT: add override keyword for FileUtilsWinRT::getFileSize(). * Update for coding styles. * More error strings. * check read and malloc return codes. * rename FileUtils::Error to FileUtils::Status. * Fixes for WinRT, use GetFileInformationByHandleEx to get file size. * Fixes build error for winrt and cleanup FileUtils::Status. * Try to fix the build error on Linux. Status must defined in some header, so move FileUtils.h up. * Remove support of text mode on WinRT (it is the last platform support text mode).
2016-04-26 13:37:22 +08:00
* - Status::NotInitialized when FileUtils is not initializes, the buffer will not changed.
* - Status::TooLarge when there file to be read is too large (> 2^32-1), the buffer will not changed.
* - Status::ObtainSizeFailed when failed to obtain the file size, the buffer will not changed.
*/
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) {
ResizableBufferAdapter<T> buf(buffer);
return getContents(filename, &buf);
}
virtual Status getContents(const std::string& filename, ResizableBuffer* buffer);
/**
* Gets resource file data
*
* @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.
* @return Upon success, a pointer to the data is returned, otherwise NULL.
* @warning Recall: you are responsible for calling free() on any Non-NULL pointer returned.
*/
CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t *size);
/**
* Gets resource file data from a zip file.
*
* @param[in] filename The resource file name which contains the relative path of the zip file.
* @param[out] size If the file read operation succeeds, it will be the data size, otherwise 0.
* @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.
*/
virtual unsigned char* getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size);
/** Returns the fullpath for a given filename.
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.
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:
2013-01-29 16:28: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/".
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
/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:
/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
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,
2013-01-29 16:28:59 +08:00
you might need to load different resources for a given file in the different platforms.
@since v2.1
*/
virtual std::string fullPathForFilename(const std::string &filename) const;
/**
* Loads the filenameLookup dictionary from the contents of a filename.
*
* @note The plist file name should follow the format below:
*
* @code
* <?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>
* <key>filenames</key>
* <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>
* @endcode
* @param filename The plist file name.
*
@since v2.1
* @js loadFilenameLookup
* @lua loadFilenameLookup
*/
virtual void loadFilenameLookupDictionaryFromFile(const std::string &filename);
/**
2013-01-28 23:28:14 +08:00
* Sets the filenameLookup dictionary.
*
2016-07-03 23:42:10 +08:00
* @param filenameLookupDict The dictionary for replacing filename.
2013-01-28 23:28:14 +08:00
* @since v2.1
*/
virtual void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);
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.
* @param filename The file name.
2016-07-03 23:42:10 +08:00
* @param relativeFile The path of the relative file.
2013-01-28 23:28:14 +08:00
* @return The full path.
* 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
*
*/
virtual std::string fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile);
/**
2013-01-28 23:28:14 +08:00
* Sets the array that contains the search order of the resources.
*
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*).
* @since v2.1
* In js:var setSearchResolutionsOrder(var jsval)
* @lua NA
*/
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
*/
virtual void addSearchResolutionsOrder(const std::string &order,const bool front=false);
2013-01-28 23:28:14 +08:00
/**
* Gets the array that contains the search order of the resources.
*
* @see setSearchResolutionsOrder(const std::vector<std::string>&), fullPathForFilename(const char*).
2013-01-28 23:28:14 +08:00
* @since v2.1
* @lua NA
2013-01-28 23:28:14 +08:00
*/
virtual const std::vector<std::string>& getSearchResolutionsOrder() const;
/**
* 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.
*
2013-01-29 16:45:11 +08:00
* @note This method could access relative path and absolute path.
* 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:
* 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
*
* @param searchPaths The array contains search paths.
* @see fullPathForFilename(const char*)
* @since v2.1
* In js:var setSearchPaths(var jsval);
* @lua NA
*/
virtual void setSearchPaths(const std::vector<std::string>& searchPaths);
/**
* Get default resource root path.
*/
const std::string& getDefaultResourceRootPath() const;
/**
* 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
*/
void addSearchPath(const std::string & path, const bool front=false);
2013-01-28 23:28:14 +08:00
/**
* Gets the array of search paths.
*
* @return The array of search paths which may contain the prefix of default resource root path.
* @note In best practise, getter function should return the value of setter function passes in.
* But since we should not break the compatibility, we keep using the old logic.
* Therefore, If you want to get the original search paths, please call 'getOriginalSearchPaths()' instead.
* @see fullPathForFilename(const char*).
* @lua NA
2013-01-28 23:28:14 +08:00
*/
virtual const std::vector<std::string>& getSearchPaths() const;
/**
* Gets the original search path array set by 'setSearchPaths' or 'addSearchPath'.
* @return The array of the original search paths
*/
virtual const std::vector<std::string>& getOriginalSearchPaths() const;
/**
2013-02-06 18:04:40 +08:00
* Gets the writable path.
* @return The path that can be write/read a file in
*/
virtual std::string getWritablePath() const = 0;
/**
2015-03-24 10:34:44 +08:00
* Sets writable path.
*/
virtual void setWritablePath(const std::string& writablePath);
/**
2015-03-24 10:34:44 +08:00
* Sets whether to pop-up a message box when failed to load an image.
*/
virtual void setPopupNotify(bool notify);
/** 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.
*/
virtual bool isPopupNotify() const;
/**
* 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.
* @note This method is used internally.
*/
virtual ValueMap getValueMapFromFile(const std::string& filename);
/** Converts the contents of a file to a ValueMap.
* This method is used internally.
*/
virtual ValueMap getValueMapFromData(const char* filedata, int filesize);
2015-03-24 10:34:44 +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
*/
virtual bool writeToFile(const ValueMap& dict, const std::string& fullPath);
/**
* 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(const std::string& dataStr, const std::string& fullPath);
/**
* 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
*
* @param dataStr the string want to save
* @param fullPath The full path to the file you want to save a string
* @param 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 void writeStringToFile(std::string dataStr, const std::string& fullPath, std::function<void(bool)> callback);
/**
* write Data into a file
*
2016-01-27 13:39:31 +08:00
*@param data the data want to save
*@param fullPath The full path to the file you want to save a string
*@return bool
*/
2016-01-27 13:39:31 +08:00
virtual bool writeDataToFile(const Data& data, const std::string& fullPath);
/**
* 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
*
*@param data The data that will be written to disk
*@param fullPath The absolute file path that the data will be written to
*@param 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 void writeDataToFile(Data data, const std::string& fullPath, std::function<void(bool)> callback);
/**
* 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(const ValueMap& dict, const std::string& fullPath);
/**
* 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
*
*@param dict The ValueMap that will be written to disk
*@param fullPath The absolute file path that the data will be written to
*@param 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 void writeValueMapToFile(ValueMap dict, const std::string& fullPath, std::function<void(bool)> callback);
/**
* 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(const ValueVector& vecData, const std::string& fullPath);
/**
* 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
*
*@param vecData The ValueVector that will be written to disk
*@param fullPath The absolute file path that the data will be written to
*@param 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 void writeValueVectorToFile(ValueVector vecData, const std::string& fullPath, std::function<void(bool)> callback);
/**
* Windows fopen can't support UTF-8 filename
* Need convert all parameters fopen and other 3rd-party libs
*
2016-07-03 23:42:10 +08:00
* @param filenameUtf8 std::string name file for conversion from utf-8
* @return std::string ansi filename in current locale
*/
virtual std::string getSuitableFOpen(const std::string& filenameUtf8) const;
2015-03-24 10:34:44 +08:00
// Converts the contents of a file to a ValueVector.
// This method is used internally.
virtual ValueVector getValueVectorFromFile(const std::string& filename);
/**
* Checks whether a file exists.
*
* @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.
*/
virtual bool isFileExist(const std::string& filename) const;
/**
* 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.
*
* @note If a relative path was passed in, it will be inserted a default root path at the beginning.
* @param filename The path of the file, it could be a relative or absolute path.
* @param 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 void isFileExist(const std::string& filename, std::function<void(bool)> callback);
/**
* 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;
/**
* Checks whether the path is an absolute path.
*
* @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.
*
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.
*/
virtual bool isAbsolutePath(const std::string& path) const;
/**
2015-03-24 10:34:44 +08:00
* Checks whether the path is a directory.
*
* @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.
*/
virtual bool isDirectoryExist(const std::string& dirPath) const;
/**
* Checks whether the absoulate path is a directory, async off of the main cocos thread.
*
* @param dirPath The path of the directory, it must be an absolute path
* @param callback that will accept a boolean, true if the file exists, false otherwise.
* Callback will happen on the main cocos thread.
*/
virtual void isDirectoryExist(const std::string& fullPath, std::function<void(bool)> callback);
/**
2015-03-24 10:34:44 +08:00
* Creates a directory.
*
* @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.
*/
virtual bool createDirectory(const std::string& dirPath);
/**
* Create a directory, async off the main cocos thread.
*
* @param dirPath the path of the directory, it must be an absolute path
* @param 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 void createDirectory(const std::string& dirPath, std::function<void(bool)> callback);
/**
2015-03-24 10:34:44 +08:00
* Removes a directory.
*
* @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.
*/
virtual bool removeDirectory(const std::string& dirPath);
/**
* Removes a directory, async off the main cocos thread.
*
* @param dirPath the path of the directory, it must be an absolute path
* @param 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 void removeDirectory(const std::string& dirPath, std::function<void(bool)> callback);
/**
2015-03-24 10:34:44 +08:00
* Removes a file.
*
* @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.
*/
virtual bool removeFile(const std::string &filepath);
/**
* Removes a file, async off the main cocos thread.
*
* @param filepath the path of the file to remove, it must be an absolute path
* @param 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 void removeFile(const std::string &filepath, std::function<void(bool)> callback);
/**
2015-03-24 10:34:44 +08:00
* Renames a file under the given directory.
*
* @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.
*/
virtual bool renameFile(const std::string &path, const std::string &oldname, const std::string &name);
/**
* Renames a file under the given directory, async off the main cocos thread.
*
* @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.
* @param 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 void renameFile(const std::string &path, const std::string &oldname, const std::string &name, std::function<void(bool)> callback);
Squashed commit of the following: commit 2c810a8e931e07b06498c07db37cbf20a2bcfc92 Merge: 61788a0 082caaf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 13 00:13:38 2015 -0700 Merge branch 'v3' into downloader_decouple commit 61788a0b34188fed5367cab121817db97198bcd8 Merge: 8c1e211 5e21580 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 20:45:12 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8c1e2119a5c671c00da5ce1fb244029f2bfab09f Merge: 2ea66af 8d73883 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 20:42:04 2015 -0700 Merge pull request #6 from minggo/downloader_decouple fix js binding error commit 8d7388302ff1d8da575b277e47a3f8ceb1017e98 Author: minggo <ming@cocos2d-x.org> Date: Thu Aug 13 11:09:58 2015 +0800 fix js binding error commit 2ea66af7684f5ec8a20067b1a49347920dbbbb40 Merge: 8cdb0b6 bc964e8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 09:14:22 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8cdb0b6e4b83210b1f68b4568c74ccb36f504806 Merge: 9272765 08eeca9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 20:11:46 2015 -0700 Merge pull request #5 from minggo/downloader_decouple fix lua-binding error commit 08eeca9d99b5160c48bac1bfecf03b459d05ed15 Author: minggo <ming@cocos2d-x.org> Date: Wed Aug 12 10:34:05 2015 +0800 fix lua-binding error commit 927276589d57432477f8ffc033e19a21a215b246 Merge: 8252464 6f4ed8a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 10:39:22 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8252464020dbe86da4e5e5aa6e4e45f6997f6b9b Merge: 5dddbb6 138df42 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 06:53:48 2015 -0700 Merge pull request #4 from minggo/downloader_decouple fix linking error in lua-tests on Android commit 138df42a3acfdcfae29eeab1b03ef5dd062f0145 Author: minggo <ming@cocos2d-x.org> Date: Tue Aug 11 16:23:53 2015 +0800 fix link error in lua-tests commit 5dddbb61a1bcda96aa93bfcbe78f42c639eb6247 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 23:22:50 2015 -0700 works for real on win32 commit ddadaf82231b60c8e343718561c0d78657afe7c2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 22:26:59 2015 -0700 compiles on win32 commit 733f1d4c2e4f8382ff19f1793f7f09950774fe78 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 21:57:49 2015 -0700 compiles on iOS!! commit f706f97962eb393bf7dfec79739ff8dfdec103d5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 21:27:03 2015 -0700 compiles and runs Ok on windows. Tested all downloadertests + assetmanagertest commit 5e333f4407952d9c2e71e18cb9393d8b6cd170b4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 20:24:28 2015 -0700 updates windows projects commit e77fd8a278594fefc24ff13cbee038508429294d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 19:56:10 2015 -0700 compiles on Android commit 5f515e3519a0688d11476ea6ef235007e8378c96 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 18:34:33 2015 -0700 trying to fix a compile issue on iOS WIP commit b144ea67dfe7a06e7bfb515adf16f536ba2ead98 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 18:25:11 2015 -0700 make bug fixes adds public `getHeader` commit a228799ba74eb8642c43d215a802f811a576a4aa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 17:42:19 2015 -0700 some linux fixes commit 2cab23d87acc138823ebe9dcec9d42f827690d99 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 16:58:53 2015 -0700 Squashed commit of the following: commit 86227e9138c1fb523bd28186d355de65cc64c588 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 16:55:17 2015 -0700 works %100 and uses less memory than before commit a9cafa6f2fe7847e279934d2c162287680760dda Merge: 0bbc62e 0cbd71f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 15:49:33 2015 -0700 Merge branch 'v3' into cocos_net commit 0bbc62ee1fbfd107a935131d5d93994e87865afa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 15:46:55 2015 -0700 more tests... and works but there is huge bug. ProgData <vector> needs the position of DownloadUnits <unordered_map> but <unordered_map> does not guaranty the order. So in my next commit I"ll merge DownloadUnit with ProgressData commit 389ed8639a1942f628773616a8e340bc66a0c1d7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 13:47:07 2015 -0700 debugging info commit af0dc45bb9c4705934ced2c1d67e5afc06b6f317 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 11:47:31 2015 -0700 sync and async tests working ok commit 999fa7a7bfb28565492f47b39caef7f1a079c183 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 11:38:38 2015 -0700 adding tests commit 91ee294942510718400edc0d00d3c744d00339ee Merge: 76ab703 7f215e9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 10:03:50 2015 -0700 Merge branch 'v3' into cocos_net commit 76ab7032d3a21156f05395a021391a16649f755a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 00:22:20 2015 -0700 Downloader tests commit b65c4dad7089660bfe697c0dab3e9290d1a6d013 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 6 21:34:54 2015 -0700 compiles on Xcode 7 commit ec0d66340b8daf0b042d36333dff759cd424f465 Merge: 878e4b5 0d77838 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 6 11:02:24 2015 -0700 Merge branch 'v3' into cocos_net commit 878e4b5d0f87bc6d5d6e507102ab15e2c24a1c8f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 18:07:02 2015 -0700 working, yeah! commit ad1e7637a5ac2e6bff44d35429f5556ea1cb8a4c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 17:49:59 2015 -0700 code is more thread-safe friendly commit 8c6baaee6b898d4b90e5d8988a174d8a33702f60 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 16:39:02 2015 -0700 fixes callbacks commit ddab4d753106c6ff11304c1c69739c5611e3f20a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 15:43:51 2015 -0700 cleaner code runs callbacks in cocos2d thread only if needed commit ef75844aaa010633e6e27ae6e69460174460d5c7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 11:58:04 2015 -0700 removed weak_ptr<> from ProgressData that logic of the code is added in the callbacks commit d9c7436cbadc611592b3c80ca2a3a9edeaa00853 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 11:54:07 2015 -0700 checks whether or not the callbacks are in the cocos2d thread commit ae6594a6f866f6b5015ba293db23343887063d2f Merge: 4c61069 6831e24 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 10:48:34 2015 -0700 Merge branch 'v3' into cocos_net commit 4c61069a7e0e33fa0332df700cc8df4807178caf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 21:36:50 2015 -0700 almost there... Downloader has std::vector<FILE*> instead of FileDescriptors code cleaner... commit c34d04a8218d2be5c316e0487e5ac46792bbed76 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 20:38:44 2015 -0700 passes progressData to batch no need to alloc memory for each ProgressData commit f7e8bbd8b85f214b919bce6118d4158dd6d26368 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 17:47:23 2015 -0700 tidier code and more optimizations commit a486dc78aedcf9c6178e490292edd572f8a7b374 Merge: 4178327 a06421b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 14:03:22 2015 -0700 Merge branch 'v3' into cocos_net commit 417832705910a6ebd16a876cd98064ea16f0f45c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 18:13:13 2015 -0700 not finished yet... ...but trying to have a common inteface with batched and non-batched downloads commit 1d0e4652763c2e1511ac102a62defae6c6a89220 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 16:29:39 2015 -0700 ErrorCallback compiles starting the batching code commit 6645a287c47dc01cbf1f2d80e9c0fdd2b16e3e18 Merge: c180425 8d836da Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 10:28:51 2015 -0700 Merge branch 'v3' into cocos_net commit c180425d8181b8e6f53be766d8d2b94f908ae168 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 15:35:53 2015 -0700 More progress on migrating multiple-file downloader to the implementation file commit 5fa273e265963af3c0cb6338bf49dc5e1b54173b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 14:00:02 2015 -0700 Dowloader moved to `cocos2d::network` commit 14b1d0825106701b5435a266b255f09291b6486b Merge: 13cfc8d f744383 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 11:13:55 2015 -0700 Merge branch 'v3' into cocos_net commit 13cfc8de4eb4835ffb8f8d671d3d070c411b0a2e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 11:11:45 2015 -0700 DownloadUnits belong to network commit e6e17564ead1fb7f6f7df343b0627a104cd756f8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 12:23:36 2015 -0700 adds `check for option` commit 5cb76dfaaed42011a0c536d01b82a0a94487d6aa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 10:37:51 2015 -0700 Downloader: using URLDownload in one case. 3 more to go. Not tested yet commit 9b26e9cfac425635027189ccab835054b68bc83d Merge: 20fe063 253d9c4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 08:35:53 2015 -0700 Merge branch 'v3' into cocos_net commit 20fe0636e36ae7f1da048545b3345f526d5b64de Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 17:36:07 2015 -0700 URLDownload: initial commit HttpAsyncConnection -> HttpAsyncConnection-apple: follows the cocos2d guidelines Adds `CCIRULDownload` interface. Adds `CCURLDownload` implementation based on libcurl. Not working yet. `Downloader` uses `URLDownload` for some of its functions. WIP commit 3a5d3b83838bee53e3bbea182f3ad09d7bec32d0 Merge: e408fe0 642ccac Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 14:00:43 2015 -0700 Merge remote-tracking branch 'cocos2d/v3' into cocos_net commit e408fe0b9303813d20bab2f7bf548f3b7af416a6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 13:59:46 2015 -0700 Adds more files to Xcode project ...even if they are not being compiled. Much easier to add missing functionality to the rest of the platforms commit 11d86acebeb61348e4ccf7e6af9a2eaf1b6bb534 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 11:36:57 2015 -0700 removes unneeded headers
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);
/**
* Renames a file under the given directory, async off the main cocos thread.
*
* @param oldfullpath The current fullpath of the file. Includes path and name.
* @param newfullpath The new fullpath of the file. Includes path and name.
* @param 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 void renameFile(const std::string &oldfullpath, const std::string &newfullpath, std::function<void(bool)> callback);
/**
2015-03-24 10:34:44 +08:00
* Retrieve the file size.
*
* @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);
/**
* Retrieve the file size, async off the main cocos thread.
*
* @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.
* @param callback The function that will be called when the operation is complete. Will have one long
* argument, the file size.
*/
virtual void getFileSize(const std::string &filepath, std::function<void(long)> callback);
/**
* List all files in a directory.
*
* @param dirPath The path of the directory, it could be a relative or an absolute path.
* @return File paths in a string vector
*/
virtual std::vector<std::string> listFiles(const std::string& dirPath) const;
/**
* List all files in a directory async, off of the main cocos thread.
*
* @param dirPath The path of the directory, it could be a relative or an absolute path.
* @param 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 void listFilesAsync(const std::string& dirPath, std::function<void(std::vector<std::string>)> callback) const;
/**
* List all files recursively in a directory.
*
* @param dirPath The path of the directory, it could be a relative or an absolute path.
* @return File paths in a string vector
*/
virtual void listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const;
/**
* List all files recursively in a directory, async off the main cocos thread.
*
* @param dirPath The path of the directory, it could be a relative or an absolute path.
* @param 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 void listFilesRecursivelyAsync(const std::string& dirPath, std::function<void(std::vector<std::string>)> callback) const;
2015-03-24 10:34:44 +08:00
/** Returns the full path cache. */
const std::unordered_map<std::string, std::string>& getFullPathCache() const { return _fullPathCache; }
/**
* Gets the new filename from the filename lookup dictionary.
* It is possible to have a override names.
* @param filename The original filename.
* @return The new filename after searching in the filename lookup dictionary.
* If the original filename wasn't in the dictionary, it will return the original filename.
*/
virtual std::string getNewFilename(const std::string &filename) const;
protected:
/**
* The default constructor.
*/
FileUtils();
/**
* Initializes the instance of FileUtils. It will set _searchPathArray and _searchResolutionsOrderArray to default values.
*
* @note When you are porting Cocos2d-x to a new platform, you may need to take care of this method.
* You could assign a default value to _defaultResRootPath in the subclass of FileUtils(e.g. FileUtilsAndroid). Then invoke the FileUtils::init().
* @return true if succeed, otherwise it returns false.
*
*/
virtual bool init();
/**
* 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
* @return Returns true if the file found at the given absolute path, otherwise returns false
*/
virtual bool isFileExistInternal(const std::string& filename) const = 0;
/**
* 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
* @return Returns true if the directory found at the given absolute path, otherwise returns false
*/
virtual bool isDirectoryExistInternal(const std::string& dirPath) const;
/**
* Gets full path for filename, resolution directory and search path.
*
* @param filename The file name.
* @param resolutionDirectory The resolution directory.
* @param searchPath The search path.
* @return The full path of the file. It will return an empty string if the full path of the file doesn't exist.
*/
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const;
/**
* 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.
* @return The full path of the file, if the file can't be found, it will return an empty string.
*/
virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const;
/** Dictionary used to lookup filenames based on a key.
* It is used internally by the following methods:
*
* std::string fullPathForFilename(const char*);
*
* @since v2.1
*/
ValueMap _filenameLookupDict;
/**
* The vector contains resolution folders.
* The lower index of the element in this vector, the higher priority for this resolution directory.
*/
std::vector<std::string> _searchResolutionsOrderArray;
/**
* The vector contains search paths.
* The lower index of the element in this vector, the higher priority for this search path.
*/
std::vector<std::string> _searchPathArray;
/**
* The search paths which was set by 'setSearchPaths' / 'addSearchPath'.
*/
std::vector<std::string> _originalSearchPaths;
/**
* The default root path of resources.
* If the default root path of resources needs to be changed, do it in the `init` method of FileUtils's subclass.
* For instance:
* 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().
*/
std::string _defaultResRootPath;
/**
* The full path cache. When a file is found, it will be added into this cache.
* This variable is used for improving the performance of file search.
*/
mutable std::unordered_map<std::string, std::string> _fullPathCache;
/**
* Writable path.
*/
std::string _writablePath;
/**
* The singleton pointer of FileUtils.
*/
static FileUtils* s_sharedFileUtils;
/**
* Remove null value key (for iOS)
*/
virtual void valueMapCompact(ValueMap& valueMap);
virtual void valueVectorCompact(ValueVector& valueVector);
template<typename T, typename R, typename ...ARGS>
static void performOperationOffthread(T&& action, R&& callback, ARGS&& ...args)
{
// Visual Studio 2013 does not support using std::bind to forward template parameters into
// a lambda. To get around this, we will just copy these arguments via lambda capture
#if defined(_MSC_VER) && _MSC_VER < 1900
auto lambda = [action, callback, args...]()
{
Director::getInstance()->getScheduler()->performFunctionInCocosThread(std::bind(callback, action(args...)));
};
#else
// As cocos2d-x uses c++11, we will use std::bind to leverage move sematics to
// move our arguments into our lambda, to potentially avoid copying.
auto lambda = std::bind([](const T& actionIn, const R& callbackIn, const ARGS& ...argsIn)
{
Director::getInstance()->getScheduler()->performFunctionInCocosThread(std::bind(callbackIn, actionIn(argsIn...)));
}, std::forward<T>(action), std::forward<R>(callback), std::forward<ARGS>(args)...);
#endif
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, [](void*){}, nullptr, std::move(lambda));
}
};
2015-03-24 11:15:40 +08:00
// end of support group
/** @} */
2012-06-20 18:09:11 +08:00
NS_CC_END
#endif // __CC_FILEUTILS_H__