CCFileUtils-android.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "platform/CCPlatformConfig.h"
  23. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  24. #include "platform/android/CCFileUtils-android.h"
  25. #include "platform/android/jni/JniHelper.h"
  26. #include "platform/android/jni/JniImp.h"
  27. #include "android/asset_manager.h"
  28. #include "android/asset_manager_jni.h"
  29. #include "base/ZipUtils.h"
  30. #include <stdlib.h>
  31. #include <sys/stat.h>
  32. #define LOG_TAG "CCFileUtils-android.cpp"
  33. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
  34. #define ASSETS_FOLDER_NAME "@assets/"
  35. #ifndef JCLS_HELPER
  36. #define JCLS_HELPER "org/cocos2dx/lib/Cocos2dxHelper"
  37. #endif
  38. NS_CC_BEGIN
  39. AAssetManager* FileUtilsAndroid::assetmanager = nullptr;
  40. ZipFile* FileUtilsAndroid::obbfile = nullptr;
  41. void FileUtilsAndroid::setassetmanager(AAssetManager* a) {
  42. if (nullptr == a) {
  43. LOGD("setassetmanager : received unexpected nullptr parameter");
  44. return;
  45. }
  46. cocos2d::FileUtilsAndroid::assetmanager = a;
  47. }
  48. FileUtils* FileUtils::getInstance()
  49. {
  50. if (s_sharedFileUtils == nullptr)
  51. {
  52. s_sharedFileUtils = new FileUtilsAndroid();
  53. if (!s_sharedFileUtils->init())
  54. {
  55. delete s_sharedFileUtils;
  56. s_sharedFileUtils = nullptr;
  57. CCLOG("ERROR: Could not init CCFileUtilsAndroid");
  58. }
  59. }
  60. return s_sharedFileUtils;
  61. }
  62. FileUtilsAndroid::FileUtilsAndroid()
  63. {
  64. }
  65. FileUtilsAndroid::~FileUtilsAndroid()
  66. {
  67. if (obbfile)
  68. {
  69. delete obbfile;
  70. obbfile = nullptr;
  71. }
  72. }
  73. bool FileUtilsAndroid::init()
  74. {
  75. _defaultResRootPath = ASSETS_FOLDER_NAME;
  76. std::string assetsPath(getApkPathJNI());
  77. if (assetsPath.find("/obb/") != std::string::npos)
  78. {
  79. obbfile = new ZipFile(assetsPath);
  80. }
  81. return FileUtils::init();
  82. }
  83. std::string FileUtilsAndroid::getNewFilename(const std::string &filename) const
  84. {
  85. std::string newFileName = FileUtils::getNewFilename(filename);
  86. // ../xxx do not fix this path
  87. auto pos = newFileName.find("../");
  88. if (pos == std::string::npos || pos == 0)
  89. {
  90. return newFileName;
  91. }
  92. std::vector<std::string> v(3);
  93. v.resize(0);
  94. auto change = false;
  95. size_t size = newFileName.size();
  96. size_t idx = 0;
  97. bool noexit = true;
  98. while (noexit)
  99. {
  100. pos = newFileName.find('/', idx);
  101. std::string tmp;
  102. if (pos == std::string::npos)
  103. {
  104. tmp = newFileName.substr(idx, size - idx);
  105. noexit = false;
  106. }else
  107. {
  108. tmp = newFileName.substr(idx, pos - idx + 1);
  109. }
  110. auto t = v.size();
  111. if (t > 0 && v[t-1].compare("../") != 0 &&
  112. (tmp.compare("../") == 0 || tmp.compare("..") == 0))
  113. {
  114. v.pop_back();
  115. change = true;
  116. }else
  117. {
  118. v.push_back(tmp);
  119. }
  120. idx = pos + 1;
  121. }
  122. if (change)
  123. {
  124. newFileName.clear();
  125. for (auto &s : v)
  126. {
  127. newFileName.append(s);
  128. }
  129. }
  130. return newFileName;
  131. }
  132. bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const
  133. {
  134. if (strFilePath.empty())
  135. {
  136. return false;
  137. }
  138. bool bFound = false;
  139. // Check whether file exists in apk.
  140. if (strFilePath[0] != '/')
  141. {
  142. const char* s = strFilePath.c_str();
  143. // Found "@assets/" at the beginning of the path and we don't want it
  144. if (strFilePath.find(ASSETS_FOLDER_NAME) == 0) s += strlen(ASSETS_FOLDER_NAME);
  145. if (obbfile && obbfile->fileExists(s))
  146. {
  147. bFound = true;
  148. }
  149. else if (FileUtilsAndroid::assetmanager)
  150. {
  151. AAsset* aa = AAssetManager_open(FileUtilsAndroid::assetmanager, s, AASSET_MODE_UNKNOWN);
  152. if (aa)
  153. {
  154. bFound = true;
  155. AAsset_close(aa);
  156. } else {
  157. // CCLOG("[AssetManager] ... in APK %s, found = false!", strFilePath.c_str());
  158. }
  159. }
  160. }
  161. else
  162. {
  163. FILE *fp = fopen(strFilePath.c_str(), "r");
  164. if (fp)
  165. {
  166. bFound = true;
  167. fclose(fp);
  168. }
  169. }
  170. return bFound;
  171. }
  172. bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath_) const
  173. {
  174. if (dirPath_.empty())
  175. {
  176. return false;
  177. }
  178. std::string dirPath = dirPath_;
  179. if (dirPath[dirPath.length() - 1] == '/')
  180. {
  181. dirPath[dirPath.length() - 1] = '\0';
  182. }
  183. // find absolute path in flash memory
  184. if (dirPath[0] == '/')
  185. {
  186. CCLOG("find in flash memory dirPath(%s)", dirPath.c_str());
  187. struct stat st;
  188. if (stat(dirPath.c_str(), &st) == 0)
  189. {
  190. return S_ISDIR(st.st_mode);
  191. }
  192. }
  193. else
  194. {
  195. // find it in apk's assets dir
  196. // Found "@assets/" at the beginning of the path and we don't want it
  197. CCLOG("find in apk dirPath(%s)", dirPath.c_str());
  198. const char* s = dirPath.c_str();
  199. if (dirPath.find(_defaultResRootPath) == 0)
  200. {
  201. s += _defaultResRootPath.length();
  202. }
  203. if (FileUtilsAndroid::assetmanager)
  204. {
  205. AAssetDir* aa = AAssetManager_openDir(FileUtilsAndroid::assetmanager, s);
  206. if (aa && AAssetDir_getNextFileName(aa))
  207. {
  208. AAssetDir_close(aa);
  209. return true;
  210. }
  211. }
  212. }
  213. return false;
  214. }
  215. bool FileUtilsAndroid::isAbsolutePath(const std::string& strPath) const
  216. {
  217. // On Android, there are two situations for full path.
  218. // 1) Files in APK, e.g. assets/path/path/file.png
  219. // 2) Files not in APK, e.g. /data/data/org.cocos2dx.hellocpp/cache/path/path/file.png, or /sdcard/path/path/file.png.
  220. // So these two situations need to be checked on Android.
  221. if (strPath[0] == '/' || strPath.find(ASSETS_FOLDER_NAME) == 0)
  222. {
  223. return true;
  224. }
  225. return false;
  226. }
  227. FileUtils::Status FileUtilsAndroid::getContents(const std::string& filename, ResizableBuffer* buffer)
  228. {
  229. if (filename.empty())
  230. return FileUtils::Status::NotExists;
  231. std::string fullPath = fullPathForFilename(filename);
  232. if (fullPath.empty())
  233. return FileUtils::Status::NotExists;
  234. if (fullPath[0] == '/')
  235. return FileUtils::getContents(fullPath, buffer);
  236. std::string relativePath;
  237. size_t position = fullPath.find(ASSETS_FOLDER_NAME);
  238. if (0 == position) {
  239. // "@assets/" is at the beginning of the path and we don't want it
  240. relativePath += fullPath.substr(strlen(ASSETS_FOLDER_NAME));
  241. } else {
  242. relativePath = fullPath;
  243. }
  244. if (obbfile)
  245. {
  246. if (obbfile->getFileData(relativePath, buffer))
  247. return FileUtils::Status::OK;
  248. }
  249. if (nullptr == assetmanager) {
  250. LOGD("... FileUtilsAndroid::assetmanager is nullptr");
  251. return FileUtils::Status::NotInitialized;
  252. }
  253. AAsset* asset = AAssetManager_open(assetmanager, relativePath.data(), AASSET_MODE_UNKNOWN);
  254. if (nullptr == asset) {
  255. LOGD("asset (%s) is nullptr", filename.c_str());
  256. return FileUtils::Status::OpenFailed;
  257. }
  258. auto size = AAsset_getLength(asset);
  259. buffer->resize(size);
  260. int readsize = AAsset_read(asset, buffer->buffer(), size);
  261. AAsset_close(asset);
  262. if (readsize < size) {
  263. if (readsize >= 0)
  264. buffer->resize(readsize);
  265. return FileUtils::Status::ReadFailed;
  266. }
  267. return FileUtils::Status::OK;
  268. }
  269. std::string FileUtilsAndroid::getWritablePath() const
  270. {
  271. // Fix for Nexus 10 (Android 4.2 multi-user environment)
  272. // the path is retrieved through Java Context.getCacheDir() method
  273. std::string dir("");
  274. std::string tmp = JniHelper::callStaticStringMethod(JCLS_HELPER, "getWritablePath");
  275. if (tmp.length() > 0)
  276. {
  277. dir.append(tmp).append("/");
  278. return dir;
  279. }
  280. else
  281. {
  282. return "";
  283. }
  284. }
  285. NS_CC_END
  286. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID