BundleLoaderManager.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. let GameConstant = require('GameConstant');
  2. let GameUpdate = require("GameUpdate");
  3. let Global = require("Global");
  4. let BaseDefine = require("BaseDefine");
  5. var topTipMsg = require("topTipMsg");
  6. let _BundleLoaderManager = null;
  7. window.bundles = {};
  8. class BundleLoaderManager {
  9. constructor() {
  10. this.taskList = {};
  11. this.bundleBaseData = {
  12. [GameConstant.KIND_ID_BRAB]: {
  13. bundle: "BRAB",
  14. taskID: 1,
  15. importBundle: ["TP_Public", "DSP_Public"],
  16. gamePrefabPath: ["BRABGameScence", "Base/TP"]
  17. }, [GameConstant.KIND_ID_BRTP]: {
  18. bundle: "BRTP",
  19. taskID: 2,
  20. importBundle: ["TP_Public", "DSP_Public"],
  21. gamePrefabPath: ["BRTPGameSence"]
  22. }, [GameConstant.KIND_ID_SGLB]: {
  23. bundle: "SGLB",
  24. taskID: 3,
  25. importBundle: ["LB"],
  26. gamePrefabPath: ["SGLBGameSence"]
  27. }, [GameConstant.KIND_ID_LBROMAN]: {
  28. bundle: "ROMAN",
  29. taskID: 4,
  30. importBundle: ["LB"],
  31. gamePrefabPath: ["RomanGameSence"]
  32. }, [GameConstant.KIND_ID_LBFRUIT]: {
  33. bundle: "FRUIT",
  34. taskID: 5,
  35. importBundle: ["LB"],
  36. gamePrefabPath: ["FruitGameSence"]
  37. }, [GameConstant.KIND_ID_BY]: {
  38. bundle: "FISH",
  39. taskID: 6,
  40. importBundle: [],
  41. gamePrefabPath: []
  42. }, [GameConstant.KIND_ID_DSP_AK47]: {
  43. bundle: "DSP",
  44. taskID: 7,
  45. importBundle: ["DSP_Public"],
  46. gamePrefabPath: ["DSP-AK47GameSence"]
  47. }, [GameConstant.KIND_ID_DSP_JOKER]: {
  48. bundle: "DSP",
  49. taskID: 8,
  50. importBundle: ["DSP_Public"],
  51. gamePrefabPath: ["DSP-JOKERGameSence"]
  52. }, [GameConstant.KIND_ID_DSP]: {
  53. bundle: "DSP",
  54. taskID: 9,
  55. importBundle: ["DSP_Public"],
  56. gamePrefabPath: ["DSPGameSence"]
  57. }, [GameConstant.KIND_ID_RUMMY2R]: {
  58. bundle: "RUMMY",
  59. taskID: 10,
  60. importBundle: [],
  61. gamePrefabPath: ["RUMMY2RGameSence"]
  62. }, [GameConstant.KIND_ID_RUMMY2R_10Z]: {
  63. bundle: "RUMMY",
  64. taskID: 11,
  65. importBundle: [],
  66. gamePrefabPath: ["RUMMY2RGameSence_10Z"]
  67. }, [GameConstant.KIND_ID_RUMMY]: {
  68. bundle: "RUMMY",
  69. taskID: 12,
  70. importBundle: [],
  71. gamePrefabPath: ["RUMMYGameSence"]
  72. }, [GameConstant.KIND_ID_RUMMY_10Z]: {
  73. bundle: "RUMMY",
  74. taskID: 13,
  75. importBundle: [],
  76. gamePrefabPath: ["RUMMYGameSence_10Z"]
  77. }, [GameConstant.KIND_ID_SCRATCH_CARD]: {
  78. bundle: "SCRATHCARD",
  79. taskID: 14,
  80. importBundle: [],
  81. gamePrefabPath: ["ScratchCardScene"]
  82. }, [GameConstant.KIND_ID_LUCKY_GAMSTONE]: {
  83. bundle: "LUCKYGEMSTONE",
  84. taskID: 15,
  85. importBundle: [],
  86. gamePrefabPath: ["LuckyGemstoneScene"]
  87. }, [GameConstant.KIND_ID_DZPK]: {
  88. bundle: "DZPK",
  89. taskID: 16,
  90. importBundle: [],
  91. gamePrefabPath: ["DZPKScene"]
  92. }, [GameConstant.KIND_ID_CACHETA]: {
  93. bundle: "CACHETA",
  94. taskID: 17,
  95. importBundle: [],
  96. gamePrefabPath: ["CachetaGameSence"]
  97. }, [GameConstant.KIND_ID_TRUCO]: {
  98. bundle: "TRUCO",
  99. taskID: 18,
  100. importBundle: [],
  101. gamePrefabPath: ["TrucoGameSence"]
  102. }, [GameConstant.KIND_ID_CRASH]: {
  103. bundle: "CRASH",
  104. taskID: 19,
  105. importBundle: [],
  106. gamePrefabPath: ["CRASH"]
  107. }, [GameConstant.KIND_ID_FRUIT9X]: {
  108. bundle: "FRUIT9X",
  109. taskID: 20,
  110. importBundle: ["LB"],
  111. gamePrefabPath: ["Fruit9XGameSence"]
  112. }, [GameConstant.KIND_ID_FRUITSLOTS]: {
  113. bundle: "FRUITSLOTS",
  114. taskID: 21,
  115. importBundle: [],
  116. gamePrefabPath: ["fruits"]
  117. }, [GameConstant.KIND_ID_TXGAME]: {
  118. bundle: "SGYYL",
  119. taskID: 22,
  120. importBundle: [],
  121. gamePrefabPath: ["sglbScene"]
  122. }, [GameConstant.KIND_ID_FOOTBALL]: {
  123. bundle: "FOOTBALL",
  124. taskID: 23,
  125. importBundle: ["LB"],
  126. gamePrefabPath: ["FootballGameSence"]
  127. }, [GameConstant.KIND_ID_MINES]: {
  128. bundle: "MINES",
  129. taskID: 24,
  130. importBundle: [],
  131. gamePrefabPath: ["MinesGameSence"]
  132. }, [GameConstant.KIND_ID_OLYMPUS]: {
  133. bundle: "OLYMPUS",
  134. taskID: 25,
  135. importBundle: [],
  136. gamePrefabPath: ["OlympusGameSence"]
  137. }, [GameConstant.KIND_ID_BCBM]: {
  138. bundle: "BCBM",
  139. taskID: 26,
  140. importBundle: [],
  141. gamePrefabPath: ["BCBM"]
  142. },
  143. };
  144. if (cc.sys.isNative) { this.jsbPath = jsb.fileUtils.getWritablePath(); }
  145. setInterval(() => {
  146. let keys = Object.keys(this.taskList);
  147. for (let i = 0; i < keys.length; i += 1) {
  148. let taskID = keys[i];
  149. if (cc.sys.os == cc.sys.OS_IOS && cc.sys.isNative) {
  150. return;
  151. }else if(cc.sys.os == cc.sys.OS_ANDROID && cc.sys.isNative){
  152. let progress = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Zip4jUtils",
  153. "getProgress",
  154. "(I)I",
  155. parseInt(taskID)
  156. );
  157. let a2 = parseInt((progress / 100).toFixed(2)) * 0.1;
  158. this.updateRefreshnComp(0.9 + a2, this.taskList[taskID]);
  159. if (progress == 100) {
  160. delete this.taskList[taskID];
  161. }
  162. }
  163. }
  164. }, 100)
  165. window.unZipProgress = (taskID, entryNumber, total) => {
  166. Global.print("window.unZipProgress taskID=" + taskID + " entryNumber="+entryNumber + " total="+total);
  167. let progress = (entryNumber)/(total);
  168. Global.print("window.unZipProgress progress=" + progress);
  169. let a2 = progress * 0.1;
  170. Global.print("window.unZipProgress a2=" + a2);
  171. this.updateRefreshnComp(0.9 + a2, this.taskList[""+taskID]);
  172. if (progress == 100) {
  173. delete this.taskList[taskID];
  174. }
  175. }
  176. window.unZipNextGo = (taskID) => {
  177. let keys = Object.keys(this.bundleBaseData);
  178. for (let i = 0; i < keys.length; i += 1) {
  179. if (this.bundleBaseData[keys[i]].taskID == taskID) {
  180. let gameName = GameConstant.getENGameNameByKindId(keys[i]);
  181. topTipMsg.showTopTipMsg(gameName + " atualização do jogo concluída");
  182. return;
  183. }
  184. }
  185. }
  186. }
  187. loadBundleGame(wKindID, callBack) {
  188. window.importBundles = {};
  189. this.callBack = callBack;
  190. let copyBundleData = this.loadBundleData(wKindID);
  191. this.loadBundle(copyBundleData);
  192. }
  193. loadBundleData(wKindID) {
  194. Global.print("BundleLoaderManager.bundleBaseData wKindID="+wKindID);
  195. let bundleData = this.bundleBaseData[wKindID];
  196. Global.print("BundleLoaderManager.bundleBaseData bundleData="+JSON.stringify(bundleData));
  197. if (bundleData) {
  198. let copyBundleData = JSON.parse(JSON.stringify(bundleData));
  199. return copyBundleData;
  200. }
  201. return false;
  202. }
  203. loadBundle(data) {
  204. Global.print("BundleLoaderManager.bundleBaseData data="+JSON.stringify(data));
  205. let readyLoaderBundle = null;
  206. if (data.importBundle.length > 0) {
  207. readyLoaderBundle = data.importBundle[0];
  208. } else {
  209. readyLoaderBundle = data.bundle;
  210. }
  211. let url = this.getBundleUrl(readyLoaderBundle);
  212. Global.print("BundleLoaderManager.bundleBaseData url="+url);
  213. Global.print("BundleLoaderManager.bundleBaseData cc.assetManager.getBundle()="+cc.assetManager.getBundle());
  214. cc.assetManager.loadBundle(url, () => {
  215. this.loadBundleProgress();
  216. }, (err, bundle) => {
  217. Global.print("BundleLoaderManager.bundleBaseData bundle.name="+bundle.name);
  218. window.bundles[bundle.name] = bundle;
  219. if (data.importBundle.length > 0) {
  220. data.importBundle.shift();
  221. window.importBundles[bundle.name] = bundle;
  222. this.loadBundle(data);
  223. } else {
  224. window.nowBundle = bundle;
  225. window.testFunction(bundle, data.gamePrefabPath);
  226. }
  227. });
  228. }
  229. getBundleUrl(readyLoaderBundle) {
  230. // if (cc.sys.localStorage.getItem(readyLoaderBundle + "isLocal") == 1) {
  231. // return this.jsbPath + 'sub-game/' + readyLoaderBundle;
  232. // } else {
  233. return readyLoaderBundle;
  234. // }
  235. }
  236. loadBundleProgress() {
  237. }
  238. hotUpdateGame(kindID) {
  239. if (this.bundleBaseData[kindID].bundleState == "updating") { return; }
  240. this.bundleBaseData[kindID].bundleState = "updating";
  241. let bundleName = this.bundleBaseData[kindID].bundle;
  242. let refreshnNode = this.foundRefreshnComp(kindID);
  243. // cc.vv.reflection.initSubGameOnCache(bundleName);
  244. let path = this.jsbPath + "blackjack-remote-asset/assets/" + bundleName + '/' + bundleName + "_project.manifest";
  245. let path1 = this.jsbPath + "blackjack-remote-asset/assets/" + bundleName + "/project.manifest";
  246. if (jsb.fileUtils.isFileExist(path1)) {
  247. path = path1;
  248. }
  249. cc.assetManager.loadRemote(path, (err, Asset) => {
  250. if (!err) {
  251. let bundleChecker = new GameUpdate;
  252. bundleChecker.goPlanA(Asset, bundleName, refreshnNode); //热更新
  253. bundleChecker.checkUpdate();
  254. }
  255. });
  256. }
  257. downloadGame(kindID) {
  258. if (this.bundleBaseData[kindID].bundleState == "downloading" ||
  259. this.bundleBaseData[kindID].bundleState == "unziping") { return; }
  260. this.bundleBaseData[kindID].bundleState = "downloading";
  261. let bundleName = this.bundleBaseData[kindID].bundle;
  262. let taskID = this.bundleBaseData[kindID].taskID;
  263. let refreshnNode = this.foundRefreshnComp(kindID);
  264. let version = parseInt(cc.vv.gameVersionInfo[bundleName]);
  265. let zip = BaseDefine.GAME_ZIP_URL + bundleName + "/ver_" + version + ".zip";
  266. cc.assetManager.loadRemote(zip, {
  267. ext: ".bin",
  268. onFileProgress: (current, total) => {
  269. this.current = current;
  270. this.total = total;
  271. this.updateRefreshnComp((this.current / this.total * 0.9).toFixed(2), refreshnNode);
  272. }
  273. }, (err, bufferAsset) => {
  274. let data = bufferAsset._buffer;
  275. let dirpath = jsb.fileUtils.getWritablePath() + 'zips/';
  276. let filepath = dirpath + bundleName + ".zip";
  277. if (!jsb.fileUtils.isDirectoryExist(dirpath)) {
  278. jsb.fileUtils.createDirectory(dirpath);
  279. } if (jsb.fileUtils.writeDataToFile(new Uint8Array(data), filepath)) {
  280. this.bundleBaseData[kindID].bundleState = "unziping";
  281. this.taskList[taskID] = refreshnNode;
  282. if (cc.sys.os == cc.sys.OS_IOS && cc.sys.isNative) {
  283. cc.vv.reflection.unzipFile(filepath, jsb.fileUtils.getWritablePath() + "blackjack-remote-asset/assets/" + bundleName, "123456", taskID);
  284. }else if(cc.sys.os == cc.sys.OS_ANDROID && cc.sys.isNative){
  285. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  286. "unZipPack1",
  287. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
  288. "zips/" + bundleName + ".zip",
  289. "/blackjack-remote-asset/assets/" + bundleName,
  290. "123456",
  291. taskID
  292. );
  293. }
  294. }
  295. })
  296. }
  297. foundRefreshnComp(kindID) {
  298. let parent0 = cc.find("Canvas/RightNodeEx/RecomNode");
  299. let parent1 = cc.find("Canvas/SecondaryGameList/Content/LeftNode/GameList/view/content");
  300. let parent2 = cc.find("Canvas/RightNodeEx/GameButtonNode/ScrollView/view/content");
  301. let refreshnNode = [];
  302. for (let j = 0; j < parent0.children.length; j += 1) {
  303. let node = parent0.children[j];
  304. let updatebar = node.getChildByName("updatebar")
  305. let comp = updatebar.getComponent("GameUpdateShower");
  306. let config = comp.getGameData();
  307. if (config.kindID == kindID) {
  308. comp.readyDownload();
  309. refreshnNode.push(comp);
  310. break;
  311. }
  312. }
  313. for (let j = 0; j < parent1.children.length; j += 1) {
  314. let node = parent1.children[j];
  315. let updatebar = node.getChildByName("updatebar")
  316. let comp = updatebar.getComponent("GameUpdateShower");
  317. let config = comp.getGameData();
  318. if (config.kindID == kindID) {
  319. comp.readyDownload();
  320. refreshnNode.push(comp);
  321. break;
  322. }
  323. }
  324. for (let k = 0; k < parent2.children.length; k += 1) {
  325. let node = parent2.children[k];
  326. let updatebar = node.getChildByName("updatebar")
  327. let comp = updatebar.getComponent("GameUpdateShower");
  328. if (comp) {
  329. let config = comp.getGameData();
  330. if (config.kindID == kindID) {
  331. comp.readyDownload();
  332. refreshnNode.push(comp);
  333. break;
  334. }
  335. }
  336. }
  337. return refreshnNode;
  338. }
  339. updateRefreshnComp(progress, refreshnNode, type) {
  340. for (let i = 0; i < refreshnNode.length; i += 1) {
  341. let comp = refreshnNode[i];
  342. let kindID = comp.getGameData().kindID;
  343. if (progress == 1) {
  344. comp.overDownload();
  345. this.bundleBaseData[kindID].bundleState = "intoTheGame";
  346. } else {
  347. comp.gameDownloading(progress);
  348. }
  349. }
  350. }
  351. getBundleNameByKindID(KindID) {
  352. if (this.bundleBaseData[KindID]) {
  353. return this.bundleBaseData[KindID].bundle;
  354. }
  355. return null;
  356. }
  357. getBundleStateBuKindID(KindID) {
  358. if (this.bundleBaseData[KindID]) {
  359. return this.bundleBaseData[KindID].bundleState;
  360. }
  361. return null;
  362. }
  363. };
  364. BundleLoaderManager.Instance = function () {
  365. if (!_BundleLoaderManager) {
  366. _BundleLoaderManager = new BundleLoaderManager();
  367. window.BundleLoaderManager = _BundleLoaderManager;
  368. }
  369. return _BundleLoaderManager;
  370. }
  371. module.exports = BundleLoaderManager;