laya.tbmini.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. window.tbMiniGame = function (exports, Laya) {
  2. 'use strict';
  3. class MiniFileMgr {
  4. static isLocalNativeFile(url) {
  5. for (var i = 0, sz = TBMiniAdapter.nativefiles.length; i < sz; i++) {
  6. if (url.indexOf(TBMiniAdapter.nativefiles[i]) != -1)
  7. return true;
  8. }
  9. return false;
  10. }
  11. static isNetFile(url) {
  12. return (url.indexOf("http://") != -1 || url.indexOf("https://") != -1) && url.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1;
  13. }
  14. static getFileInfo(fileUrl) {
  15. var fileNativePath = fileUrl;
  16. var fileObj = MiniFileMgr.fakeObj[fileNativePath];
  17. if (fileObj == null)
  18. return null;
  19. else
  20. return fileObj;
  21. }
  22. static read(filePath, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "") {
  23. var fileUrl;
  24. if (readyUrl != "" && (readyUrl.indexOf("http://") != -1 || readyUrl.indexOf("https://") != -1)) {
  25. fileUrl = MiniFileMgr.getFileNativePath(filePath);
  26. }
  27. else {
  28. fileUrl = filePath;
  29. }
  30. fileUrl = Laya.URL.getAdptedFilePath(fileUrl);
  31. MiniFileMgr.fs.readFile({ filePath: fileUrl, encoding: encoding, success: function (data) {
  32. callBack != null && callBack.runWith([0, data]);
  33. }, fail: function (data) {
  34. if (data && readyUrl != "")
  35. MiniFileMgr.downFiles(TBMiniAdapter.safeEncodeURI(readyUrl), encoding, callBack, readyUrl, isSaveFile, fileType);
  36. else
  37. callBack != null && callBack.runWith([1]);
  38. } });
  39. }
  40. static isFile(url) {
  41. let stat;
  42. try {
  43. stat = MiniFileMgr.fs.statSync(url);
  44. }
  45. catch (err) {
  46. return false;
  47. }
  48. return stat.isFile();
  49. }
  50. static downFiles(fileUrl, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "", isAutoClear = true) {
  51. var downloadTask = MiniFileMgr.wxdown({ url: fileUrl, success: function (data) {
  52. if (!data.hasOwnProperty("statusCode")) {
  53. data.statusCode = 200;
  54. }
  55. if (data.statusCode === 200)
  56. MiniFileMgr.readFile(data.apFilePath, encoding, callBack, readyUrl, isSaveFile, fileType, isAutoClear);
  57. else if (data.statusCode === 403) {
  58. callBack != null && callBack.runWith([0, fileUrl]);
  59. }
  60. else {
  61. callBack != null && callBack.runWith([1, data]);
  62. }
  63. }, fail: function (data) {
  64. console.log("downloadfile fail:", readyUrl, data);
  65. callBack != null && callBack.runWith([1, data]);
  66. } });
  67. downloadTask.onProgressUpdate(function (data) {
  68. callBack != null && callBack.runWith([2, data.progress]);
  69. });
  70. }
  71. static readFile(filePath, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "", isAutoClear = true) {
  72. filePath = Laya.URL.getAdptedFilePath(filePath);
  73. MiniFileMgr.fs.readFile({ filePath: filePath, encoding: encoding, success: function (data) {
  74. if (filePath.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) && (filePath.indexOf("http://") != -1 || filePath.indexOf("https://") != -1)) {
  75. if (isSaveFile) {
  76. callBack != null && callBack.runWith([0, data]);
  77. MiniFileMgr.copyTOCache(filePath, readyUrl, null, encoding, isAutoClear);
  78. }
  79. else
  80. callBack != null && callBack.runWith([0, data]);
  81. }
  82. else {
  83. callBack != null && callBack.runWith([0, data]);
  84. }
  85. }, fail: function (data) {
  86. if (data)
  87. callBack != null && callBack.runWith([1, data]);
  88. } });
  89. }
  90. static downOtherFiles(fileUrl, callBack = null, readyUrl = "", isSaveFile = false, isAutoClear = true) {
  91. MiniFileMgr.wxdown({ url: fileUrl, success: function (data) {
  92. if (!data.hasOwnProperty("statusCode"))
  93. data.statusCode = 200;
  94. if (data.statusCode === 200) {
  95. if (isSaveFile && readyUrl.indexOf(".php") == -1) {
  96. callBack != null && callBack.runWith([0, data.apFilePath]);
  97. MiniFileMgr.copyTOCache(data.apFilePath, readyUrl, null, "", isAutoClear);
  98. }
  99. else
  100. callBack != null && callBack.runWith([0, data.apFilePath]);
  101. }
  102. else {
  103. callBack != null && callBack.runWith([1, data]);
  104. }
  105. }, fail: function (data) {
  106. console.log("downloadfile fail:", readyUrl, data);
  107. callBack != null && callBack.runWith([1, data]);
  108. } });
  109. }
  110. static copyFile(src, dest, complete = null) {
  111. MiniFileMgr.fs.copyFile({
  112. srcPath: src,
  113. destPath: dest,
  114. success: function () {
  115. complete && complete.runWith(0);
  116. },
  117. fail: function (err) {
  118. complete && complete.runWith([1, err]);
  119. }
  120. });
  121. }
  122. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "utf8", cacheFile = false) {
  123. if (window.navigator.userAgent.indexOf('AlipayMiniGame') < 0) {
  124. Laya.Laya.loader.load(fileUrl, callBack);
  125. }
  126. else {
  127. if (fileType == Laya.Loader.IMAGE || fileType == Laya.Loader.SOUND)
  128. MiniFileMgr.downOtherFiles(TBMiniAdapter.safeEncodeURI(fileUrl), callBack, fileUrl, cacheFile, false);
  129. else
  130. MiniFileMgr.downFiles(TBMiniAdapter.safeEncodeURI(fileUrl), encoding, callBack, fileUrl, true, fileType, cacheFile);
  131. }
  132. }
  133. static copyTOCache(tempFilePath, readyUrl, callBack, encoding = "", isAutoClear = true) {
  134. var temp = tempFilePath.split("/");
  135. var tempFileName = temp[temp.length - 1];
  136. var fileurlkey = readyUrl;
  137. var fileObj = MiniFileMgr.getFileInfo(readyUrl);
  138. var saveFilePath = MiniFileMgr.getFileNativePath(tempFileName);
  139. MiniFileMgr.fakeObj[fileurlkey] = { md5: tempFileName, readyUrl: readyUrl, size: 0, times: Laya.Browser.now(), encoding: encoding, tempFilePath: tempFilePath };
  140. var totalSize = TBMiniAdapter.sizeLimit;
  141. var chaSize = 4 * 1024 * 1024;
  142. var fileUseSize = MiniFileMgr.getCacheUseSize();
  143. if (fileObj) {
  144. if (fileObj.readyUrl != readyUrl) {
  145. MiniFileMgr.fs.getFileInfo({
  146. filePath: tempFilePath,
  147. success: function (data) {
  148. if ((isAutoClear && (fileUseSize + chaSize + data.size) >= totalSize)) {
  149. if (data.size > TBMiniAdapter.minClearSize)
  150. TBMiniAdapter.minClearSize = data.size;
  151. MiniFileMgr.onClearCacheRes();
  152. }
  153. MiniFileMgr.deleteFile(tempFilePath, readyUrl, callBack, encoding, data.size);
  154. },
  155. fail: function (data) {
  156. callBack != null && callBack.runWith([1, data]);
  157. }
  158. });
  159. }
  160. else
  161. callBack != null && callBack.runWith([0]);
  162. }
  163. else {
  164. MiniFileMgr.fs.getFileInfo({
  165. filePath: tempFilePath,
  166. success: function (data) {
  167. if ((isAutoClear && (fileUseSize + chaSize + data.size) >= totalSize)) {
  168. if (data.size > TBMiniAdapter.minClearSize)
  169. TBMiniAdapter.minClearSize = data.size;
  170. MiniFileMgr.onClearCacheRes();
  171. }
  172. MiniFileMgr.fs.copyFile({ srcPath: tempFilePath, destPath: saveFilePath, success: function (data2) {
  173. MiniFileMgr.onSaveFile(readyUrl, tempFileName, true, encoding, callBack, data.size);
  174. }, fail: function (data) {
  175. callBack != null && callBack.runWith([1, data]);
  176. } });
  177. },
  178. fail: function (data) {
  179. callBack != null && callBack.runWith([1, data]);
  180. }
  181. });
  182. }
  183. }
  184. static onClearCacheRes() {
  185. var memSize = TBMiniAdapter.minClearSize;
  186. var tempFileListArr = [];
  187. for (var key in MiniFileMgr.filesListObj) {
  188. if (key != "fileUsedSize")
  189. tempFileListArr.push(MiniFileMgr.filesListObj[key]);
  190. }
  191. MiniFileMgr.sortOn(tempFileListArr, "times", MiniFileMgr.NUMERIC);
  192. var clearSize = 0;
  193. for (var i = 1, sz = tempFileListArr.length; i < sz; i++) {
  194. var fileObj = tempFileListArr[i];
  195. if (clearSize >= memSize)
  196. break;
  197. clearSize += fileObj.size;
  198. MiniFileMgr.deleteFile("", fileObj.readyUrl);
  199. }
  200. }
  201. static sortOn(array, name, options = 0) {
  202. if (options == MiniFileMgr.NUMERIC)
  203. return array.sort(function (a, b) { return a[name] - b[name]; });
  204. if (options == (MiniFileMgr.NUMERIC | MiniFileMgr.DESCENDING))
  205. return array.sort(function (a, b) { return b[name] - a[name]; });
  206. return array.sort(function (a, b) { return a[name] - b[name]; });
  207. }
  208. static getFileNativePath(fileName) {
  209. return MiniFileMgr.fileNativeDir + "/" + fileName;
  210. }
  211. static deleteFile(tempFileName, readyUrl = "", callBack = null, encoding = "", fileSize = 0) {
  212. var fileObj = MiniFileMgr.getFileInfo(readyUrl);
  213. var deleteFileUrl = MiniFileMgr.getFileNativePath(fileObj.md5);
  214. MiniFileMgr.fs.unlink({ filePath: deleteFileUrl, success: function (data) {
  215. if (tempFileName != "") {
  216. var saveFilePath = MiniFileMgr.getFileNativePath(tempFileName);
  217. MiniFileMgr.fs.copyFile({ srcPath: tempFileName, destPath: saveFilePath, success: function (data) {
  218. MiniFileMgr.onSaveFile(readyUrl, tempFileName, true, encoding, callBack, fileSize);
  219. }, fail: function (data) {
  220. callBack != null && callBack.runWith([1, data]);
  221. } });
  222. }
  223. else {
  224. MiniFileMgr.onSaveFile(readyUrl, tempFileName, false, encoding, callBack, fileSize);
  225. }
  226. }, fail: function (data) {
  227. callBack != null && callBack.runWith([1, data]);
  228. } });
  229. }
  230. static deleteAll() {
  231. var tempFileListArr = [];
  232. for (var key in MiniFileMgr.filesListObj) {
  233. if (key != "fileUsedSize")
  234. tempFileListArr.push(MiniFileMgr.filesListObj[key]);
  235. }
  236. for (var i = 1, sz = tempFileListArr.length; i < sz; i++) {
  237. var fileObj = tempFileListArr[i];
  238. MiniFileMgr.deleteFile("", fileObj.readyUrl);
  239. }
  240. if (MiniFileMgr.filesListObj && MiniFileMgr.filesListObj.fileUsedSize) {
  241. MiniFileMgr.filesListObj.fileUsedSize = 0;
  242. }
  243. MiniFileMgr.writeFilesList("", JSON.stringify({}), false);
  244. }
  245. static onSaveFile(readyUrl, md5Name, isAdd = true, encoding = "", callBack = null, fileSize = 0) {
  246. var fileurlkey = readyUrl;
  247. if (MiniFileMgr.filesListObj['fileUsedSize'] == null)
  248. MiniFileMgr.filesListObj['fileUsedSize'] = 0;
  249. if (isAdd) {
  250. var fileNativeName = MiniFileMgr.getFileNativePath(md5Name);
  251. MiniFileMgr.filesListObj[fileurlkey] = { md5: md5Name, readyUrl: readyUrl, size: fileSize, times: Laya.Browser.now(), encoding: encoding, tempFilePath: fileNativeName };
  252. MiniFileMgr.filesListObj['fileUsedSize'] = parseInt(MiniFileMgr.filesListObj['fileUsedSize']) + fileSize;
  253. MiniFileMgr.writeFilesList(fileurlkey, JSON.stringify(MiniFileMgr.filesListObj), true);
  254. callBack != null && callBack.runWith([0]);
  255. }
  256. else {
  257. if (MiniFileMgr.filesListObj[fileurlkey]) {
  258. var deletefileSize = parseInt(MiniFileMgr.filesListObj[fileurlkey].size);
  259. MiniFileMgr.filesListObj['fileUsedSize'] = parseInt(MiniFileMgr.filesListObj['fileUsedSize']) - deletefileSize;
  260. if (MiniFileMgr.fakeObj[fileurlkey].md5 == MiniFileMgr.filesListObj[fileurlkey].md5) {
  261. delete MiniFileMgr.fakeObj[fileurlkey];
  262. }
  263. delete MiniFileMgr.filesListObj[fileurlkey];
  264. MiniFileMgr.writeFilesList(fileurlkey, JSON.stringify(MiniFileMgr.filesListObj), false);
  265. callBack != null && callBack.runWith([0]);
  266. }
  267. }
  268. }
  269. static writeFilesList(fileurlkey, filesListStr, isAdd) {
  270. var listFilesPath = MiniFileMgr.fileNativeDir + "/" + MiniFileMgr.fileListName;
  271. MiniFileMgr.fs.writeFile({ filePath: listFilesPath, encoding: 'utf8', data: filesListStr, success: function (data) {
  272. }, fail: function (data) {
  273. } });
  274. if (!TBMiniAdapter.isZiYu && TBMiniAdapter.isPosMsgYu && TBMiniAdapter.window.my.postMessage) {
  275. TBMiniAdapter.window.my.postMessage({ url: fileurlkey, data: MiniFileMgr.filesListObj[fileurlkey], isLoad: "filenative", isAdd: isAdd });
  276. }
  277. }
  278. static getCacheUseSize() {
  279. if (MiniFileMgr.filesListObj && MiniFileMgr.filesListObj['fileUsedSize'])
  280. return MiniFileMgr.filesListObj['fileUsedSize'];
  281. return 0;
  282. }
  283. static getCacheList(dirPath, cb) {
  284. let stat;
  285. try {
  286. stat = MiniFileMgr.fs.statSync(dirPath);
  287. }
  288. catch (err) {
  289. stat = null;
  290. }
  291. if (stat) {
  292. MiniFileMgr.readSync(MiniFileMgr.fileListName, "utf8", cb);
  293. }
  294. else {
  295. MiniFileMgr.fs.mkdirSync(dirPath, true);
  296. cb && cb.runWith([1]);
  297. }
  298. }
  299. static existDir(dirPath, callBack) {
  300. MiniFileMgr.fs.mkdir({ dirPath: dirPath, success: function (data) {
  301. callBack != null && callBack.runWith([0, { data: JSON.stringify({}) }]);
  302. }, fail: function (data) {
  303. if (data.error == 10025)
  304. MiniFileMgr.readSync(MiniFileMgr.fileListName, "utf8", callBack);
  305. else
  306. callBack != null && callBack.runWith([1, data]);
  307. } });
  308. }
  309. static readSync(filePath, encoding = "utf8", callBack = null, readyUrl = "") {
  310. var fileUrl = MiniFileMgr.getFileNativePath(filePath);
  311. var filesListStr;
  312. try {
  313. MiniFileMgr.fs.readFile({
  314. filePath: fileUrl,
  315. encoding: encoding,
  316. success: function (data) {
  317. filesListStr = data.data;
  318. callBack != null && callBack.runWith([0, { data: filesListStr }]);
  319. },
  320. fail: function () {
  321. callBack != null && callBack.runWith([1]);
  322. }
  323. });
  324. }
  325. catch (error) {
  326. callBack != null && callBack.runWith([1]);
  327. }
  328. }
  329. static setNativeFileDir(value) {
  330. MiniFileMgr.fileNativeDir = TBMiniAdapter.window.my.env.USER_DATA_PATH + value;
  331. }
  332. }
  333. MiniFileMgr.fs = window.my.getFileSystemManager();
  334. MiniFileMgr.wxdown = window.my.downloadFile;
  335. MiniFileMgr.filesListObj = {};
  336. MiniFileMgr.fakeObj = {};
  337. MiniFileMgr.fileListName = "layaairfiles.txt";
  338. MiniFileMgr.ziyuFileData = {};
  339. MiniFileMgr.ziyuFileTextureData = {};
  340. MiniFileMgr.loadPath = "";
  341. MiniFileMgr.DESCENDING = 2;
  342. MiniFileMgr.NUMERIC = 16;
  343. class MiniSoundChannel extends Laya.SoundChannel {
  344. constructor(sound) {
  345. super();
  346. this._sound = sound;
  347. this._audio = sound._sound;
  348. this._onCanplay = this.onCanPlay.bind(this);
  349. this._onError = this.onError.bind(this);
  350. this._onEnd = this.__onEnd.bind(this);
  351. this.addEventListener();
  352. }
  353. addEventListener() {
  354. this._audio.onError(this._onError);
  355. this._audio.onCanplay(this._onCanplay);
  356. }
  357. offEventListener() {
  358. this._audio.offError(this._onError);
  359. this._audio.offCanplay(this._onCanplay);
  360. this._audio.offEnded(this._onEnd);
  361. }
  362. onError(error) {
  363. console.log("-----1---------------minisound-----url:", this.url);
  364. console.log(error);
  365. this.event(Laya.Event.ERROR);
  366. if (!this._audio)
  367. return;
  368. this._sound.dispose();
  369. this.offEventListener();
  370. this._sound = this._audio = null;
  371. }
  372. onCanPlay() {
  373. if (!this._audio)
  374. return;
  375. this.event(Laya.Event.COMPLETE);
  376. this.offEventListener();
  377. this._audio.onEnded(this._onEnd);
  378. if (!this.isStopped) {
  379. this.play();
  380. }
  381. else {
  382. this.stop();
  383. }
  384. }
  385. __onEnd() {
  386. if (this.loops == 1) {
  387. if (this.completeHandler) {
  388. Laya.Laya.systemTimer.once(10, this, this.__runComplete, [this.completeHandler], false);
  389. this.completeHandler = null;
  390. }
  391. this.stop();
  392. this.event(Laya.Event.COMPLETE);
  393. return;
  394. }
  395. if (this.loops > 0) {
  396. this.loops--;
  397. }
  398. this.startTime = 0;
  399. this.play();
  400. }
  401. play() {
  402. this.isStopped = false;
  403. Laya.SoundManager.addChannel(this);
  404. if (!this._audio)
  405. return;
  406. this._audio.play();
  407. }
  408. set startTime(time) {
  409. if (!this._audio)
  410. return;
  411. this._audio.startTime = time;
  412. }
  413. set autoplay(value) {
  414. if (!this._audio)
  415. return;
  416. this._audio.autoplay = value;
  417. }
  418. get autoplay() {
  419. if (!this._audio)
  420. return false;
  421. return this._audio.autoplay;
  422. }
  423. get position() {
  424. if (!this._audio)
  425. return 0;
  426. return this._audio.currentTime;
  427. }
  428. get duration() {
  429. if (!this._audio)
  430. return 0;
  431. return this._audio.duration;
  432. }
  433. stop() {
  434. super.stop();
  435. this.isStopped = true;
  436. Laya.SoundManager.removeChannel(this);
  437. this.completeHandler = null;
  438. if (!this._audio)
  439. return;
  440. this._audio.stop();
  441. if (!this.loop) {
  442. this.offEventListener();
  443. this._sound.dispose();
  444. this._sound = null;
  445. this._audio = null;
  446. }
  447. }
  448. pause() {
  449. this.isStopped = true;
  450. if (!this._audio)
  451. return;
  452. this._audio.pause();
  453. }
  454. get loop() {
  455. if (!this._audio)
  456. return false;
  457. return this._audio.loop;
  458. }
  459. set loop(value) {
  460. if (!this._audio)
  461. return;
  462. this._audio.loop = value;
  463. }
  464. resume() {
  465. this.isStopped = false;
  466. Laya.SoundManager.addChannel(this);
  467. if (!this._audio)
  468. return;
  469. this._audio.play();
  470. }
  471. set volume(v) {
  472. if (!this._audio)
  473. return;
  474. this._audio.volume = v;
  475. }
  476. get volume() {
  477. if (!this._audio)
  478. return 0;
  479. return this._audio.volume;
  480. }
  481. }
  482. class MiniSound extends Laya.EventDispatcher {
  483. constructor() {
  484. super();
  485. this.loaded = false;
  486. this._sound = MiniSound._createSound();
  487. }
  488. static __init__() {
  489. for (let index = 0; index < 10; index++) {
  490. let s = TBMiniAdapter.window.my.createInnerAudioContext();
  491. MiniSound.cachePool.push(s);
  492. }
  493. }
  494. static _createSound() {
  495. if (MiniSound.cachePool.length) {
  496. return MiniSound.cachePool.pop();
  497. }
  498. else {
  499. return TBMiniAdapter.window.my.createInnerAudioContext();
  500. }
  501. }
  502. load(url) {
  503. if (!MiniFileMgr.isLocalNativeFile(url)) {
  504. url = Laya.URL.formatURL(url);
  505. }
  506. else {
  507. if (url.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  508. if (MiniFileMgr.loadPath != "") {
  509. url = url.split(MiniFileMgr.loadPath)[1];
  510. }
  511. else {
  512. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  513. if (tempStr != "")
  514. url = url.split(tempStr)[1];
  515. }
  516. }
  517. }
  518. this.url = url;
  519. this.readyUrl = url;
  520. if (TBMiniAdapter.autoCacheFile && MiniFileMgr.getFileInfo(url)) {
  521. this.onDownLoadCallBack(url, 0);
  522. }
  523. else {
  524. if (!TBMiniAdapter.autoCacheFile) {
  525. this.onDownLoadCallBack(url, 0);
  526. }
  527. else {
  528. if (MiniFileMgr.isLocalNativeFile(url)) {
  529. if (TBMiniAdapter.subNativeFiles && TBMiniAdapter.subNativeheads.length == 0) {
  530. for (var key in TBMiniAdapter.subNativeFiles) {
  531. var tempArr = TBMiniAdapter.subNativeFiles[key];
  532. TBMiniAdapter.subNativeheads = TBMiniAdapter.subNativeheads.concat(tempArr);
  533. for (let i = 0; i < tempArr.length; i++) {
  534. TBMiniAdapter.subMaps[tempArr[i]] = key + "/" + tempArr[i];
  535. }
  536. }
  537. }
  538. if (TBMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  539. var curfileHead = url.split("/")[0] + "/";
  540. if (curfileHead && TBMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  541. var newfileHead = TBMiniAdapter.subMaps[curfileHead];
  542. url = url.replace(curfileHead, newfileHead);
  543. }
  544. }
  545. this.onDownLoadCallBack(url, 0);
  546. }
  547. else {
  548. if ((url.indexOf("http://") == -1 && url.indexOf("https://") == -1)
  549. || url.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) != -1) {
  550. this.onDownLoadCallBack(url, 0);
  551. }
  552. else {
  553. MiniFileMgr.downOtherFiles(url, Laya.Handler.create(this, this.onDownLoadCallBack, [url]), url, TBMiniAdapter.autoCacheFile);
  554. }
  555. }
  556. }
  557. }
  558. }
  559. onDownLoadCallBack(sourceUrl, errorCode, tempFilePath = null) {
  560. if (!errorCode && this._sound) {
  561. var fileNativeUrl;
  562. if (TBMiniAdapter.autoCacheFile) {
  563. if (!tempFilePath) {
  564. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  565. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  566. var tempUrl = sourceUrl;
  567. if (tempStr != "" && (sourceUrl.indexOf("http://") != -1 || sourceUrl.indexOf("https://") != -1))
  568. fileNativeUrl = sourceUrl.split(tempStr)[1];
  569. if (!fileNativeUrl) {
  570. fileNativeUrl = tempUrl;
  571. }
  572. fileNativeUrl = TBMiniAdapter.baseDir + fileNativeUrl;
  573. }
  574. else {
  575. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  576. if (fileObj && fileObj.md5) {
  577. var fileMd5Name = fileObj.md5;
  578. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  579. }
  580. else {
  581. if (sourceUrl.indexOf("http://") == -1
  582. && sourceUrl.indexOf("https://") == -1
  583. && sourceUrl.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1) {
  584. fileNativeUrl = TBMiniAdapter.baseDir + sourceUrl;
  585. }
  586. else {
  587. fileNativeUrl = sourceUrl;
  588. }
  589. }
  590. }
  591. }
  592. else {
  593. fileNativeUrl = tempFilePath;
  594. }
  595. this._sound.src = this.readyUrl = fileNativeUrl;
  596. }
  597. else {
  598. if (MiniFileMgr.isLocalNativeFile(sourceUrl) ||
  599. (sourceUrl.indexOf("http://") == -1
  600. && sourceUrl.indexOf("https://") == -1
  601. && sourceUrl.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1)) {
  602. sourceUrl = TBMiniAdapter.baseDir + sourceUrl;
  603. }
  604. this._sound.src = this.readyUrl = sourceUrl;
  605. }
  606. }
  607. else {
  608. this.event(Laya.Event.ERROR);
  609. }
  610. }
  611. play(startTime = 0, loops = 0) {
  612. if (!this.url)
  613. return null;
  614. var channel = new MiniSoundChannel(this);
  615. channel.url = this.url;
  616. channel.loops = loops;
  617. channel.loop = (loops === 0 ? true : false);
  618. channel.startTime = startTime;
  619. channel.isStopped = false;
  620. Laya.SoundManager.addChannel(channel);
  621. return channel;
  622. }
  623. get duration() {
  624. return this._sound.duration;
  625. }
  626. dispose() {
  627. if (this._sound) {
  628. MiniSound.cachePool.push(this._sound);
  629. this._sound = null;
  630. this.readyUrl = this.url = null;
  631. }
  632. }
  633. }
  634. MiniSound.cachePool = [];
  635. class MiniInput {
  636. constructor() {
  637. }
  638. static _createInputElement() {
  639. Laya.Input['_initInput'](Laya.Input['area'] = Laya.Browser.createElement("textarea"));
  640. Laya.Input['_initInput'](Laya.Input['input'] = Laya.Browser.createElement("input"));
  641. Laya.Input['inputContainer'] = Laya.Browser.createElement("div");
  642. Laya.Input['inputContainer'].style.position = "absolute";
  643. Laya.Input['inputContainer'].style.zIndex = 1E5;
  644. Laya.Browser.container.appendChild(Laya.Input['inputContainer']);
  645. Laya.SoundManager._soundClass = MiniSound;
  646. Laya.SoundManager._musicClass = MiniSound;
  647. var model = TBMiniAdapter.systemInfo.model;
  648. var system = TBMiniAdapter.systemInfo.system;
  649. if (model.indexOf("iPhone") != -1) {
  650. Laya.Browser.onIPhone = true;
  651. Laya.Browser.onIOS = true;
  652. Laya.Browser.onIPad = true;
  653. Laya.Browser.onAndroid = false;
  654. }
  655. if (system.indexOf("Android") != -1 || system.indexOf("Adr") != -1) {
  656. Laya.Browser.onAndroid = true;
  657. Laya.Browser.onIPhone = false;
  658. Laya.Browser.onIOS = false;
  659. Laya.Browser.onIPad = false;
  660. }
  661. }
  662. static _onStageResize() {
  663. var ts = Laya.Laya.stage._canvasTransform.identity();
  664. ts.scale((Laya.Browser.width / Laya.Render.canvas.width / Laya.Browser.pixelRatio), Laya.Browser.height / Laya.Render.canvas.height / Laya.Browser.pixelRatio);
  665. }
  666. static wxinputFocus(e) {
  667. return;
  668. var _inputTarget = Laya.Input['inputElement'].target;
  669. if (_inputTarget && !_inputTarget.editable) {
  670. return;
  671. }
  672. TBMiniAdapter.window.my.showKeyboard({ defaultValue: _inputTarget.text, maxLength: _inputTarget.maxChars, multiple: _inputTarget.multiline, confirmHold: true, confirmType: _inputTarget["confirmType"] || 'done', success: function (res) {
  673. }, fail: function (res) {
  674. } });
  675. TBMiniAdapter.window.my.onKeyboardConfirm(function (res) {
  676. var str = res ? res.value : "";
  677. if (_inputTarget._restrictPattern) {
  678. str = str.replace(/\u2006|\x27/g, "");
  679. if (_inputTarget._restrictPattern.test(str)) {
  680. str = str.replace(_inputTarget._restrictPattern, "");
  681. }
  682. }
  683. _inputTarget.text = str;
  684. _inputTarget.event(Laya.Event.INPUT);
  685. MiniInput.inputEnter();
  686. _inputTarget.event("confirm");
  687. });
  688. TBMiniAdapter.window.my.onKeyboardInput(function (res) {
  689. var str = res ? res.value : "";
  690. if (!_inputTarget.multiline) {
  691. if (str.indexOf("\n") != -1) {
  692. MiniInput.inputEnter();
  693. return;
  694. }
  695. }
  696. if (_inputTarget._restrictPattern) {
  697. str = str.replace(/\u2006|\x27/g, "");
  698. if (_inputTarget._restrictPattern.test(str)) {
  699. str = str.replace(_inputTarget._restrictPattern, "");
  700. }
  701. }
  702. _inputTarget.text = str;
  703. _inputTarget.event(Laya.Event.INPUT);
  704. });
  705. }
  706. static inputEnter() {
  707. Laya.Input['inputElement'].target.focus = false;
  708. }
  709. static wxinputblur() {
  710. MiniInput.hideKeyboard();
  711. }
  712. static hideKeyboard() {
  713. return;
  714. TBMiniAdapter.window.my.hideKeyboard({ success: function (res) {
  715. console.log('隐藏键盘');
  716. }, fail: function (res) {
  717. console.log("隐藏键盘出错:" + (res ? res.errMsg : ""));
  718. } });
  719. }
  720. }
  721. class MiniLoader extends Laya.EventDispatcher {
  722. constructor() {
  723. super();
  724. }
  725. _loadResourceFilter(type, url) {
  726. var thisLoader = this;
  727. this.sourceUrl = Laya.URL.formatURL(url);
  728. if (MiniFileMgr.isNetFile(url)) {
  729. if (MiniFileMgr.loadPath != "") {
  730. url = url.split(MiniFileMgr.loadPath)[1];
  731. }
  732. else {
  733. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  734. var tempUrl = url;
  735. if (tempStr != "")
  736. url = url.split(tempStr)[1];
  737. if (!url) {
  738. url = tempUrl;
  739. }
  740. }
  741. }
  742. if (TBMiniAdapter.subNativeFiles && TBMiniAdapter.subNativeheads.length == 0) {
  743. for (var key in TBMiniAdapter.subNativeFiles) {
  744. var tempArr = TBMiniAdapter.subNativeFiles[key];
  745. TBMiniAdapter.subNativeheads = TBMiniAdapter.subNativeheads.concat(tempArr);
  746. for (var aa = 0; aa < tempArr.length; aa++) {
  747. TBMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  748. }
  749. }
  750. }
  751. if (TBMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  752. var curfileHead = url.split("/")[0] + "/";
  753. if (curfileHead && TBMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  754. var newfileHead = TBMiniAdapter.subMaps[curfileHead];
  755. url = url.replace(curfileHead, newfileHead);
  756. }
  757. }
  758. switch (type) {
  759. case Laya.Loader.IMAGE:
  760. case "htmlimage":
  761. case "nativeimage":
  762. MiniLoader._transformImgUrl(url, type, thisLoader);
  763. break;
  764. case Laya.Loader.SOUND:
  765. thisLoader._loadSound(url);
  766. break;
  767. default:
  768. thisLoader._loadResource(type, url);
  769. }
  770. }
  771. _loadSound(url) {
  772. var thisLoader = this;
  773. if (!TBMiniAdapter.autoCacheFile) {
  774. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  775. }
  776. else {
  777. var tempurl = Laya.URL.formatURL(url);
  778. if (MiniFileMgr.getFileInfo(tempurl)) {
  779. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  780. }
  781. else {
  782. if (!MiniFileMgr.isLocalNativeFile(url)) {
  783. if (MiniFileMgr.isNetFile(tempurl)) {
  784. MiniFileMgr.downOtherFiles(TBMiniAdapter.safeEncodeURI(tempurl), Laya.Handler.create(MiniLoader, MiniLoader.onDownLoadCallBack, [tempurl, thisLoader]), tempurl, TBMiniAdapter.autoCacheFile);
  785. }
  786. else {
  787. MiniLoader.onDownLoadCallBack(TBMiniAdapter.baseDir + url, thisLoader, 0);
  788. }
  789. }
  790. else {
  791. if (tempurl.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1) {
  792. MiniLoader.onDownLoadCallBack(TBMiniAdapter.baseDir + url, thisLoader, 0);
  793. }
  794. else
  795. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  796. }
  797. }
  798. }
  799. }
  800. static onDownLoadCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = null) {
  801. if (!errorCode) {
  802. var fileNativeUrl;
  803. if (TBMiniAdapter.autoCacheFile) {
  804. if (!tempFilePath) {
  805. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  806. fileNativeUrl = sourceUrl;
  807. }
  808. else {
  809. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  810. if (fileObj && fileObj.md5) {
  811. fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  812. }
  813. else {
  814. fileNativeUrl = sourceUrl;
  815. }
  816. }
  817. }
  818. else {
  819. fileNativeUrl = tempFilePath;
  820. }
  821. }
  822. else {
  823. fileNativeUrl = Laya.URL.formatURL(sourceUrl);
  824. }
  825. sourceUrl = fileNativeUrl;
  826. var sound = (new Laya.SoundManager._soundClass());
  827. sound.load(TBMiniAdapter.safeEncodeURI(sourceUrl));
  828. thisLoader.onLoaded(sound);
  829. }
  830. else {
  831. thisLoader.event(Laya.Event.ERROR, "Load sound failed");
  832. }
  833. }
  834. static bindToThis(fun, scope) {
  835. var rst = fun;
  836. rst = fun.bind(scope);
  837. return rst;
  838. }
  839. complete(data) {
  840. if (data instanceof Laya.Resource) {
  841. data._setCreateURL(this.sourceUrl);
  842. }
  843. else if ((data instanceof Laya.Texture) && (data.bitmap instanceof Laya.Resource)) {
  844. data.bitmap._setCreateURL(this.sourceUrl);
  845. }
  846. this.originComplete(data);
  847. }
  848. _loadHttpRequestWhat(url, contentType) {
  849. var thisLoader = this;
  850. var encoding = TBMiniAdapter.getUrlEncode(url, contentType);
  851. if (Laya.Loader.preLoadedMap[url])
  852. thisLoader.onLoaded(Laya.Loader.preLoadedMap[url]);
  853. else {
  854. var tempurl = Laya.URL.formatURL(url);
  855. var fileObj = MiniFileMgr.getFileInfo(tempurl);
  856. if (fileObj) {
  857. fileObj.encoding = fileObj.encoding == null ? "utf8" : fileObj.encoding;
  858. var fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  859. MiniFileMgr.readFile(fileNativeUrl, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  860. }
  861. else {
  862. if ((tempurl.indexOf("http://") == -1 && tempurl.indexOf("https://") == -1) || MiniFileMgr.isLocalNativeFile(url)) {
  863. if (tempurl.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1) {
  864. MiniFileMgr.readFile(TBMiniAdapter.baseDir + url, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  865. }
  866. else
  867. MiniFileMgr.readFile(url, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  868. }
  869. else {
  870. MiniFileMgr.downFiles(TBMiniAdapter.safeEncodeURI(tempurl), encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), tempurl, TBMiniAdapter.AutoCacheDownFile);
  871. }
  872. }
  873. }
  874. }
  875. static onReadNativeCallBack(url, type = null, thisLoader = null, errorCode = 0, data = null) {
  876. if (!errorCode) {
  877. var tempData;
  878. if (type == Laya.Loader.JSON || type == Laya.Loader.ATLAS || type == Laya.Loader.PREFAB || type == Laya.Loader.PLF) {
  879. tempData = TBMiniAdapter.getJson(data.data);
  880. }
  881. else if (type == Laya.Loader.XML) {
  882. tempData = Laya.Utils.parseXMLFromString(data.data);
  883. }
  884. else {
  885. tempData = data.data;
  886. }
  887. thisLoader.onLoaded(tempData);
  888. }
  889. else if (errorCode == 1) {
  890. thisLoader.onError && thisLoader.onError(data);
  891. }
  892. }
  893. static _transformImgUrl(url, type, thisLoader) {
  894. let tempurl = Laya.URL.formatURL(url);
  895. if (MiniFileMgr.isLocalNativeFile(url) || (tempurl.indexOf("http://") == -1 && tempurl.indexOf("https://") == -1)) {
  896. if (tempurl.indexOf(TBMiniAdapter.window.my.env.USER_DATA_PATH) == -1) {
  897. MiniLoader.onCreateImage(url, thisLoader, true);
  898. }
  899. else
  900. MiniLoader.onCreateImage(url, thisLoader, false, url);
  901. }
  902. else {
  903. MiniLoader.onCreateImage(url, thisLoader, false, tempurl);
  904. }
  905. }
  906. static onCreateImage(sourceUrl, thisLoader, isLocal = false, tempFilePath = "") {
  907. var fileNativeUrl;
  908. if (!isLocal) {
  909. if (tempFilePath != "") {
  910. fileNativeUrl = tempFilePath;
  911. }
  912. else {
  913. fileNativeUrl = sourceUrl;
  914. }
  915. }
  916. else
  917. fileNativeUrl = TBMiniAdapter.baseDir + sourceUrl;
  918. thisLoader._loadImage(TBMiniAdapter.safeEncodeURI(fileNativeUrl), false);
  919. }
  920. }
  921. class MiniLocalStorage {
  922. constructor() {
  923. }
  924. static __init__() {
  925. MiniLocalStorage.items = MiniLocalStorage;
  926. }
  927. static setItem(key, value) {
  928. TBMiniAdapter.window.my.setStorageSync({ key: key, data: value });
  929. }
  930. static getItem(key) {
  931. return TBMiniAdapter.window.my.getStorageSync({ "key": key }).data;
  932. }
  933. static setJSON(key, value) {
  934. try {
  935. MiniLocalStorage.setItem(key, JSON.stringify(value));
  936. }
  937. catch (e) {
  938. console.warn("set localStorage failed", e);
  939. }
  940. }
  941. static getJSON(key) {
  942. return JSON.parse(MiniLocalStorage.getItem(key));
  943. }
  944. static removeItem(key) {
  945. TBMiniAdapter.window.my.removeStorageSync({ key: key });
  946. }
  947. static clear() {
  948. TBMiniAdapter.window.my.clearStorageSync();
  949. }
  950. static getStorageInfoSync() {
  951. try {
  952. var res = TBMiniAdapter.window.my.getStorageInfoSync();
  953. console.log(res.keys);
  954. console.log(res.currentSize);
  955. console.log(res.limitSize);
  956. return res;
  957. }
  958. catch (e) {
  959. }
  960. return null;
  961. }
  962. }
  963. MiniLocalStorage.support = true;
  964. class TBMiniAdapter {
  965. static getJson(data) {
  966. return JSON.parse(data);
  967. }
  968. static enable() {
  969. TBMiniAdapter.init(Laya.Laya.isWXPosMsg, Laya.Laya.isWXOpenDataContext);
  970. }
  971. static init(isPosMsg = false, isSon = false) {
  972. if (TBMiniAdapter._inited)
  973. return;
  974. TBMiniAdapter._inited = true;
  975. TBMiniAdapter.window = window;
  976. if (!TBMiniAdapter.window.hasOwnProperty("my"))
  977. return;
  978. TBMiniAdapter.isZiYu = isSon;
  979. TBMiniAdapter.isPosMsgYu = isPosMsg;
  980. TBMiniAdapter.EnvConfig = {};
  981. if (!TBMiniAdapter.isZiYu) {
  982. MiniFileMgr.setNativeFileDir("/layaairGame");
  983. MiniFileMgr.getCacheList(MiniFileMgr.fileNativeDir, Laya.Handler.create(TBMiniAdapter, TBMiniAdapter.onMkdirCallBack));
  984. }
  985. TBMiniAdapter.systemInfo = TBMiniAdapter.window.my.getSystemInfoSync();
  986. TBMiniAdapter.window.focus = function () {
  987. };
  988. Laya.Laya['_getUrlPath'] = function () {
  989. return "";
  990. };
  991. TBMiniAdapter.window.logtime = function (str) {
  992. };
  993. TBMiniAdapter.window.alertTimeLog = function (str) {
  994. };
  995. TBMiniAdapter.window.resetShareInfo = function () {
  996. };
  997. TBMiniAdapter.window.CanvasRenderingContext2D = function () {
  998. };
  999. Laya.HttpRequest._urlEncode = TBMiniAdapter.safeEncodeURI;
  1000. TBMiniAdapter._preCreateElement = Laya.Browser.createElement;
  1001. TBMiniAdapter.window.CanvasRenderingContext2D.prototype = TBMiniAdapter._preCreateElement("canvas").getContext('2d').__proto__;
  1002. TBMiniAdapter.window.document.body.appendChild = function () {
  1003. };
  1004. Laya.Browser["createElement"] = TBMiniAdapter.createElement;
  1005. Laya.RunDriver.createShaderCondition = TBMiniAdapter.createShaderCondition;
  1006. Laya.Utils['parseXMLFromString'] = TBMiniAdapter.parseXMLFromString;
  1007. Laya.Input['_createInputElement'] = MiniInput['_createInputElement'];
  1008. Laya.Loader.prototype._loadResourceFilter = MiniLoader.prototype._loadResourceFilter;
  1009. Laya.Loader.prototype._loadSound = MiniLoader.prototype._loadSound;
  1010. Laya.Loader.prototype.originComplete = Laya.Loader.prototype.complete;
  1011. Laya.Loader.prototype.complete = MiniLoader.prototype.complete;
  1012. Laya.Loader.prototype._loadHttpRequestWhat = MiniLoader.prototype._loadHttpRequestWhat;
  1013. Laya.Config.useRetinalCanvas = true;
  1014. Laya.LocalStorage._baseClass = MiniLocalStorage;
  1015. MiniLocalStorage.__init__();
  1016. MiniSound.__init__();
  1017. TBMiniAdapter.window.my.onMessage && TBMiniAdapter.window.my.onMessage(TBMiniAdapter._onMessage);
  1018. }
  1019. static _onMessage(data) {
  1020. switch (data.type) {
  1021. case "changeMatrix":
  1022. Laya.Laya.stage.transform.identity();
  1023. Laya.Laya.stage._width = data.w;
  1024. Laya.Laya.stage._height = data.h;
  1025. Laya.Laya.stage._canvasTransform = new Laya.Matrix(data.a, data.b, data.c, data.d, data.tx, data.ty);
  1026. break;
  1027. case "display":
  1028. Laya.Laya.stage.frameRate = data.rate || Laya.Stage.FRAME_FAST;
  1029. break;
  1030. case "undisplay":
  1031. Laya.Laya.stage.frameRate = Laya.Stage.FRAME_SLEEP;
  1032. break;
  1033. }
  1034. if (data['isLoad'] == "opendatacontext") {
  1035. if (data.url) {
  1036. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1037. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1038. }
  1039. }
  1040. else if (data['isLoad'] == "openJsondatacontext") {
  1041. if (data.url) {
  1042. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1043. }
  1044. }
  1045. else if (data['isLoad'] == "openJsondatacontextPic") {
  1046. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1047. }
  1048. }
  1049. static getUrlEncode(url, type) {
  1050. if (type == "arraybuffer")
  1051. return "";
  1052. return "utf8";
  1053. }
  1054. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "utf8") {
  1055. var fileObj = MiniFileMgr.getFileInfo(fileUrl);
  1056. if (!fileObj)
  1057. MiniFileMgr.downLoadFile(TBMiniAdapter.safeEncodeURI(fileUrl), fileType, callBack, encoding);
  1058. else {
  1059. callBack != null && callBack.runWith([0]);
  1060. }
  1061. }
  1062. static remove(fileUrl, callBack = null) {
  1063. MiniFileMgr.deleteFile("", fileUrl, callBack, "", 0);
  1064. }
  1065. static removeAll() {
  1066. MiniFileMgr.deleteAll();
  1067. }
  1068. static hasNativeFile(fileUrl) {
  1069. return MiniFileMgr.isLocalNativeFile(fileUrl);
  1070. }
  1071. static getFileInfo(fileUrl) {
  1072. return MiniFileMgr.getFileInfo(fileUrl);
  1073. }
  1074. static getFileList() {
  1075. return MiniFileMgr.filesListObj;
  1076. }
  1077. static exitMiniProgram() {
  1078. TBMiniAdapter.window.my.exitMiniProgram();
  1079. }
  1080. static onMkdirCallBack(errorCode, data) {
  1081. if (!errorCode) {
  1082. MiniFileMgr.filesListObj = JSON.parse(data.data);
  1083. MiniFileMgr.fakeObj = JSON.parse(data.data) || {};
  1084. }
  1085. else {
  1086. MiniFileMgr.fakeObj = {};
  1087. MiniFileMgr.filesListObj = {};
  1088. }
  1089. let files = MiniFileMgr.fs.readdirSync(MiniFileMgr.fileNativeDir);
  1090. if (!files || !files.length)
  1091. return;
  1092. var tempMd5ListObj = {};
  1093. var fileObj;
  1094. for (let key in MiniFileMgr.filesListObj) {
  1095. if (key != "fileUsedSize") {
  1096. fileObj = MiniFileMgr.filesListObj[key];
  1097. tempMd5ListObj[fileObj.md5] = fileObj.readyUrl;
  1098. }
  1099. }
  1100. var fileName;
  1101. for (let i = 0, sz = files.length; i < sz; i++) {
  1102. fileName = files[i];
  1103. if (fileName == MiniFileMgr.fileListName)
  1104. continue;
  1105. if (!tempMd5ListObj[fileName]) {
  1106. let deleteFileUrl = MiniFileMgr.getFileNativePath(fileName);
  1107. MiniFileMgr.fs.unlink({
  1108. filePath: deleteFileUrl,
  1109. success: function (data) {
  1110. console.log("删除无引用的磁盘文件:" + fileName);
  1111. }
  1112. });
  1113. }
  1114. delete tempMd5ListObj[fileName];
  1115. }
  1116. for (let key in tempMd5ListObj) {
  1117. delete MiniFileMgr.filesListObj[tempMd5ListObj[key]];
  1118. delete MiniFileMgr.fakeObj[tempMd5ListObj[key]];
  1119. console.log("删除错误记录:", tempMd5ListObj[key]);
  1120. }
  1121. }
  1122. static pixelRatio() {
  1123. if (!TBMiniAdapter.EnvConfig.pixelRatioInt) {
  1124. try {
  1125. TBMiniAdapter.EnvConfig.pixelRatioInt = TBMiniAdapter.systemInfo.pixelRatio;
  1126. return TBMiniAdapter.systemInfo.pixelRatio;
  1127. }
  1128. catch (error) {
  1129. }
  1130. }
  1131. return TBMiniAdapter.EnvConfig.pixelRatioInt;
  1132. }
  1133. static createElement(type) {
  1134. if (type == "canvas") {
  1135. var _source;
  1136. if (TBMiniAdapter.idx == 1) {
  1137. _source = TBMiniAdapter.window.canvas.getRealCanvas();
  1138. }
  1139. else {
  1140. _source = TBMiniAdapter._preCreateElement(type);
  1141. }
  1142. (!_source.style) && (_source.style = {});
  1143. TBMiniAdapter.idx++;
  1144. return _source;
  1145. }
  1146. else if (type == "textarea" || type == "input") {
  1147. return TBMiniAdapter.onCreateInput(type);
  1148. }
  1149. else if (type == "div") {
  1150. var node = TBMiniAdapter._preCreateElement(type);
  1151. node.contains = function (value) {
  1152. return null;
  1153. };
  1154. node.removeChild = function (value) {
  1155. };
  1156. return node;
  1157. }
  1158. else {
  1159. return TBMiniAdapter._preCreateElement(type);
  1160. }
  1161. }
  1162. static onCreateInput(type) {
  1163. var node = TBMiniAdapter._preCreateElement(type);
  1164. node.focus = MiniInput.wxinputFocus;
  1165. node.blur = MiniInput.wxinputblur;
  1166. node.value = 0;
  1167. node.placeholder = {};
  1168. node.type = {};
  1169. node.setColor = function (value) {
  1170. };
  1171. node.setType = function (value) {
  1172. };
  1173. node.setFontFace = function (value) {
  1174. };
  1175. node.contains = function (value) {
  1176. return null;
  1177. };
  1178. return node;
  1179. }
  1180. static createShaderCondition(conditionScript) {
  1181. var func = function () {
  1182. return this[conditionScript.replace("this.", "")];
  1183. };
  1184. return func;
  1185. }
  1186. }
  1187. TBMiniAdapter.IGNORE = new RegExp("[-_.!~*'();/?:@&=+$,#%]|[0-9|A-Z|a-z]");
  1188. TBMiniAdapter.safeEncodeURI = function (str) {
  1189. var strTemp = "";
  1190. var length = str.length;
  1191. for (var i = 0; i < length; i++) {
  1192. var word = str[i];
  1193. if (TBMiniAdapter.IGNORE.test(word)) {
  1194. strTemp += word;
  1195. }
  1196. else {
  1197. try {
  1198. strTemp += encodeURI(word);
  1199. }
  1200. catch (e) {
  1201. console.log("errorInfo", ">>>" + word);
  1202. }
  1203. }
  1204. }
  1205. return strTemp;
  1206. };
  1207. TBMiniAdapter._inited = false;
  1208. TBMiniAdapter.autoCacheFile = true;
  1209. TBMiniAdapter.minClearSize = (5 * 1024 * 1024);
  1210. TBMiniAdapter.sizeLimit = (50 * 1024 * 1024);
  1211. TBMiniAdapter.nativefiles = ["layaNativeDir"];
  1212. TBMiniAdapter.subNativeFiles = [];
  1213. TBMiniAdapter.subNativeheads = [];
  1214. TBMiniAdapter.subMaps = [];
  1215. TBMiniAdapter.AutoCacheDownFile = false;
  1216. TBMiniAdapter.baseDir = "pages/index/";
  1217. TBMiniAdapter.parseXMLFromString = function (value) {
  1218. var rst;
  1219. value = value.replace(/>\s+</g, '><');
  1220. try {
  1221. rst = (new TBMiniAdapter.window.Parser.DOMParser()).parseFromString(value, 'text/xml');
  1222. }
  1223. catch (error) {
  1224. throw "需要引入xml解析库文件";
  1225. }
  1226. return rst;
  1227. };
  1228. TBMiniAdapter.idx = 1;
  1229. class MiniAccelerator extends Laya.EventDispatcher {
  1230. constructor() {
  1231. super();
  1232. }
  1233. static __init__() {
  1234. try {
  1235. var Acc;
  1236. Acc = Laya.Accelerator;
  1237. if (!Acc)
  1238. return;
  1239. Acc["prototype"]["on"] = MiniAccelerator["prototype"]["on"];
  1240. Acc["prototype"]["off"] = MiniAccelerator["prototype"]["off"];
  1241. }
  1242. catch (e) {
  1243. }
  1244. }
  1245. static startListen(callBack) {
  1246. MiniAccelerator._callBack = callBack;
  1247. if (MiniAccelerator._isListening)
  1248. return;
  1249. MiniAccelerator._isListening = true;
  1250. try {
  1251. TBMiniAdapter.window.my.onAccelerometerChange(MiniAccelerator.onAccelerometerChange);
  1252. }
  1253. catch (e) { }
  1254. }
  1255. static stopListen() {
  1256. MiniAccelerator._isListening = false;
  1257. try {
  1258. TBMiniAdapter.window.my.stopAccelerometer({});
  1259. }
  1260. catch (e) { }
  1261. }
  1262. static onAccelerometerChange(res) {
  1263. var e;
  1264. e = {};
  1265. e.acceleration = res;
  1266. e.accelerationIncludingGravity = res;
  1267. e.rotationRate = {};
  1268. if (MiniAccelerator._callBack != null) {
  1269. MiniAccelerator._callBack(e);
  1270. }
  1271. }
  1272. on(type, caller, listener, args = null) {
  1273. super.on(type, caller, listener, args);
  1274. MiniAccelerator.startListen(this["onDeviceOrientationChange"]);
  1275. return this;
  1276. }
  1277. off(type, caller, listener, onceOnly = false) {
  1278. if (!this.hasListener(type))
  1279. MiniAccelerator.stopListen();
  1280. return super.off(type, caller, listener, onceOnly);
  1281. }
  1282. }
  1283. MiniAccelerator._isListening = false;
  1284. class MiniLocation {
  1285. constructor() {
  1286. }
  1287. static __init__() {
  1288. TBMiniAdapter.window.navigator.geolocation.getCurrentPosition = MiniLocation.getCurrentPosition;
  1289. TBMiniAdapter.window.navigator.geolocation.watchPosition = MiniLocation.watchPosition;
  1290. TBMiniAdapter.window.navigator.geolocation.clearWatch = MiniLocation.clearWatch;
  1291. }
  1292. static getCurrentPosition(success = null, error = null, options = null) {
  1293. var paramO;
  1294. paramO = {};
  1295. paramO.success = getSuccess;
  1296. paramO.fail = error;
  1297. TBMiniAdapter.window.my.getLocation(paramO);
  1298. function getSuccess(res) {
  1299. if (success != null) {
  1300. success(res);
  1301. }
  1302. }
  1303. }
  1304. static watchPosition(success = null, error = null, options = null) {
  1305. MiniLocation._curID++;
  1306. var curWatchO;
  1307. curWatchO = {};
  1308. curWatchO.success = success;
  1309. curWatchO.error = error;
  1310. MiniLocation._watchDic[MiniLocation._curID] = curWatchO;
  1311. Laya.Laya.systemTimer.loop(1000, null, MiniLocation._myLoop);
  1312. return MiniLocation._curID;
  1313. }
  1314. static clearWatch(id) {
  1315. delete MiniLocation._watchDic[id];
  1316. if (!MiniLocation._hasWatch()) {
  1317. Laya.Laya.systemTimer.clear(null, MiniLocation._myLoop);
  1318. }
  1319. }
  1320. static _hasWatch() {
  1321. var key;
  1322. for (key in MiniLocation._watchDic) {
  1323. if (MiniLocation._watchDic[key])
  1324. return true;
  1325. }
  1326. return false;
  1327. }
  1328. static _myLoop() {
  1329. MiniLocation.getCurrentPosition(MiniLocation._mySuccess, MiniLocation._myError);
  1330. }
  1331. static _mySuccess(res) {
  1332. var rst = {};
  1333. rst.coords = res;
  1334. rst.timestamp = Laya.Browser.now();
  1335. var key;
  1336. for (key in MiniLocation._watchDic) {
  1337. if (MiniLocation._watchDic[key].success) {
  1338. MiniLocation._watchDic[key].success(rst);
  1339. }
  1340. }
  1341. }
  1342. static _myError(res) {
  1343. var key;
  1344. for (key in MiniLocation._watchDic) {
  1345. if (MiniLocation._watchDic[key].error) {
  1346. MiniLocation._watchDic[key].error(res);
  1347. }
  1348. }
  1349. }
  1350. }
  1351. MiniLocation._watchDic = {};
  1352. MiniLocation._curID = 0;
  1353. class MiniVideo {
  1354. constructor(width = 320, height = 240) {
  1355. this.videoend = false;
  1356. this.videourl = "";
  1357. this.videoElement = TBMiniAdapter.window.my.createVideo({ width: width, height: height, autoplay: true });
  1358. }
  1359. static __init__() {
  1360. }
  1361. on(eventType, ths, callBack) {
  1362. if (eventType == "loadedmetadata") {
  1363. this.onPlayFunc = callBack.bind(ths);
  1364. this.videoElement.onPlay = this.onPlayFunction.bind(this);
  1365. }
  1366. else if (eventType == "ended") {
  1367. this.onEndedFunC = callBack.bind(ths);
  1368. this.videoElement.onEnded = this.onEndedFunction.bind(this);
  1369. }
  1370. this.videoElement.onTimeUpdate = this.onTimeUpdateFunc.bind(this);
  1371. }
  1372. onTimeUpdateFunc(data) {
  1373. this.position = data.position;
  1374. this._duration = data.duration;
  1375. }
  1376. get duration() {
  1377. return this._duration;
  1378. }
  1379. onPlayFunction() {
  1380. if (this.videoElement)
  1381. this.videoElement.readyState = 200;
  1382. console.log("=====视频加载完成========");
  1383. this.onPlayFunc != null && this.onPlayFunc();
  1384. }
  1385. onEndedFunction() {
  1386. if (!this.videoElement)
  1387. return;
  1388. this.videoend = true;
  1389. console.log("=====视频播放完毕========");
  1390. this.onEndedFunC != null && this.onEndedFunC();
  1391. }
  1392. off(eventType, ths, callBack) {
  1393. if (eventType == "loadedmetadata") {
  1394. this.onPlayFunc = callBack.bind(ths);
  1395. this.videoElement.offPlay = this.onPlayFunction.bind(this);
  1396. }
  1397. else if (eventType == "ended") {
  1398. this.onEndedFunC = callBack.bind(ths);
  1399. this.videoElement.offEnded = this.onEndedFunction.bind(this);
  1400. }
  1401. }
  1402. load(url) {
  1403. if (!this.videoElement)
  1404. return;
  1405. this.videoElement.src = url;
  1406. }
  1407. play() {
  1408. if (!this.videoElement)
  1409. return;
  1410. this.videoend = false;
  1411. this.videoElement.play();
  1412. }
  1413. pause() {
  1414. if (!this.videoElement)
  1415. return;
  1416. this.videoend = true;
  1417. this.videoElement.pause();
  1418. }
  1419. get currentTime() {
  1420. if (!this.videoElement)
  1421. return 0;
  1422. return this.videoElement.initialTime;
  1423. }
  1424. set currentTime(value) {
  1425. if (!this.videoElement)
  1426. return;
  1427. this.videoElement.initialTime = value;
  1428. }
  1429. get videoWidth() {
  1430. if (!this.videoElement)
  1431. return 0;
  1432. return this.videoElement.width;
  1433. }
  1434. get videoHeight() {
  1435. if (!this.videoElement)
  1436. return 0;
  1437. return this.videoElement.height;
  1438. }
  1439. get ended() {
  1440. return this.videoend;
  1441. }
  1442. get loop() {
  1443. if (!this.videoElement)
  1444. return false;
  1445. return this.videoElement.loop;
  1446. }
  1447. set loop(value) {
  1448. if (!this.videoElement)
  1449. return;
  1450. this.videoElement.loop = value;
  1451. }
  1452. get playbackRate() {
  1453. if (!this.videoElement)
  1454. return 0;
  1455. return this.videoElement.playbackRate;
  1456. }
  1457. set playbackRate(value) {
  1458. if (!this.videoElement)
  1459. return;
  1460. this.videoElement.playbackRate = value;
  1461. }
  1462. get muted() {
  1463. if (!this.videoElement)
  1464. return false;
  1465. return this.videoElement.muted;
  1466. }
  1467. set muted(value) {
  1468. if (!this.videoElement)
  1469. return;
  1470. this.videoElement.muted = value;
  1471. }
  1472. get paused() {
  1473. if (!this.videoElement)
  1474. return false;
  1475. return this.videoElement.paused;
  1476. }
  1477. size(width, height) {
  1478. if (!this.videoElement)
  1479. return;
  1480. this.videoElement.width = width;
  1481. this.videoElement.height = height;
  1482. }
  1483. get x() {
  1484. if (!this.videoElement)
  1485. return 0;
  1486. return this.videoElement.x;
  1487. }
  1488. set x(value) {
  1489. if (!this.videoElement)
  1490. return;
  1491. this.videoElement.x = value;
  1492. }
  1493. get y() {
  1494. if (!this.videoElement)
  1495. return 0;
  1496. return this.videoElement.y;
  1497. }
  1498. set y(value) {
  1499. if (!this.videoElement)
  1500. return;
  1501. this.videoElement.y = value;
  1502. }
  1503. get currentSrc() {
  1504. return this.videoElement.src;
  1505. }
  1506. destroy() {
  1507. if (this.videoElement)
  1508. this.videoElement.destroy();
  1509. this.videoElement = null;
  1510. this.onEndedFunC = null;
  1511. this.onPlayFunc = null;
  1512. this.videoend = false;
  1513. this.videourl = null;
  1514. }
  1515. reload() {
  1516. if (!this.videoElement)
  1517. return;
  1518. this.videoElement.src = this.videourl;
  1519. }
  1520. }
  1521. exports.MiniAccelerator = MiniAccelerator;
  1522. exports.MiniFileMgr = MiniFileMgr;
  1523. exports.MiniInput = MiniInput;
  1524. exports.MiniLoader = MiniLoader;
  1525. exports.MiniLocalStorage = MiniLocalStorage;
  1526. exports.MiniLocation = MiniLocation;
  1527. exports.MiniSound = MiniSound;
  1528. exports.MiniSoundChannel = MiniSoundChannel;
  1529. exports.MiniVideo = MiniVideo;
  1530. exports.TBMiniAdapter = TBMiniAdapter;
  1531. }