微信小游戏分包加载 zip 包

  • 确保模板中 AssetManager.js 和 game.json 内容正确

    • AssetManager 中, 修改 loadSubpackage 方法回调

    • game.json 中, 修改 subpackages 字段

  • settings.xxxxx.json 文件中, 将 remoteBundles 内容移至 subpackages 中

  • src/bundle-scripts 目录中 index.xxxx.js 批量改名为 game.js

  • 将 remote 目录和 src/bundle-scripts 目录统一移至 subpackage 中

  • AssetManager 内容

function downloadBundle(nameOrUrl, options, onComplete) {
    var bundleName = cc.path.basename(nameOrUrl);
    var version = options.version || cc.assetManager.downloader.bundleVers[bundleName];
    var suffix = version ? version + "." : "";
    // console.log("[Z] nameOrUrl:" + nameOrUrl);
    // console.log("[Z] bundleVer:" + cc.assetManager.downloader.bundleVers[bundleName]);

    if (subpackages[bundleName]) {
        var config = "subpackages/".concat(bundleName, "/config.").concat(suffix, "json");
        loadSubpackage(bundleName, options.onFileProgress, function (err) {
            if (err) {
                onComplete(err, null);
                return;
            }

            downloadJson(config, options, function (err, data) {
                if (!data) {
                    console.error(bundleName);
                    return;
                }
                if (data.isZip) {
                    // 解压缩
                    console.log(`[Z] isZip; bundle=` + data.name);
                    if (!data) {
                        return;
                    }
                    var zipVersion = data.zipVersion;
                    var zipUrl = "subpackages/"
                        .concat(bundleName, "/")
                        .concat("res.")
                        .concat(zipVersion ? zipVersion + "." : "", "zip");
                    console.log(zipUrl);

                    options.__cacheBundleRoot__ = bundleName;

                    handleZip(zipUrl, options, function (err, unzipPath) {
                        if (err) {
                            onComplete && onComplete(err);
                            return;
                        }

                        console.log(`unzipPath:${unzipPath}`);
                        data.base = unzipPath + "/res/"; // PATCH: for android alipay version before v10.1.95 (v10.1.95 included)
                        // to remove in the future

                        var sys = cc.sys;

                        if (
                            sys.platform === sys.Platform.ALIPAY_MINI_GAME &&
                            sys.os === sys.OS.ANDROID
                        ) {
                            var resPath = unzipPath + "res/";

                            if (
                                fs.accessSync({
                                    path: resPath,
                                })
                            ) {
                                data.base = resPath;
                            }
                        }

                        onComplete && onComplete(null, data);
                    });
                } else {
                    data && (data.base = "subpackages/".concat(bundleName, "/"));
                    onComplete(err, data);
                }
            });
        });
    } else {
        var js, url;

        if (REGEX.test(nameOrUrl) || nameOrUrl.startsWith(getUserDataPath())) {
            url = nameOrUrl;
            js = "src/bundle-scripts/".concat(bundleName, "/index.").concat(suffix, "js");
            cacheManager.makeBundleFolder(bundleName);
        } else {
            if (downloader.remoteBundles.indexOf(bundleName) !== -1) {
                url = "".concat(downloader.remoteServerAddress, "remote/").concat(bundleName);
                js = "src/bundle-scripts/".concat(bundleName, "/index.").concat(suffix, "js");
                cacheManager.makeBundleFolder(bundleName);
            } else {
                url = "assets/".concat(bundleName);
                js = "assets/".concat(bundleName, "/index.").concat(suffix, "js");
            }
        }

        require("../../../" + js);

        options.__cacheBundleRoot__ = bundleName;
        var config = "".concat(url, "/config.").concat(suffix, "json");
        downloadJson(config, options, function (err, data) {
            if (err) {
                onComplete && onComplete(err);
                return;
            }

            if (data.isZip) {
                var zipVersion = data.zipVersion;
                var zipUrl = ""
                    .concat(url, "/res.")
                    .concat(zipVersion ? zipVersion + "." : "", "zip");
                handleZip(zipUrl, options, function (err, unzipPath) {
                    if (err) {
                        onComplete && onComplete(err);
                        return;
                    }

                    data.base = unzipPath + "/res/"; // PATCH: for android alipay version before v10.1.95 (v10.1.95 included)
                    // to remove in the future

                    var sys = cc.sys;

                    if (
                        sys.platform === sys.Platform.ALIPAY_MINI_GAME &&
                        sys.os === sys.OS.ANDROID
                    ) {
                        var resPath = unzipPath + "res/";

                        if (
                            fs.accessSync({
                                path: resPath,
                            })
                        ) {
                            data.base = resPath;
                        }
                    }

                    onComplete && onComplete(null, data);
                });
            } else {
                data.base = url + "/";
                onComplete && onComplete(null, data);
            }
        });
    }
}