@@ -808,7 +808,7 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
808808 path += file_path;
809809 path += resolutionDirectory;
810810
811- path = getFullPathForDirectoryAndFilename (path, file);
811+ path = getFullPathForFilenameWithinDirectory (path, file);
812812
813813 return path;
814814}
@@ -864,6 +864,60 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const
864864 return " " ;
865865}
866866
867+
868+ std::string FileUtils::fullPathForDirectory (const std::string &dir) const
869+ {
870+ DECLARE_GUARD;
871+
872+ if (dir.empty ())
873+ {
874+ return " " ;
875+ }
876+
877+ if (isAbsolutePath (dir))
878+ {
879+ return dir;
880+ }
881+
882+ // Already Cached ?
883+ auto cacheIter = _fullPathCacheDir.find (dir);
884+ if (cacheIter != _fullPathCacheDir.end ())
885+ {
886+ return cacheIter->second ;
887+ }
888+ std::string longdir = dir;
889+ std::string fullpath;
890+
891+ if (longdir[longdir.length () - 1 ] != ' /' )
892+ {
893+ longdir +=" /" ;
894+ }
895+
896+ for (const auto & searchIt : _searchPathArray)
897+ {
898+ for (const auto & resolutionIt : _searchResolutionsOrderArray)
899+ {
900+ fullpath = searchIt + longdir + resolutionIt;
901+ auto exists = isDirectoryExistInternal (fullpath);
902+
903+ if (exists && !fullpath.empty ())
904+ {
905+ // Using the filename passed in as key.
906+ _fullPathCacheDir.emplace (dir, fullpath);
907+ return fullpath;
908+ }
909+
910+ }
911+ }
912+
913+ if (isPopupNotify ()){
914+ CCLOG (" cocos2d: fullPathForDirectory: No directory found at %s. Possible missing directory." , dir.c_str ());
915+ }
916+
917+ // The file wasn't found, return empty string.
918+ return " " ;
919+ }
920+
867921std::string FileUtils::fullPathFromRelativeFile (const std::string &filename, const std::string &relativeFile) const
868922{
869923 return relativeFile.substr (0 , relativeFile.rfind (' /' )+1 ) + getNewFilename (filename);
@@ -1053,7 +1107,7 @@ void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename
10531107 }
10541108}
10551109
1056- std::string FileUtils::getFullPathForDirectoryAndFilename (const std::string& directory, const std::string& filename) const
1110+ std::string FileUtils::getFullPathForFilenameWithinDirectory (const std::string& directory, const std::string& filename) const
10571111{
10581112 // get directory+filename, safely adding '/' as necessary
10591113 std::string ret = directory;
@@ -1063,7 +1117,7 @@ std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& dir
10631117 ret += filename;
10641118
10651119 // if the file doesn't exist, return an empty string
1066- if (!isFileExistInternal (ret) && ! isDirectoryExistInternal (ret) ) {
1120+ if (!isFileExistInternal (ret)) {
10671121 ret = " " ;
10681122 }
10691123 return ret;
@@ -1122,7 +1176,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
11221176 for (const auto & resolutionIt : _searchResolutionsOrderArray)
11231177 {
11241178 // searchPath + file_path + resourceDirectory
1125- fullpath = fullPathForFilename (searchIt + dirPath + resolutionIt);
1179+ fullpath = fullPathForDirectory (searchIt + dirPath + resolutionIt);
11261180 if (isDirectoryExistInternal (fullpath))
11271181 {
11281182 _fullPathCache.emplace (dirPath, fullpath);
@@ -1188,15 +1242,15 @@ void FileUtils::getFileSize(const std::string &filepath, std::function<void(long
11881242
11891243void FileUtils::listFilesAsync (const std::string& dirPath, std::function<void (std::vector<std::string>)> callback) const
11901244{
1191- auto fullPath = fullPathForFilename (dirPath);
1245+ auto fullPath = fullPathForDirectory (dirPath);
11921246 performOperationOffthread ([fullPath]() {
11931247 return FileUtils::getInstance ()->listFiles (fullPath);
11941248 }, std::move (callback));
11951249}
11961250
11971251void FileUtils::listFilesRecursivelyAsync (const std::string& dirPath, std::function<void (std::vector<std::string>)> callback) const
11981252{
1199- auto fullPath = fullPathForFilename (dirPath);
1253+ auto fullPath = fullPathForDirectory (dirPath);
12001254 performOperationOffthread ([fullPath]() {
12011255 std::vector<std::string> retval;
12021256 FileUtils::getInstance ()->listFilesRecursively (fullPath, &retval);
@@ -1459,7 +1513,7 @@ long FileUtils::getFileSize(const std::string &filepath) const
14591513std::vector<std::string> FileUtils::listFiles (const std::string& dirPath) const
14601514{
14611515 std::vector<std::string> files;
1462- std::string fullpath = fullPathForFilename (dirPath);
1516+ std::string fullpath = fullPathForDirectory (dirPath);
14631517 if (isDirectoryExist (fullpath))
14641518 {
14651519 tinydir_dir dir;
@@ -1497,7 +1551,7 @@ std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
14971551
14981552void FileUtils::listFilesRecursively (const std::string& dirPath, std::vector<std::string> *files) const
14991553{
1500- std::string fullpath = fullPathForFilename (dirPath);
1554+ std::string fullpath = fullPathForDirectory (dirPath);
15011555 if (isDirectoryExist (fullpath))
15021556 {
15031557 tinydir_dir dir;
0 commit comments