Skip to content

[bugfix] v3 Fileutils iOS isDirectory #20080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions cocos/platform/CCFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,11 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
return path;
}

std::string FileUtils::getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const
{
return searchPath + resolutionDiretory + dir;
}

std::string FileUtils::fullPathForFilename(const std::string &filename) const
{

Expand Down Expand Up @@ -893,14 +898,14 @@ std::string FileUtils::fullPathForDirectory(const std::string &dir) const
longdir +="/";
}

const std::string newdirname( getNewFilename(longdir) );

for (const auto& searchIt : _searchPathArray)
{
for (const auto& resolutionIt : _searchResolutionsOrderArray)
{
fullpath.assign(searchIt).append(longdir).append(resolutionIt);
auto exists = isDirectoryExistInternal(fullpath);

if (exists && !fullpath.empty())
fullpath = this->getPathForDirectory(newdirname, resolutionIt, searchIt);
if (!fullpath.empty() && isDirectoryExistInternal(fullpath))
{
// Using the filename passed in as key.
_fullPathCacheDir.emplace(dir, fullpath);
Expand Down Expand Up @@ -1164,30 +1169,10 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
if (isAbsolutePath(dirPath))
{
return isDirectoryExistInternal(dirPath);
} else {
auto fullPath = fullPathForDirectory(dirPath);
return !fullPath.empty();
}

// Already Cached ?
auto cacheIter = _fullPathCacheDir.find(dirPath);
if( cacheIter != _fullPathCacheDir.end() )
{
return isDirectoryExistInternal(cacheIter->second);
}

std::string fullpath;
for (const auto& searchIt : _searchPathArray)
{
for (const auto& resolutionIt : _searchResolutionsOrderArray)
{
// searchPath + file_path + resourceDirectory
fullpath = fullPathForDirectory(std::string(searchIt).append(dirPath).append(resolutionIt));
if (isDirectoryExistInternal(fullpath))
{
_fullPathCacheDir.emplace(dirPath, fullpath);
return true;
}
}
}
return false;
}

void FileUtils::isDirectoryExist(const std::string& fullPath, std::function<void(bool)> callback) const
Expand Down
7 changes: 5 additions & 2 deletions cocos/platform/CCFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,9 @@ class CC_DLL FileUtils
*/
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const;

virtual std::string getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const;


/**
* Gets full path for the directory and the filename.
*
Expand All @@ -907,8 +910,8 @@ class CC_DLL FileUtils
* @return The full path of the file, if the file can't be found, it will return an empty string.
*/
virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const;


/**
* Returns the fullpath for a given dirname.
* @since 3.17.1
Expand Down
2 changes: 2 additions & 0 deletions cocos/platform/apple/CCFileUtils-apple.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class CC_DLL FileUtilsApple : public FileUtils
#endif

virtual bool createDirectory(const std::string& path) const override;
virtual std::string getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const override;

private:
virtual bool isFileExistInternal(const std::string& filePath) const override;
virtual bool removeDirectory(const std::string& dirPath) const override;
Expand Down
28 changes: 28 additions & 0 deletions cocos/platform/apple/CCFileUtils-apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,34 @@ static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, str
return true;
}

std::string FileUtilsApple::getPathForDirectory(const std::string &dir, const std::string &resolutionDiretory, const std::string &searchPath) const
{
auto path = searchPath + resolutionDiretory + dir;

if(!path.empty() && path[path.length() -1] == '/') {
path.erase(path.end() - 1);
}

if(path[0] == '/')
{
BOOL isDir = false;
if([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:dir.c_str()]
isDirectory:&isDir]) {
return isDir ? path : "";
}
}
else
{
NSString *fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:path.c_str()]
ofType:nil];
if(fullpath != nil) {
return [fullpath UTF8String];
}
}
return "";
}


std::string FileUtilsApple::getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const
{
if (directory[0] != '/')
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ void TestGetContents::onEnter()

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


Expand Down