2023年7月3日发(作者:)
cocos2d-js直接解压运⾏本地zip⽂件这⼏天在尝试使⽤cocos2d-js解压运⾏本地zip⽂件,⽤的是cocos2d-x 3.13版本,xcode8.2。之前想到的是既然在热更新的时候,已经有从服务端下载zip⽂件解压运⾏的接⼝,那么直接把解压运⾏的接⼝提取出来封装成单独⼀个接⼝应该也是可⾏的。因为之前我这边的热更新已经做好了,所以修改直接在我这个热更新demo上继续。1、分析热更新时下载解压zip⽂件的流程;1. ⾸先找到js代码中初始化AssetsManager的地⽅;
2. 然后按⽬录打开roj。/TestJs001/frameworks/cocos2d-x/cocos/scripting/js-bindings/_mac;
3. 在此处搜索AssetsManager,定位到jsb_cocos2dx_extension_类,接下来就是在这⾥做⽂章了。
4. 通过在⼏个可能会调到的⽅法⾥添加了log发现,会调⽤到AssetsManager的构造函数。这样就可以直接去C++那边查看构造函数那边是如何做处理的了。 5. 按⽬录打开cocos2d_roj。/TestJs001/frameworks/cocos2d-x/build/cocos2d_roj;
6. 搜索找到AssetsManagerEx.h,及其构造函数。AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath);
7. 分析了下,加载过程,会发现在加载成功时会调⽤onSuccess⽅法,然后就会调⽤到这个decompressDownloadedZip(),再进⽽调⽤到decompress(),decompress⼀听就知道是⼲解压事情的,那么接下来就是在这个⽅法上动⼿啦。 2、加⼊解压zip⽂件的接⼝.1 拷贝⼀份decompress⽅法,重新命名为decompressLocalZip,即
bool AssetsManagerEx::decompressLocalZip(const std::string &zip),并在.h⽂件中添加相应头⽂件,注意要添加到public下,这样js-binding才能调⽤到。2 修改,bool AssetsManagerEx::decompressLocalZip(const std::string &zip){ CCLOG("AssetsManagerEx : decompressLocalZip zip = %s ..........." , zip.c_str()); FileUtils* fileTtils = FileUtils::getInstance(); //获得可写的具体⽂件路径 std::string writeablePath = fileTtils->getWritablePath(); size_t pos_0 = _last_of("/"); if (pos_0 == std::string::npos) { CCLOG("ip not exit"); return false; } std::string zipName = (pos_0 + 1, strlen(zip.c_str())); log("zipName = %s",zipName.c_str()); std::string xmlPath = writeablePath + zipName; log("xmlPath = %s",xmlPath.c_str()); std::string curFullPath = fileTtils->fullPathForFilename(zip.c_str()); CCLOG("AssetsManagerEx : decompress fullPath = %s ..........." , curFullPath.c_str()); CCLOG("AssetsManagerEx : decompress xmlPath = %s ..........." , xmlPath.c_str()); // Find root path for zip file size_t pos = _last_of("/"); if (pos == std::string::npos) { CCLOG("AssetsManagerEx : no root path specified for curFullPath file %sn", curFullPath.c_str()); return false; } const std::string rootPath = (0, pos+1); CCLOG("AssetsManagerEx : rootPath = %s ...........1111" , rootPath.c_str()); CCLOG("AssetsManagerEx : getSuitableFOpen zip = %s ..........." , FileUtils::getInstance()->getSuitableFOpen(curFullPath).c_str()); // Open the zip file unzFile zipfile = unzOpen(FileUtils::getInstance()->getSuitableFOpen(curFullPath).c_str()); if (! zipfile) { CCLOG("AssetsManagerEx : can not open downloaded zip file %sn", curFullPath.c_str()); return false; } // Get info about the zip file unz_global_info global_info; if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK) { CCLOG("AssetsManagerEx : can not read file global info of %sn", curFullPath.c_str()); unzClose(zipfile); return false; return false; } // Buffer to hold data read from the zip file char readBuffer[BUFFER_SIZE]; // Loop to extract all files. uLong i; for (i = 0; i < global__entry; ++i) { // Get info about current file. unz_file_info fileInfo; char fileName[MAX_FILENAME]; if (unzGetCurrentFileInfo(zipfile, &fileInfo, fileName, MAX_FILENAME, NULL, 0, NULL, 0) != UNZ_OK) { CCLOG("AssetsManagerEx : can not read compressed file infon"); unzClose(zipfile); return false; } const std::string fullPath = rootPath + fileName; CCLOG("AssetsManagerEx : create directory fullPath %sn", fullPath.c_str()); // Check if this entry is a directory or a file. const size_t filenameLength = strlen(fileName); if (fileName[filenameLength-1] == '/') { //There are not directory entry in some case. //So we need to create directory when decompressing file entry if ( !_fileUtils->createDirectory(basename(fullPath)) ) { // Failed to create directory CCLOG("AssetsManagerEx : can not create directory %sn", fullPath.c_str()); unzClose(zipfile); return false; } } else { // jsb depress zip xuyuanteng add 20170324 std::string dir = basename(fullPath); CCLOG("AssetsManagerEx : basename dir %sn", dir.c_str()); if(!_fileUtils->isDirectoryExist(dir)) { CCLOG("AssetsManagerEx : isDirectoryExist no no no"); if(!_fileUtils->createDirectory(dir)) { // Failed to create directory CCLOG("AssetsManagerEx : can not create directory %sn", fullPath.c_str()); unzClose(zipfile); return false; } } // Entry is a file, so extract it. // Open current file. if (unzOpenCurrentFile(zipfile) != UNZ_OK) { CCLOG("AssetsManagerEx : can not extract file %sn", fileName); unzClose(zipfile); unzClose(zipfile); return false; } // Create a file to store current file. CCLOG("AssetsManagerEx : fullPath = %sn", fullPath.c_str()); CCLOG("AssetsManagerEx : fopen getSuitableFOpen fullPath = %sn", FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str()); FILE *out = fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), "wb"); if (!out) { CCLOG("AssetsManagerEx : can not create decompress destination file %sn", fullPath.c_str()); unzCloseCurrentFile(zipfile); unzClose(zipfile); return false; } // Write current file content to destinate file. int error = UNZ_OK; do { error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE); if (error < 0) { CCLOG("AssetsManagerEx : can not read zip file %s, error code is %dn", fileName, error); fclose(out); unzCloseCurrentFile(zipfile); unzClose(zipfile); return false; } if (error > 0) { fwrite(readBuffer, error, 1, out); } } while(error > 0); fclose(out); } unzCloseCurrentFile(zipfile); // Goto next entry listed in the zip file. if ((i+1) < global__entry) { if (unzGoToNextFile(zipfile) != UNZ_OK) { CCLOG("AssetsManagerEx : can not read next file for decompressingn"); unzClose(zipfile); return false; } } } unzClose(zipfile); return true;}3 回到jsb_cocos2dx_extension_类,添加头⽂件
bool js_cocos2dx_extension_AssetsManagerEx_decompressLocalZip(JSContext *cx, uint32_t argc, jsval *vp);,在jsb_cocos2dx_extension_中加⼊实现,//jsb uncompress xuyuanteng add 20170324bool js_cocos2dx_extension_AssetsManagerEx_decompressLocalZip(JSContext *cx, uint32_t argc, jsval *vp){ cocos2d::log("js_cocos2dx_extension_"); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; JS::RootedObject obj(cx, ().toObjectOrNull()); js_proxy_t *proxy = jsb_get_js_proxy(obj); cocos2d::extension::AssetsManagerEx* cobj = (cocos2d::extension::AssetsManagerEx *)(proxy ? proxy->ptr : NULL); JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_extension_AssetsManagerEx_decompressLocalZip : Invalid Native Object"); if (argc == 1) { std::string arg0; ok &= jsval_to_std_string(cx, (0), &arg0); bool ret = cobj->decompressLocalZip(arg0); jsval jsret = JSVAL_NULL; jsret = BOOLEAN_TO_JSVAL(ret); ().set(jsret); return true; } JS_ReportError(cx, "js_cocos2dx_extension_AssetsManagerEx_decompressLocalZip : wrong number of arguments: %d, was expecting %d", argc,
return false;} JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_extension_Assetjs_cocos2dx_extension_AssetsManagerEx_decompressLocalZipsManagerEx_de4 在js_register_cocos2dx_extension_AssetsManagerEx⽅法⾥进⾏绑定。
JS_FN("decompressLocalZip", js_cocos2dx_extension_AssetsManagerEx_decompressLocalZip, 1, JSPROP_PERMANENT |JSPROP_ENUMERATE),5 编译cocos2d_js_bindings⼯程,⽣成静态库后,放到cocos2d_roj⼯程下,编译⽣成静态库。6 js调⽤代码如下。 this._am = new Manager("test", "test");
this._(); var isSucceed = this._ressLocalZip("src/"); ("isSucceed = " + isSucceed); var storagePath = (ils ? tablePath() : "./"); ("storagePath is " + storagePath); //添加搜索路径 rchPath(storagePath); //解压成功后运⾏场景 if (isSucceed) { (["src/"], function(){ (jsList, function(){ ne(new HelloWorldScene()); });
}); }
发布者:admin,转转请注明出处:http://www.yc00.com/web/1688383124a129768.html
评论列表(0条)