Skip to content

Commit

Permalink
Some fixes for search paths: (#17435)
Browse files Browse the repository at this point in the history
* Some fixes for search paths:
1. Adds ‘_originalSearchPaths’ variable, ’getSearchPaths’ returns the original values set by ‘setSearchPaths’  or ‘addSearchPath’.
2. Adds a getter function ‘getDefaultResourceRootPath’.
3. ‘setDefaultResourceRootPath’ should also update search paths and remove file path cache internally.
4. ‘setSearchPaths’  supports to pass self (_originalSearchPath), could be used in ‘setDefaultResourceRootPath’ to update the final ’_searchPathArray’ for searching full path.
5. ‘addSearchPath’ fix, the default resource root path should be the last element in ‘_searchPathArray’.

* Checks whether the parameter ‘searchResolutionOrder’ passed in is the same as ‘_searchResolutionsOrderArray’.

* Don’t break the compatibility, add a new method called ‘FileUtils::getOriginalSearchPaths’.
  • Loading branch information
James Chen authored and minggo committed Mar 6, 2017
1 parent 2983207 commit bdcacd0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
56 changes: 48 additions & 8 deletions cocos/platform/CCFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,13 @@ std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, con

void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
{
if (_searchResolutionsOrderArray == searchResolutionsOrder)
{
return;
}

bool existDefault = false;

_fullPathCache.clear();
_searchResolutionsOrderArray.clear();
for(const auto& iter : searchResolutionsOrder)
Expand Down Expand Up @@ -907,41 +913,64 @@ const std::vector<std::string>& FileUtils::getSearchPaths() const
return _searchPathArray;
}

const std::vector<std::string>& FileUtils::getOriginalSearchPaths() const
{
return _originalSearchPaths;
}

void FileUtils::setWritablePath(const std::string& writablePath)
{
_writablePath = writablePath;
}

const std::string& FileUtils::getDefaultResourceRootPath() const
{
return _defaultResRootPath;
}

void FileUtils::setDefaultResourceRootPath(const std::string& path)
{
_defaultResRootPath = path;
if (_defaultResRootPath != path)
{
_fullPathCache.clear();
_defaultResRootPath = path;
if (!_defaultResRootPath.empty() && _defaultResRootPath[_defaultResRootPath.length()-1] != '/')
{
_defaultResRootPath += '/';
}

// Updates search paths
setSearchPaths(_originalSearchPaths);
}
}

void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
{
bool existDefaultRootPath = false;
_originalSearchPaths = searchPaths;

_fullPathCache.clear();
_searchPathArray.clear();
for (const auto& iter : searchPaths)

for (const auto& path : _originalSearchPaths)
{
std::string prefix;
std::string path;
std::string fullPath;

if (!isAbsolutePath(iter))
if (!isAbsolutePath(path))
{ // Not an absolute path
prefix = _defaultResRootPath;
}
path = prefix + (iter);
fullPath = prefix + path;
if (!path.empty() && path[path.length()-1] != '/')
{
path += "/";
fullPath += "/";
}
if (!existDefaultRootPath && path == _defaultResRootPath)
{
existDefaultRootPath = true;
}
_searchPathArray.push_back(path);
_searchPathArray.push_back(fullPath);
}

if (!existDefaultRootPath)
Expand All @@ -962,10 +991,21 @@ void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
{
path += "/";
}

if (front) {
_originalSearchPaths.insert(_originalSearchPaths.begin(), searchpath);
_searchPathArray.insert(_searchPathArray.begin(), path);
} else {
_searchPathArray.push_back(path);
_originalSearchPaths.push_back(searchpath);

if (!_searchPathArray.empty() && _searchPathArray[_searchPathArray.size()-1] == _defaultResRootPath)
{
_searchPathArray.insert(_searchPathArray.begin() + _searchPathArray.size() -1, path);
}
else
{
_searchPathArray.push_back(path);
}
}
}

Expand Down
21 changes: 20 additions & 1 deletion cocos/platform/CCFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ class CC_DLL FileUtils
*/
virtual void setSearchPaths(const std::vector<std::string>& searchPaths);

/**
* Get default resource root path.
*/
const std::string& getDefaultResourceRootPath() const;

/**
* Set default resource root path.
*/
Expand All @@ -457,12 +462,21 @@ class CC_DLL FileUtils
/**
* Gets the array of search paths.
*
* @return The array of search paths.
* @return The array of search paths which may contain the prefix of default resource root path.
* @note In best practise, getter function should return the value of setter function passes in.
* But since we should not break the compatibility, we keep using the old logic.
* Therefore, If you want to get the original search paths, please call 'getOriginalSearchPaths()' instead.
* @see fullPathForFilename(const char*).
* @lua NA
*/
virtual const std::vector<std::string>& getSearchPaths() const;

/**
* Gets the original search path array set by 'setSearchPaths' or 'addSearchPath'.
* @return The array of the original search paths
*/
virtual const std::vector<std::string>& getOriginalSearchPaths() const;

/**
* Gets the writable path.
* @return The path that can be write/read a file in
Expand Down Expand Up @@ -873,6 +887,11 @@ class CC_DLL FileUtils
*/
std::vector<std::string> _searchPathArray;

/**
* The search paths which was set by 'setSearchPaths' / 'addSearchPath'.
*/
std::vector<std::string> _originalSearchPaths;

/**
* The default root path of resources.
* If the default root path of resources needs to be changed, do it in the `init` method of FileUtils's subclass.
Expand Down
21 changes: 20 additions & 1 deletion tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ void TestSearchPath::onEnter()
fclose(fp);
}
}

// Save old resource root path
std::string oldDefaultRootPath = sharedFileUtils->getDefaultResourceRootPath();
sharedFileUtils->setDefaultResourceRootPath("extensions");
auto sp1 = Sprite::create("orange_edit.png");
sp1->setPosition(VisibleRect::center());
addChild(sp1);

// Recover resource root path
sharedFileUtils->setDefaultResourceRootPath(oldDefaultRootPath);

auto oldSearchPaths = sharedFileUtils->getSearchPaths();
sharedFileUtils->addSearchPath("Images");
auto sp2 = Sprite::create("btn-about-normal.png");
sp2->setPosition(VisibleRect::center() + Vec2(0, -50));
addChild(sp2);

// Recover old search paths
sharedFileUtils->setSearchPaths(oldSearchPaths);
}

void TestSearchPath::onExit()
Expand All @@ -155,7 +174,7 @@ std::string TestSearchPath::title() const

std::string TestSearchPath::subtitle() const
{
return "See the console";
return "See the console, can see a orange box and a 'about' picture";
}

// TestFilenameLookup
Expand Down

0 comments on commit bdcacd0

Please sign in to comment.