HotLoad.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. let AppFlyerUtil = require("AppFlyerUtil");
  2. let WebAPIMd5 = require("WebAPIMd5");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. loadingBg: cc.Node,
  7. loadingSprite: cc.Node,
  8. label1: cc.Node,
  9. label2: cc.Node,
  10. },
  11. // start() {
  12. // AppFlyerUtil.init();
  13. // this.getLoginAddress();
  14. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Native", "clearWelcomeImage", "()V");
  15. // },
  16. // checkLoadingSceneBool() {
  17. // if (Troy_ReviewApk == "0" || cc.Troy.ReviewApk == false) {
  18. // cc.Troy.ReviewApk = false;
  19. // this.initApkPack();
  20. // } else if (window.loadingSceneBool != null) {
  21. // cc.Troy.ReviewApk = !window.loadingSceneBool;
  22. // this.initApkPack();
  23. // } else {
  24. // setTimeout(() => {
  25. // this.checkLoadingSceneBool();
  26. // }, 1000);
  27. // }
  28. //
  29. // },
  30. start() {
  31. },
  32. doSth(){
  33. console.log("HotLoad=========>"+1);
  34. if (cc.sys.os == cc.sys.OS_ANDROID && cc.sys.isNative) {
  35. console.log("HotLoad=========>"+2);
  36. // if (fsUtils && fsUtils.initJsbDownloader)
  37. // fsUtils.initJsbDownloader();
  38. // this.packageName = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Native", "getAppId", "()Ljava/lang/String;");
  39. // this.appPackName = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getAppId", "()Ljava/lang/String;");
  40. // this.initApkPack();
  41. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  42. "initConfigLoader",
  43. "()V",
  44. );
  45. setTimeout(()=>{
  46. this.initApkPack1();
  47. }, 1000)
  48. // this.initApkPack();
  49. return;
  50. }
  51. },
  52. buttonTest() {
  53. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "googleReferrer", "()V");
  54. },
  55. getLoginAddress() {
  56. // let appPackName = "com.brazil.xfnb";
  57. // let URL = "http://web-cb1d5017dd23866d.elb.sa-east-1.amazonaws.com/api/login_ip_map?" + "PackageName=" + appPackName;
  58. // URL = WebAPIMd5(URL);
  59. // let xhr = new XMLHttpRequest();
  60. // xhr.timeout = 8000;
  61. // xhr.ontimeout = () => { this.getLoginAddress(); }
  62. // xhr.open("GET", URL);
  63. // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  64. // xhr.onreadystatechange = () => {
  65. // if (xhr.readyState !== 4) { return; }
  66. // if (xhr.status === 200) {
  67. // let json = JSON.parse(xhr.responseText);
  68. // cc.log(json);
  69. // this.LoginIndex = 0;
  70. // this.LoginAddress = json.data;
  71. // this.LoginWebSocket();
  72. // }
  73. // };
  74. // xhr.send();
  75. },
  76. LoginWebSocket() {
  77. // let data = this.LoginAddress[this.LoginIndex]
  78. // let url = "ws://" + data.LoginAddr + ":" + data.LoginPort;
  79. // let ws = new WebSocket(url);
  80. // ws.onopen = (event) => {
  81. // cc.log("onopen", event);
  82. // this.makeGetVerinfoReq();
  83. // this.sendCMD(1, 13);
  84. // };
  85. // ws.onmessage = (event) => {
  86. // cc.log("onmessage", event);
  87. // };
  88. // ws.onclose = (event) => {
  89. // cc.log("onclose", event);
  90. // };
  91. // ws.onerror = (event) => {
  92. // cc.log("onerror", event);
  93. // };
  94. // ws.ontimeout = (event) => {
  95. // cc.log("ontimeout", event);
  96. // };
  97. },
  98. // makeGetVerinfoReq() {
  99. // let VersionInfo = {};
  100. // VersionInfo.UserID = 0;
  101. // VersionInfo.Channel
  102. // },
  103. // sendCMD(main, sub, dv = new DataView(new ArrayBuffer(8))) {
  104. // dv.pos = 0;
  105. // Global.SetUint8(dv, 2);
  106. // Global.SetUint8(dv, 0);
  107. // Global.SetUint16(dv, dv.byteLength);
  108. // Global.SetUint16(dv, main);
  109. // Global.SetUint16(dv, sub);
  110. // Global.print("SendSocketData main:" + main + " sub:" + sub);
  111. // return this.mSocketEngine.send(dv.buffer);
  112. // },
  113. initApkPack1(){
  114. window.unZipNextGo = (taskID) => { this.unZipNextGo(); }
  115. // this.checkReviewApk();
  116. this.passWord = "52%^##11%";
  117. this.localPath = "gameJsonData.bin";
  118. this.inputString = "gameJsonData/gameJsonData.bin";
  119. this.outputString = "/blackjack-remote-asset";
  120. this.downloadPath = "gameJsonData.bin";
  121. let bool1 = this.checkHasTargetZip();
  122. if (bool1) {
  123. this.unZipLocal();
  124. } else {
  125. let bool2 = jsb.fileUtils.isFileExist(jsb.fileUtils.getWritablePath() + this.inputString);
  126. if (bool2) {
  127. this.unZipDownload();
  128. } else {
  129. this.downloadZip();
  130. }
  131. }
  132. },
  133. initApkPack() {
  134. console.log("HotLoad=========>"+3);
  135. this.initGlobalFunc();
  136. this.checkReviewApk();
  137. let bool1 = this.checkHasTargetZip();
  138. if (bool1) {
  139. console.log("HotLoad=========>"+3.1);
  140. this.unZipLocal();
  141. } else {
  142. console.log("HotLoad=========>"+3.2);
  143. let bool2 = jsb.fileUtils.isFileExist(jsb.fileUtils.getWritablePath() + this.inputString);
  144. if (bool2) {
  145. this.unZipDownload();
  146. } else {
  147. this.downloadZip();
  148. }
  149. }
  150. },
  151. initGlobalFunc() {
  152. window.unZipNextGo = () => { this.unZipNextGo(); }
  153. this.loadSprite = this.loadingSprite.getComponent(cc.Sprite);
  154. },
  155. checkReviewApk() {
  156. if (cc.Troy.ReviewApk) {
  157. this.passWord = cc.Troy.APack_PassWord;
  158. this.localPath = cc.Troy.APack_LocalPath;
  159. this.inputString = cc.Troy.APack_IutPutString;
  160. this.outputString = cc.Troy.APack_OutPutString;
  161. this.downloadPath = cc.Troy.APack_DownloadPath;
  162. } else {
  163. this.passWord = cc.Troy.BPack_PassWord;
  164. this.localPath = cc.Troy.BPack_LocalPath;
  165. this.inputString = cc.Troy.BPack_IutPutString;
  166. this.outputString = cc.Troy.BPack_OutPutString;
  167. this.downloadPath = cc.Troy.BPack_DownloadPath;
  168. }
  169. },
  170. checkHasTargetZip() {
  171. let bool = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  172. "checkHasZip",
  173. "(Ljava/lang/String;)Z",
  174. this.localPath
  175. );
  176. return bool;
  177. },
  178. unZipDownload() {
  179. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  180. "unZipPack1",
  181. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
  182. this.inputString,
  183. this.outputString,
  184. this.passWord,
  185. );
  186. },
  187. unZipLocal() {
  188. console.log("HotLoad=========>"+4);
  189. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  190. "unZipPack0",
  191. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
  192. this.localPath,
  193. this.outputString,
  194. this.passWord,
  195. );
  196. },
  197. // unZipLocal() {
  198. // console.log("HotLoad=========>"+4);
  199. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  200. // "unZipPack2",
  201. // "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
  202. // this.localPath,
  203. // this.outputString,
  204. // this.passWord,
  205. // );
  206. // },
  207. downloadZip() {
  208. let index = 0;
  209. let arr = ["Look before you leap", "The best is the enemy of the good", "When one door shuts anther opens"];
  210. this.loadingBg.active = true;
  211. this.loadingSprite.active = true;
  212. this.label1.active = true;
  213. this.label2.active = true;
  214. let func = () => {
  215. this.label2.getComponent(cc.Label).string = arr[index];
  216. index += 1;
  217. index = index > 2 ? 0 : index;
  218. }
  219. func();
  220. this.schedule(func, 5, 99999);
  221. let url = cc.Troy.DownloadPath + this.downloadPath;
  222. cc.log(url);
  223. cc.assetManager.loadRemote(url, {
  224. ext: ".bin",
  225. onFileProgress: (current, total) => {
  226. this.current = current;
  227. this.total = total;
  228. this.loadSprite.fillRange = this.current / this.total;
  229. }
  230. }, (err, bufferAsset) => {
  231. this.loadingBg.active = false;
  232. this.loadingSprite.active = false;
  233. this.label1.active = false;
  234. this.label2.active = false;
  235. let data = bufferAsset._buffer;
  236. let dirpath = jsb.fileUtils.getWritablePath() + 'zips/';
  237. let filepath = dirpath + this.inputString.split("zips/")[1];
  238. if (!jsb.fileUtils.isDirectoryExist(dirpath)) {
  239. jsb.fileUtils.createDirectory(dirpath);
  240. } if (jsb.fileUtils.writeDataToFile(new Uint8Array(data), filepath)) {
  241. this.unZipDownload();
  242. }
  243. })
  244. },
  245. unZipNextGo() {
  246. console.log("HotLoad=========>"+5);
  247. let path = [
  248. jsb.fileUtils.getWritablePath() + "blackjack-remote-asset/",
  249. ]
  250. jsb.fileUtils.setSearchPaths(path);
  251. cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(path));
  252. // setTimeout(()=>{
  253. // cc.game.restart();
  254. // },3000)
  255. cc.audioEngine.stopAll();
  256. //延迟0.5秒重启
  257. setTimeout(()=>{
  258. cc.game.restart();
  259. }, 500)
  260. },
  261. });