PreLoader.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. var PreLoader = cc.Class({
  2. ctor: function () {
  3. this.m_MapBundle = {};
  4. this.m_MapPrefab = {};
  5. this.m_MapAudio = {};
  6. this.m_MapRes = {};
  7. this.m_szToolPhp = 'Tool.php';
  8. this.m_szRemote = 'remote';
  9. // this.m_sBundles = new Array("10017","21201", "Chat_2", "GamePublic_3", "GameSetting_2", "GPS_2", "Jetton_2",
  10. // "PublicMJ_2", "Activity", "AgreeMent", "Alert_6", "BindPhone", "Club", "ClubAndroid", "Com_6", "CSY", "DLG_7", "FilterNode",
  11. // "GameRecord_2", "InviteCode", "Login", "Pay","MailInfo", "PublicAudio", "RealAuth", "Room", "SelfInfo", "Service",
  12. // "Setting_3", "Share", "Sign", "TurnTable", "UpdateManager_7");
  13. this.m_sBundles = new Array("21201", "Chat_2", "GamePublic_3", "GameSetting_2", "Jetton_2",
  14. "PublicMJ_2", "Activity", "AgreeMent", "Alert_6", "BindPhone", "Club", "ClubAndroid", "Com_6", "CSY", "DLG_7", "FilterNode",
  15. "GameRecord_2", "InviteCode", "Login", "MailInfo", "PublicAudio", "Room", "SelfInfo", "Service",
  16. "Setting_3", "Share", "Sign", "TurnTable", "Store", "UpdateManager_7");
  17. },
  18. Init: function (CallFunc) {
  19. // this.LoadBundleVer(CallFunc);
  20. this.getAppVersion(() => {
  21. if (window.IS_UPDATE) {
  22. this.LoadBundleVer(CallFunc)
  23. } else {
  24. this.loadLocalBundle(CallFunc)
  25. }
  26. });
  27. },
  28. loadLocalBundle: function (CallFunc) {
  29. //console.log("this.m_MapBundle-------2", this.m_MapPrefab, this.m_MapAudio, this.m_MapRes)
  30. let Cnt = 0;
  31. let Index = 0;
  32. for (let index = 0; index < this.m_sBundles.length; index++) {
  33. this.m_MapBundle[this.m_sBundles[index]] = {
  34. Name: this.m_sBundles[index],
  35. Ver: 0,
  36. Bundle: null,
  37. PrefabCnt: 0,
  38. }
  39. Cnt++;
  40. }
  41. // console.log("this.m_MapBundle---------3")
  42. for (let index = 0; index < this.m_sBundles.length; index++) {
  43. let BundleName = this.m_sBundles[index];
  44. cc.assetManager.loadBundle(BundleName, "", function (err, bundle) {
  45. if (err) {
  46. return console.error(err);
  47. }
  48. for (let i in bundle._config.paths._map) {
  49. let asset = bundle._config.paths._map[i];
  50. for (let j = 0; j < asset.length; j++) {
  51. let Name = asset[j].path.substring(asset[j].path.lastIndexOf('/') + 1, asset[j].path.length);
  52. let Path = asset[j].path.substring(asset[j].path.lastIndexOf('/') + 1, 0);
  53. let pathSpl = Path.split('/')
  54. let isTo = false;
  55. let isImage = false;
  56. for (let p = 0; p < pathSpl.length; p++) {
  57. if (pathSpl[p] == "Prefab") {
  58. if (!this.m_MapPrefab[Name]) {
  59. this.m_MapPrefab[Name] = {
  60. BundleName: BundleName,
  61. Name: Name,
  62. ComName: Name,
  63. Path: Path,
  64. Prefab: null,
  65. Type: cc.Prefab,
  66. };
  67. }
  68. isTo = true;
  69. break;
  70. } else if (pathSpl[p] == 'Audio' || BundleName == "PublicAudio") {
  71. if (!this.m_MapAudio[BundleName]) this.m_MapAudio[BundleName] = {};
  72. var AudioName = `${Path}${Name}`.replace(/\//g, '_');
  73. this.m_MapAudio[BundleName][AudioName] = {
  74. BundleName: BundleName,
  75. Name: Name,
  76. Path: Path,
  77. Audio: null,
  78. Type: cc.AudioClip,
  79. };
  80. isTo = true;
  81. break;
  82. } else if (pathSpl[p] == 'Image') {
  83. isImage = true;
  84. break;
  85. }
  86. }
  87. if (isTo == false) {
  88. if (!this.m_MapRes[BundleName]) {
  89. this.m_MapRes[BundleName] = {};
  90. }
  91. let ResName = `${Path}${Name}`.replace(/\//g, '_');
  92. //let replacedStr = asset[j].ctor.name.replace("_", ".");
  93. this.m_MapRes[BundleName][ResName] = {
  94. BundleName: BundleName,
  95. Name: Name,
  96. Path: Path,
  97. Body: null,
  98. Type: isImage ? cc.SpriteFrame : null,
  99. // Type: eval(replacedStr),
  100. };
  101. }
  102. }
  103. }
  104. Index++;
  105. if (Index == Cnt && CallFunc) CallFunc();
  106. }.bind(this));
  107. }
  108. //console.log("this.m_MapBundle-------3", this.m_MapPrefab, this.m_MapAudio, this.m_MapRes)
  109. },
  110. getAppVersion: function (cb) {
  111. let webUrl = `${window.PHP_HOME}/${this.m_szToolPhp}`;
  112. if (cc.sys.OS_ANDROID == cc.sys.os) {
  113. webUrl += '?GetMark=3';
  114. } else {
  115. webUrl += '?GetMark=2';
  116. }
  117. WebCenter.GetData(webUrl, 0, function (data) {
  118. let pData = JSON.parse(data);
  119. if (pData) {
  120. window.APP_VERSION_ON_LINE = pData.verIos
  121. window.IS_UPDATE = pData.IS_UPDATE == "1" ? true : false;
  122. console.log("window.APP_VERSION_ON_LINE----", pData.verIos, pData.IS_UPDATE, window.IS_UPDATE)
  123. if (window.isAppVerSame()) {
  124. console.log("GetMark=2----版本一样")
  125. } else {
  126. console.log("GetMark=2----版本不一样")
  127. }
  128. }
  129. cb()
  130. }.bind(this))
  131. console.log("window.APP_BOTTOM_VER--", window.APP_BOTTOM_VER_ANDROID, window.APP_BOTTOM_VER_IOS)
  132. },
  133. LoadBundleVer: function (CallFunc) {
  134. var webUrl = `${window.PHP_HOME}/${this.m_szToolPhp}`;
  135. if (cc.share.IsH5_WX()) {
  136. webUrl += '?GetMark=1';
  137. this.m_szRemote = 'game/assets';
  138. } else {
  139. webUrl += '?GetMark=0';
  140. }
  141. WebCenter.GetData(webUrl, 0, function (data) {
  142. var Cnt = 0;
  143. var Index = 0;
  144. var pData = JSON.parse(data);
  145. // console.log("pData-------",pData)
  146. if (pData) {
  147. for (var i in pData) {
  148. if (i == 'RemotePath') {
  149. this.m_szRemote = pData[i];
  150. continue;
  151. }
  152. if (pData[i].length == 0) continue;
  153. var ver = pData[i][0].substring(pData[i][0].indexOf('.') + 1, pData[i][0].lastIndexOf('.'));
  154. if (this.m_MapBundle[i]) {
  155. this.m_MapBundle[i].Ver = ver;
  156. this.m_MapBundle[i].PrefabCnt = 0;
  157. }
  158. else this.m_MapBundle[i] = {
  159. Name: i,
  160. Ver: ver,
  161. Bundle: null,
  162. PrefabCnt: 0,
  163. };
  164. Cnt++;
  165. }
  166. // console.log("this.m_MapBundle-------", this.m_MapBundle)
  167. }
  168. for (var i in this.m_MapBundle) {
  169. var UrlRemote = `${window.PHP_HOME}/${this.m_szRemote}/${this.m_MapBundle[i].Name}/config.${this.m_MapBundle[i].Ver}.json`;
  170. if (window.LOG_NET_DATA) console.log(` ###### LoadBundleVer ${UrlRemote} `);
  171. cc.assetManager.loadRemote(UrlRemote, function (a, b, c) {
  172. //console.log("abc-------", a, b, c)
  173. var BundleName = b.json.name;
  174. for (var j in b.json.paths) {
  175. if (!b.json.types[b.json.paths[j][1]]) continue;
  176. var Name = b.json.paths[j][0].substring(b.json.paths[j][0].lastIndexOf('/') + 1, b.json.paths[j][0].length);
  177. var Path = b.json.paths[j][0].substring(b.json.paths[j][0].lastIndexOf('/') + 1, 0);
  178. if (b.json.types[b.json.paths[j][1]] == 'cc.Prefab') {
  179. if (!this.m_MapPrefab[Name]) {
  180. this.m_MapPrefab[Name] = {
  181. BundleName: BundleName,
  182. Name: Name,
  183. ComName: Name,
  184. Path: Path,
  185. Prefab: null,
  186. Type: cc.Prefab,
  187. };
  188. }
  189. this.m_MapBundle[BundleName].PrefabCnt++;
  190. } else if (b.json.types[b.json.paths[j][1]] == 'cc.AudioClip') {
  191. if (!this.m_MapAudio[BundleName]) this.m_MapAudio[BundleName] = {};
  192. var AudioName = `${Path}${Name}`.replace(/\//g, '_');
  193. this.m_MapAudio[BundleName][AudioName] = {
  194. BundleName: BundleName,
  195. Name: Name,
  196. Path: Path,
  197. Audio: null,
  198. Type: cc.AudioClip,
  199. };
  200. } else {
  201. if (!this.m_MapRes[BundleName]) this.m_MapRes[BundleName] = {};
  202. var ResName = `${Path}${Name}`.replace(/\//g, '_');
  203. this.m_MapRes[BundleName][ResName] = {
  204. BundleName: BundleName,
  205. Name: Name,
  206. Path: Path,
  207. Body: null,
  208. Type: eval(b.json.types[b.json.paths[j][1]]),
  209. };
  210. }
  211. }
  212. Index++;
  213. if (Index == Cnt && CallFunc) CallFunc();
  214. }.bind(this));
  215. }
  216. //console.log("this.m_MapBundle-------2", this.m_MapPrefab, this.m_MapAudio, this.m_MapRes)
  217. }.bind(this));
  218. },
  219. _LoadBundle: function (BundleName, Callback, Param) {
  220. //console.log("_LoadBundle---1----",BundleName)
  221. if (this.m_MapBundle[BundleName].Bundle == 'loading') return 'loading';
  222. if (this.m_MapBundle[BundleName].Bundle) {
  223. if (window.LOG_NET_DATA) console.log(` ###### _LoadBundle => ${BundleName} is loaded, to loadprefab `);
  224. if (Callback) Callback(this.m_MapBundle[BundleName].Bundle, Param);
  225. } else {
  226. this.m_MapBundle[BundleName].Bundle = 'loading';
  227. var UrlBundle = `${BundleName}`;
  228. var Version = null;
  229. if (cc.sys.isNative && window.IS_UPDATE) {
  230. UrlBundle = `${window.PHP_HOME}/${this.m_szRemote}/${BundleName}`;
  231. Version = new Object();
  232. Version.version = this.m_MapBundle[BundleName].Ver;
  233. }
  234. if (window.LOG_NET_DATA) console.log(` ###### _LoadBundle => ${UrlBundle} `);
  235. cc.assetManager.loadBundle(UrlBundle, Version, function (err, bundle) {
  236. if (err) {
  237. this.m_MapBundle[BundleName].Bundle = null;
  238. this.m_MapBundle[BundleName].Error = err;
  239. if (window.LOG_NET_DATA) (`loadBundle[${BundleName}] error ${err}`);
  240. if (window.trapExceptional) {
  241. window.trapExceptional(`_LoadBundle ${BundleName} ${err}`, `Preloader`);
  242. }
  243. return null;
  244. }
  245. if (window.LOG_NET_DATA) console.log(` ###### _LoadBundle success => ${BundleName} `);
  246. cc.sys.localStorage.setItem(`${window.Key_ABVersion}_${BundleName}`, this.m_MapBundle[BundleName].Ver);
  247. this.m_MapBundle[BundleName].Bundle = (bundle);
  248. this.m_MapBundle[BundleName].Error = null;
  249. if (Param) Param.BundleName = BundleName;
  250. if (Callback) Callback(bundle, Param);
  251. }.bind(this));
  252. }
  253. },
  254. _LoadPrefab: function (PrefabName, Bundle, OnSuccess, OnFalied, Param) {
  255. if (this.m_MapPrefab[PrefabName].Prefab == 'loading') return 'loading';
  256. this.m_MapPrefab[PrefabName].Prefab = 'loading';
  257. var UrlPrefab = `${this.m_MapPrefab[PrefabName].Path}${this.m_MapPrefab[PrefabName].Name}`;
  258. if (window.LOG_NET_DATA) console.log(` ###### _LoadPrefab => loadPrefab url ${UrlPrefab} `);
  259. Bundle.load(UrlPrefab, cc.Prefab, function (a, b, c) {
  260. cc.director.emit('UpdateItemProgress', a, b);
  261. }.bind(this), function (err, prefab) {
  262. if (err) {
  263. this.m_MapPrefab[PrefabName].Prefab = null;
  264. this.m_MapPrefab[PrefabName].Error = err;
  265. console.log(`_loadPrefab[${PrefabName}] error ${err}`);
  266. if (OnFalied) OnFalied(err);
  267. return null;
  268. }
  269. if (window.LOG_NET_DATA) console.log(` ###### _LoadPrefab success => ${PrefabName} => ${this.m_MapPrefab[PrefabName].Path}${this.m_MapPrefab[PrefabName].Name} `);
  270. this.m_MapPrefab[PrefabName].Prefab = prefab;
  271. this.m_MapPrefab[PrefabName].Error = null;
  272. if (OnSuccess) OnSuccess(prefab, Param);
  273. }.bind(this));
  274. },
  275. LoadPrefab: function (Key, OnSuccess, OnFalied) {
  276. if (!this.m_MapPrefab[Key]) return null;
  277. var szBundleName = this.m_MapPrefab[Key].BundleName;
  278. if (!szBundleName) return null;
  279. if (this.m_MapBundle[szBundleName].Bundle == 'loading') {
  280. if (window.LOG_NET_DATA) console.log(` ###### LoadPrefab => bundle ${szBundleName} is loading `);
  281. return null;
  282. }
  283. if (this.m_MapPrefab[Key].Prefab == 'loading') {
  284. if (window.LOG_NET_DATA) console.log(` ###### LoadPrefab => prefab ${Key} is loading `);
  285. return null;
  286. }
  287. if (this.m_MapPrefab[Key].Prefab) {
  288. var TempPre = cc.instantiate(this.m_MapPrefab[Key].Prefab).getComponent(this.m_MapPrefab[Key].ComName);
  289. if (!TempPre) {
  290. this.m_MapPrefab[Key].Prefab = null;
  291. if (window.LOG_NET_DATA) console.log(` ###### LoadPrefab faild => reload prefab ${Key} `);
  292. this.LoadPrefab(Key, OnSuccess, OnFalied);
  293. return null;
  294. }
  295. if (OnSuccess) OnSuccess(TempPre);
  296. return TempPre;
  297. } else {
  298. this._LoadBundle(szBundleName, function (Bundle, Param1) {
  299. this._LoadPrefab(Key, Bundle, function (prefab, Param2) {
  300. var TempPre = cc.instantiate(prefab).getComponent(this.m_MapPrefab[Param1.Key].ComName);
  301. if (Param1 && Param1.OnSuccess) Param1.OnSuccess(TempPre);
  302. }.bind(this), function (err) {
  303. if (Param1 && Param1.OnFalied) Param1.OnFalied(err);
  304. else if (window.trapExceptional) {
  305. window.trapExceptional(`Loadprefab ${Param1.Key} ${err}`, `Preloader`);
  306. }
  307. },);
  308. }.bind(this), { Key: Key, OnSuccess: OnSuccess, OnFalied: OnFalied });
  309. }
  310. return null;
  311. },
  312. LoadAudio: function (Key, Path, KindID, Callback, Param) {
  313. this.LoadAudioByBundle(KindID, Key, Path, Callback, Param);
  314. },
  315. LoadPublicAudio: function (Key, Callback, Param) {
  316. this.LoadAudioByBundle('PublicAudio', Key, null, Callback, Param);
  317. },
  318. LoadAudioByBundle: function (BundleName, Key, Path, Callback, Param) {
  319. if (!this.m_MapAudio[BundleName]) return null;
  320. for (var i in this.m_MapAudio[BundleName]) {
  321. var UrlAudio = `${this.m_MapAudio[BundleName][i].Path}${this.m_MapAudio[BundleName][i].Name}`;
  322. if (Path) { if (Path != UrlAudio) continue; }
  323. else { if (Key != i) continue; }
  324. if (this.m_MapBundle[BundleName].Bundle == 'loading') break;
  325. if (this.m_MapBundle[BundleName].Bundle) {
  326. this.m_MapBundle[BundleName].Bundle.load(UrlAudio, cc.AudioClip, function (a, b, c) { }.bind(this), function (err, audio) {
  327. if (!err && Callback) Callback(audio, Param);
  328. }.bind(this));
  329. } else {
  330. this._LoadBundle(BundleName, function (Bundle) {
  331. this.m_MapBundle[BundleName].Bundle.load(UrlAudio, cc.AudioClip, function (a, b, c) { }.bind(this), function (err, audio) {
  332. if (!err && Callback) Callback(audio, Param);
  333. }.bind(this));
  334. }.bind(this));
  335. }
  336. break;
  337. }
  338. },
  339. LoadGamePrefab: function (Key, Callback) {
  340. return this.LoadPrefab(Key, Callback);
  341. },
  342. LoadByGame: function (KindID) {
  343. },
  344. LoadPrefabByBundle: function (BundleName) {
  345. for (var i in this.m_MapPrefab) {
  346. if (this.m_MapPrefab[i].BundleName != BundleName) continue;
  347. this.LoadPrefab(i);
  348. }
  349. },
  350. LoadResByBundle: function (BundleName, path, Callback) {
  351. this._LoadBundle(BundleName, function (Bundle) {
  352. Bundle.loadDir(path, cc.SpriteFrame, function (err, assets) {
  353. console.log(assets);
  354. if (Callback) Callback();
  355. }.bind(this));
  356. }.bind(this));
  357. },
  358. LoadRes: function (Name, BundleName, Callback, Param) {
  359. if (!this.m_MapRes[BundleName]) return null;
  360. if (!this.m_MapRes[BundleName][Name]) return null;
  361. if (this.m_MapRes[BundleName][Name].Body) {
  362. if (Callback) Callback(this.m_MapRes[BundleName][Name].Body, Param);
  363. return this.m_MapRes[BundleName][Name].Body;
  364. }
  365. if (Callback && this.m_MapRes[BundleName][Name]) {
  366. if (!!!this.m_MapRes[BundleName][Name].CallbackList) this.m_MapRes[BundleName][Name].CallbackList = [];
  367. this.m_MapRes[BundleName][Name].CallbackList.push({
  368. Callback: Callback,
  369. Param: Param
  370. })
  371. }
  372. return this._LoadBundle(BundleName, function (Bundle) {
  373. for (var i in this.m_MapRes[BundleName]) {
  374. if (this.m_MapRes[BundleName][i].CallbackList && this.m_MapRes[BundleName][i].CallbackList.length > 0) {
  375. this._LoadRes(Bundle, `${this.m_MapRes[BundleName][i].Path}${this.m_MapRes[BundleName][i].Name}`, i, BundleName);
  376. }
  377. }
  378. }.bind(this), Param);
  379. },
  380. _LoadRes: function (Bundle, Url, Name, BundleName, Callback, Param) {
  381. Bundle.load(Url, this.m_MapRes[BundleName][Name].Type, function (a, b, c) { }.bind(this), function (err, res) {
  382. if (err) {
  383. this.m_MapRes[BundleName][Name].Body = null;
  384. this.m_MapRes[BundleName][Name].Error = err;
  385. console.log(`_LoadRes[ ${BundleName} => ${Name} ] error ${err}`);
  386. if (window.trapExceptional) {
  387. window.trapExceptional(`_LoadRes ${BundleName} => ${Name} ${err}`, `Preloader`);
  388. }
  389. return;
  390. }
  391. this.m_MapRes[BundleName][Name].Body = res;
  392. for (var j in this.m_MapRes[BundleName][Name].CallbackList) {
  393. this.m_MapRes[BundleName][Name].CallbackList[j].Callback(this.m_MapRes[BundleName][Name].Body, this.m_MapRes[BundleName][Name].CallbackList[j].Param);
  394. }
  395. this.m_MapRes[BundleName][Name].CallbackList = new Array();
  396. }.bind(this));
  397. },
  398. Exist_Res: function (BundleName, Name) {
  399. if (!this.m_MapRes[BundleName]) return false;
  400. if (!this.m_MapRes[BundleName][Name]) return false;
  401. return true;
  402. },
  403. Exist_Prefab: function (Name) {
  404. if (!this.m_MapPrefab[Name]) return false;
  405. var BundleName = this.m_MapPrefab[Name].BundleName;
  406. if (!BundleName) return false;
  407. if (!this.m_MapBundle[BundleName]) return false;
  408. return true;
  409. },
  410. _GetGameSupport: function (wKindID) {
  411. var LoadArray = ['GamePublic_3', 'Chat_2', 'GPS_2', 'Jetton_2', 'GameSetting_2'];
  412. if (!wKindID) {
  413. LoadArray.push('GamePHZ_2');
  414. LoadArray.push('PublicMJ_2');
  415. } else {
  416. // for (var i in window.GameList_MJ_New) {
  417. // if (window.GameList_MJ_New[i] == wKindID) {
  418. // LoadArray.push('GameMJ_2');
  419. // break;
  420. // }
  421. // }
  422. for (var i in window.GameList_MJ) {
  423. if (window.GameList_MJ[i] == wKindID) {
  424. LoadArray.push('PublicMJ_2');
  425. break;
  426. }
  427. }
  428. // for (var i in window.GameList_PHZ){
  429. // if (window.GameList_PHZ[i] == wKindID) {
  430. // LoadArray.push('GamePHZ_2');
  431. // break;
  432. // }
  433. // }
  434. }
  435. return LoadArray;
  436. },
  437. StartPreload: function (bLoadLobby, wKindID) {
  438. this.m_nPrefabCnt = 0;
  439. this.m_nLoadCnt = 0;
  440. this.m_LoadArray = [];
  441. this.m_GameSupport = this._GetGameSupport(wKindID);
  442. if (bLoadLobby) {
  443. for (var i = 7; i > 1; --i) {
  444. for (var j in this.m_MapPrefab) {
  445. var BundleName = this.m_MapPrefab[j].BundleName;
  446. var bLoad = true;
  447. for (var k in this.m_GameSupport) {
  448. if (BundleName == this.m_GameSupport[k]) {
  449. bLoad = false;
  450. break;
  451. }
  452. }
  453. if (!bLoad) continue;
  454. var temp = BundleName.split('_');
  455. if (temp.length < 2) continue;
  456. if (Number(temp[1]) != i) continue;
  457. this.m_nPrefabCnt++;
  458. this.m_LoadArray.push(this.m_MapPrefab[j].Name);
  459. }
  460. }
  461. for (var j in this.m_MapPrefab) {
  462. var BundleName = this.m_MapPrefab[j].BundleName;
  463. var bLoad = true;
  464. if (Number(BundleName) > 0) bLoad = false;
  465. else {
  466. for (var k in this.m_GameSupport) {
  467. if (BundleName == this.m_GameSupport[k]) {
  468. bLoad = false;
  469. break;
  470. }
  471. }
  472. }
  473. if (!bLoad) continue;
  474. var temp = BundleName.split('_');
  475. if (temp.length > 1) continue;
  476. this.m_nPrefabCnt++;
  477. this.m_LoadArray.push(this.m_MapPrefab[j].Name);
  478. }
  479. for (var i in window.GameList) {
  480. this.m_LoadArray.push(`SubRoom_${i}`);
  481. }
  482. }
  483. if (wKindID) {
  484. for (var i = 7; i > 1; --i) {
  485. for (var j in this.m_MapPrefab) {
  486. var BundleName = this.m_MapPrefab[j].BundleName;
  487. var bLoad = false;
  488. for (var k in this.m_GameSupport) {
  489. if (BundleName == this.m_GameSupport[k]) {
  490. bLoad = true;
  491. break;
  492. }
  493. }
  494. if (!bLoad) continue;
  495. var temp = BundleName.split('_');
  496. if (temp.length < 2) continue;
  497. if (Number(temp[1]) != i) continue;
  498. this.m_nPrefabCnt++;
  499. this.m_LoadArray.push(this.m_MapPrefab[j].Name);
  500. }
  501. }
  502. for (var j in this.m_MapPrefab) {
  503. var BundleName = this.m_MapPrefab[j].BundleName;
  504. if (BundleName != wKindID) continue;
  505. var temp = BundleName.split('_');
  506. if (temp.length > 1) continue;
  507. this.m_nPrefabCnt++;
  508. this.m_LoadArray.push(this.m_MapPrefab[j].Name);
  509. }
  510. return this.m_LoadArray;
  511. }
  512. return this.CheckVer(this.m_LoadArray);
  513. },
  514. NextPreload: function (Name, OnSuccess, OnFalied) {
  515. this.LoadPrefab(Name, OnSuccess, OnFalied);
  516. },
  517. CheckVer: function (LoadArray) {
  518. for (var i = LoadArray.length - 1; i >= 0; --i) {
  519. if (!this.m_MapPrefab[LoadArray[i]]) {
  520. LoadArray.splice(i, 1);
  521. } else {
  522. var BundleName = this.m_MapPrefab[LoadArray[i]].BundleName;
  523. var Ver = null;
  524. if (this.m_MapBundle[BundleName]) Ver = this.m_MapBundle[BundleName].Ver;
  525. var LocalVer = null;
  526. LocalVer = cc.sys.localStorage.getItem(`${window.Key_ABVersion}_${BundleName}`);
  527. if (Ver && LocalVer && Ver == LocalVer) LoadArray.splice(i, 1);
  528. }
  529. }
  530. return LoadArray;
  531. }
  532. });
  533. cc.gPreLoader = new PreLoader();
  534. var SoundHead = '';
  535. var GSoundHead = '';
  536. //音頻
  537. var tagSoundRes = cc.Class({
  538. ctor: function () {
  539. this.m_SoundMap = new Object();
  540. this.m_GameSoundMap = new Object();
  541. this.m_GameIndex = null;
  542. },
  543. //播放声音
  544. PlaySound: function (Key) {
  545. var value = this.GetSoundVolume();
  546. // if (value == 0) return;
  547. if (value == 0) {
  548. return;
  549. };
  550. if (this.m_SoundMap[Key]) {
  551. cc.audioEngine.play(this.m_SoundMap[Key], false, value);
  552. } else {
  553. this.LoadSound(Key, function (audio) {
  554. cc.audioEngine.play(audio, false, value)
  555. }.bind(this));
  556. }
  557. },
  558. //加载声音
  559. LoadSound: function (Key, Callback) {
  560. // console.log("加载声音1:" + Key);
  561. cc.gPreLoader.LoadPublicAudio(Key, function (audio, Param) {
  562. this.m_SoundMap[Param.Key] = audio;
  563. if (this.m_WBGM == Param.Key) this.PlayMusic(Key, false);
  564. if (Callback) Callback(audio);
  565. }.bind(this), { Key: Key });
  566. },
  567. LoadSoundArr: function (SoundArr, Bundle) {
  568. for (var i in SoundArr) {
  569. // console.log("加载声音中2:" ,SoundArr[i][0], SoundArr[i][1]);
  570. cc.gPreLoader.LoadAudio(SoundArr[i][0], SoundArr[i][1], Bundle, function (audio, Param) {
  571. // console.log("加载声音成功11111:" ,SoundArr[i][0], SoundArr[i][1]);
  572. var Key = Param.Key;
  573. this.m_SoundMap[Param.Key] = audio;
  574. if (this.m_WBGM == Key) this.PlayMusic(Key, true);
  575. }.bind(this), { Key: SoundArr[i][0], Bundle: Bundle });
  576. }
  577. },
  578. //播放游戏声音
  579. PlayGameSound: function (Key) {
  580. try {
  581. var value = this.GetSoundVolume();
  582. if (value == 0) return;
  583. var sound = this.m_GameSoundMap[this.m_GameIndex][Key];
  584. if (sound == null) console.log("播放声音失败:" + Key);
  585. console.log(this.m_GameSoundMap);
  586. if (sound) {
  587. cc.audioEngine.play(sound, false, value);
  588. console.log("播放声音成功:" + Key);
  589. }
  590. } catch (e) {
  591. if (window.trapExceptional) {
  592. window.trapExceptional(`cc.gSoundRes.PlayGameSound: ${this.m_GameIndex} => ${Key} ${e}`, `tagSoundRes`);
  593. }
  594. }
  595. },
  596. //加载游戏声音列表
  597. LoadGameSoundArr: function (wKindID, SoundArr) {
  598. if (wKindID == null) return;
  599. if (this.m_GameSoundMap[wKindID] == null) this.m_GameSoundMap[wKindID] = new Object();
  600. this.m_GameIndex = wKindID;
  601. //已加载
  602. if (this.m_GameSoundMap[wKindID].bFinish) return;
  603. //加载资源
  604. for (var i in SoundArr) {
  605. // this.LoadGameSound(SoundArr[i][0], 'Audio/' + SoundArr[i][1]);
  606. // console.log("加载声音中:" ,SoundArr[i][0], 'Audio/' + SoundArr[i][1]);
  607. cc.gPreLoader.LoadAudio(SoundArr[i][0], 'Audio/' + SoundArr[i][1], this.m_GameIndex, function (audio, Param) {
  608. var Key = Param.Key;
  609. var KindID = Param.KindID;
  610. this.m_GameSoundMap[KindID][Key] = audio;
  611. if (audio == null) {
  612. // console.log("加载声音失败:" , audio,Param);
  613. }
  614. // console.log("加载声音成功:" , audio,Param);
  615. if (this.m_WBGM == Key) this.PlayMusic(Key, true);
  616. }.bind(this), { Key: SoundArr[i][0], KindID: this.m_GameIndex });
  617. }
  618. this.m_GameSoundMap[wKindID].bFinish = true;
  619. },
  620. PlaySoundPhrase: function (wItemIndex, sex, bGame) {
  621. var value = this.GetSoundVolume();
  622. if (value == 0) return;
  623. var szName = "Phrase"
  624. if (sex == 0) szName += '_w'
  625. if (sex == 1) szName += '_m'
  626. szName += wItemIndex;
  627. if (bGame) this.PlayGameSound(szName);
  628. else this.PlaySound(szName);
  629. },
  630. //更新BGM音量
  631. UpdateVolume: function () {
  632. var value = this.GetBGMVolume();
  633. cc.audioEngine.setVolume(this.m_PlayingID, value);
  634. },
  635. //播放BGM
  636. PlayMusic: function (Key, bGame) {
  637. this.m_WBGM = Key;
  638. this.m_WBGame = bGame;
  639. cc.audioEngine.stopAll();
  640. if (!bGame && this.m_SoundMap[Key] == null) {
  641. cc.gPreLoader.LoadPublicAudio(Key, function (audio, Param) {
  642. this.m_SoundMap[Param.Key] = audio;
  643. this.PlayMusic(Param.Key, bGame);
  644. }.bind(this), { Key: Key });
  645. return;
  646. }
  647. if (bGame && this.m_GameSoundMap[this.m_GameIndex][Key] == null) return
  648. var value = this.GetBGMVolume();
  649. this.m_WBGM = null;
  650. if (bGame) {
  651. this.m_PlayingID = cc.audioEngine.play(this.m_GameSoundMap[this.m_GameIndex][Key], true, value);
  652. } else {
  653. this.m_PlayingID = cc.audioEngine.play(this.m_SoundMap[Key], true, value);
  654. }
  655. },
  656. //停止BGM
  657. StopMusic: function () {
  658. cc.audioEngine.stop(this.m_PlayingID);
  659. },
  660. GetBGMVolume: function () {
  661. return parseFloat(window.g_Setting[window.SetKey_Music]);
  662. },
  663. GetSoundVolume: function () {
  664. return parseFloat(window.g_Setting[window.SetKey_Sound]);
  665. },
  666. });
  667. cc.gSoundRes = new tagSoundRes();