ios 覆盖安装问题研究

在实现cocos热更方案过程中,一切都很顺利。所有工作完成之后,发现ios 在覆盖安装之后,如果没有清理缓存,即使安装的版本是旧的也不会进行热更。这种情况,比较好理解,版本文件还在,所以比对版本文件没有问题就不会进行热更。真正出问题的是游戏启动后,竟然使用的脚本是覆盖安装后的,而不是热更缓存的。这个问题挺头疼。

经过研究发现,覆盖安装后的ios包,并不是在原来的安装包内存上覆盖替换,而是会把旧包的内存清理掉,在新的内存上安装包。放在游戏内理解即WritablePath 发生了变化,所以我们在官方的热更方案上,将新的发现加了上去

let writablepath = jsb.fileUtils.getWritablePath();
    var hotUpdateSearchPaths = localStorage.getItem("HotUpdateSearchPaths");

    if (hotUpdateSearchPaths && hotUpdateSearchPaths.indexOf(writablepath) != -1) {
        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);
        }
    } else {
        let path = writablepath + "remote-hot/hall"; //热更存放路径添加
        jsb.fileUtils.addSearchPath(path, true);
    }

This article was updated on 八月 1, 2023