mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Refactor fileutils (#2649)
This commit is contained in:
@@ -611,7 +611,6 @@ bool FileUtils::writeDataToFile(const Data& data, const std::string& fullPath)
|
||||
bool FileUtils::init()
|
||||
{
|
||||
_searchPathArray.push_back(_defaultResRootPath);
|
||||
_searchResolutionsOrderArray.push_back("");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -674,20 +673,6 @@ FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableB
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
|
||||
{
|
||||
CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
|
||||
(void)(mode); // mode is unused, as we do not support text mode any more...
|
||||
|
||||
Data d;
|
||||
if (getContents(filename, &d) != Status::OK) {
|
||||
*size = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return d.takeBuffer(size);
|
||||
}
|
||||
|
||||
unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size)
|
||||
{
|
||||
unsigned char * buffer = nullptr;
|
||||
@@ -733,25 +718,7 @@ unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, con
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string FileUtils::getNewFilename(const std::string &filename) const
|
||||
{
|
||||
std::string newFileName;
|
||||
|
||||
// in Lookup Filename dictionary ?
|
||||
auto iter = _filenameLookupDict.find(filename);
|
||||
|
||||
if (iter == _filenameLookupDict.end())
|
||||
{
|
||||
newFileName = filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
newFileName = iter->second.asString();
|
||||
}
|
||||
return newFileName;
|
||||
}
|
||||
|
||||
std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
|
||||
std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& searchPath) const
|
||||
{
|
||||
std::string file = filename;
|
||||
std::string file_path = "";
|
||||
@@ -762,10 +729,9 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
|
||||
file = filename.substr(pos+1);
|
||||
}
|
||||
|
||||
// searchPath + file_path + resourceDirectory
|
||||
// searchPath + file_path
|
||||
std::string path = searchPath;
|
||||
path += file_path;
|
||||
path += resolutionDirectory;
|
||||
|
||||
path = getFullPathForDirectoryAndFilename(path, file);
|
||||
|
||||
@@ -791,87 +757,27 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const
|
||||
return cacheIter->second;
|
||||
}
|
||||
|
||||
// Get the new file name.
|
||||
const std::string newFilename( getNewFilename(filename) );
|
||||
|
||||
std::string fullpath;
|
||||
|
||||
for (const auto& searchIt : _searchPathArray)
|
||||
{
|
||||
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
||||
fullpath = this->getPathForFilename(filename, searchIt);
|
||||
|
||||
if (!fullpath.empty())
|
||||
{
|
||||
fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);
|
||||
|
||||
if (!fullpath.empty())
|
||||
{
|
||||
// Using the filename passed in as key.
|
||||
_fullPathCache.insert(std::make_pair(filename, fullpath));
|
||||
return fullpath;
|
||||
}
|
||||
// Using the filename passed in as key.
|
||||
_fullPathCache.insert(std::make_pair(filename, fullpath));
|
||||
return fullpath;
|
||||
}
|
||||
}
|
||||
|
||||
if(isPopupNotify()){
|
||||
CC_LOG_DEBUG("fullPathForFilename: No file found at %s. Possible missing file.", filename.c_str());
|
||||
}
|
||||
|
||||
// The file wasn't found, return empty string.
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile)
|
||||
{
|
||||
return relativeFile.substr(0, relativeFile.rfind('/')+1) + getNewFilename(filename);
|
||||
}
|
||||
|
||||
void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
|
||||
{
|
||||
if (_searchResolutionsOrderArray == searchResolutionsOrder)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool existDefault = false;
|
||||
_fullPathCache.clear();
|
||||
_searchResolutionsOrderArray.clear();
|
||||
for(const auto& iter : searchResolutionsOrder)
|
||||
{
|
||||
std::string resolutionDirectory = iter;
|
||||
if (!existDefault && resolutionDirectory == "")
|
||||
{
|
||||
existDefault = true;
|
||||
}
|
||||
|
||||
if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
|
||||
{
|
||||
resolutionDirectory += "/";
|
||||
}
|
||||
|
||||
_searchResolutionsOrderArray.push_back(resolutionDirectory);
|
||||
}
|
||||
|
||||
if (!existDefault)
|
||||
{
|
||||
_searchResolutionsOrderArray.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
void FileUtils::addSearchResolutionsOrder(const std::string &order,const bool front)
|
||||
{
|
||||
std::string resOrder = order;
|
||||
if (!resOrder.empty() && resOrder[resOrder.length()-1] != '/')
|
||||
resOrder.append("/");
|
||||
|
||||
if (front) {
|
||||
_searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
|
||||
} else {
|
||||
_searchResolutionsOrderArray.push_back(resOrder);
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<std::string>& FileUtils::getSearchResolutionsOrder() const
|
||||
{
|
||||
return _searchResolutionsOrderArray;
|
||||
return relativeFile.substr(0, relativeFile.rfind('/')+1) + filename;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& FileUtils::getSearchPaths() const
|
||||
@@ -966,32 +872,6 @@ void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
|
||||
}
|
||||
}
|
||||
|
||||
void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
|
||||
{
|
||||
_fullPathCache.clear();
|
||||
_filenameLookupDict = filenameLookupDict;
|
||||
}
|
||||
|
||||
void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename)
|
||||
{
|
||||
const std::string fullPath = fullPathForFilename(filename);
|
||||
if (!fullPath.empty())
|
||||
{
|
||||
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
||||
if (!dict.empty())
|
||||
{
|
||||
ValueMap& metadata = dict["metadata"].asValueMap();
|
||||
int version = metadata["version"].asInt();
|
||||
if (version != 1)
|
||||
{
|
||||
CC_LOG_DEBUG("ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version, filename.c_str());
|
||||
return;
|
||||
}
|
||||
setFilenameLookupDictionary( dict["filenames"].asValueMap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
|
||||
{
|
||||
// get directory+filename, safely adding '/' as necessary
|
||||
@@ -1049,15 +929,12 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
|
||||
std::string fullpath;
|
||||
for (const auto& searchIt : _searchPathArray)
|
||||
{
|
||||
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
||||
// searchPath + file_path
|
||||
fullpath = fullPathForFilename(searchIt + dirPath);
|
||||
if (isDirectoryExistInternal(fullpath))
|
||||
{
|
||||
// searchPath + file_path + resourceDirectory
|
||||
fullpath = fullPathForFilename(searchIt + dirPath + resolutionIt);
|
||||
if (isDirectoryExistInternal(fullpath))
|
||||
{
|
||||
_fullPathCache.insert(std::make_pair(dirPath, fullpath));
|
||||
return true;
|
||||
}
|
||||
_fullPathCache.insert(std::make_pair(dirPath, fullpath));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -1428,23 +1305,6 @@ long FileUtils::getFileSize(const std::string &filepath)
|
||||
}
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Notification support when getFileData from invalid file path.
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/* Default to false, enable it by setPopupNotify if needed */
|
||||
static bool s_popupNotify = false;
|
||||
|
||||
void FileUtils::setPopupNotify(bool notify)
|
||||
{
|
||||
s_popupNotify = notify;
|
||||
}
|
||||
|
||||
bool FileUtils::isPopupNotify() const
|
||||
{
|
||||
return s_popupNotify;
|
||||
}
|
||||
|
||||
std::string FileUtils::getFileExtension(const std::string& filePath) const
|
||||
{
|
||||
std::string fileExtension;
|
||||
|
||||
@@ -245,17 +245,6 @@ public:
|
||||
}
|
||||
virtual Status getContents(const std::string& filename, ResizableBuffer* buffer);
|
||||
|
||||
/**
|
||||
* Gets resource file data
|
||||
*
|
||||
* @param[in] filename The resource file name which contains the path.
|
||||
* @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.
|
||||
*
|
||||
@@ -314,49 +303,6 @@ public:
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* Sets the filenameLookup dictionary.
|
||||
*
|
||||
* @param filenameLookupDict The dictionary for replacing filename.
|
||||
* @since v2.1
|
||||
*/
|
||||
virtual void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);
|
||||
|
||||
/**
|
||||
* Gets full path from a file name and the path of the relative file.
|
||||
* @param filename The file name.
|
||||
@@ -368,34 +314,6 @@ public:
|
||||
*/
|
||||
virtual std::string fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile);
|
||||
|
||||
/**
|
||||
* Sets the array that contains the search order of the resources.
|
||||
*
|
||||
* @param searchResolutionsOrder The source array that contains the search order of the resources.
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Append search order of the resources.
|
||||
*
|
||||
* @see setSearchResolutionsOrder(), fullPathForFilename().
|
||||
* @since v2.1
|
||||
*/
|
||||
virtual void addSearchResolutionsOrder(const std::string &order,const bool front=false);
|
||||
|
||||
/**
|
||||
* Gets the array that contains the search order of the resources.
|
||||
*
|
||||
* @see setSearchResolutionsOrder(const std::vector<std::string>&), fullPathForFilename(const char*).
|
||||
* @since v2.1
|
||||
* @lua NA
|
||||
*/
|
||||
virtual const std::vector<std::string>& getSearchResolutionsOrder() const;
|
||||
|
||||
/**
|
||||
* Sets the array of search paths.
|
||||
*
|
||||
@@ -463,16 +381,6 @@ public:
|
||||
*/
|
||||
virtual void setWritablePath(const std::string& writablePath);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @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.
|
||||
* @param filename The filename of the file to gets content.
|
||||
@@ -672,15 +580,6 @@ protected:
|
||||
*/
|
||||
virtual bool init();
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Checks whether a file exists without considering search paths and resolution orders.
|
||||
* @param filename The file (with absolute path) to look up for
|
||||
@@ -699,11 +598,10 @@ protected:
|
||||
* 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;
|
||||
virtual std::string getPathForFilename(const std::string& filename, const std::string& searchPath) const;
|
||||
|
||||
/**
|
||||
* Gets full path for the directory and the filename.
|
||||
@@ -717,21 +615,6 @@ protected:
|
||||
*/
|
||||
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.
|
||||
|
||||
@@ -101,58 +101,6 @@ bool FileUtilsAndroid::init()
|
||||
return FileUtils::init();
|
||||
}
|
||||
|
||||
std::string FileUtilsAndroid::getNewFilename(const std::string &filename) const
|
||||
{
|
||||
std::string newFileName = FileUtils::getNewFilename(filename);
|
||||
// ../xxx do not fix this path
|
||||
auto pos = newFileName.find("../");
|
||||
if (pos == std::string::npos || pos == 0)
|
||||
{
|
||||
return newFileName;
|
||||
}
|
||||
|
||||
std::vector<std::string> v(3);
|
||||
v.resize(0);
|
||||
auto change = false;
|
||||
size_t size = newFileName.size();
|
||||
size_t idx = 0;
|
||||
bool noexit = true;
|
||||
while (noexit)
|
||||
{
|
||||
pos = newFileName.find('/', idx);
|
||||
std::string tmp;
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
tmp = newFileName.substr(idx, size - idx);
|
||||
noexit = false;
|
||||
}else
|
||||
{
|
||||
tmp = newFileName.substr(idx, pos - idx + 1);
|
||||
}
|
||||
auto t = v.size();
|
||||
if (t > 0 && v[t-1].compare("../") != 0 &&
|
||||
(tmp.compare("../") == 0 || tmp.compare("..") == 0))
|
||||
{
|
||||
v.pop_back();
|
||||
change = true;
|
||||
}else
|
||||
{
|
||||
v.push_back(tmp);
|
||||
}
|
||||
idx = pos + 1;
|
||||
}
|
||||
|
||||
if (change)
|
||||
{
|
||||
newFileName.clear();
|
||||
for (auto &s : v)
|
||||
{
|
||||
newFileName.append(s);
|
||||
}
|
||||
}
|
||||
return newFileName;
|
||||
}
|
||||
|
||||
bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const
|
||||
{
|
||||
if (strFilePath.empty())
|
||||
|
||||
@@ -63,9 +63,6 @@ public:
|
||||
|
||||
/* override functions */
|
||||
bool init() override;
|
||||
|
||||
virtual std::string getNewFilename(const std::string &filename) const override;
|
||||
|
||||
virtual FileUtils::Status getContents(const std::string& filename, ResizableBuffer* buffer) override;
|
||||
|
||||
virtual std::string getWritablePath() const override;
|
||||
|
||||
@@ -203,13 +203,12 @@ FileUtils::Status FileUtilsWin32::getContents(const std::string& filename, Resiz
|
||||
return FileUtils::Status::OK;
|
||||
}
|
||||
|
||||
std::string FileUtilsWin32::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
|
||||
std::string FileUtilsWin32::getPathForFilename(const std::string& filename, const std::string& searchPath) const
|
||||
{
|
||||
std::string unixFileName = convertPathFormatToUnixStyle(filename);
|
||||
std::string unixResolutionDirectory = convertPathFormatToUnixStyle(resolutionDirectory);
|
||||
std::string unixSearchPath = convertPathFormatToUnixStyle(searchPath);
|
||||
|
||||
return FileUtils::getPathForFilename(unixFileName, unixResolutionDirectory, unixSearchPath);
|
||||
return FileUtils::getPathForFilename(unixFileName, unixSearchPath);
|
||||
}
|
||||
|
||||
std::string FileUtilsWin32::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename) const
|
||||
|
||||
@@ -114,11 +114,10 @@ protected:
|
||||
* 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 override;
|
||||
virtual std::string getPathForFilename(const std::string& filename, const std::string& searchPath) const override;
|
||||
|
||||
/**
|
||||
* Gets full path for the directory and the filename.
|
||||
|
||||
Reference in New Issue
Block a user