当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 修复目前CocosCreator2.3.4热更新插件以及热更新的问题

修复目前CocosCreator2.3.4热更新插件以及热更新的问题

2020年07月09日  | 移动技术网IT编程  | 我要评论

最近在做项目用的是2.3.4的CocosCreator

我用老项目(1.9.3版)的main.js覆盖到2.3.4里发现热更新各种问题,

使用热更插件也是同样的问题,

搞了好久还是出现热更新第二次打开游戏丢失文件等一系列问题。

今天无意之间看到官方文档里面的一段代码

https://github.com/cocos-creator/tutorial-hot-update

// 在 main.js 的开头添加如下代码
(function () {
    if (typeof window.jsb === 'object') {
        var hotUpdateSearchPaths = localStorage.getItem('HotUpdateSearchPaths');
        if (hotUpdateSearchPaths) {
            var paths = JSON.parse(hotUpdateSearchPaths);
            jsb.fileUtils.setSearchPaths(paths);

            var fileList = [];
            var storagePath = paths[0] || '';
            var tempPath = storagePath + '_temp/';
            var baseOffset = tempPath.length;

            if (jsb.fileUtils.isDirectoryExist(tempPath) && !jsb.fileUtils.isFileExist(tempPath + 'project.manifest.temp')) {
                jsb.fileUtils.listFilesRecursively(tempPath, fileList);
                fileList.forEach(srcPath => {
                    var relativePath = srcPath.substr(baseOffset);
                    var dstPath = storagePath + relativePath;

                    if (srcPath[srcPath.length] == '/') {
                        cc.fileUtils.createDirectory(dstPath)
                    }
                    else {
                        if (cc.fileUtils.isFileExist(dstPath)) {
                            cc.fileUtils.removeFile(dstPath)
                        }
                        cc.fileUtils.renameFile(srcPath, dstPath);
                    }
                })
                cc.fileUtils.removeDirectory(tempPath);
            }
        }
    }
})();

发现比热更新插件里面的内容多好多,比我之前老版本的也多好多,也没仔细看。

打了个包测试热更新,居然没任何错误了。

因为我装了热更新插件(插件还是挺好用的),导致每次编译都会覆盖main.js。

我就想着把热更新插件改改。以下是修改过的热更新插件的main.js文件:

文件无法上传是咋回事?

我直接贴代码了

module.exports={load(){},unload(){},messages:{showPanel(){Editor.Panel.open("hot-update-tools")},test(t,e){Editor.log(e),Editor.Ipc.sendToPanel("hot-update-tools","hot-update-tools:onBuildFinished")},"editor:build-finished":function(t,e){let o=require("fire-fs"),a=require("fire-path");if(Editor.log("[HotUpdateTools] build platform:"+e.platform),"win32"===e.platform||"android"===e.platform||"ios"===e.platform||"mac"===e.platform){let t=a.normalize(e.dest),i=a.join(t,"main.js");o.readFile(i,"utf8",function(t,e){if(t)throw t;let a=e.replace("window.boot = function () {","(function () {\n  if (typeof window.jsb === 'object') {\n    var hotUpdateSearchPaths = localStorage.getItem('HotUpdateSearchPaths');\n    if (hotUpdateSearchPaths) {\n      var paths = JSON.parse(hotUpdateSearchPaths);\n      jsb.fileUtils.setSearchPaths(paths);\n      var fileList = [];\n      var storagePath = paths[0] || '';\n      var tempPath = storagePath + '_temp/';\n      var baseOffset = tempPath.length;\n      if (jsb.fileUtils.isDirectoryExist(tempPath) && !jsb.fileUtils.isFileExist(tempPath + 'project.manifest.temp')) {\n        jsb.fileUtils.listFilesRecursively(tempPath, fileList);\n        fileList.forEach(srcPath => {\n          var relativePath = srcPath.substr(baseOffset);\n          var dstPath = storagePath + relativePath;\n          if (srcPath[srcPath.length] == '/') {\n            cc.fileUtils.createDirectory(dstPath)\n          }\n          else {\n            if (cc.fileUtils.isFileExist(dstPath)) {\n              cc.fileUtils.removeFile(dstPath)\n            }\n            cc.fileUtils.renameFile(srcPath, dstPath);\n          }\n        })\n        cc.fileUtils.removeDirectory(tempPath);\n      }\n    }\n  }\n})();\n\nwindow.boot = function () {\n");o.writeFile(i,a,function(e){if(t)throw t;Editor.log("[HotUpdateTools] SearchPath updated in built main.js for hot update")})})}else Editor.log("[HotUpdateTools] don't need update main.js, platform: "+e.platform);let i=(new Date).getTime();Editor.Ipc.sendToPanel("hot-update-tools","hot-update-tools:onBuildFinished",i),Editor.require("packages://hot-update-tools/core/CfgUtil.js").updateBuildTimeByMain(i)}}};

覆盖C:\Users\Administrator\.CocosCreator\packages\hot-update-tools\main.js里面的内容就行了

 

本文地址:https://blog.csdn.net/MrLizs/article/details/107199387

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网