CCFileUtils-android.cpp 10 KB

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