laya.bdmini.js 68 KB

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