main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. 'use strict';
  2. var configFileUrl = '\\packages\\hotupdate\\config.json';
  3. var configFileUrl_test = '\\packages\\hotupdate\\config_test.json';
  4. //版本控制变量
  5. var versions = {
  6. };
  7. //测试版 版本控制变量
  8. var versions_test = {
  9. hall: '1', //大厅版本
  10. Rummy: '1', //
  11. TeenPatti: '1', //
  12. //brnn:'1', //
  13. };
  14. var url_subs_test = [
  15. "assets/",
  16. "games/Rummy/",
  17. "games/TeenPatti/",
  18. "games/brnn/",
  19. ];
  20. //版本控制变量
  21. var versions_official = {
  22. hall: '1', //大厅版本
  23. Rummy: '1', //
  24. TeenPatti: '1', //
  25. //brnn:'1' //
  26. };
  27. var url_subs_official = [
  28. "assets/",
  29. "games/Rummy/",
  30. "games/TeenPatti/",
  31. "games/brnn/",
  32. ];
  33. //var url_head = 'http://yd-files.teenpattiplayer.com/IndiaGame/update/';
  34. var url_head = 'http://d2ovxx8ky9h1zv.cloudfront.net/IndiaGame/update2/';
  35. var url_head_test = 'https://ddjjzs.oss-cn-shenzhen.aliyuncs.com/update/ddjj_test/';
  36. var url_subs = [
  37. ];
  38. //以filter-开头的文件夹作为打包大厅时的一个过滤条件
  39. var packfliter = [
  40. "filter-RUMMY", //Rummy
  41. "filter-DSP", //TeenPatti
  42. "filter-BRNN", //百人牛牛
  43. ];
  44. var autopack = false; //构建完成自动打包
  45. var packType = -1; //打包类型:-1-大厅,0-捕鱼,1-拼十,2-五子棋,3-百人捞腌菜
  46. var isTest = false;
  47. var dest = '/assets/';
  48. var src = '/';
  49. // var buildstring = '/build/jsb-link/';
  50. var buildstring = '\\build\\jsb-link\\';
  51. var fs = require('fs');
  52. var path = require('path');
  53. var crypto = require('crypto');
  54. var manifest = {
  55. packageUrl: '',
  56. remoteManifestUrl: '',
  57. remoteVersionUrl: '',
  58. version: '1.0.0',
  59. assets: {},
  60. searchPaths: []
  61. };
  62. function readDir(srcpath, dir, obj) {
  63. if (!fs.existsSync(dir))
  64. return;
  65. var stat = fs.statSync(dir);
  66. if (!stat.isDirectory()) {
  67. return;
  68. }
  69. var subpaths = fs.readdirSync(dir), subpath, size, md5, compressed, relative;
  70. for (var i = 0; i < subpaths.length; ++i) {
  71. if (subpaths[i][0] === '.') {
  72. continue;
  73. }
  74. subpath = path.join(dir, subpaths[i]);
  75. stat = fs.statSync(subpath);
  76. if (stat.isDirectory()) {
  77. readDir(srcpath, subpath, obj);
  78. }
  79. else if (stat.isFile()) {
  80. //跳过project.manifest
  81. if (path.basename(subpath).toLowerCase() === 'a8e36a08-ebce-47a1-8686-75b4bdc97058.manifest' ||
  82. path.basename(subpath).toLowerCase() === 'ba6a908e-9b09-4d66-842f-a105cb3e59ea.json')
  83. continue;
  84. //过滤文件
  85. var basenames = path.basename(subpath).split('.')
  86. if (basenames[0].length > 30) {
  87. var urlname = Editor.assetdb.uuidToUrl(basenames[0])
  88. Editor.log(basenames[0] + "--" + urlname);
  89. //打包大厅
  90. if (packType < 0) {
  91. //过滤掉需要排除的文件
  92. if (urlname.indexOf('filter-') >= 0) {
  93. Editor.log('需要删除:' + subpath + ' url:' + urlname);
  94. //删除该文件
  95. fs.unlink(subpath, function (error) {
  96. if (error) {
  97. console.log(error);
  98. return false;
  99. }
  100. Editor.log('成功删除!');
  101. })
  102. continue;
  103. }
  104. }
  105. }
  106. // Size in Bytes
  107. size = stat['size'];
  108. //md5 = crypto.createHash('md5').update(fs.readFileSync(subpath, 'binary')).digest('hex');
  109. //这里fs.readFileSycn(subpath,"binary")返回的并非二进制类型,而是String。这会导致非文本文件md5计算错误。
  110. md5 = crypto.createHash('md5').update(fs.readFileSync(subpath)).digest('hex');
  111. compressed = path.extname(subpath).toLowerCase() === '.zip';
  112. relative = path.relative(srcpath, subpath);
  113. relative = relative.replace(/\\/g, '/');
  114. relative = encodeURI(relative);
  115. obj[relative] = {
  116. 'size': size,
  117. 'md5': md5
  118. };
  119. if (compressed) {
  120. obj[relative].compressed = true;
  121. }
  122. }
  123. }
  124. }
  125. var mkdirSync = function (path) {
  126. try {
  127. fs.mkdirSync(path);
  128. } catch (e) {
  129. if (e.code != 'EEXIST') throw e;
  130. }
  131. }
  132. // 递归创建目录
  133. function mkdirsSync(dirname) {
  134. if (fs.existsSync(dirname)) {
  135. return true;
  136. } else {
  137. if (mkdirsSync(path.dirname(dirname))) {
  138. fs.mkdirSync(dirname);
  139. return true;
  140. }
  141. }
  142. }
  143. //拷贝文件到指定目录,并重建目录结构
  144. function copyFile(fromfile, topath) {
  145. var pos = fromfile.indexOf(buildstring);
  146. if (pos < 0) {
  147. Editor.log('路径有问题,拷贝失败:' + fromfile);
  148. return;
  149. }
  150. var startp = pos + buildstring.length;
  151. var subdir = fromfile.substring(startp);
  152. var tofilename = topath + subdir;
  153. Editor.log('#####copy:' + tofilename);
  154. mkdirsSync(path.dirname(tofilename));
  155. fs.writeFileSync(tofilename, fs.readFileSync(fromfile));
  156. }
  157. function copying(srcpath, destpath, type) {
  158. if (!fs.existsSync(srcpath))
  159. return;
  160. var stat = fs.statSync(srcpath);
  161. if (!stat.isDirectory()) {
  162. return;
  163. }
  164. var subpaths = fs.readdirSync(srcpath), subpath;
  165. for (var i = 0; i < subpaths.length; ++i) {
  166. if (subpaths[i][0] === '.') {
  167. continue;
  168. }
  169. subpath = path.join(srcpath, subpaths[i]);
  170. stat = fs.statSync(subpath);
  171. if (stat.isDirectory()) {
  172. copying(subpath, destpath, type);
  173. }
  174. else if (stat.isFile()) {
  175. //解析文件
  176. var basenames = path.basename(subpath).split('.')
  177. var urlname = Editor.assetdb.uuidToUrl(basenames[0])
  178. if (urlname != null) {
  179. //查找匹配的文件
  180. if (urlname.indexOf(packfliter[packType]) >= 0) {
  181. Editor.log('需要拷贝:' + subpath + ' url:' + urlname);
  182. //拷贝该文件
  183. copyFile(subpath, destpath);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. function packing(srcpath, destpath, ver, type) {
  190. //版本覆盖
  191. manifest.version = ver;
  192. manifest.assets = {};
  193. manifest.searchPaths = [];
  194. packType = type;
  195. var profilename = 'project.manifest';
  196. var versionename = 'version.manifest';
  197. manifest.packageUrl = url_head + url_subs[packType + 1];
  198. if (isTest) {
  199. manifest.packageUrl = url_head_test + url_subs[packType + 1];
  200. }
  201. manifest.remoteManifestUrl = manifest.packageUrl + profilename;
  202. manifest.remoteVersionUrl = manifest.packageUrl + versionename;
  203. // Iterate res and src folder
  204. readDir(srcpath, path.join(srcpath, 'src'), manifest.assets);
  205. readDir(srcpath, path.join(srcpath, 'assets'), manifest.assets);
  206. var destManifest = path.join(destpath, profilename);
  207. var destVersion = path.join(destpath, versionename);
  208. mkdirSync(destpath);
  209. fs.writeFile(destManifest, JSON.stringify(manifest), (err) => {
  210. if (err) throw err;
  211. Editor.log('Manifest successfully generated');
  212. });
  213. //替换到assets/resources/native/a8/a8e36a08-ebce-47a1-8686-75b4bdc97058.manifest
  214. //需要注意:打包后的文件名变化情况
  215. if (type < 0) {
  216. var packMainfest = srcpath + 'assets/resources/native/a8/a8e36a08-ebce-47a1-8686-75b4bdc97058.manifest';
  217. fs.writeFile(packMainfest, JSON.stringify(manifest), (err) => {
  218. if (err) throw err;
  219. Editor.log('a8e36a08-ebce-47a1-8686-75b4bdc97058.manifest successfully replaced');
  220. });
  221. }
  222. delete manifest.assets;
  223. delete manifest.searchPaths;
  224. fs.writeFile(destVersion, JSON.stringify(manifest), (err) => {
  225. if (err) throw err;
  226. Editor.log('Version successfully generated');
  227. });
  228. }
  229. function onBuildFinish(options, callback) {
  230. if (!autopack) {
  231. Editor.log('已关闭自动打包');
  232. }
  233. else {
  234. Editor.log('Building ' + options.platform + ' to ' + options.dest);
  235. // Editor.log(JSON.stringify(options));
  236. src = options.dest + "/";
  237. dest = options.dest + '/pack_assets_' + versions.hall + '/';
  238. packing(src, dest, versions.hall, -1);
  239. }
  240. callback();
  241. }
  242. function initConfig() {
  243. //test
  244. {
  245. let filePathUrl = Editor.Project.path + configFileUrl_test;
  246. if (fs.existsSync(filePathUrl)) {
  247. fs.readFile(filePathUrl, 'utf-8', function (err, data) {
  248. if (!err) {
  249. let saveData = JSON.parse(data.toString());
  250. versions_test = saveData;
  251. }
  252. }.bind(this));
  253. } else {
  254. let stringData = JSON.stringify(versions_test, null, '\t')
  255. fs.writeFileSync(filePathUrl, stringData);
  256. Editor.log("配置不存在文件,已自动创建");
  257. }
  258. }
  259. //official
  260. {
  261. let filePathUrl = Editor.Project.path + configFileUrl;
  262. if (fs.existsSync(filePathUrl)) {
  263. fs.readFile(filePathUrl, 'utf-8', function (err, data) {
  264. if (!err) {
  265. let saveData = JSON.parse(data.toString());
  266. versions_official = saveData;
  267. }
  268. }.bind(this));
  269. } else {
  270. let stringData = JSON.stringify(versions_official, null, '\t')
  271. fs.writeFileSync(filePathUrl, stringData);
  272. Editor.log("配置不存在文件,已自动创建");
  273. }
  274. }
  275. }
  276. function saveConfig() {
  277. let filePathUrl = Editor.Project.path + configFileUrl
  278. if (isTest) {
  279. filePathUrl = Editor.Project.path + configFileUrl_test;
  280. }
  281. fs.writeFile(filePathUrl, JSON.stringify(versions), function (error) {
  282. if (!error) {
  283. Editor.log("保存配置成功");
  284. }
  285. }.bind(this));
  286. }
  287. var inject_script = `
  288. (function () {
  289. if (typeof window.jsb === 'object') {
  290. var hotUpdateSearchPaths = localStorage.getItem('HotUpdateSearchPaths');
  291. if (hotUpdateSearchPaths) {
  292. var paths = JSON.parse(hotUpdateSearchPaths);
  293. jsb.fileUtils.setSearchPaths(paths);
  294. var fileList = [];
  295. var storagePath = paths[0] || '';
  296. var tempPath = storagePath + '_temp/';
  297. var baseOffset = tempPath.length;
  298. if (jsb.fileUtils.isDirectoryExist(tempPath) && !jsb.fileUtils.isFileExist(tempPath + 'project.manifest.temp')) {
  299. jsb.fileUtils.listFilesRecursively(tempPath, fileList);
  300. fileList.forEach(srcPath => {
  301. var relativePath = srcPath.substr(baseOffset);
  302. var dstPath = storagePath + relativePath;
  303. if (srcPath[srcPath.length] == '/') {
  304. cc.fileUtils.createDirectory(dstPath)
  305. }
  306. else {
  307. if (cc.fileUtils.isFileExist(dstPath)) {
  308. cc.fileUtils.removeFile(dstPath)
  309. }
  310. cc.fileUtils.renameFile(srcPath, dstPath);
  311. }
  312. })
  313. cc.fileUtils.removeDirectory(tempPath);
  314. }
  315. }
  316. }
  317. })();
  318. `;
  319. function writeMainJs() {
  320. var url = Editor.Project.path + buildstring + "main.js";
  321. Editor.log(url)
  322. if (fs.existsSync(url)) {
  323. fs.readFile(url, 'utf-8', function (err, data) {
  324. if (err) {
  325. Editor.log(err);
  326. throw err;
  327. }
  328. var newStr = inject_script + data;
  329. fs.writeFile(url, newStr, function (err) {
  330. if (err) {
  331. Editor.log(err);
  332. throw err;
  333. }
  334. Editor.log("SearchPath updated in built main.js for hot update");
  335. });
  336. });
  337. }
  338. }
  339. module.exports = {
  340. load() {
  341. // execute when package loaded
  342. //Editor.Builder.on('build-start', onStartBuild);
  343. // Editor.Builder.on('before-change-files', onBeforeBuildFinish);
  344. Editor.Builder.on('build-finished', onBuildFinish);
  345. },
  346. unload() {
  347. // execute when package unloaded
  348. Editor.Builder.removeListener('build-finished', onBuildFinish);
  349. },
  350. // register your ipc messages here
  351. messages: {
  352. 'getverinfo'(event) {
  353. initConfig();
  354. if (isTest) {
  355. versions = versions_test;
  356. url_subs = url_subs_test;
  357. }
  358. else {
  359. versions = versions_official;
  360. url_subs = url_subs_official;
  361. }
  362. Editor.log('hall version:' + versions.hall);
  363. Editor.log('Rummy version:' + versions.Rummy);
  364. Editor.log('TeenPatti version:' + versions.TeenPatti);
  365. Editor.log('brnn version:' + versions.brnn);
  366. Editor.log('istest value:' + isTest);
  367. event.reply(null, {
  368. HallVersion: versions.hall,
  369. RummyVersion: versions.Rummy,
  370. TeenPattiVersion: versions.TeenPatti,
  371. BrnnVersion: versions.brnn,
  372. auto: autopack,
  373. istest: isTest
  374. });
  375. },
  376. 'open'() {
  377. // open entry panel registered in package.json
  378. Editor.Panel.open('hotupdate');
  379. Editor.Ipc.sendToPanel('hotupdate', 'hotupdate:open');
  380. },
  381. // 'gen-mainfest' () {
  382. // Editor.log('Hello World!');
  383. // // send ipc message to panel
  384. // // Editor.Ipc.sendToPanel('hotupdate', 'hotupdate:hello');
  385. // },
  386. 'HallVersion'(event, version) {
  387. Editor.log('打包大厅,version:' + version);
  388. // Editor.log('Editor.Project.path:' + Editor.Project.path);
  389. packType = -1;
  390. //执行打包
  391. versions.hall = version;
  392. src = Editor.Project.path + buildstring;
  393. dest = src + 'pack_assets_' + versions.hall + '/';
  394. packing(src, dest, versions.hall, packType);
  395. saveConfig();
  396. writeMainJs();
  397. },
  398. 'RummyVersion'(event, version) {
  399. Editor.log('打包子游戏:Rummy,version:' + version);
  400. //执行打包
  401. packType = 0;
  402. versions.Rummy = version;
  403. src = Editor.Project.path + buildstring;
  404. dest = src + 'pack_game_Rummy_' + versions.Rummy + '/';
  405. //执行打包过程
  406. if (packfliter[packType] != null) {
  407. //【1】从src下拷贝出子游戏的相关资源
  408. copying(path.join(src, 'assets'), dest, packType);
  409. //copying(path.join(src, 'src'), dest, packType);
  410. //【2】开始打包
  411. packing(dest, dest, versions.Rummy, packType);
  412. }
  413. },
  414. 'TeenPattiVersion'(event, version) {
  415. Editor.log('打包子游戏:TeenPatti,version:' + version);
  416. //执行打包
  417. packType = 1;
  418. versions.TeenPatti = version;
  419. src = Editor.Project.path + buildstring;
  420. dest = src + 'pack_game_TeenPatti_' + versions.TeenPatti + '/';
  421. //执行打包过程
  422. if (packfliter[packType] != null) {
  423. //【1】从src下拷贝出子游戏的相关资源
  424. copying(path.join(src, 'assets'), dest, packType);
  425. //copying(path.join(src, 'src'), dest, packType);
  426. //【2】开始打包
  427. packing(dest, dest, versions.TeenPatti, packType);
  428. }
  429. },
  430. 'BrnnVersion'(event, version) {
  431. Editor.log('打包子游戏:百人牛牛,version:' + version);
  432. //执行打包
  433. packType = 2;
  434. versions.brnn = version;
  435. src = Editor.Project.path + buildstring + "assets";
  436. dest = src + 'pack_game_brnn_' + versions.brnn + '/';
  437. //执行打包过程
  438. if (packfliter[packType] != null) {
  439. //【1】从src下拷贝出子游戏的相关资源
  440. copying(path.join(src, 'assets'), dest, packType);
  441. //copying(path.join(src, 'src'), dest, packType);
  442. //【2】开始打包
  443. packing(dest, dest, versions.brnn, packType);
  444. }
  445. },
  446. 'changeauto'(event, checked) {
  447. autopack = checked;
  448. if (autopack)
  449. Editor.log('已开启自动打包');
  450. else
  451. Editor.log('已关闭自动打包');
  452. },
  453. 'settest'(event, checked) {
  454. Editor.log('+++++++++++++++++++++++++++++++++++++++++++++++');
  455. isTest = checked;
  456. if (isTest) {
  457. versions = versions_test;
  458. url_subs = url_subs_test;
  459. Editor.log('目前在打正式包');
  460. }
  461. else {
  462. versions = versions_official;
  463. url_subs = url_subs_official;
  464. Editor.log('目前在打测试包');
  465. }
  466. }
  467. },
  468. };