BigFile.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // BigFile.cpp
  3. // HelloLua
  4. //
  5. // Created by mac on 14-11-27.
  6. //
  7. //
  8. #include "BigFile.h"
  9. #include "cocos2d.h"
  10. #include <assert.h>
  11. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  12. #include "platform/CCCommon.h"
  13. #include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
  14. #include "platform/android/jni/JniHelper.h"
  15. #include <android/log.h>
  16. #include <stdio.h>
  17. #include <jni.h>
  18. #include "platform/android/CCFileUtils-android.h"
  19. #else
  20. #endif
  21. USING_NS_CC;
  22. #define CONFIGFILENAME "bin/Data/mainData"
  23. BigFile* BigFile::s_sharedBigFile = NULL;
  24. bool BigFile::inited = false;
  25. BigFile::BigFile(bool useCache) {
  26. _useCache = useCache;
  27. b_configInited = false;
  28. _confFilePath= FileUtils::getInstance()->fullPathForFilename(CONFIGFILENAME);
  29. b_IsHunXiao = _confFilePath != "";
  30. if (b_IsHunXiao) {
  31. _assertDir = "";
  32. size_t pos = _confFilePath.find(CONFIGFILENAME);
  33. if (pos != string::npos){
  34. _assertDir = _confFilePath.substr(0, pos);
  35. }
  36. CCLOG("BigFile: assetDir:%s",_assertDir.c_str());
  37. }
  38. setupPackName();
  39. setupConfFileRoot();
  40. }
  41. BigFile* BigFile::getInstance()
  42. {
  43. if (s_sharedBigFile == NULL){
  44. s_sharedBigFile = new BigFile(false);
  45. inited = true;
  46. }
  47. return s_sharedBigFile;
  48. }
  49. void BigFile::setupPackName(){
  50. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  51. _packname = _getAndroidPackageName();
  52. #else
  53. _packname = _getIOSAppId();
  54. #endif
  55. CCLOG("packName:%s", _packname.c_str());
  56. }
  57. void BigFile::setupConfFileRoot(){
  58. if (b_IsHunXiao) {
  59. Data datas = FileUtils::getInstance()->getDataFromFile(_confFilePath);
  60. unsigned char* data = datas.getBytes();
  61. ssize_t len = datas.getSize();
  62. rc4_crypt(data,len);
  63. string err = "";
  64. string cfg(data,data+len);
  65. // CCLOG("cfg:%s",cfg.c_str());
  66. _confFileRoot = Json::parse(cfg, err);
  67. if (err == ""){
  68. CCLOG("BigFile: Setup mainData Succeed!");
  69. _pathCfgRoot = _confFileRoot["path"];
  70. _decCfgRoot = _confFileRoot["decode"];
  71. }
  72. else{
  73. CCLOG("BigFile: Setup failed:%s", err.c_str());
  74. }
  75. b_configInited = true;
  76. }
  77. }
  78. void BigFile::targetFileNameFromDirAndFile(string& directory, string& filename){
  79. if (b_IsHunXiao) {
  80. if(b_configInited){
  81. string oldPath = directory+filename;
  82. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  83. oldPath = oldPath.substr(strlen("assets/"));
  84. #endif
  85. Json arrayObj=_pathCfgRoot[oldPath];
  86. if(!arrayObj.is_null()){
  87. filename = arrayObj["fileName"].string_value();
  88. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  89. directory = "assets/";
  90. #else
  91. directory = "";
  92. #endif
  93. filename = directory + filename;
  94. size_t pos = filename.find_last_of("/");
  95. if (pos != string::npos){
  96. directory = filename.substr(0, pos+1);
  97. filename = filename.substr(pos+1);
  98. }
  99. CCLOG("BigFile: 地址重定向:%s -> %s \n",oldPath.c_str(), (directory+filename).c_str());
  100. }
  101. else{
  102. // CCLOG("BigFile: 找不到这货:%s",oldPath.c_str());
  103. }
  104. }
  105. }
  106. }
  107. bool BigFile::needDecode(string filepath){
  108. bool ret = false;
  109. if (b_IsHunXiao) {
  110. filepath.find(_assertDir);
  111. size_t pos = filepath.find(_assertDir);
  112. if (pos != string::npos){
  113. string fileName = filepath.substr(_assertDir.length());
  114. //check if encrypted...
  115. Json needRc4=_decCfgRoot[fileName];
  116. if(!needRc4.is_null()){
  117. ret = true;
  118. }
  119. }
  120. }
  121. return ret;
  122. }
  123. void BigFile::clearDataCache(){
  124. if(_useCache){
  125. _cachedDataDicMutex.lock();
  126. for (auto & item : _cachedDataDic) {
  127. free(get<0>(item.second));
  128. }
  129. _cachedDataDic.clear();
  130. _cachedDataDicMutex.unlock();
  131. }
  132. }
  133. void BigFile::saveDataToCache(string path, unsigned char* buffer, ssize_t size){
  134. if(_useCache){
  135. _cachedDataDicMutex.lock();
  136. path = path.substr(path.rfind('/')+1);
  137. unsigned char *tmp=NULL;
  138. tmp = (unsigned char*)malloc(size);
  139. memcpy(tmp, buffer, size);
  140. _cachedDataDic[path] = tuple<unsigned char*,unsigned long>{tmp, size};
  141. _cachedDataDicMutex.unlock();
  142. }
  143. }
  144. const unsigned char* BigFile::getDataFromCache(string path, ssize_t *size){
  145. if (_useCache) {
  146. _cachedDataDicMutex.lock();
  147. path = path.substr(path.rfind('/')+1);
  148. if (_cachedDataDic.find(path) != _cachedDataDic.end()) {
  149. auto &item = _cachedDataDic[path];
  150. *size = get<1>(item);
  151. unsigned char * buffer = get<0>(item);
  152. CCLOG("load from cache: %s",path.c_str());
  153. return buffer;
  154. }
  155. _cachedDataDicMutex.unlock();
  156. }
  157. return NULL;
  158. }
  159. void BigFile::rc4_crypt(unsigned char *Data, ssize_t Len) { //加解密
  160. const char * key = _packname.c_str();
  161. int x,y,j=0;
  162. int box[256];
  163. for (int i = 0; i < 256; ++i)
  164. {
  165. box[i]=i;
  166. }
  167. for (int i = 0; i < 256; ++i)
  168. {
  169. j=(key[i % strlen(key)] + box[i] + j)%256;
  170. x=box[i];
  171. box[i]=box[j];
  172. box[j]=x;
  173. }
  174. for (int i = 0; i < Len; ++i)
  175. {
  176. y = i%256;
  177. j = (box[y] + j) %256;
  178. x = box[y];
  179. box[y] = box[j];
  180. box[j] = x;
  181. Data[i]=Data[i] ^ box[(box[y] + box[j]) %256];
  182. }
  183. }