Skip to content
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

[android lua] improve performance of lua loader #19234

Merged
merged 2 commits into from
Dec 10, 2018
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
12 changes: 8 additions & 4 deletions cocos/platform/CCFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ void FileUtils::purgeCachedEntries()
{
DECLARE_GUARD;
_fullPathCache.clear();
_fullPathCacheDir.clear();
}

std::string FileUtils::getStringFromFile(const std::string& filename) const
Expand Down Expand Up @@ -935,6 +936,7 @@ void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& search
bool existDefault = false;

_fullPathCache.clear();
_fullPathCacheDir.clear();
_searchResolutionsOrderArray.clear();
for(const auto& iter : searchResolutionsOrder)
{
Expand Down Expand Up @@ -1010,6 +1012,7 @@ void FileUtils::setDefaultResourceRootPath(const std::string& path)
if (_defaultResRootPath != path)
{
_fullPathCache.clear();
_fullPathCacheDir.clear();
_defaultResRootPath = path;
if (!_defaultResRootPath.empty() && _defaultResRootPath[_defaultResRootPath.length()-1] != '/')
{
Expand All @@ -1028,6 +1031,7 @@ void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
_originalSearchPaths = searchPaths;

_fullPathCache.clear();
_fullPathCacheDir.clear();
_searchPathArray.clear();

for (const auto& path : _originalSearchPaths)
Expand Down Expand Up @@ -1084,6 +1088,7 @@ void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
{
DECLARE_GUARD;
_fullPathCache.clear();
_fullPathCacheDir.clear();
_filenameLookupDict = filenameLookupDict;
}

Expand Down Expand Up @@ -1115,7 +1120,6 @@ std::string FileUtils::getFullPathForFilenameWithinDirectory(const std::string&
ret += '/';
}
ret += filename;

// if the file doesn't exist, return an empty string
if (!isFileExistInternal(ret)) {
ret = "";
Expand Down Expand Up @@ -1164,8 +1168,8 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
}

// Already Cached ?
auto cacheIter = _fullPathCache.find(dirPath);
if( cacheIter != _fullPathCache.end() )
auto cacheIter = _fullPathCacheDir.find(dirPath);
if( cacheIter != _fullPathCacheDir.end() )
{
return isDirectoryExistInternal(cacheIter->second);
}
Expand All @@ -1179,7 +1183,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
fullpath = fullPathForDirectory(searchIt + dirPath + resolutionIt);
if (isDirectoryExistInternal(fullpath))
{
_fullPathCache.emplace(dirPath, fullpath);
_fullPathCacheDir.emplace(dirPath, fullpath);
return true;
}
}
Expand Down
13 changes: 12 additions & 1 deletion cocos/platform/android/CCFileUtils-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons
return false;
}

const char* s = dirPath.c_str();
std::string dirPathCopy = dirPath;
if(dirPathCopy[dirPathCopy.length() - 1] == '/')
{
dirPathCopy.erase(dirPathCopy.length() - 1);
}

const char* s = dirPathCopy.c_str();

// find absolute path in flash memory
if (s[0] == '/')
Expand Down Expand Up @@ -317,6 +323,11 @@ std::vector<std::string> FileUtilsAndroid::listFiles(const std::string& dirPath)
return fileList;
}

if(relativePath[relativePath.length() - 1] == '/')
{
relativePath.erase(relativePath.length() - 1);
}

auto *dir = AAssetManager_openDir(assetmanager, relativePath.c_str());
if(nullptr == dir) {
LOGD("... FileUtilsAndroid::failed to open dir %s", relativePath.c_str());
Expand Down
6 changes: 3 additions & 3 deletions cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ extern "C"
pos = prefix.find_first_of("?", pos + filename.length() + 1);
}
chunkName = prefix + BYTECODE_FILE_EXT;
if (utils->isFileExist(chunkName) && !utils->isDirectoryExist(chunkName))
if (utils->isFileExist(chunkName)) // && !utils->isDirectoryExist(chunkName))
{
chunk = utils->getDataFromFile(chunkName);
break;
}
else
{
chunkName = prefix + NOT_BYTECODE_FILE_EXT;
if (utils->isFileExist(chunkName) && !utils->isDirectoryExist(chunkName))
if (utils->isFileExist(chunkName) ) //&& !utils->isDirectoryExist(chunkName))
{
chunk = utils->getDataFromFile(chunkName);
break;
}
else
{
chunkName = prefix;
if (utils->isFileExist(chunkName) && !utils->isDirectoryExist(chunkName))
if (utils->isFileExist(chunkName)) // && !utils->isDirectoryExist(chunkName))
{
chunk = utils->getDataFromFile(chunkName);
break;
Expand Down
41 changes: 41 additions & 0 deletions tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ FileUtilsTests::FileUtilsTests()
ADD_TEST_CASE(TestWriteStringAsync);
ADD_TEST_CASE(TestWriteDataAsync);
ADD_TEST_CASE(TestListFiles);
ADD_TEST_CASE(TestIsFileExistRejectFolder);
}

// TestResolutionDirectories
Expand Down Expand Up @@ -1429,3 +1430,43 @@ std::string TestListFiles::subtitle() const
{
return "";
}



void TestIsFileExistRejectFolder::onEnter()
{
FileUtilsDemo::onEnter();

auto winSize = Director::getInstance()->getWinSize();

auto infoLabel = Label::createWithTTF("tests folder 'NavMesh/maps', expect to be false", "fonts/Thonburi.ttf", 18);
this->addChild(infoLabel);
infoLabel->setPosition(winSize.width / 2, winSize.height * 3 / 4);

auto cntLabel = Label::createWithTTF("waiting...", "fonts/Thonburi.ttf", 18);
this->addChild(cntLabel);
cntLabel->setPosition(winSize.width / 2, winSize.height / 3);

auto exists = FileUtils::getInstance()->isFileExist("NavMesh/maps");
auto isDirectory = FileUtils::getInstance()->isDirectoryExist("NavMesh/maps");

char cntBuffer[200] = { 0 };
snprintf(cntBuffer, 200, "isDir: %s, isFile: %s, %s", isDirectory ? "true": "false" , exists ? "true" : "false", exists ? "failure!" : "ok!" );
cntLabel->setString(cntBuffer);

}

void TestIsFileExistRejectFolder::onExit()
{
FileUtilsDemo::onExit();
}

std::string TestIsFileExistRejectFolder::title() const
{
return "FileUtils: isFileExist(direname)";
}

std::string TestIsFileExistRejectFolder::subtitle() const
{
return "";
}
11 changes: 11 additions & 0 deletions tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,15 @@ class TestListFiles : public FileUtilsDemo
virtual std::string subtitle() const override;
};

class TestIsFileExistRejectFolder : public FileUtilsDemo
{
public:
CREATE_FUNC(TestIsFileExistRejectFolder);

virtual void onEnter() override;
virtual void onExit() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};

#endif /* __FILEUTILSTEST_H__ */