"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const compressing_1 = __importDefault(require("compressing")); const child_process_1 = __importDefault(require("child_process")); const XXTea_1 = __importDefault(require("./XXTea")); const XXTEA_KEY = 'ba5c2f3d-e52f-48'; const ANDROID_BUILD_TOOLS = 'D:\\Android\\SDK\\build-tools\\30.0.3'; const KEYSTOTE = path_1.default.join(__dirname, 'lib', 'keystore', 'fish.jks'); const KEYSTOTE_PASSWORD = 'fish123456'; const KEYSTOTE_ALIAS = 'paopaobuyu'; const KEYSTOTE_ALIAS_PASSWORD = 'fish123456'; const unzipApk = (apkPath) => __awaiter(void 0, void 0, void 0, function* () { const dirPath = path_1.default.dirname(apkPath); const outputPath = path_1.default.join(dirPath, '.___apk'); const newApkPath = path_1.default.join(dirPath, path_1.default.basename(apkPath, '.apk') + '_poco.apk'); console.log('创建输出目录'); fs_1.default.existsSync(outputPath) && removeFile(outputPath); fs_1.default.mkdirSync(outputPath); console.log('游戏解包'); yield compressing_1.default.zip.uncompress(apkPath, outputPath); console.log('删除证书'); removeFile(path_1.default.join(outputPath, 'META-INF')); console.log('替换引擎库'); copyFile(path_1.default.join(__dirname, 'lib', 'so', 'libcocos2djs.so'), path_1.default.join(outputPath, 'lib', 'armeabi-v7a', 'libcocos2djs.so')); console.log('添加PocoSDK'); copyFile(path_1.default.join(__dirname, 'lib', 'sdk', 'Poco.js'), path_1.default.join(outputPath, 'assets', 'Poco.js')); console.log('修改入口脚本'); const scriptPath = path_1.default.join(outputPath, 'assets', 'main.js'); fs_1.default.existsSync(scriptPath) ? hackMain(scriptPath) : hackMain(scriptPath + 'c'); console.log('游戏打包'); removeFile(newApkPath); yield compressing_1.default.zip.compressDir(outputPath, newApkPath, { ignoreBase: true }); console.log('删除临时文件'); removeFile(outputPath); console.log('游戏重新签名'); const signCmd = `${path_1.default.join(ANDROID_BUILD_TOOLS, 'apksigner')} sign --ks ${KEYSTOTE} --ks-pass pass:${KEYSTOTE_PASSWORD} --ks-key-alias ${KEYSTOTE_ALIAS} --key-pass pass:${KEYSTOTE_ALIAS_PASSWORD} ${newApkPath} `; child_process_1.default.execSync(signCmd); removeFile(newApkPath + '.idsig'); console.log('完成'); }); const removeFile = (filePath) => { if (!fs_1.default.existsSync(filePath)) return; const stat = fs_1.default.statSync(filePath); if (stat.isDirectory()) { const subFiles = fs_1.default.readdirSync(filePath); subFiles.forEach(value => removeFile(path_1.default.join(filePath, value))); fs_1.default.rmdirSync(filePath); } else { fs_1.default.unlinkSync(filePath); } }; const copyFile = (filePath, destPath) => { const stat = fs_1.default.statSync(filePath); if (stat.isDirectory()) { fs_1.default.existsSync(destPath) || fs_1.default.mkdirSync(destPath); const subFiles = fs_1.default.readdirSync(filePath); subFiles.forEach(value => copyFile(path_1.default.join(filePath, value), path_1.default.join(destPath, value))); } else { fs_1.default.existsSync(destPath) && removeFile(destPath); fs_1.default.copyFileSync(filePath, destPath); } }; const hackMain = (scriptPath, isEncrypt = false) => { let script = fs_1.default.readFileSync(scriptPath).toString(); if (isEncrypt) script = XXTea_1.default.decrypt(script, XXTEA_KEY); script = script.replace('var launchScene = settings.launchScene;', 'var launchScene = settings.launchScene; \n' + 'require("Poco.js"); \n'); if (isEncrypt) script = XXTea_1.default.encrypt(script, XXTEA_KEY); fs_1.default.writeFileSync(scriptPath, script); }; const apkPath = process.argv[2]; unzipApk(apkPath);