PreLoader.js 31 KB

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