laya.bilimini.js 69 KB

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