-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Some fixes for search paths: #17435
Some fixes for search paths: #17435
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -860,9 +860,11 @@ std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, con | |
void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder) | ||
{ | ||
bool existDefault = false; | ||
std::vector<std::string> searchResolutionOrderCopied = searchResolutionsOrder; | ||
|
||
_fullPathCache.clear(); | ||
_searchResolutionsOrderArray.clear(); | ||
for(const auto& iter : searchResolutionsOrder) | ||
for(const auto& iter : searchResolutionOrderCopied) | ||
{ | ||
std::string resolutionDirectory = iter; | ||
if (!existDefault && resolutionDirectory == "") | ||
|
@@ -904,44 +906,62 @@ const std::vector<std::string>& FileUtils::getSearchResolutionsOrder() const | |
|
||
const std::vector<std::string>& FileUtils::getSearchPaths() const | ||
{ | ||
return _searchPathArray; | ||
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) | ||
|
@@ -962,10 +982,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sbrednikhin, @balmerdx Anyway, if you think I was wrong, feel free to submit a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't care where default resource path is, actually. My only point is that if you breaking regular code behavior, causing people to fix their code (which must rely on some order of paths), you should have some good reason for that. Not just thinking that it is better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK,you're totally right, thanks for reminding me. I will revert that line. I was wrong, I used it as 'my own my private branch', good luck guys, thank you guys. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, for understanding. We are very appreciate your work. |
||
} | ||
else | ||
{ | ||
_searchPathArray.push_back(path); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
*/ | ||
|
@@ -873,6 +878,11 @@ class CC_DLL FileUtils | |
*/ | ||
std::vector<std::string> _searchPathArray; | ||
|
||
/** | ||
* The search paths which was set by 'setSearchPaths' / 'addSearchPath'. | ||
*/ | ||
std::vector<std::string> _originalSearchPaths; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why need _originalSearchPaths There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FileUtils uses If If current search path is auto oldDefaultResourceRoot = fileUtils->getDefaultResourceRootPath(); // Suppose it return "" which is an empty string.
auto oldSearchPaths = fileUtils->getSearchPaths(); // oldSearchPaths is : ["bbb/", "ccc/"]
fileUtils->setDefaultResouceRootPath("aaa"); // oldSearchPath is : ["aaa/bbb/", "aaa/ccc"]
// oldSearchPaths will be modified by adding a prefix `aaa` since `_searchPathArray` was modified.
... ...
// reset resource root
fileUtils->setDefaultResourceRootPath(oldDefaultResourceRoot);
auto newSearchPath = fileUtils->getSearchPaths(); // newSearchPath is incorrect: ["aaa/bbb", "aaa/ccc"] since it returns `_searchPathArray` which was added by prefix 'aaa' |
||
|
||
/** | ||
* 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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why need to copy
searchResolutionsOrder
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose this invocation:
fileUtils->setSearchResolutionsOrder(_searchResolutionsOrderArray);
_searchResolutionsOrderArray will be cleared before the for loop.
The same situation could happen in setSearchPaths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will aways fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you are right. But i think it is better to check it at first like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For setSearchResolutionsOrders, we could.
But for setSearchPaths, we couldn't.
Because setSearchPaths will update
_searchPathArray
which will be affected by_defaultResourceRootPath
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, i just mean searchResolutionsOrder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@minggo , updated
setSearchResolutionsOrder
.