Skip to content

Commit 96eb639

Browse files
PatriceJiangminggo
authored andcommitted
[bugfix] v3 Fileutils iOS isDirectory (#20080)
1 parent 1875590 commit 96eb639

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

cocos/platform/CCFileUtils.cpp

+12-27
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
813813
return path;
814814
}
815815

816+
std::string FileUtils::getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const
817+
{
818+
return searchPath + resolutionDiretory + dir;
819+
}
820+
816821
std::string FileUtils::fullPathForFilename(const std::string &filename) const
817822
{
818823

@@ -893,14 +898,14 @@ std::string FileUtils::fullPathForDirectory(const std::string &dir) const
893898
longdir +="/";
894899
}
895900

901+
const std::string newdirname( getNewFilename(longdir) );
902+
896903
for (const auto& searchIt : _searchPathArray)
897904
{
898905
for (const auto& resolutionIt : _searchResolutionsOrderArray)
899906
{
900-
fullpath.assign(searchIt).append(longdir).append(resolutionIt);
901-
auto exists = isDirectoryExistInternal(fullpath);
902-
903-
if (exists && !fullpath.empty())
907+
fullpath = this->getPathForDirectory(newdirname, resolutionIt, searchIt);
908+
if (!fullpath.empty() && isDirectoryExistInternal(fullpath))
904909
{
905910
// Using the filename passed in as key.
906911
_fullPathCacheDir.emplace(dir, fullpath);
@@ -1164,30 +1169,10 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
11641169
if (isAbsolutePath(dirPath))
11651170
{
11661171
return isDirectoryExistInternal(dirPath);
1172+
} else {
1173+
auto fullPath = fullPathForDirectory(dirPath);
1174+
return !fullPath.empty();
11671175
}
1168-
1169-
// Already Cached ?
1170-
auto cacheIter = _fullPathCacheDir.find(dirPath);
1171-
if( cacheIter != _fullPathCacheDir.end() )
1172-
{
1173-
return isDirectoryExistInternal(cacheIter->second);
1174-
}
1175-
1176-
std::string fullpath;
1177-
for (const auto& searchIt : _searchPathArray)
1178-
{
1179-
for (const auto& resolutionIt : _searchResolutionsOrderArray)
1180-
{
1181-
// searchPath + file_path + resourceDirectory
1182-
fullpath = fullPathForDirectory(std::string(searchIt).append(dirPath).append(resolutionIt));
1183-
if (isDirectoryExistInternal(fullpath))
1184-
{
1185-
_fullPathCacheDir.emplace(dirPath, fullpath);
1186-
return true;
1187-
}
1188-
}
1189-
}
1190-
return false;
11911176
}
11921177

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

cocos/platform/CCFileUtils.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,9 @@ class CC_DLL FileUtils
896896
*/
897897
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const;
898898

899+
virtual std::string getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const;
900+
901+
899902
/**
900903
* Gets full path for the directory and the filename.
901904
*
@@ -907,8 +910,8 @@ class CC_DLL FileUtils
907910
* @return The full path of the file, if the file can't be found, it will return an empty string.
908911
*/
909912
virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const;
910-
911-
913+
914+
912915
/**
913916
* Returns the fullpath for a given dirname.
914917
* @since 3.17.1

cocos/platform/apple/CCFileUtils-apple.h

+2
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

+28
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ void TestGetContents::onEnter()
717717

718718
// Text read string in text mode
719719
std::string ts = fs->getStringFromFile(_generatedFile);
720-
if (ts != "\r\n\r\n")
720+
if (strcmp(ts.c_str(), "\r\n\r\n")!=0)
721721
return std::string("failed: read as zero terminated string");
722722

723723

0 commit comments

Comments
 (0)