mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-09-03 07:27:12 +08:00
基本完成热更新打包工具与流程
This commit is contained in:
503
HotUpdateDemo/package-lock.json
generated
503
HotUpdateDemo/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
const fileUtil = require('FileUtil');
|
||||
const fileUtil = require('./FileUtil');
|
||||
let version, androidVersionXml;
|
||||
|
||||
function initParams(configPath) {
|
||||
@@ -16,6 +16,6 @@ function repreatAndroidVersion(path) {
|
||||
|
||||
function main(params) {
|
||||
initParams('./GameConfig.json');
|
||||
//修改android 版本号
|
||||
//修改android 版本号
|
||||
repreatAndroidVersion(androidVersionXml);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
const fileUtil = require('FileUtil');
|
||||
const fileUtil = require('./FileUtil');
|
||||
|
||||
let hotUpdateRoot, hotUpdateDesRoot;
|
||||
//要复制的目录
|
||||
@@ -7,13 +7,13 @@ const targetDir = ['res', 'src'];
|
||||
function initParams(configPath) {
|
||||
let data = JSON.parse(fileUtil.read(configPath));
|
||||
hotUpdateRoot = data.src;
|
||||
hotUpdateDesRoot = data.hotUpdateDirRoot;
|
||||
hotUpdateDesRoot = data.root + data.hotUpdateDirName;
|
||||
}
|
||||
|
||||
function forEachDir(list) {
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
let item = list[i];
|
||||
let desPath = hotUpdateDesRoot + item;
|
||||
let desPath = hotUpdateDesRoot + '/' + item;
|
||||
let srcPath = hotUpdateRoot + item;
|
||||
fileUtil.copyDir(srcPath, desPath);
|
||||
}
|
||||
@@ -26,6 +26,8 @@ function main() {
|
||||
forEachDir(targetDir);
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const fileUtil = require('FileUtil');
|
||||
const fileUtil = require('./FileUtil');
|
||||
|
||||
let hotUpdateRoot, hotUpdateFile, exportHotUpdateDir, version, packageUrl;
|
||||
const hotUpdateDir = 'hotUpdate', dirNameTemplate = 'hotUpdate-v', time = Date.parse(new Date()) / 1000;
|
||||
@@ -9,19 +9,15 @@ function initParams(configPath) {
|
||||
hotUpdateFile = hotUpdateRoot + data.hotUpdateDirName;
|
||||
exportHotUpdateDir = hotUpdateRoot + data.hotupdateDirExport;
|
||||
version = data.version;
|
||||
packageUrl = packageUrl;
|
||||
packageUrl = data.packageUrl;
|
||||
}
|
||||
|
||||
function getVersion() {
|
||||
return version.replace(/\./g, '-');
|
||||
}
|
||||
|
||||
const versionNo = getVersion();
|
||||
|
||||
let dirName = `${dirNameTemplate}${versionNo}-${time}`;
|
||||
|
||||
function reWritePackageUrl(dir, replaceName) {
|
||||
let desPath = hotUpdateFile + '/' + dir;
|
||||
let desPath = dir;
|
||||
let game = fileUtil.read(desPath);
|
||||
let conf = JSON.parse(game);
|
||||
conf.packageUrl = conf.packageUrl.replace(hotUpdateDir, replaceName);
|
||||
@@ -31,17 +27,24 @@ function reWritePackageUrl(dir, replaceName) {
|
||||
|
||||
function main() {
|
||||
initParams('./GameConfig.json');
|
||||
const versionNo = getVersion(), dirName = `${dirNameTemplate}${versionNo}-${time}`;
|
||||
|
||||
//消除目录文件
|
||||
fileUtil.checkDir(exportHotUpdateDir);
|
||||
|
||||
const time = Date.parse(new Date()) / 1000;
|
||||
let desPath = exportHotUpdateDir + '/' + dirName;
|
||||
//复制到新目录
|
||||
fileUtil.copyDir(hotUpdateFile, desPath);
|
||||
|
||||
reWritePackageUrl('project.manifest', dirName);
|
||||
//复制project.manifest/version.manifest
|
||||
fileUtil.copy(hotUpdateFile + '/project.manifest', exportHotUpdateDir + '/project.manifest');
|
||||
fileUtil.copy(hotUpdateFile + '/version.manifest', exportHotUpdateDir + '/version.manifest');
|
||||
//修改project.manifest 中的packageUrl为新资源
|
||||
reWritePackageUrl(exportHotUpdateDir + '/project.manifest', dirName);
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -14,16 +14,16 @@ function read(path) {
|
||||
}
|
||||
|
||||
function readDir(dir, fn) {
|
||||
if (jetpack.exits() == 'dir') {
|
||||
if (jetpack.exists(dir) == 'dir') {
|
||||
let list = jetpack.list(dir);
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
let name = list[i];
|
||||
if (name[0] === '.') continue;
|
||||
let subPath = jetpach.path(dir, name);
|
||||
if (jetpack.exits(subPath) == 'dir') {
|
||||
let subPath = jetpack.path(dir, name);
|
||||
if (jetpack.exists(subPath) == 'dir') {
|
||||
readDir(subPath, fn);
|
||||
} else if (jetpack.exits(subPath) == 'file') {
|
||||
fn && fn(subpath);
|
||||
} else if (jetpack.exists(subPath) == 'file') {
|
||||
fn && fn(subPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,8 @@ function write(path, data) {
|
||||
* "file" if path is a file.
|
||||
* "other" if none of the above.
|
||||
*/
|
||||
function exits(path) {
|
||||
const state = jetpack.exits(path);
|
||||
function exists(path) {
|
||||
const state = jetpack.exists(path);
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -76,14 +76,12 @@ function dir(path) {
|
||||
* @param {*} src
|
||||
* @param {*} des
|
||||
*/
|
||||
function copy(src, des) {
|
||||
if (jetpack.exits(des)) {
|
||||
function copy(src, des, isClear) {
|
||||
if (isClear && jetpack.exists(des)) {
|
||||
jetpack.remove(des);
|
||||
}
|
||||
//过滤文件 jetpack.copy('foo', 'bar', { matching: ['*.md', '!top-secret.md'] });
|
||||
jetpack.copy(src, des, {
|
||||
overwrite: true
|
||||
});
|
||||
jetpack.copy(src, des);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,10 +118,8 @@ function checkDir(dir) {
|
||||
jetpack.dir(dir);
|
||||
}
|
||||
|
||||
function copyDir(src, des) {
|
||||
if (jetpack.exists(des)) {
|
||||
jetpack.remove(des);
|
||||
}
|
||||
function copyDir(src, des, isClear) {
|
||||
isClear && checkDir(des);
|
||||
jetpack.copy(src, des);
|
||||
}
|
||||
|
||||
@@ -131,7 +127,7 @@ module.exports = {
|
||||
read,
|
||||
readDir,
|
||||
write,
|
||||
exits,
|
||||
exists,
|
||||
list,
|
||||
dir,
|
||||
copy,
|
||||
|
||||
@@ -6,9 +6,6 @@
|
||||
"src": "../build/jsb-link/",
|
||||
"hotUpdateDirName": "hotUpdateFiles",
|
||||
"hotupdateDirExport": "exportHotUpdate",
|
||||
"des": [
|
||||
"../build/jsb-link/"
|
||||
],
|
||||
"zipDir": [
|
||||
"/res/import"
|
||||
]
|
||||
|
||||
@@ -33,8 +33,11 @@ def modify(rootdir):
|
||||
def initParams(configPath):
|
||||
data = open(configPath, 'r')
|
||||
data = json.load(data)
|
||||
relativePath = data.root + data.hotUpdateDirName
|
||||
zipDirList = data.zipDir
|
||||
global relativePath
|
||||
relativePath = data['root'] + data['hotUpdateDirName']
|
||||
global zipDirList
|
||||
zipDirList = data['zipDir']
|
||||
des = data['des']
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -20,17 +20,16 @@ npm install
|
||||
安装Node的库 fs-jetpack 用于对文件操作
|
||||
|
||||
### 操作
|
||||
首先
|
||||
* 运行 BuildBeforeSetting.js
|
||||
对项目初始化
|
||||
首先进入tools
|
||||
* 运行 BuildBeforeSetting.js 对项目初始化
|
||||
然后进行构建工程,
|
||||
* 构建Android 工程 选择link模式
|
||||

|
||||
进入tools后
|
||||
* 运行CopyHotFiles.js 把热更新文件放到新文件夹
|
||||
* ModifyFileTime.js and ZipFile 对文件修改时间(用于生成zip后,进行MD5后,md5信息不变)、zip压缩(用于更新时可以减少下载请求,提高热更速度、与更新文件的稳定性)
|
||||
|
||||
* 运行VersionGenerator.js 得到版本信息。
|
||||
* 运行CopyHotFiles.js 把热更新文件放到hotUpdateFiles文件夹
|
||||
* ModifyFileTime.py and ZipFile.py 对hotUpdateFiles中zipDir变量内文件修改时间(用于生成zip后,进行MD5后,md5信息不变)、zip压缩(用于更新时可以减少下载请求,提高热更速度、与更新文件的稳定性)
|
||||
* 运行VersionGenerator.js 对hotUpdateFiles进行md5得到版本信息
|
||||
* 运行ExportHotupdateDir.js 得到热更新资源到exportHotUpdate文件夹
|
||||
|
||||
### 支持https
|
||||
* 打开工程,修改libcocos2dx工程中Cocos2dxDownloader.java类。找到new AsyncHttpClient() 修改成new AsyncHttpClient(true, 80, 443)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const fileUtil = require('FileUtil');
|
||||
const fileUtil = require('./FileUtil');
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
var path = require('path');
|
||||
@@ -15,7 +15,7 @@ let manifest = {
|
||||
let dest = './remote-assets/';
|
||||
let src = './jsb/';
|
||||
|
||||
function initManifest(configPath) {
|
||||
function initParams(configPath) {
|
||||
let data = JSON.parse(fileUtil.read(configPath));
|
||||
//得到远程url
|
||||
manifest.packageUrl = data.packageUrl;
|
||||
@@ -24,23 +24,21 @@ function initManifest(configPath) {
|
||||
//版本信息
|
||||
manifest.version = data.version;
|
||||
//构建后的对应目录
|
||||
src = data.src;
|
||||
src = data.root + data.hotUpdateDirName;
|
||||
|
||||
//目的目录
|
||||
desp = data.des;
|
||||
|
||||
|
||||
dest = src;
|
||||
}
|
||||
|
||||
function md5InfoFromDir(dir, obj) {
|
||||
function fn(subpath) {
|
||||
function fn(subPath) {
|
||||
let stat, size, md5, compressed, relative;
|
||||
stat = fs.statSync(subpath);
|
||||
stat = fs.statSync(subPath);
|
||||
size = stat['size'];
|
||||
// md5 = crypto.createHash('md5').update(fs.readFileSync(subpath, 'binary')).digest('hex');//返回的并非二进制类型,而是String。这会导致非文本文件md5计算错误
|
||||
md5 = crypto.createHash('md5').update(fs.readFileSync(subPath)).digest('hex');
|
||||
compressed = path.extname(subpath).toLowerCase() === '.zip';
|
||||
relative = path.relative(src, subpath);
|
||||
compressed = path.extname(subPath).toLowerCase() === '.zip';
|
||||
relative = path.relative(src, subPath);
|
||||
relative = relative.replace(/\\/g, '/');
|
||||
relative = encodeURI(relative);
|
||||
obj[relative] = {
|
||||
@@ -55,28 +53,23 @@ function md5InfoFromDir(dir, obj) {
|
||||
}
|
||||
|
||||
function main() {
|
||||
initManifest('./GameConfig.json');
|
||||
initParams('./GameConfig.json');
|
||||
|
||||
md5InfoFromDir(path.join(src, 'src'), manifest.assets);
|
||||
md5InfoFromDir(path.join(src, 'res'), manifest.assets);
|
||||
|
||||
let length = desp.length;
|
||||
for (let i = 0; i < length; i++) {
|
||||
let des = desp[i], destManifest;
|
||||
destManifest = path.join(des, 'project.manifest');
|
||||
fileUtil.write(destManifest, JSON.stringify(manifest));
|
||||
}
|
||||
let destManifest = path.join(dest, 'project.manifest');
|
||||
fileUtil.write(destManifest, JSON.stringify(manifest));
|
||||
|
||||
delete manifest.assets;
|
||||
delete manifest.searchPaths;
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
let des = desp[i], destVersion;
|
||||
destVersion = path.join(des, 'version.manifest');
|
||||
fileUtil.write(destVersion, JSON.stringify(manifest));
|
||||
}
|
||||
destVersion = path.join(dest, 'version.manifest');
|
||||
fileUtil.write(destVersion, JSON.stringify(manifest));
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
import os.path
|
||||
import zipfile
|
||||
import shutil
|
||||
import json
|
||||
|
||||
relativePath = ''
|
||||
zipDirList = []
|
||||
@@ -69,8 +70,10 @@ def checkDir(rootdir):
|
||||
def initParams(configPath):
|
||||
data = open(configPath, 'r')
|
||||
data = json.load(data)
|
||||
relativePath = data.root + data.hotUpdateDirName
|
||||
zipDirList = data.zipDir
|
||||
global relativePath
|
||||
relativePath = data['root'] + data['hotUpdateDirName']
|
||||
global zipDirList
|
||||
zipDirList = data['zipDir']
|
||||
|
||||
|
||||
def main():
|
||||
@@ -82,3 +85,6 @@ def main():
|
||||
print(abspath)
|
||||
encodeDir(abspath)
|
||||
pass
|
||||
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user