PreLoader.js 32 KB

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