@@ -808,7 +808,7 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
808
808
path += file_path;
809
809
path += resolutionDirectory;
810
810
811
- path = getFullPathForDirectoryAndFilename (path, file);
811
+ path = getFullPathForFilenameWithinDirectory (path, file);
812
812
813
813
return path;
814
814
}
@@ -864,6 +864,60 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const
864
864
return " " ;
865
865
}
866
866
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
+
867
921
std::string FileUtils::fullPathFromRelativeFile (const std::string &filename, const std::string &relativeFile) const
868
922
{
869
923
return relativeFile.substr (0 , relativeFile.rfind (' /' )+1 ) + getNewFilename (filename);
@@ -1053,7 +1107,7 @@ void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename
1053
1107
}
1054
1108
}
1055
1109
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
1057
1111
{
1058
1112
// get directory+filename, safely adding '/' as necessary
1059
1113
std::string ret = directory;
@@ -1063,7 +1117,7 @@ std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& dir
1063
1117
ret += filename;
1064
1118
1065
1119
// if the file doesn't exist, return an empty string
1066
- if (!isFileExistInternal (ret) && ! isDirectoryExistInternal (ret) ) {
1120
+ if (!isFileExistInternal (ret)) {
1067
1121
ret = " " ;
1068
1122
}
1069
1123
return ret;
@@ -1122,7 +1176,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
1122
1176
for (const auto & resolutionIt : _searchResolutionsOrderArray)
1123
1177
{
1124
1178
// searchPath + file_path + resourceDirectory
1125
- fullpath = fullPathForFilename (searchIt + dirPath + resolutionIt);
1179
+ fullpath = fullPathForDirectory (searchIt + dirPath + resolutionIt);
1126
1180
if (isDirectoryExistInternal (fullpath))
1127
1181
{
1128
1182
_fullPathCache.emplace (dirPath, fullpath);
@@ -1188,15 +1242,15 @@ void FileUtils::getFileSize(const std::string &filepath, std::function<void(long
1188
1242
1189
1243
void FileUtils::listFilesAsync (const std::string& dirPath, std::function<void (std::vector<std::string>)> callback) const
1190
1244
{
1191
- auto fullPath = fullPathForFilename (dirPath);
1245
+ auto fullPath = fullPathForDirectory (dirPath);
1192
1246
performOperationOffthread ([fullPath]() {
1193
1247
return FileUtils::getInstance ()->listFiles (fullPath);
1194
1248
}, std::move (callback));
1195
1249
}
1196
1250
1197
1251
void FileUtils::listFilesRecursivelyAsync (const std::string& dirPath, std::function<void (std::vector<std::string>)> callback) const
1198
1252
{
1199
- auto fullPath = fullPathForFilename (dirPath);
1253
+ auto fullPath = fullPathForDirectory (dirPath);
1200
1254
performOperationOffthread ([fullPath]() {
1201
1255
std::vector<std::string> retval;
1202
1256
FileUtils::getInstance ()->listFilesRecursively (fullPath, &retval);
@@ -1459,7 +1513,7 @@ long FileUtils::getFileSize(const std::string &filepath) const
1459
1513
std::vector<std::string> FileUtils::listFiles (const std::string& dirPath) const
1460
1514
{
1461
1515
std::vector<std::string> files;
1462
- std::string fullpath = fullPathForFilename (dirPath);
1516
+ std::string fullpath = fullPathForDirectory (dirPath);
1463
1517
if (isDirectoryExist (fullpath))
1464
1518
{
1465
1519
tinydir_dir dir;
@@ -1497,7 +1551,7 @@ std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
1497
1551
1498
1552
void FileUtils::listFilesRecursively (const std::string& dirPath, std::vector<std::string> *files) const
1499
1553
{
1500
- std::string fullpath = fullPathForFilename (dirPath);
1554
+ std::string fullpath = fullPathForDirectory (dirPath);
1501
1555
if (isDirectoryExist (fullpath))
1502
1556
{
1503
1557
tinydir_dir dir;
0 commit comments