Skip to content

Commit 55f6f6d

Browse files
PatriceJiangminggo
authored andcommitted
[bugfix] Fileutils iOS isDirectory (#20079)
1 parent eb8fffe commit 55f6f6d

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

cocos/platform/CCFileUtils.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
800800
return path;
801801
}
802802

803+
std::string FileUtils::getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const
804+
{
805+
return searchPath + resolutionDiretory + dir;
806+
}
807+
803808
std::string FileUtils::fullPathForFilename(const std::string &filename) const
804809
{
805810

@@ -880,14 +885,14 @@ std::string FileUtils::fullPathForDirectory(const std::string &dir) const
880885
longdir +="/";
881886
}
882887

888+
const std::string newdirname( getNewFilename(longdir) );
889+
883890
for (const auto& searchIt : _searchPathArray)
884891
{
885892
for (const auto& resolutionIt : _searchResolutionsOrderArray)
886893
{
887-
fullpath = searchIt + longdir + resolutionIt;
888-
auto exists = isDirectoryExistInternal(fullpath);
889-
890-
if (exists && !fullpath.empty())
894+
fullpath = this->getPathForDirectory(newdirname, resolutionIt, searchIt);
895+
if (!fullpath.empty() && isDirectoryExistInternal(fullpath))
891896
{
892897
// Using the filename passed in as key.
893898
_fullPathCacheDir.emplace(dir, fullpath);
@@ -1151,30 +1156,10 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
11511156
if (isAbsolutePath(dirPath))
11521157
{
11531158
return isDirectoryExistInternal(dirPath);
1159+
} else {
1160+
auto fullPath = fullPathForDirectory(dirPath);
1161+
return !fullPath.empty();
11541162
}
1155-
1156-
// Already Cached ?
1157-
auto cacheIter = _fullPathCacheDir.find(dirPath);
1158-
if( cacheIter != _fullPathCacheDir.end() )
1159-
{
1160-
return isDirectoryExistInternal(cacheIter->second);
1161-
}
1162-
1163-
std::string fullpath;
1164-
for (const auto& searchIt : _searchPathArray)
1165-
{
1166-
for (const auto& resolutionIt : _searchResolutionsOrderArray)
1167-
{
1168-
// searchPath + file_path + resourceDirectory
1169-
fullpath = fullPathForDirectory(searchIt + dirPath + resolutionIt);
1170-
if (isDirectoryExistInternal(fullpath))
1171-
{
1172-
_fullPathCacheDir.emplace(dirPath, fullpath);
1173-
return true;
1174-
}
1175-
}
1176-
}
1177-
return false;
11781163
}
11791164

11801165
void FileUtils::isDirectoryExist(const std::string& fullPath, std::function<void(bool)> callback) const

cocos/platform/CCFileUtils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ class CC_DLL FileUtils
879879
*/
880880
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const;
881881

882+
virtual std::string getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const;
883+
884+
882885
/**
883886
* Gets full path for the directory and the filename.
884887
*
@@ -890,8 +893,8 @@ class CC_DLL FileUtils
890893
* @return The full path of the file, if the file can't be found, it will return an empty string.
891894
*/
892895
virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const;
893-
894-
896+
897+
895898
/**
896899
* Returns the fullpath for a given dirname.
897900
* @since 3.17.1

cocos/platform/apple/CCFileUtils-apple.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class CC_DLL FileUtilsApple : public FileUtils
6262
#endif
6363

6464
virtual bool createDirectory(const std::string& path) const override;
65+
virtual std::string getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const override;
66+
6567
private:
6668
virtual bool isFileExistInternal(const std::string& filePath) const override;
6769
virtual bool removeDirectory(const std::string& dirPath) const override;

cocos/platform/apple/CCFileUtils-apple.mm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,34 @@ static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, str
315315
return true;
316316
}
317317

318+
std::string FileUtilsApple::getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const
319+
{
320+
auto path = searchPath + resolutionDiretory + dir;
321+
322+
if(!path.empty() && path[path.length() -1] == '/') {
323+
path.erase(path.end() - 1);
324+
}
325+
326+
if(path[0] == '/')
327+
{
328+
BOOL isDir = false;
329+
if([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:dir.c_str()]
330+
isDirectory:&isDir]) {
331+
return isDir ? path : "";
332+
}
333+
}
334+
else
335+
{
336+
NSString *fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:path.c_str()]
337+
ofType:nil];
338+
if(fullpath != nil) {
339+
return [fullpath UTF8String];
340+
}
341+
}
342+
return "";
343+
}
344+
345+
318346
std::string FileUtilsApple::getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const
319347
{
320348
if (directory[0] != '/')

tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ void TestGetContents::onEnter()
626626

627627
// Text read string in text mode
628628
std::string ts = fs->getStringFromFile(_generatedFile);
629-
if (ts != "\r\n\r\n")
629+
if (strcmp(ts.c_str(), "\r\n\r\n")!=0)
630630
return std::string("failed: read as zero terminated string");
631631

632632

0 commit comments

Comments
 (0)