cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.

Home Page:https://www.cocos.com/en/cocos2d-x

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AAssetDir_getNextFileName filtered directories

HTMLgtMK opened this issue · comments

commented
  • cocos2d-x version: v3.17.2
  • devices test on: Huawei ATU-AL10
  • developing environments
    • NDK version: 21.4.7075529

I'd like to list all files and directories in the assets, but FileUtils#listFiles return files only.

After reading the source code, i found the code of CCFileUtils-android.cpp L340 --- function AAssetDir_getNextFileName will filter directories in the result list.

auto *dir = AAssetManager_openDir(assetmanager, relativePath.c_str());
    if(nullptr == dir) {
        LOGD("... FileUtilsAndroid::failed to open dir %s", relativePath.c_str());
        AAssetDir_close(dir);
        return fileList;
    }
    const char *tmpDir = nullptr;
    while((tmpDir = AAssetDir_getNextFileName(dir))!= nullptr)
    {
        string filepath(tmpDir);
        if(isDirectoryExistInternal(filepath)) filepath += "/";
        fileList.push_back(filepath);
    }
    AAssetDir_close(dir);

The source code of AAssetDir_getNextFileName is asset_manager.cpp.

126 | const char* AAssetDir_getNextFileName(AAssetDir* assetDir)
  | 127 | {
  | 128 | const char* returnName = NULL;
  | 129 | size_t index = assetDir->mCurFileIndex;
  | 130 | const size_t max = assetDir->mAssetDir->getFileCount();
  | 131 |  
  | 132 | // Find the next regular file; explicitly don't report directories even if the
  | 133 | // underlying implementation changes to report them.  At that point we can add
  | 134 | // a more general iterator to this native interface set if appropriate.
  | 135 | while ((index < max) && (assetDir->mAssetDir->getFileType(index) != kFileTypeRegular)) {
  | 136 | index++;
  | 137 | }
  | 138 |  
  | 139 | // still in bounds? then the one at 'index' is the next to be reported; generate
  | 140 | // the string to return and advance the iterator for next time.
  | 141 | if (index < max) {
  | 142 | assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
  | 143 | returnName = assetDir->mCachedFileName.string();
  | 144 | index++;
  | 145 | }
  | 146 |  
  | 147 | assetDir->mCurFileIndex = index;
  | 148 | return returnName;
  | 149 | }

So, is there anyone who can help to fix this problem? thx.