From 3b87dd6be4ef99e8ab9fe8e0aaf7e373195c9ca8 Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 09:43:39 +0800 Subject: [PATCH 01/11] save --- .../platform/android/CCFileUtils-android.cpp | 2 + .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index c3c04abd9da8..b3e7a7cc8a52 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -227,6 +227,8 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons } else { + auto std::string fileListStr= JniHelper::callStaticStringMethod("org.cocos2dx.lib.Cocos2dxHelper", "getAssertFileList"); + // find it in apk's assets dir // Found "assets/" at the beginning of the path and we don't want it //CCLOG("find in apk dirPath(%s)", s); diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index d8fc5ba68bf3..ca6d5018630c 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -57,16 +57,23 @@ of this software and associated documentation files (the "Software"), to deal import com.enhance.gameservice.IGameTuningService; +import java.io.FileDescriptor; import java.io.IOException; import java.io.File; import java.io.FilenameFilter; import java.io.UnsupportedEncodingException; +import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.Vector; public class Cocos2dxHelper { @@ -288,6 +295,39 @@ public static AssetManager getAssetManager() { return Cocos2dxHelper.sAssetManager; } + public static String getAssertFileList(){ + AssetManager manager = getAssetManager(); + List stack = new ArrayList(); + List ret = new ArrayList(); + stack.add("/"); + while(!stack.isEmpty()) + { + stack.remove(stack.size() - 1); + String file = stack.get(stack.size() - 1); + String fileList[] = null; + try { + fileList = manager.list(file); + }catch (Exception e){} + if(fileList == null|| fileList.length == 0) + { + ret.add("0"+file); + } + else + { + ret.add("1"+file); + Collections.addAll(ret, fileList); + } + } + StringBuffer buffer = new StringBuffer(); + Iterator it = ret.iterator(); + while(it.hasNext()) + { + buffer.append(it.next()); + if(it.hasNext()) buffer.append("|"); + } + return buffer.toString(); + } + public static void enableAccelerometer() { Cocos2dxHelper.sAccelerometerEnabled = true; Cocos2dxHelper.getAccelerometer().enableAccel(); From ae935d12f16f47de661aa7f28ac2d9e04f2d1900 Mon Sep 17 00:00:00 2001 From: patricejiang Date: Thu, 6 Dec 2018 15:08:28 +0800 Subject: [PATCH 02/11] save --- cocos/base/ccConfig.h | 4 + .../platform/android/CCFileUtils-android.cpp | 70 +++++++++++++++++- cocos/platform/android/CCFileUtils-android.h | 10 +++ .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 25 ++++--- tests/cpp-tests/Classes/BaseTest.cpp | 20 +++++ .../Classes/FileUtilsTest/FileUtilsTest.cpp | 73 ++++++++++++------- 6 files changed, 165 insertions(+), 37 deletions(-) diff --git a/cocos/base/ccConfig.h b/cocos/base/ccConfig.h index 767ee4904429..43a81398741f 100644 --- a/cocos/base/ccConfig.h +++ b/cocos/base/ccConfig.h @@ -417,6 +417,10 @@ THE SOFTWARE. #define CC_STRIP_FPS 0 #endif +#ifndef CC_ENABLE_ANDROID_ASSET_PATH_CACHE +#define CC_ENABLE_ANDROID_ASSET_PATH_CACHE 1 +#endif + #define CC_LABEL_MAX_LENGTH ((1<<16)/4) #endif // __CCCONFIG_H__ diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index b3e7a7cc8a52..710d20adb935 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -80,6 +80,11 @@ FileUtils* FileUtils::getInstance() FileUtilsAndroid::FileUtilsAndroid() { +#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE + _assetCache = std::make_shared>(); + _assetCacheLoaded = std::make_shared(false); + _assetCacheLoading = std::make_shared(false); +#endif } FileUtilsAndroid::~FileUtilsAndroid() @@ -184,6 +189,14 @@ bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const } else if (FileUtilsAndroid::assetmanager) { +#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE + loadAssetCache(); + auto itCache = findInAssetCache(s); + if(itCache != -2) + { + return itCache == 0; + } +#endif AAsset* aa = AAssetManager_open(FileUtilsAndroid::assetmanager, s, AASSET_MODE_UNKNOWN); if (aa) { @@ -206,6 +219,52 @@ bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const return bFound; } +#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE + +void FileUtilsAndroid::loadAssetCache() const +{ + if(*_assetCacheLoaded || *_assetCacheLoading) return; + *_assetCacheLoading = true; + + auto assetCache = _assetCache; + auto assetCacheLoaded = _assetCacheLoaded; + auto assetCacheLoading = _assetCacheLoading; + + std::thread t([assetCache, assetCacheLoaded, assetCacheLoading](){ + CCLOG("CCFileUtilsAndroid: start indexing assets/ ... "); + std::string fileListStr= JniHelper::callStaticStringMethod("org.cocos2dx.lib.Cocos2dxHelper", "getAssetFileList"); + int i = 0; + do { + int sep = fileListStr.find("|", i); + if(sep == std::string::npos) + { + if( sep - i > 1)assetCache->emplace(fileListStr.substr(i + 1, sep - i - 1), fileListStr[i] - '0'); + break; + } + else + { + if( sep - i > 1) assetCache->emplace(fileListStr.substr(i + 1, sep - i - 1), fileListStr[i] - '0'); + } + i = sep + 1; + }while( i < fileListStr.length()); + *assetCacheLoaded = true; + *assetCacheLoading = false; + CCLOG("CCFileUtilsAndroid: finish indexing assets/"); + }); + + t.detach(); + +} + +int FileUtilsAndroid::findInAssetCache(const std::string &path) const +{ + if(!*_assetCacheLoaded) return -2; + auto it = _assetCache->find(path); + return it == _assetCache->end() ? -1 : it->second; +} + +#endif + bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) const { if (dirPath.empty()) @@ -227,7 +286,7 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons } else { - auto std::string fileListStr= JniHelper::callStaticStringMethod("org.cocos2dx.lib.Cocos2dxHelper", "getAssertFileList"); + // find it in apk's assets dir // Found "assets/" at the beginning of the path and we don't want it @@ -236,6 +295,15 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons { s += ASSETS_FOLDER_NAME_LENGTH; } +#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE + loadAssetCache(); + auto itCache = findInAssetCache(s); + if(itCache != -2) + { + return itCache == 1; + } +#endif + if (FileUtilsAndroid::assetmanager) { AAssetDir* aa = AAssetManager_openDir(FileUtilsAndroid::assetmanager, s); diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h index a20107548784..23e0788cfba8 100644 --- a/cocos/platform/android/CCFileUtils-android.h +++ b/cocos/platform/android/CCFileUtils-android.h @@ -34,6 +34,8 @@ Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. #include "base/ccTypes.h" #include #include +#include +#include #include "jni.h" #include "android/asset_manager.h" @@ -78,6 +80,14 @@ class CC_DLL FileUtilsAndroid : public FileUtils virtual bool isFileExistInternal(const std::string& strFilePath) const override; virtual bool isDirectoryExistInternal(const std::string& dirPath) const override; +#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE + void loadAssetCache() const; + int findInAssetCache(const std::string &path) const; + mutable std::shared_ptr> _assetCache; + mutable std::shared_ptr _assetCacheLoading; + mutable std::shared_ptr _assetCacheLoaded; +#endif + static AAssetManager* assetmanager; static ZipFile* obbfile; }; diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index ca6d5018630c..99280f2cde65 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -295,27 +295,30 @@ public static AssetManager getAssetManager() { return Cocos2dxHelper.sAssetManager; } - public static String getAssertFileList(){ + public static String getAssetFileList(){ AssetManager manager = getAssetManager(); List stack = new ArrayList(); List ret = new ArrayList(); - stack.add("/"); - while(!stack.isEmpty()) - { - stack.remove(stack.size() - 1); + stack.add(""); + while(!stack.isEmpty()) { String file = stack.get(stack.size() - 1); + stack.remove(stack.size() - 1); String fileList[] = null; try { fileList = manager.list(file); }catch (Exception e){} - if(fileList == null|| fileList.length == 0) - { + + if(fileList == null|| fileList.length == 0) { ret.add("0"+file); - } - else - { + } else { ret.add("1"+file); - Collections.addAll(ret, fileList); + if(file.isEmpty()) { + Collections.addAll(stack, fileList); + }else { + for (String sub : fileList) { + stack.add(file + "/" + sub); + } + } } } StringBuffer buffer = new StringBuffer(); diff --git a/tests/cpp-tests/Classes/BaseTest.cpp b/tests/cpp-tests/Classes/BaseTest.cpp index 3d97522013f2..e7e8bace64ed 100644 --- a/tests/cpp-tests/Classes/BaseTest.cpp +++ b/tests/cpp-tests/Classes/BaseTest.cpp @@ -311,18 +311,30 @@ void TestSuite::restartCurrTest() void TestSuite::enterNextTest() { + auto start = std::chrono::high_resolution_clock::now(); _currTestIndex = (_currTestIndex + 1) % _childTestNames.size(); auto scene = _testCallbacks[_currTestIndex](); + + auto end = std::chrono::high_resolution_clock::now(); + auto takes_time = std::chrono::nanoseconds(end - start).count(); + log("TestSuite::enterNextTest -create scene %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); + auto testCase = getTestCase(scene); testCase->setTestSuite(this); testCase->setTestCaseName(_childTestNames[_currTestIndex]); Director::getInstance()->replaceScene(scene); + + end = std::chrono::high_resolution_clock::now(); + takes_time = std::chrono::nanoseconds(end - start).count(); + log("TestSuite::enterNextTest takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); } void TestSuite::enterPreviousTest() { + + auto start = std::chrono::high_resolution_clock::now(); if (_currTestIndex > 0) { _currTestIndex -= 1; @@ -333,11 +345,19 @@ void TestSuite::enterPreviousTest() } auto scene = _testCallbacks[_currTestIndex](); + + auto end = std::chrono::high_resolution_clock::now(); + auto takes_time = std::chrono::nanoseconds(end - start).count(); + log("TestSuite::enterPreviousTest -create scene %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); + auto testCase = getTestCase(scene); testCase->setTestSuite(this); testCase->setTestCaseName(_childTestNames[_currTestIndex]); Director::getInstance()->replaceScene(scene); + end = std::chrono::high_resolution_clock::now(); + takes_time = std::chrono::nanoseconds(end - start).count(); + log("TestSuite::enterPreviousTest takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); } //TestCase diff --git a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp index 36331002fb40..158b9497cd55 100644 --- a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp +++ b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp @@ -24,12 +24,20 @@ #include "FileUtilsTest.h" +#include + USING_NS_CC; FileUtilsTests::FileUtilsTests() { - ADD_TEST_CASE(TestResolutionDirectories); - ADD_TEST_CASE(TestSearchPath); + //ADD_TEST_CASE(TestResolutionDirectories); + addTestCase("TestResolutionDirectories", []() ->cocos2d::Scene* { + return TestResolutionDirectories::create(); + }); + //ADD_TEST_CASE(TestSearchPath); + addTestCase("TestSearchPath", []() ->cocos2d::Scene* { + return TestSearchPath::create(); + }); ADD_TEST_CASE(TestFilenameLookup); ADD_TEST_CASE(TestIsFileExist); ADD_TEST_CASE(TestIsDirectoryExist); @@ -57,29 +65,35 @@ void TestResolutionDirectories::onEnter() auto sharedFileUtils = FileUtils::getInstance(); std::string ret; - - sharedFileUtils->purgeCachedEntries(); - _defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths(); - std::vector searchPaths = _defaultSearchPathArray; - searchPaths.insert(searchPaths.begin(), "Misc"); - sharedFileUtils->setSearchPaths(searchPaths); - - _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); - std::vector resolutionsOrder = _defaultResolutionsOrderArray; - - resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); - resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); - resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); - resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); - resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); - resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); - - sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); - - for( int i=1; i<7; i++) { - auto filename = StringUtils::format("test%d.txt", i); - ret = sharedFileUtils->fullPathForFilename(filename); - log("%s -> %s", filename.c_str(), ret.c_str()); + for(int i=0; i < 1;i++) + { + sharedFileUtils->purgeCachedEntries(); + auto start = std::chrono::high_resolution_clock::now(); + _defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths(); + std::vector searchPaths = _defaultSearchPathArray; + searchPaths.insert(searchPaths.begin(), "Misc"); + sharedFileUtils->setSearchPaths(searchPaths); + + _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); + std::vector resolutionsOrder = _defaultResolutionsOrderArray; + + resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); + resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); + resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); + resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); + resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); + resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); + + sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); + + for( int i=1; i<7; i++) { + auto filename = StringUtils::format("test%d.txt", i); + ret = sharedFileUtils->fullPathForFilename(filename); + //log("%s -> %s", filename.c_str(), ret.c_str()); + } + auto end = std::chrono::high_resolution_clock::now(); + auto takes_time = std::chrono::nanoseconds(end - start).count(); + log("TestResolutionDirectories: takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); } } @@ -112,7 +126,11 @@ void TestSearchPath::onEnter() std::string ret; + auto start = std::chrono::high_resolution_clock::now(); + for(int k=0;k<1;k++) + { sharedFileUtils->purgeCachedEntries(); + _defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths(); std::vector searchPaths = _defaultSearchPathArray; std::string writablePath = sharedFileUtils->getWritablePath(); @@ -183,6 +201,11 @@ void TestSearchPath::onEnter() // Recover old search paths sharedFileUtils->setSearchPaths(oldSearchPaths); #endif + + auto end = std::chrono::high_resolution_clock::now(); + auto takes_time = std::chrono::nanoseconds(end - start).count(); + log("TestSearchPath: takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); + } } void TestSearchPath::onExit() From 80b7d46c790ef6078bfe7f79b2d076e1baa2a16e Mon Sep 17 00:00:00 2001 From: patricejiang Date: Thu, 6 Dec 2018 15:55:09 +0800 Subject: [PATCH 03/11] save --- tests/cpp-tests/proj.android/app/AndroidManifest.xml | 2 +- tests/cpp-tests/proj.android/app/build.gradle | 2 +- tests/cpp-tests/proj.android/app/res/values/strings.xml | 2 +- .../src/org/cocos2dx/{cpp_tests => cpp_tests3}/AppActivity.java | 2 +- .../app/src/org/cocos2dx/{cpp_tests => cpp_tests3}/JNITest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename tests/cpp-tests/proj.android/app/src/org/cocos2dx/{cpp_tests => cpp_tests3}/AppActivity.java (98%) rename tests/cpp-tests/proj.android/app/src/org/cocos2dx/{cpp_tests => cpp_tests3}/JNITest.java (99%) diff --git a/tests/cpp-tests/proj.android/app/AndroidManifest.xml b/tests/cpp-tests/proj.android/app/AndroidManifest.xml index c7d83e557293..7fa295357b92 100644 --- a/tests/cpp-tests/proj.android/app/AndroidManifest.xml +++ b/tests/cpp-tests/proj.android/app/AndroidManifest.xml @@ -1,6 +1,6 @@ diff --git a/tests/cpp-tests/proj.android/app/build.gradle b/tests/cpp-tests/proj.android/app/build.gradle index 7fce939a168e..2851ff0f4d22 100644 --- a/tests/cpp-tests/proj.android/app/build.gradle +++ b/tests/cpp-tests/proj.android/app/build.gradle @@ -6,7 +6,7 @@ android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() defaultConfig { - applicationId "org.cocos2dx.cpp_tests" + applicationId "org.cocos2dx.cpp_tests3" minSdkVersion PROP_MIN_SDK_VERSION targetSdkVersion PROP_TARGET_SDK_VERSION versionCode 1 diff --git a/tests/cpp-tests/proj.android/app/res/values/strings.xml b/tests/cpp-tests/proj.android/app/res/values/strings.xml index 1f4d55e23aef..a2ba0c2e7794 100644 --- a/tests/cpp-tests/proj.android/app/res/values/strings.xml +++ b/tests/cpp-tests/proj.android/app/res/values/strings.xml @@ -1,3 +1,3 @@ - CppTests + CppFast diff --git a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java similarity index 98% rename from tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java rename to tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java index 896a910e2803..a7515410f2d5 100644 --- a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java +++ b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java @@ -22,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -package org.cocos2dx.cpp_tests; +package org.cocos2dx.cpp_tests3; import android.os.Bundle; diff --git a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java similarity index 99% rename from tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java rename to tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java index 4b621c7cf255..2b22344b7791 100644 --- a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java +++ b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java @@ -22,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -package org.cocos2dx.cpp_tests; +package org.cocos2dx.cpp_tests3; import android.util.Log; import java.lang.StringBuilder; From e60f7b4dd788ce61c6a2b37c922cb00a40db3606 Mon Sep 17 00:00:00 2001 From: patricejiang Date: Thu, 6 Dec 2018 16:46:14 +0800 Subject: [PATCH 04/11] add fullPathForDirectory --- cocos/platform/CCFileUtils.cpp | 69 ++++++++++++++++--- cocos/platform/CCFileUtils.h | 6 +- .../platform/android/CCFileUtils-android.cpp | 4 +- cocos/platform/android/CCFileUtils-android.h | 4 +- 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index 3ab33ac7120a..77c349ed32b2 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -808,7 +808,7 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std path += file_path; path += resolutionDirectory; - path = getFullPathForDirectoryAndFilename(path, file); + path = getFullPathForFilenameWithinDirectory(path, file); return path; } @@ -864,6 +864,59 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const return ""; } + +std::string FileUtils::fullPathForDirectory(const std::string &dir) const +{ + DECLARE_GUARD; + + if (dir.empty()) + { + return ""; + } + + if (isAbsolutePath(dir)) + { + return dir; + } + + // Already Cached ? + auto cacheIter = _fullPathCacheDir.find(dir); + if(cacheIter != _fullPathCacheDir.end()) + { + return cacheIter->second; + } + std::string longdir = dir; + std::string fullpath; + + if(longdir[longdir.length() - 1] != '/') + { + longdir +="/"; + } + + for (const auto& searchIt : _searchPathArray) + { + for (const auto& resolutionIt : _searchResolutionsOrderArray) + { + fullpath = isDirectoryExistInternal(searchIt + longdir + resolutionIt); + + if (!fullpath.empty()) + { + // Using the filename passed in as key. + _fullPathCacheDir.emplace(dir, fullpath); + return fullpath; + } + + } + } + + if(isPopupNotify()){ + CCLOG("cocos2d: fullPathForDirectory: No directory found at %s. Possible missing directory.", dir.c_str()); + } + + // The file wasn't found, return empty string. + return ""; +} + std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile) const { return relativeFile.substr(0, relativeFile.rfind('/')+1) + getNewFilename(filename); @@ -1053,7 +1106,7 @@ void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename } } -std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const +std::string FileUtils::getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const { // get directory+filename, safely adding '/' as necessary std::string ret = directory; @@ -1063,7 +1116,7 @@ std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& dir ret += filename; // if the file doesn't exist, return an empty string - if (!isFileExistInternal(ret) && !isDirectoryExistInternal(ret)) { + if (!isFileExistInternal(ret)) { ret = ""; } return ret; @@ -1122,7 +1175,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const for (const auto& resolutionIt : _searchResolutionsOrderArray) { // searchPath + file_path + resourceDirectory - fullpath = fullPathForFilename(searchIt + dirPath + resolutionIt); + fullpath = fullPathForDirectory(searchIt + dirPath + resolutionIt); if (isDirectoryExistInternal(fullpath)) { _fullPathCache.emplace(dirPath, fullpath); @@ -1188,7 +1241,7 @@ void FileUtils::getFileSize(const std::string &filepath, std::function)> callback) const { - auto fullPath = fullPathForFilename(dirPath); + auto fullPath = fullPathForDirectory(dirPath); performOperationOffthread([fullPath]() { return FileUtils::getInstance()->listFiles(fullPath); }, std::move(callback)); @@ -1196,7 +1249,7 @@ void FileUtils::listFilesAsync(const std::string& dirPath, std::function)> callback) const { - auto fullPath = fullPathForFilename(dirPath); + auto fullPath = fullPathForDirectory(dirPath); performOperationOffthread([fullPath]() { std::vector retval; FileUtils::getInstance()->listFilesRecursively(fullPath, &retval); @@ -1459,7 +1512,7 @@ long FileUtils::getFileSize(const std::string &filepath) const std::vector FileUtils::listFiles(const std::string& dirPath) const { std::vector files; - std::string fullpath = fullPathForFilename(dirPath); + std::string fullpath = fullPathForDirectory(dirPath); if (isDirectoryExist(fullpath)) { tinydir_dir dir; @@ -1497,7 +1550,7 @@ std::vector FileUtils::listFiles(const std::string& dirPath) const void FileUtils::listFilesRecursively(const std::string& dirPath, std::vector *files) const { - std::string fullpath = fullPathForFilename(dirPath); + std::string fullpath = fullPathForDirectory(dirPath); if (isDirectoryExist(fullpath)) { tinydir_dir dir; diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h index 130c4b39eb32..9c5ceaf5f013 100644 --- a/cocos/platform/CCFileUtils.h +++ b/cocos/platform/CCFileUtils.h @@ -343,6 +343,8 @@ class CC_DLL FileUtils */ virtual std::string fullPathForFilename(const std::string &filename) const; + virtual std::string fullPathForDirectory(const std::string &dirname) const; + /** * Loads the filenameLookup dictionary from the contents of a filename. * @@ -905,7 +907,7 @@ class CC_DLL FileUtils * @param filename The name of the file. * @return The full path of the file, if the file can't be found, it will return an empty string. */ - virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const; + virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const; /** @@ -955,6 +957,8 @@ class CC_DLL FileUtils */ mutable std::unordered_map _fullPathCache; + mutable std::unordered_map _fullPathCacheDir; + /** * Writable path. */ diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index 710d20adb935..9a94499be3d6 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -81,7 +81,7 @@ FileUtils* FileUtils::getInstance() FileUtilsAndroid::FileUtilsAndroid() { #if CC_ENABLE_ANDROID_ASSET_PATH_CACHE - _assetCache = std::make_shared>(); + _assetCache = std::make_shared>(); _assetCacheLoaded = std::make_shared(false); _assetCacheLoading = std::make_shared(false); #endif @@ -365,7 +365,7 @@ std::vector FileUtilsAndroid::listFiles(const std::string& dirPath) if(isAbsolutePath(dirPath)) return FileUtils::listFiles(dirPath); std::vector fileList; - string fullPath = fullPathForFilename(dirPath); + string fullPath = fullPathForDirectory(dirPath); static const std::string apkprefix("assets/"); string relativePath = ""; diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h index 23e0788cfba8..58e26a79eafb 100644 --- a/cocos/platform/android/CCFileUtils-android.h +++ b/cocos/platform/android/CCFileUtils-android.h @@ -34,7 +34,7 @@ Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. #include "base/ccTypes.h" #include #include -#include +#include #include #include "jni.h" #include "android/asset_manager.h" @@ -83,7 +83,7 @@ class CC_DLL FileUtilsAndroid : public FileUtils #if CC_ENABLE_ANDROID_ASSET_PATH_CACHE void loadAssetCache() const; int findInAssetCache(const std::string &path) const; - mutable std::shared_ptr> _assetCache; + mutable std::shared_ptr> _assetCache; mutable std::shared_ptr _assetCacheLoading; mutable std::shared_ptr _assetCacheLoaded; #endif From a152884d3e049e10faf8c792a2228d059f5c062f Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 17:22:22 +0800 Subject: [PATCH 05/11] rename method --- cocos/platform/CCFileUtils.h | 10 +++++++++- cocos/platform/apple/CCFileUtils-apple.h | 2 +- cocos/platform/apple/CCFileUtils-apple.mm | 2 +- cocos/platform/win32/CCFileUtils-win32.cpp | 4 ++-- cocos/platform/win32/CCFileUtils-win32.h | 2 +- cocos/platform/winrt/CCFileUtilsWinRT.cpp | 4 ++-- cocos/platform/winrt/CCFileUtilsWinRT.h | 2 +- 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h index 9c5ceaf5f013..31bec5f7e1d5 100644 --- a/cocos/platform/CCFileUtils.h +++ b/cocos/platform/CCFileUtils.h @@ -343,6 +343,10 @@ class CC_DLL FileUtils */ virtual std::string fullPathForFilename(const std::string &filename) const; + /** + * Returns the fullpath for a given dirname. + * @since 3.17.1 + */ virtual std::string fullPathForDirectory(const std::string &dirname) const; /** @@ -952,11 +956,15 @@ class CC_DLL FileUtils std::string _defaultResRootPath; /** - * The full path cache. When a file is found, it will be added into this cache. + * The full path cache for normal files. When a file is found, it will be added into this cache. * This variable is used for improving the performance of file search. */ mutable std::unordered_map _fullPathCache; + /** + * The full path cache for directories. When a diretory is found, it will be added into this cache. + * This variable is used for improving the performance of file search. + */ mutable std::unordered_map _fullPathCacheDir; /** diff --git a/cocos/platform/apple/CCFileUtils-apple.h b/cocos/platform/apple/CCFileUtils-apple.h index 5e700d6cdb14..8c659689f829 100644 --- a/cocos/platform/apple/CCFileUtils-apple.h +++ b/cocos/platform/apple/CCFileUtils-apple.h @@ -50,7 +50,7 @@ class CC_DLL FileUtilsApple : public FileUtils virtual ~FileUtilsApple(); /* override functions */ virtual std::string getWritablePath() const override; - virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const override; + virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const override; virtual ValueMap getValueMapFromFile(const std::string& filename) const override; virtual ValueMap getValueMapFromData(const char* filedata, int filesize) const override; diff --git a/cocos/platform/apple/CCFileUtils-apple.mm b/cocos/platform/apple/CCFileUtils-apple.mm index 92bbfb45a8b0..e0d2274d8155 100644 --- a/cocos/platform/apple/CCFileUtils-apple.mm +++ b/cocos/platform/apple/CCFileUtils-apple.mm @@ -315,7 +315,7 @@ static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, str return true; } -std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const +std::string FileUtilsApple::getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const { if (directory[0] != '/') { diff --git a/cocos/platform/win32/CCFileUtils-win32.cpp b/cocos/platform/win32/CCFileUtils-win32.cpp index 534e8072c33b..242ae2a606db 100644 --- a/cocos/platform/win32/CCFileUtils-win32.cpp +++ b/cocos/platform/win32/CCFileUtils-win32.cpp @@ -220,12 +220,12 @@ std::string FileUtilsWin32::getPathForFilename(const std::string& filename, cons return FileUtils::getPathForFilename(unixFileName, unixResolutionDirectory, unixSearchPath); } -std::string FileUtilsWin32::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename) const +std::string FileUtilsWin32::getFullPathForFilenameWithinDirectory(const std::string& strDirectory, const std::string& strFilename) const { std::string unixDirectory = convertPathFormatToUnixStyle(strDirectory); std::string unixFilename = convertPathFormatToUnixStyle(strFilename); - return FileUtils::getFullPathForDirectoryAndFilename(unixDirectory, unixFilename); + return FileUtils::getFullPathForFilenameWithinDirectory(unixDirectory, unixFilename); } void FileUtilsWin32::listFilesRecursively(const std::string& dirPath, std::vector *files) const diff --git a/cocos/platform/win32/CCFileUtils-win32.h b/cocos/platform/win32/CCFileUtils-win32.h index 4de669b82cd0..d1c2fccbef10 100644 --- a/cocos/platform/win32/CCFileUtils-win32.h +++ b/cocos/platform/win32/CCFileUtils-win32.h @@ -134,7 +134,7 @@ class CC_DLL FileUtilsWin32 : public FileUtils * @param filename The name of the file. * @return The full path of the file, if the file can't be found, it will return an empty string. */ - virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const override; + virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const override; /** * List all files in a directory. diff --git a/cocos/platform/winrt/CCFileUtilsWinRT.cpp b/cocos/platform/winrt/CCFileUtilsWinRT.cpp index afacc7fe4d68..749c48c775d4 100644 --- a/cocos/platform/winrt/CCFileUtilsWinRT.cpp +++ b/cocos/platform/winrt/CCFileUtilsWinRT.cpp @@ -119,11 +119,11 @@ std::string CCFileUtilsWinRT::getPathForFilename(const std::string& filename, co return FileUtils::getPathForFilename(unixFileName, unixResolutionDirectory, unixSearchPath); } -std::string CCFileUtilsWinRT::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename) const +std::string CCFileUtilsWinRT::getFullPathForFilenameWithinDirectory(const std::string& strDirectory, const std::string& strFilename) const { std::string unixDirectory = convertPathFormatToUnixStyle(strDirectory); std::string unixFilename = convertPathFormatToUnixStyle(strFilename); - return FileUtils::getFullPathForDirectoryAndFilename(unixDirectory, unixFilename); + return FileUtils::getFullPathForFilenameWithinDirectory(unixDirectory, unixFilename); } std::string CCFileUtilsWinRT::getSuitableFOpen(const std::string& filenameUtf8) const diff --git a/cocos/platform/winrt/CCFileUtilsWinRT.h b/cocos/platform/winrt/CCFileUtilsWinRT.h index ff2c35c7f6e8..431b8b314d5b 100644 --- a/cocos/platform/winrt/CCFileUtilsWinRT.h +++ b/cocos/platform/winrt/CCFileUtilsWinRT.h @@ -52,7 +52,7 @@ class CC_DLL CCFileUtilsWinRT : public FileUtils virtual std::string getWritablePath() const; virtual bool isAbsolutePath(const std::string& strPath) const; virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const override; - virtual std::string getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename) const override; + virtual std::string getFullPathForFilenameWithinDirectory(const std::string& strDirectory, const std::string& strFilename) const override; virtual std::string getSuitableFOpen(const std::string& filenameUtf8) const override; virtual long getFileSize(const std::string &filepath) override; virtual FileUtils::Status getContents(const std::string& filename, ResizableBuffer* buffer) override; From 73c3d89a33f069e6a692584a984ec230e46bedb6 Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 18:00:53 +0800 Subject: [PATCH 06/11] add test case --- cocos/platform/CCFileUtils.cpp | 5 ++- cocos/platform/win32/CCFileUtils-win32.cpp | 2 +- .../Classes/FileUtilsTest/FileUtilsTest.cpp | 39 +++++++++++++++++++ .../Classes/FileUtilsTest/FileUtilsTest.h | 11 ++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index 77c349ed32b2..3c65fd4a52d1 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -897,9 +897,10 @@ std::string FileUtils::fullPathForDirectory(const std::string &dir) const { for (const auto& resolutionIt : _searchResolutionsOrderArray) { - fullpath = isDirectoryExistInternal(searchIt + longdir + resolutionIt); + fullpath = searchIt + longdir + resolutionIt; + auto exists = isDirectoryExistInternal(fullpath); - if (!fullpath.empty()) + if (exists && !fullpath.empty()) { // Using the filename passed in as key. _fullPathCacheDir.emplace(dir, fullpath); diff --git a/cocos/platform/win32/CCFileUtils-win32.cpp b/cocos/platform/win32/CCFileUtils-win32.cpp index 242ae2a606db..d0cc12ebae8e 100644 --- a/cocos/platform/win32/CCFileUtils-win32.cpp +++ b/cocos/platform/win32/CCFileUtils-win32.cpp @@ -286,7 +286,7 @@ long FileUtilsWin32::getFileSize(const std::string &filepath) const std::vector FileUtilsWin32::listFiles(const std::string& dirPath) const { - std::string fullpath = fullPathForFilename(dirPath); + std::string fullpath = fullPathForDirectory(dirPath); std::vector files; if (isDirectoryExist(fullpath)) { diff --git a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp index 158b9497cd55..bdc660b2c955 100644 --- a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp +++ b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp @@ -55,6 +55,7 @@ FileUtilsTests::FileUtilsTests() ADD_TEST_CASE(TestFileFuncsAsync); ADD_TEST_CASE(TestWriteStringAsync); ADD_TEST_CASE(TestWriteDataAsync); + ADD_TEST_CASE(TestListFiles); } // TestResolutionDirectories @@ -1414,3 +1415,41 @@ std::string TestWriteDataAsync::subtitle() const { return ""; } + + +void TestListFiles::onEnter() +{ + FileUtilsDemo::onEnter(); + + auto winSize = Director::getInstance()->getWinSize(); + + auto infoLabel = Label::createWithTTF("show file count, should not be 0", "fonts/Thonburi.ttf", 18); + this->addChild(infoLabel); + infoLabel->setPosition(winSize.width / 2, winSize.height * 3 / 4); + + auto cntLabel = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18); + this->addChild(cntLabel); + cntLabel->setPosition(winSize.width / 2, winSize.height / 3); + // writeTest + auto list = FileUtils::getInstance()->listFiles("fonts"); + + char cntBuffer[200] = { 0 }; + snprintf(cntBuffer, 200, "%d", list.size()); + cntLabel->setString(cntBuffer); + +} + +void TestListFiles::onExit() +{ + FileUtilsDemo::onExit(); +} + +std::string TestListFiles::title() const +{ + return "FileUtils: list files of directory"; +} + +std::string TestListFiles::subtitle() const +{ + return ""; +} diff --git a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.h b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.h index 3706ebc80019..232e8a841733 100644 --- a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.h +++ b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.h @@ -258,4 +258,15 @@ class TestWriteDataAsync : public FileUtilsDemo virtual std::string subtitle() const override; }; +class TestListFiles : public FileUtilsDemo +{ +public: + CREATE_FUNC(TestListFiles); + + virtual void onEnter() override; + virtual void onExit() override; + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + #endif /* __FILEUTILSTEST_H__ */ From 75aeefec8a2ac13702d4dad4acd8b2c4a341941f Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 18:05:40 +0800 Subject: [PATCH 07/11] cleanup --- tests/cpp-tests/Classes/BaseTest.cpp | 20 --- .../Classes/FileUtilsTest/FileUtilsTest.cpp | 74 ++++------- .../proj.android/app/AndroidManifest.xml | 2 +- tests/cpp-tests/proj.android/app/build.gradle | 2 +- .../proj.android/app/res/values/strings.xml | 2 +- .../org/cocos2dx/cpp_tests/AppActivity.java | 47 +++++++ .../src/org/cocos2dx/cpp_tests/JNITest.java | 119 ++++++++++++++++++ 7 files changed, 194 insertions(+), 72 deletions(-) create mode 100644 tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java create mode 100644 tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java diff --git a/tests/cpp-tests/Classes/BaseTest.cpp b/tests/cpp-tests/Classes/BaseTest.cpp index e7e8bace64ed..3d97522013f2 100644 --- a/tests/cpp-tests/Classes/BaseTest.cpp +++ b/tests/cpp-tests/Classes/BaseTest.cpp @@ -311,30 +311,18 @@ void TestSuite::restartCurrTest() void TestSuite::enterNextTest() { - auto start = std::chrono::high_resolution_clock::now(); _currTestIndex = (_currTestIndex + 1) % _childTestNames.size(); auto scene = _testCallbacks[_currTestIndex](); - - auto end = std::chrono::high_resolution_clock::now(); - auto takes_time = std::chrono::nanoseconds(end - start).count(); - log("TestSuite::enterNextTest -create scene %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); - auto testCase = getTestCase(scene); testCase->setTestSuite(this); testCase->setTestCaseName(_childTestNames[_currTestIndex]); Director::getInstance()->replaceScene(scene); - - end = std::chrono::high_resolution_clock::now(); - takes_time = std::chrono::nanoseconds(end - start).count(); - log("TestSuite::enterNextTest takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); } void TestSuite::enterPreviousTest() { - - auto start = std::chrono::high_resolution_clock::now(); if (_currTestIndex > 0) { _currTestIndex -= 1; @@ -345,19 +333,11 @@ void TestSuite::enterPreviousTest() } auto scene = _testCallbacks[_currTestIndex](); - - auto end = std::chrono::high_resolution_clock::now(); - auto takes_time = std::chrono::nanoseconds(end - start).count(); - log("TestSuite::enterPreviousTest -create scene %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); - auto testCase = getTestCase(scene); testCase->setTestSuite(this); testCase->setTestCaseName(_childTestNames[_currTestIndex]); Director::getInstance()->replaceScene(scene); - end = std::chrono::high_resolution_clock::now(); - takes_time = std::chrono::nanoseconds(end - start).count(); - log("TestSuite::enterPreviousTest takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); } //TestCase diff --git a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp index bdc660b2c955..8f319d32f292 100644 --- a/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp +++ b/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp @@ -24,20 +24,12 @@ #include "FileUtilsTest.h" -#include - USING_NS_CC; FileUtilsTests::FileUtilsTests() { - //ADD_TEST_CASE(TestResolutionDirectories); - addTestCase("TestResolutionDirectories", []() ->cocos2d::Scene* { - return TestResolutionDirectories::create(); - }); - //ADD_TEST_CASE(TestSearchPath); - addTestCase("TestSearchPath", []() ->cocos2d::Scene* { - return TestSearchPath::create(); - }); + ADD_TEST_CASE(TestResolutionDirectories); + ADD_TEST_CASE(TestSearchPath); ADD_TEST_CASE(TestFilenameLookup); ADD_TEST_CASE(TestIsFileExist); ADD_TEST_CASE(TestIsDirectoryExist); @@ -66,35 +58,29 @@ void TestResolutionDirectories::onEnter() auto sharedFileUtils = FileUtils::getInstance(); std::string ret; - for(int i=0; i < 1;i++) - { - sharedFileUtils->purgeCachedEntries(); - auto start = std::chrono::high_resolution_clock::now(); - _defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths(); - std::vector searchPaths = _defaultSearchPathArray; - searchPaths.insert(searchPaths.begin(), "Misc"); - sharedFileUtils->setSearchPaths(searchPaths); - - _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); - std::vector resolutionsOrder = _defaultResolutionsOrderArray; - - resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); - resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); - resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); - resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); - resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); - resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); - - sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); - - for( int i=1; i<7; i++) { - auto filename = StringUtils::format("test%d.txt", i); - ret = sharedFileUtils->fullPathForFilename(filename); - //log("%s -> %s", filename.c_str(), ret.c_str()); - } - auto end = std::chrono::high_resolution_clock::now(); - auto takes_time = std::chrono::nanoseconds(end - start).count(); - log("TestResolutionDirectories: takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); + + sharedFileUtils->purgeCachedEntries(); + _defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths(); + std::vector searchPaths = _defaultSearchPathArray; + searchPaths.insert(searchPaths.begin(), "Misc"); + sharedFileUtils->setSearchPaths(searchPaths); + + _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); + std::vector resolutionsOrder = _defaultResolutionsOrderArray; + + resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); + resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); + resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); + resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); + resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); + resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); + + sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); + + for( int i=1; i<7; i++) { + auto filename = StringUtils::format("test%d.txt", i); + ret = sharedFileUtils->fullPathForFilename(filename); + log("%s -> %s", filename.c_str(), ret.c_str()); } } @@ -127,11 +113,7 @@ void TestSearchPath::onEnter() std::string ret; - auto start = std::chrono::high_resolution_clock::now(); - for(int k=0;k<1;k++) - { sharedFileUtils->purgeCachedEntries(); - _defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths(); std::vector searchPaths = _defaultSearchPathArray; std::string writablePath = sharedFileUtils->getWritablePath(); @@ -202,11 +184,6 @@ void TestSearchPath::onEnter() // Recover old search paths sharedFileUtils->setSearchPaths(oldSearchPaths); #endif - - auto end = std::chrono::high_resolution_clock::now(); - auto takes_time = std::chrono::nanoseconds(end - start).count(); - log("TestSearchPath: takes %f miliseconds ", (float)(takes_time * 1.0/ 1000000)); - } } void TestSearchPath::onExit() @@ -1416,7 +1393,6 @@ std::string TestWriteDataAsync::subtitle() const return ""; } - void TestListFiles::onEnter() { FileUtilsDemo::onEnter(); diff --git a/tests/cpp-tests/proj.android/app/AndroidManifest.xml b/tests/cpp-tests/proj.android/app/AndroidManifest.xml index 7fa295357b92..c7d83e557293 100644 --- a/tests/cpp-tests/proj.android/app/AndroidManifest.xml +++ b/tests/cpp-tests/proj.android/app/AndroidManifest.xml @@ -1,6 +1,6 @@ diff --git a/tests/cpp-tests/proj.android/app/build.gradle b/tests/cpp-tests/proj.android/app/build.gradle index 2851ff0f4d22..7fce939a168e 100644 --- a/tests/cpp-tests/proj.android/app/build.gradle +++ b/tests/cpp-tests/proj.android/app/build.gradle @@ -6,7 +6,7 @@ android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() defaultConfig { - applicationId "org.cocos2dx.cpp_tests3" + applicationId "org.cocos2dx.cpp_tests" minSdkVersion PROP_MIN_SDK_VERSION targetSdkVersion PROP_TARGET_SDK_VERSION versionCode 1 diff --git a/tests/cpp-tests/proj.android/app/res/values/strings.xml b/tests/cpp-tests/proj.android/app/res/values/strings.xml index a2ba0c2e7794..1f4d55e23aef 100644 --- a/tests/cpp-tests/proj.android/app/res/values/strings.xml +++ b/tests/cpp-tests/proj.android/app/res/values/strings.xml @@ -1,3 +1,3 @@ - CppFast + CppTests diff --git a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java new file mode 100644 index 000000000000..896a910e2803 --- /dev/null +++ b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/AppActivity.java @@ -0,0 +1,47 @@ +/**************************************************************************** +Copyright (c) 2015-2016 Chukong Technologies Inc. +Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +package org.cocos2dx.cpp_tests; + +import android.os.Bundle; + +import org.cocos2dx.lib.Cocos2dxActivity; + +public class AppActivity extends Cocos2dxActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.setEnableVirtualButton(false); + super.onCreate(savedInstanceState); + // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508 + if (!isTaskRoot()) { + // Android launched another instance of the root activity into an existing task + // so just quietly finish and go away, dropping the user back into the activity + // at the top of the stack (ie: the last state of this task) + // Don't need to finish it again since it's finished in super.onCreate . + return; + } + // DO OTHER INITIALIZATION BELOW + + } +} \ No newline at end of file diff --git a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java new file mode 100644 index 000000000000..4b621c7cf255 --- /dev/null +++ b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests/JNITest.java @@ -0,0 +1,119 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +package org.cocos2dx.cpp_tests; + +import android.util.Log; +import java.lang.StringBuilder; + +public class JNITest { + private static final String TAG = "JNITest"; + + public static void voidMethod1() { + Log.v(TAG, "Called voidMethod1"); + } + + public static void voidMethod2(final String str) { + StringBuilder message = new StringBuilder(); + message.append("Called voidMethod2 with str = "); + message.append(str); + Log.v(TAG, message.toString()); + } + + public static void voidMethod3(int n, float x, final String str) { + StringBuilder message = new StringBuilder(); + message.append("Called voidMethod3 with n = "); + message.append(n); + message.append(", x = "); + message.append(x); + message.append(", str = "); + message.append(str); + Log.v(TAG, message.toString()); + } + + public static void voidMethod4(final String str) { + // Used to test garbage collection + } + + public static boolean booleanMethod(int n) { + boolean ret = n > 0; + StringBuilder message = new StringBuilder(); + message.append("Called booleanMethod with n = "); + message.append(n); + message.append("\nReturning "); + message.append(ret); + Log.v(TAG, message.toString()); + return ret; + } + + public static int intMethod(int a, int b) { + int ret = a + b; + StringBuilder message = new StringBuilder(); + message.append("Called intMethod with a = "); + message.append(a); + message.append(", b = "); + message.append(b); + message.append("\nReturning "); + message.append(ret); + Log.v(TAG, message.toString()); + return ret; + } + + public static float floatMethod(float x, float y) { + float ret = x + y; + StringBuilder message = new StringBuilder(); + message.append("Called floatMethod with x = "); + message.append(x); + message.append(", y = "); + message.append(y); + message.append("\nReturning "); + message.append(ret); + Log.v(TAG, message.toString()); + return ret; + } + + public static double doubleMethod(double x, int n) { + double ret = x*n; + StringBuilder message = new StringBuilder(); + message.append("Called doubleMethod with x = "); + message.append(x); + message.append(", n = "); + message.append(n); + message.append("\nReturning "); + message.append(ret); + Log.v(TAG, message.toString()); + return ret; + } + + public static String stringMethod(final String str, boolean reverse) { + String ret = reverse ? new StringBuilder(str).reverse().toString() : str; + StringBuilder message = new StringBuilder(); + message.append("Called stringMethod with str = "); + message.append(str); + message.append("\nReturning "); + message.append(ret); + Log.v(TAG, message.toString()); + return ret; + } +} From 4af770af06c5cecf51a849fb55b5059f6ff26b0e Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 18:09:59 +0800 Subject: [PATCH 08/11] remove bad files --- .../org/cocos2dx/cpp_tests3/AppActivity.java | 47 ------- .../src/org/cocos2dx/cpp_tests3/JNITest.java | 119 ------------------ 2 files changed, 166 deletions(-) delete mode 100644 tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java delete mode 100644 tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java diff --git a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java deleted file mode 100644 index a7515410f2d5..000000000000 --- a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/AppActivity.java +++ /dev/null @@ -1,47 +0,0 @@ -/**************************************************************************** -Copyright (c) 2015-2016 Chukong Technologies Inc. -Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ -package org.cocos2dx.cpp_tests3; - -import android.os.Bundle; - -import org.cocos2dx.lib.Cocos2dxActivity; - -public class AppActivity extends Cocos2dxActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.setEnableVirtualButton(false); - super.onCreate(savedInstanceState); - // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508 - if (!isTaskRoot()) { - // Android launched another instance of the root activity into an existing task - // so just quietly finish and go away, dropping the user back into the activity - // at the top of the stack (ie: the last state of this task) - // Don't need to finish it again since it's finished in super.onCreate . - return; - } - // DO OTHER INITIALIZATION BELOW - - } -} \ No newline at end of file diff --git a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java b/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java deleted file mode 100644 index 2b22344b7791..000000000000 --- a/tests/cpp-tests/proj.android/app/src/org/cocos2dx/cpp_tests3/JNITest.java +++ /dev/null @@ -1,119 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ -package org.cocos2dx.cpp_tests3; - -import android.util.Log; -import java.lang.StringBuilder; - -public class JNITest { - private static final String TAG = "JNITest"; - - public static void voidMethod1() { - Log.v(TAG, "Called voidMethod1"); - } - - public static void voidMethod2(final String str) { - StringBuilder message = new StringBuilder(); - message.append("Called voidMethod2 with str = "); - message.append(str); - Log.v(TAG, message.toString()); - } - - public static void voidMethod3(int n, float x, final String str) { - StringBuilder message = new StringBuilder(); - message.append("Called voidMethod3 with n = "); - message.append(n); - message.append(", x = "); - message.append(x); - message.append(", str = "); - message.append(str); - Log.v(TAG, message.toString()); - } - - public static void voidMethod4(final String str) { - // Used to test garbage collection - } - - public static boolean booleanMethod(int n) { - boolean ret = n > 0; - StringBuilder message = new StringBuilder(); - message.append("Called booleanMethod with n = "); - message.append(n); - message.append("\nReturning "); - message.append(ret); - Log.v(TAG, message.toString()); - return ret; - } - - public static int intMethod(int a, int b) { - int ret = a + b; - StringBuilder message = new StringBuilder(); - message.append("Called intMethod with a = "); - message.append(a); - message.append(", b = "); - message.append(b); - message.append("\nReturning "); - message.append(ret); - Log.v(TAG, message.toString()); - return ret; - } - - public static float floatMethod(float x, float y) { - float ret = x + y; - StringBuilder message = new StringBuilder(); - message.append("Called floatMethod with x = "); - message.append(x); - message.append(", y = "); - message.append(y); - message.append("\nReturning "); - message.append(ret); - Log.v(TAG, message.toString()); - return ret; - } - - public static double doubleMethod(double x, int n) { - double ret = x*n; - StringBuilder message = new StringBuilder(); - message.append("Called doubleMethod with x = "); - message.append(x); - message.append(", n = "); - message.append(n); - message.append("\nReturning "); - message.append(ret); - Log.v(TAG, message.toString()); - return ret; - } - - public static String stringMethod(final String str, boolean reverse) { - String ret = reverse ? new StringBuilder(str).reverse().toString() : str; - StringBuilder message = new StringBuilder(); - message.append("Called stringMethod with str = "); - message.append(str); - message.append("\nReturning "); - message.append(ret); - Log.v(TAG, message.toString()); - return ret; - } -} From b7a1abd4273f00d845043678d941a613ed0a838b Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 18:17:05 +0800 Subject: [PATCH 09/11] some --- cocos/platform/CCFileUtils.h | 11 ++++++----- cocos/platform/winrt/CCFileUtilsWinRT.cpp | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h index 31bec5f7e1d5..db20f0124366 100644 --- a/cocos/platform/CCFileUtils.h +++ b/cocos/platform/CCFileUtils.h @@ -343,11 +343,6 @@ class CC_DLL FileUtils */ virtual std::string fullPathForFilename(const std::string &filename) const; - /** - * Returns the fullpath for a given dirname. - * @since 3.17.1 - */ - virtual std::string fullPathForDirectory(const std::string &dirname) const; /** * Loads the filenameLookup dictionary from the contents of a filename. @@ -914,6 +909,12 @@ class CC_DLL FileUtils virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const; + /** + * Returns the fullpath for a given dirname. + * @since 3.17.1 + */ + virtual std::string fullPathForDirectory(const std::string &dirname) const; + /** * mutex used to protect fields. */ diff --git a/cocos/platform/winrt/CCFileUtilsWinRT.cpp b/cocos/platform/winrt/CCFileUtilsWinRT.cpp index 749c48c775d4..ac8d9e5a5be9 100644 --- a/cocos/platform/winrt/CCFileUtilsWinRT.cpp +++ b/cocos/platform/winrt/CCFileUtilsWinRT.cpp @@ -380,7 +380,7 @@ string CCFileUtilsWinRT::getWritablePath() const void CCFileUtilsWinRT::listFilesRecursively(const std::string& dirPath, std::vector *files) const { - std::string fullpath = fullPathForFilename(dirPath); + std::string fullpath = fullPathForDirectory(dirPath); if (isDirectoryExist(fullpath)) { tinydir_dir dir; @@ -426,7 +426,7 @@ void CCFileUtilsWinRT::listFilesRecursively(const std::string& dirPath, std::vec std::vector CCFileUtilsWinRT::listFiles(const std::string& dirPath) const { - std::string fullpath = fullPathForFilename(dirPath); + std::string fullpath = fullPathForDirectory(dirPath); std::vector files; if (isDirectoryExist(fullpath)) { From 4e48937c411e07b085aa4691144d6d70400eb7b3 Mon Sep 17 00:00:00 2001 From: Arnold <397136899@qq.com> Date: Thu, 6 Dec 2018 18:18:57 +0800 Subject: [PATCH 10/11] remove CC_ENABLE_ANDROID_ASSET_PATH_CACHE" --- cocos/base/ccConfig.h | 4 -- .../platform/android/CCFileUtils-android.cpp | 67 ------------------- cocos/platform/android/CCFileUtils-android.h | 8 --- 3 files changed, 79 deletions(-) diff --git a/cocos/base/ccConfig.h b/cocos/base/ccConfig.h index 43a81398741f..767ee4904429 100644 --- a/cocos/base/ccConfig.h +++ b/cocos/base/ccConfig.h @@ -417,10 +417,6 @@ THE SOFTWARE. #define CC_STRIP_FPS 0 #endif -#ifndef CC_ENABLE_ANDROID_ASSET_PATH_CACHE -#define CC_ENABLE_ANDROID_ASSET_PATH_CACHE 1 -#endif - #define CC_LABEL_MAX_LENGTH ((1<<16)/4) #endif // __CCCONFIG_H__ diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index 9a94499be3d6..201f7dd2018b 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -80,11 +80,6 @@ FileUtils* FileUtils::getInstance() FileUtilsAndroid::FileUtilsAndroid() { -#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE - _assetCache = std::make_shared>(); - _assetCacheLoaded = std::make_shared(false); - _assetCacheLoading = std::make_shared(false); -#endif } FileUtilsAndroid::~FileUtilsAndroid() @@ -189,14 +184,6 @@ bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const } else if (FileUtilsAndroid::assetmanager) { -#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE - loadAssetCache(); - auto itCache = findInAssetCache(s); - if(itCache != -2) - { - return itCache == 0; - } -#endif AAsset* aa = AAssetManager_open(FileUtilsAndroid::assetmanager, s, AASSET_MODE_UNKNOWN); if (aa) { @@ -219,52 +206,6 @@ bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const return bFound; } -#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE - -void FileUtilsAndroid::loadAssetCache() const -{ - if(*_assetCacheLoaded || *_assetCacheLoading) return; - *_assetCacheLoading = true; - - auto assetCache = _assetCache; - auto assetCacheLoaded = _assetCacheLoaded; - auto assetCacheLoading = _assetCacheLoading; - - std::thread t([assetCache, assetCacheLoaded, assetCacheLoading](){ - CCLOG("CCFileUtilsAndroid: start indexing assets/ ... "); - std::string fileListStr= JniHelper::callStaticStringMethod("org.cocos2dx.lib.Cocos2dxHelper", "getAssetFileList"); - int i = 0; - do { - int sep = fileListStr.find("|", i); - if(sep == std::string::npos) - { - if( sep - i > 1)assetCache->emplace(fileListStr.substr(i + 1, sep - i - 1), fileListStr[i] - '0'); - break; - } - else - { - if( sep - i > 1) assetCache->emplace(fileListStr.substr(i + 1, sep - i - 1), fileListStr[i] - '0'); - } - i = sep + 1; - }while( i < fileListStr.length()); - *assetCacheLoaded = true; - *assetCacheLoading = false; - CCLOG("CCFileUtilsAndroid: finish indexing assets/"); - }); - - t.detach(); - -} - -int FileUtilsAndroid::findInAssetCache(const std::string &path) const -{ - if(!*_assetCacheLoaded) return -2; - auto it = _assetCache->find(path); - return it == _assetCache->end() ? -1 : it->second; -} - -#endif - bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) const { if (dirPath.empty()) @@ -295,14 +236,6 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons { s += ASSETS_FOLDER_NAME_LENGTH; } -#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE - loadAssetCache(); - auto itCache = findInAssetCache(s); - if(itCache != -2) - { - return itCache == 1; - } -#endif if (FileUtilsAndroid::assetmanager) { diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h index 58e26a79eafb..2153199b18ef 100644 --- a/cocos/platform/android/CCFileUtils-android.h +++ b/cocos/platform/android/CCFileUtils-android.h @@ -80,14 +80,6 @@ class CC_DLL FileUtilsAndroid : public FileUtils virtual bool isFileExistInternal(const std::string& strFilePath) const override; virtual bool isDirectoryExistInternal(const std::string& dirPath) const override; -#if CC_ENABLE_ANDROID_ASSET_PATH_CACHE - void loadAssetCache() const; - int findInAssetCache(const std::string &path) const; - mutable std::shared_ptr> _assetCache; - mutable std::shared_ptr _assetCacheLoading; - mutable std::shared_ptr _assetCacheLoaded; -#endif - static AAssetManager* assetmanager; static ZipFile* obbfile; }; From e67b9372e6c0c97f0ce9890dc3b7a36115cf2635 Mon Sep 17 00:00:00 2001 From: patricejiang Date: Thu, 6 Dec 2018 18:26:21 +0800 Subject: [PATCH 11/11] revert Cocos2dxHelper.java --- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 43 ------------------- 1 file changed, 43 deletions(-) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 99280f2cde65..d8fc5ba68bf3 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -57,23 +57,16 @@ of this software and associated documentation files (the "Software"), to deal import com.enhance.gameservice.IGameTuningService; -import java.io.FileDescriptor; import java.io.IOException; import java.io.File; import java.io.FilenameFilter; import java.io.UnsupportedEncodingException; -import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; import java.util.LinkedHashSet; -import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.Vector; public class Cocos2dxHelper { @@ -295,42 +288,6 @@ public static AssetManager getAssetManager() { return Cocos2dxHelper.sAssetManager; } - public static String getAssetFileList(){ - AssetManager manager = getAssetManager(); - List stack = new ArrayList(); - List ret = new ArrayList(); - stack.add(""); - while(!stack.isEmpty()) { - String file = stack.get(stack.size() - 1); - stack.remove(stack.size() - 1); - String fileList[] = null; - try { - fileList = manager.list(file); - }catch (Exception e){} - - if(fileList == null|| fileList.length == 0) { - ret.add("0"+file); - } else { - ret.add("1"+file); - if(file.isEmpty()) { - Collections.addAll(stack, fileList); - }else { - for (String sub : fileList) { - stack.add(file + "/" + sub); - } - } - } - } - StringBuffer buffer = new StringBuffer(); - Iterator it = ret.iterator(); - while(it.hasNext()) - { - buffer.append(it.next()); - if(it.hasNext()) buffer.append("|"); - } - return buffer.toString(); - } - public static void enableAccelerometer() { Cocos2dxHelper.sAccelerometerEnabled = true; Cocos2dxHelper.getAccelerometer().enableAccel();