main.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const Fs = require('fire-fs');
  2. const Path = require('fire-path');
  3. const FsExtra = require('fs-extra');
  4. module.exports = {
  5. load () {
  6. },
  7. unload () {
  8. },
  9. messages: {
  10. showPanel () {
  11. Editor.Panel.open('hot-update-tools');
  12. // let engineCode = "if(jsb.fileUtils.isDirectoryExist(jsb.fileUtils.getWritablePath()+\"blackjack-remote-asset/assets/\"+nameOrUrl+\"/assets\")){url += \"/assets\";}\n";
  13. // let findCode = "if(jsb.fileUtils.isDirectoryExist(jsb.fileUtils.getWritablePath()+";
  14. // let codeUrl = Editor.url('packages://hot-update-tools/../../build/jsb-link');
  15. // let root = Path.normalize(codeUrl);
  16. // let url = Path.join(root, 'jsb-adapter/jsb-engine.js');
  17. // let data = Fs.readFileSync(url, 'utf8');
  18. // let findStr = "var config = \"\".concat(url, \"/config.\").concat(version ? version + '.' : '', \"json\");";
  19. // let findJ = data.indexOf(engineCode);
  20. // Editor.log(findJ);
  21. // if(findJ != -1){
  22. // Editor.log(`代码片段已存在: ${engineCode}, 无需添加`);
  23. // return
  24. // }
  25. // let findI = data.indexOf(findStr);
  26. // let startStr = data.slice(0, findI);
  27. // let lastStr = data.slice(findI);
  28. // Editor.log(findI);
  29. // Editor.log(lastStr);
  30. // Editor.log(data.length);
  31. // let newData = startStr + engineCode + lastStr;
  32. // Editor.log(newData);
  33. // Editor.log(newData.length);
  34. // Fs.writeFileSync(url, newData);
  35. // Editor.log(newData);
  36. // Editor.log(newData.length);
  37. // const CfgUtil = Editor.require('packages://hot-update-tools/core/CfgUtil.js');
  38. // const Util = Editor.require('packages://hot-update-tools/core/Util.js');
  39. // const OutPut = Editor.require('packages://hot-update-tools/core/OutPut.js');
  40. // let data = CfgUtil._getConfigData();
  41. // Editor.log(data)
  42. // if(data) {
  43. // let subPackArr = data.subPackArr;
  44. // let projectDir = data.resourceRootDir;
  45. // Editor.log(`项目路径:${projectDir}`)
  46. // let keys = Object.keys(subPackArr);
  47. // Editor.log(keys)
  48. // for(let i = 0; i < keys.length; ++i) {
  49. // const name = keys[i];
  50. // const element = subPackArr[name];
  51. // Editor.log(name)
  52. // Editor.log(element)
  53. // if(element) {
  54. // let destDir = Path.join(projectDir, 'assets\\'+name);
  55. // Editor.log(destDir)
  56. // if(Fs.existsSync(destDir)) {
  57. // Editor.log(`${name} 存在`);
  58. // let assetsDir = Path.join(destDir, 'assets');
  59. // if(!Fs.existsSync(assetsDir)) {
  60. // Fs.mkdirSync(assetsDir);
  61. // }
  62. // FsExtra.emptyDirSync(assetsDir);
  63. // FsExtra.copySync(Path.join(assetsDir, 'import'), Path.join(destDir, 'import'));
  64. // // FsExtra.move(Path.join(assetsDir, 'native'), Path.join(destDir, 'native'));
  65. // FsExtra.copyFileSync(Path.join(assetsDir, 'config.json'), Path.join(destDir, 'config.json'));
  66. // }else{
  67. // Editor.log(`${name} 不存在`);
  68. // }
  69. // }
  70. // }
  71. // }
  72. },
  73. 'editor:build-finished': (event, target) => {
  74. // Editor.log('[HotUpdateTools] build platform:' + target.platform);
  75. if (
  76. target.platform === 'win32' ||
  77. target.platform === 'android' ||
  78. target.platform === 'ios' ||
  79. target.platform === 'mac'
  80. ) {
  81. //修改main.js
  82. let codeUrl = Editor.url('packages://hot-update-tools/main_code.js');
  83. if (Fs.existsSync(codeUrl)) {
  84. const injectScript = Fs.readFileSync(codeUrl, 'utf-8');
  85. let root = Path.normalize(target.dest);
  86. let url = Path.join(root, 'main.js');
  87. let data = Fs.readFileSync(url, 'utf8');
  88. let newData = injectScript + data;
  89. Fs.writeFileSync(url, newData);
  90. Editor.log(
  91. '[HotUpdateTools] SearchPath updated in built main.js for hot update',
  92. );
  93. let time = new Date().getTime();
  94. Editor.Ipc.sendToPanel(
  95. 'hot-update-tools',
  96. 'hot-update-tools:onBuildFinished',
  97. time,
  98. );
  99. let CfgUtil = Editor.require(
  100. 'packages://hot-update-tools/core/CfgUtil.js',
  101. );
  102. CfgUtil.updateBuildTimeByMain(time);
  103. } else {
  104. Editor.log(`文件丢失: ${codeUrl}`);
  105. }
  106. //修改jsb-engine.js
  107. let engineCode = "if(jsb.fileUtils.isDirectoryExist(jsb.fileUtils.getWritablePath()+\"blackjack-remote-asset/assets/\"+nameOrUrl+\"/assets\")){url += \"/assets\";}\n";
  108. let root = Path.normalize(target.dest);
  109. let url = Path.join(root, 'jsb-adapter/jsb-engine.js');
  110. let data = Fs.readFileSync(url, 'utf8');
  111. let findStr = "var config = \"\".concat(url, \"/config.\").concat(version ? version + '.' : '', \"json\");";
  112. let findJ = data.indexOf(engineCode);
  113. if(findJ != -1){
  114. Editor.log(`代码片段已存在: ${engineCode}, 无需添加`);
  115. return
  116. }
  117. let findI = data.indexOf(findStr);
  118. // Editor.log(findI);
  119. let startStr = data.slice(0, findI);
  120. let lastStr = data.slice(findI);
  121. let newData = startStr + engineCode + lastStr;
  122. // setTimeout(() => {
  123. // Fs.writeFileSync(url, newData);
  124. // }, 2000);
  125. Fs.writeFileSync(url, newData);
  126. Editor.log(`代码片段添加完成`);
  127. // Editor.log(data);
  128. // Editor.log(engineCode);
  129. } else {
  130. Editor.log(
  131. `[HotUpdateTools] don't need update main.js, platform: ${target.platform}`,
  132. );
  133. }
  134. },
  135. },
  136. };