求助,2.4.11版本,热更在重启游戏后失效

2.4.11,热更完成后,自动重启没问题,但是关闭游戏再启动热更的所有东西就都失效了,看上去像是热更下载的地址变了,找不到了。没有勾选MD5 Cache。

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] === '/') {
                    jsb.fileUtils.createDirectory(dstPath);
                } else {
                    if (jsb.fileUtils.isFileExist(dstPath)) {
                        jsb.fileUtils.removeFile(dstPath);
                    }
                    jsb.fileUtils.renameFile(srcPath, dstPath);
                }
            });
            jsb.fileUtils.removeDirectory(tempPath);
        }
    }
}

})();
window.boot = function () {

。。。

1:打印下 hotUpdateSearchPaths 这个看看路径有哪些
2:热更代码 怎么保存路径的?
3:可以用模拟器玩,下mt管理器 去对应的路径看看热更资源在不在
4: 断点load 看看查找地址是从那个路径开始找,热更后热更路径默认是第一个

// 更新完成

_onUpdateFinished(event: jsb.EventAssetsManager) {
    this._assetsMgr.setEventCallback(null)
    let searchPaths = jsb.fileUtils.getSearchPaths();
    let manifest = this._assetsMgr.getLocalManifest();
    let newPaths = manifest.getSearchPaths();
    // console.log('SearchPaths', JSON.stringify(search_paths));
    for (let i = 0; i < newPaths.length; i++) {
        const newPath = newPaths[i];
        let find: boolean = false;
        for (let j = 0; j < searchPaths.length; j++) {
            const oldPath = searchPaths[j];
            if (newPath == oldPath) {
                find = true;
                break;
            }
        }

        if (!find) {
            console.log("[HotUpdate] 添加路径 : ", newPath);
            Array.prototype.unshift.apply(searchPaths, newPath);
        }
    }

    cc.log("[HotUpdate] 最终路径: " + JSON.stringify(searchPaths));
    cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
    cc.sys.localStorage.setItem('ClientVersion', manifest.getVersion());
    jsb.fileUtils.setSearchPaths(searchPaths);
    this._options.OnUpdateSucceed && this._options.OnUpdateSucceed(event);
}

//创建AssetsManager
let storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : ‘/’) + ‘remote-asset’);
this._assetsMgr = new jsb.AssetsManager(url, storagePath, versionCompare);

代码看上去没啥问题,热更重启就是不行

后面想了个临时方案,版本比较全部返回-1
去拉取热更,走热更完成流程,然后看下载文件数量
下载文件为0就不重启,有下载文件就重启

这样保证每次启动游戏都会去写 HotUpdateSearchPaths 这个值
然后就可以了……搞不懂为啥

贴上修改后的代码

// 更新完成
_onUpdateFinished(event: jsb.EventAssetsManager) {
if (this._assetsMgr.getDownloadedFiles() < this._assetsMgr.getTotalFiles()) {
if (this.failCount >= 3) {
this._onUpdateFailed(event);
} else {
this._assetsMgr.downloadFailedAssets();
this.failCount++;
}
} else {
this._assetsMgr.setEventCallback(null)
let searchPaths = jsb.fileUtils.getSearchPaths();
let manifest = this._assetsMgr.getLocalManifest();
let newPaths = manifest.getSearchPaths();
// console.log(‘SearchPaths’, JSON.stringify(search_paths));
for (let i = 0; i < newPaths.length; i++) {
const newPath = newPaths[i];
let find: boolean = false;
for (let j = 0; j < searchPaths.length; j++) {
const oldPath = searchPaths[j];
if (newPath == oldPath) {
find = true;
break;
}
}

            if (!find) {
                console.log("[HotUpdate] 添加路径 : ", newPath);
                Array.prototype.unshift.apply(searchPaths, newPath);
            }
        }

        console.log("[HotUpdate] 最终路径: " + JSON.stringify(searchPaths));
        cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
        cc.sys.localStorage.setItem('ClientVersion', manifest.getVersion());
        jsb.fileUtils.setSearchPaths(searchPaths);
        let needRestart = this._assetsMgr.getTotalFiles() > 0;
        console.log("[HotUpdate] 最终下载数量: " + this._assetsMgr.getTotalFiles() + "  需要重启: " + needRestart);
        this._options.OnUpdateSucceed && this._options.OnUpdateSucceed(needRestart);
    }
}


看了看代码差不多 可以看看重启后路径打印是啥样的 或者自己找。。哪里删除了