"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var path_1 = __importDefault(require("path"));
var fs_1 = __importDefault(require("fs"));
var FileUtil_1 = __importDefault(require("../../util/FileUtil"));
var PlatformBuildCfg_1 = require("../PlatformBuildCfg");
/** 首屏加载 */
var SpeedLoadModule = /** @class */ (function () {
    function SpeedLoadModule() {
    }
    /** 编译WX工程 */
    SpeedLoadModule.prototype.init = function (buildGameData) {
        this.buildGameData = buildGameData;
        this.addGameVersion();
        this.addLoadSpeed();
    };
    /** 首屏加载优化 */
    SpeedLoadModule.prototype.addLoadSpeed = function () {
        console.log("================ 添加首屏加载优化 ================\n");
        var PROJECT_DIR = this.buildGameData.PROJECT_DIR;
        var platformFolder = PlatformBuildCfg_1.PlatformFolder[this.buildGameData.platformName];
        // 此代码参考 https://forum.cocos.org/t/cocos-creator-50/94999
        var curBuildPath = path_1["default"].join(PROJECT_DIR, "build", platformFolder);
        if (fs_1["default"].existsSync(path_1["default"].join(curBuildPath, 'engine'))) {
            console.warn("首屏加载已完成！");
            return;
        }
        var files = fs_1["default"].readdirSync(curBuildPath);
        var exclude = ["InitSDK.js", "game.json", "project.config.json", "assets", "remote", "subpackages", "splash"];
        //拷贝引擎内容至引擎分包
        FileUtil_1["default"].moveFileBySearchName(curBuildPath + "/assets", curBuildPath + "/engine/assets", 'index.js');
        files.forEach(function (file) {
            if (exclude.indexOf(file) != -1) {
                return;
            }
            var filePath = path_1["default"].join(curBuildPath, file);
            if (fs_1["default"].statSync(filePath).isDirectory()) {
                FileUtil_1["default"].copyFolderSync(path_1["default"].join(curBuildPath, file), path_1["default"].join(curBuildPath, "engine", file));
                FileUtil_1["default"].removeDirsSync(path_1["default"].join(curBuildPath, file));
            }
            else {
                FileUtil_1["default"].moveFileSync(path_1["default"].join(curBuildPath, file), path_1["default"].join(curBuildPath, "engine", file));
            }
        });
        // 修改引擎包内的 game.js,不需要加载cocos引擎了
        var engineGameJsPath = path_1["default"].join(curBuildPath, "engine", "game.js");
        var engineGameJsStr = fs_1["default"].readFileSync(engineGameJsPath, 'utf8');
        engineGameJsStr.replace("require('cocos/cocos2d-js-min.js');", "");
        fs_1["default"].writeFileSync(engineGameJsPath, engineGameJsStr);
        // 修改game.js
        var gameJsPath = path_1["default"].join(curBuildPath, "game.js");
        var jsStr = "";
        if (fs_1["default"].existsSync(gameJsPath)) {
            jsStr = fs_1["default"].readFileSync(gameJsPath, 'utf8');
        }
        jsStr = 'require("InitSDK.js");\n require("./splash/splash1");\n';
        fs_1["default"].writeFileSync(gameJsPath, jsStr);
        // 修改game.json 加入引擎分包
        var enginePackage = {
            "name": "engine",
            "root": "engine/"
        };
        var gameJsonPath = curBuildPath + "\\game.json";
        var jsonStr = fs_1["default"].readFileSync(gameJsonPath, 'utf8');
        var jsonData = JSON.parse(jsonStr);
        jsonData.subpackages.push(enginePackage);
        jsonStr = JSON.stringify(jsonData);
        fs_1["default"].writeFileSync(gameJsonPath, jsonStr);
    };
    /** 替换游戏版本号 */
    SpeedLoadModule.prototype.addGameVersion = function () {
        console.log("================ 添加游戏版本号 ================\n");
        var PROJECT_DIR = this.buildGameData.PROJECT_DIR;
        var platformName = this.buildGameData.platformName;
        var platformFolder = PlatformBuildCfg_1.PlatformFolder[platformName];
        var path = path_1["default"].join(PROJECT_DIR, "build", platformFolder, "game.js");
        var jsonStr = fs_1["default"].readFileSync(path, 'utf8');
        var qqStr = "\n" + platformName + ".dataversion = '" + this.buildGameData.appVersionCfg.version + "';\n";
        var channelName = this.buildGameData.channelName;
        var channelStr = "\nwindow&&(window.gameChannelName = '" + channelName + "');\n";
        var versionStr = "\nwindow&&(window.dataversion = '" + this.buildGameData.appVersionCfg.version + "');\n";
        console.log("当前游戏版本号", this.buildGameData.appVersionCfg.version);
        jsonStr = jsonStr + qqStr + channelStr + versionStr;
        fs_1["default"].writeFileSync(path, jsonStr);
    };
    return SpeedLoadModule;
}());
exports["default"] = SpeedLoadModule;
