laya.bdmini.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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 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(BMiniAdapter.window.swan.env.USER_DATA_PATH) == -1 && (filePath.indexOf("http://") != -1 || filePath.indexOf("https://") != -1)) {
  59. if (BMiniAdapter.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 ((BMiniAdapter.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('SwanGame') < 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 > BMiniAdapter.minClearSize)
  118. BMiniAdapter.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 > BMiniAdapter.minClearSize)
  137. BMiniAdapter.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 = BMiniAdapter.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 (!BMiniAdapter.isZiYu && BMiniAdapter.isPosMsgYu) {
  237. BMiniAdapter.window.swan.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)
  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 = BMiniAdapter.window.swan.env.USER_DATA_PATH + value;
  268. }
  269. }
  270. MiniFileMgr.fs = window.swan.getFileSystemManager();
  271. MiniFileMgr.wxdown = window.swan.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 BMiniAdapter.window.swan.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 (BMiniAdapter.autoCacheFile && MiniFileMgr.getFileInfo(url)) {
  412. this.onDownLoadCallBack(url, 0);
  413. }
  414. else {
  415. if (!BMiniAdapter.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 (BMiniAdapter.subNativeFiles && BMiniAdapter.subNativeheads.length == 0) {
  428. for (var key in BMiniAdapter.subNativeFiles) {
  429. var tempArr = BMiniAdapter.subNativeFiles[key];
  430. BMiniAdapter.subNativeheads = BMiniAdapter.subNativeheads.concat(tempArr);
  431. for (var aa = 0; aa < tempArr.length; aa++) {
  432. BMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  433. }
  434. }
  435. }
  436. if (BMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  437. var curfileHead = url.split("/")[0] + "/";
  438. if (curfileHead && BMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  439. var newfileHead = BMiniAdapter.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(BMiniAdapter.window.swan.env.USER_DATA_PATH) != -1)) {
  447. this.onDownLoadCallBack(url, 0);
  448. }
  449. else {
  450. MiniFileMgr.downOtherFiles(encodeURI(url), Laya.Handler.create(this, this.onDownLoadCallBack, [url]), url);
  451. }
  452. }
  453. }
  454. }
  455. }
  456. onDownLoadCallBack(sourceUrl, errorCode) {
  457. if (!errorCode) {
  458. var fileNativeUrl;
  459. if (BMiniAdapter.autoCacheFile) {
  460. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  461. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  462. var tempUrl = sourceUrl;
  463. if (tempStr != "" && (sourceUrl.indexOf("http://") != -1 || sourceUrl.indexOf("https://") != -1))
  464. fileNativeUrl = sourceUrl.split(tempStr)[1];
  465. if (!fileNativeUrl) {
  466. fileNativeUrl = tempUrl;
  467. }
  468. }
  469. else {
  470. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  471. if (fileObj && fileObj.md5) {
  472. var fileMd5Name = fileObj.md5;
  473. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  474. }
  475. else {
  476. fileNativeUrl = sourceUrl;
  477. }
  478. }
  479. if (this.url != Laya.SoundManager._bgMusic) {
  480. this._sound = MiniSound._createSound();
  481. this._sound.src = this.url = fileNativeUrl;
  482. }
  483. else {
  484. this._sound = MiniSound._musicAudio;
  485. this._sound.src = this.url = fileNativeUrl;
  486. }
  487. }
  488. else {
  489. if (this.url != Laya.SoundManager._bgMusic) {
  490. this._sound = MiniSound._createSound();
  491. this._sound.src = sourceUrl;
  492. }
  493. else {
  494. this._sound = MiniSound._musicAudio;
  495. this._sound.src = sourceUrl;
  496. }
  497. }
  498. this._sound.onCanplay(MiniSound.bindToThis(this.onCanPlay, this));
  499. this._sound.onError(MiniSound.bindToThis(this.onError, this));
  500. }
  501. else {
  502. this.event(Laya.Event.ERROR);
  503. }
  504. }
  505. onError(error) {
  506. try {
  507. console.log("-----1---------------minisound-----id:" + MiniSound._id);
  508. console.log(error);
  509. }
  510. catch (error) {
  511. console.log("-----2---------------minisound-----id:" + MiniSound._id);
  512. console.log(error);
  513. }
  514. this.event(Laya.Event.ERROR);
  515. this._sound.offError(null);
  516. }
  517. onCanPlay() {
  518. this.loaded = true;
  519. this.event(Laya.Event.COMPLETE);
  520. this._sound.offCanplay(null);
  521. }
  522. static bindToThis(fun, scope) {
  523. var rst = fun;
  524. rst = fun.bind(scope);
  525. return rst;
  526. }
  527. play(startTime = 0, loops = 0) {
  528. var tSound;
  529. if (this.url == Laya.SoundManager._bgMusic) {
  530. if (!MiniSound._musicAudio)
  531. MiniSound._musicAudio = MiniSound._createSound();
  532. tSound = MiniSound._musicAudio;
  533. }
  534. else {
  535. if (MiniSound._audioCache[this.readyUrl]) {
  536. tSound = MiniSound._audioCache[this.readyUrl]._sound;
  537. }
  538. else {
  539. tSound = MiniSound._createSound();
  540. }
  541. }
  542. if (!tSound)
  543. return null;
  544. if (BMiniAdapter.autoCacheFile && MiniFileMgr.getFileInfo(this.url)) {
  545. var fileObj = MiniFileMgr.getFileInfo(this.url);
  546. var fileMd5Name = fileObj.md5;
  547. tSound.src = this.url = MiniFileMgr.getFileNativePath(fileMd5Name);
  548. }
  549. else {
  550. tSound.src = encodeURI(this.url);
  551. }
  552. var channel = new MiniSoundChannel(tSound, this);
  553. channel.url = this.url;
  554. channel.loops = loops;
  555. channel.loop = (loops === 0 ? true : false);
  556. channel.startTime = startTime;
  557. channel.play();
  558. Laya.SoundManager.addChannel(channel);
  559. return channel;
  560. }
  561. get duration() {
  562. return this._sound.duration;
  563. }
  564. dispose() {
  565. var ad = MiniSound._audioCache[this.readyUrl];
  566. if (ad) {
  567. ad.src = "";
  568. if (ad._sound) {
  569. ad._sound.destroy();
  570. ad._sound = null;
  571. ad = null;
  572. }
  573. delete MiniSound._audioCache[this.readyUrl];
  574. }
  575. if (this._sound) {
  576. this._sound.destroy();
  577. this._sound = null;
  578. this.readyUrl = this.url = null;
  579. }
  580. }
  581. }
  582. MiniSound._id = 0;
  583. MiniSound._audioCache = {};
  584. class MiniInput {
  585. constructor() {
  586. }
  587. static _createInputElement() {
  588. Laya.Input['_initInput'](Laya.Input['area'] = Laya.Browser.createElement("textarea"));
  589. Laya.Input['_initInput'](Laya.Input['input'] = Laya.Browser.createElement("input"));
  590. Laya.Input['inputContainer'] = Laya.Browser.createElement("div");
  591. Laya.Input['inputContainer'].style.position = "absolute";
  592. Laya.Input['inputContainer'].style.zIndex = 1E5;
  593. Laya.Browser.container.appendChild(Laya.Input['inputContainer']);
  594. Laya.Laya.stage.on("resize", null, MiniInput._onStageResize);
  595. BMiniAdapter.window.swan.onWindowResize && BMiniAdapter.window.swan.onWindowResize(function (res) {
  596. });
  597. Laya.SoundManager._soundClass = MiniSound;
  598. Laya.SoundManager._musicClass = MiniSound;
  599. var model = BMiniAdapter.systemInfo.model;
  600. var system = BMiniAdapter.systemInfo.system;
  601. if (model.indexOf("iPhone") != -1) {
  602. Laya.Browser.onIPhone = true;
  603. Laya.Browser.onIOS = true;
  604. Laya.Browser.onIPad = true;
  605. Laya.Browser.onAndroid = false;
  606. }
  607. if (system.indexOf("Android") != -1 || system.indexOf("Adr") != -1) {
  608. Laya.Browser.onAndroid = true;
  609. Laya.Browser.onIPhone = false;
  610. Laya.Browser.onIOS = false;
  611. Laya.Browser.onIPad = false;
  612. }
  613. }
  614. static _onStageResize() {
  615. var ts = Laya.Laya.stage._canvasTransform.identity();
  616. ts.scale((Laya.Browser.width / Laya.Render.canvas.width / Laya.Browser.pixelRatio), Laya.Browser.height / Laya.Render.canvas.height / Laya.Browser.pixelRatio);
  617. }
  618. static wxinputFocus(e) {
  619. var _inputTarget = Laya.Input['inputElement'].target;
  620. if (_inputTarget && !_inputTarget.editable) {
  621. return;
  622. }
  623. BMiniAdapter.window.swan.offKeyboardConfirm();
  624. BMiniAdapter.window.swan.offKeyboardInput();
  625. BMiniAdapter.window.swan.showKeyboard({ defaultValue: _inputTarget.text, maxLength: _inputTarget.maxChars, multiple: _inputTarget.multiline, confirmHold: true, confirmType: _inputTarget["confirmType"] || 'done', success: function (res) {
  626. }, fail: function (res) {
  627. } });
  628. BMiniAdapter.window.swan.onKeyboardConfirm(function (res) {
  629. var str = res ? res.value : "";
  630. if (_inputTarget._restrictPattern) {
  631. str = str.replace(/\u2006|\x27/g, "");
  632. if (_inputTarget._restrictPattern.test(str)) {
  633. str = str.replace(_inputTarget._restrictPattern, "");
  634. }
  635. }
  636. _inputTarget.text = str;
  637. _inputTarget.event(Laya.Event.INPUT);
  638. MiniInput.inputEnter();
  639. _inputTarget.event("confirm");
  640. });
  641. BMiniAdapter.window.swan.onKeyboardInput(function (res) {
  642. var str = res ? res.value : "";
  643. if (!_inputTarget.multiline) {
  644. if (str.indexOf("\n") != -1) {
  645. MiniInput.inputEnter();
  646. return;
  647. }
  648. }
  649. if (_inputTarget._restrictPattern) {
  650. str = str.replace(/\u2006|\x27/g, "");
  651. if (_inputTarget._restrictPattern.test(str)) {
  652. str = str.replace(_inputTarget._restrictPattern, "");
  653. }
  654. }
  655. _inputTarget.text = str;
  656. _inputTarget.event(Laya.Event.INPUT);
  657. });
  658. }
  659. static inputEnter() {
  660. Laya.Input['inputElement'].target.focus = false;
  661. }
  662. static wxinputblur() {
  663. MiniInput.hideKeyboard();
  664. }
  665. static hideKeyboard() {
  666. BMiniAdapter.window.swan.offKeyboardConfirm();
  667. BMiniAdapter.window.swan.offKeyboardInput();
  668. BMiniAdapter.window.swan.hideKeyboard({ success: function (res) {
  669. console.log('隐藏键盘');
  670. }, fail: function (res) {
  671. console.log("隐藏键盘出错:" + (res ? res.errMsg : ""));
  672. } });
  673. }
  674. }
  675. class MiniLoader extends Laya.EventDispatcher {
  676. constructor() {
  677. super();
  678. }
  679. _loadResourceFilter(type, url) {
  680. var thisLoader = this;
  681. this.sourceUrl = Laya.URL.formatURL(url);
  682. if (url.indexOf(BMiniAdapter.window.swan.env.USER_DATA_PATH) == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  683. if (MiniFileMgr.loadPath != "") {
  684. url = url.split(MiniFileMgr.loadPath)[1];
  685. }
  686. else {
  687. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  688. var tempUrl = url;
  689. if (tempStr != "")
  690. url = url.split(tempStr)[1];
  691. if (!url) {
  692. url = tempUrl;
  693. }
  694. }
  695. }
  696. if (BMiniAdapter.subNativeFiles && BMiniAdapter.subNativeheads.length == 0) {
  697. for (var key in BMiniAdapter.subNativeFiles) {
  698. var tempArr = BMiniAdapter.subNativeFiles[key];
  699. BMiniAdapter.subNativeheads = BMiniAdapter.subNativeheads.concat(tempArr);
  700. for (var aa = 0; aa < tempArr.length; aa++) {
  701. BMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  702. }
  703. }
  704. }
  705. if (BMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  706. var curfileHead = url.split("/")[0] + "/";
  707. if (curfileHead && BMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  708. var newfileHead = BMiniAdapter.subMaps[curfileHead];
  709. url = url.replace(curfileHead, newfileHead);
  710. }
  711. }
  712. switch (type) {
  713. case Laya.Loader.IMAGE:
  714. case "htmlimage":
  715. case "nativeimage":
  716. MiniLoader._transformImgUrl(url, type, thisLoader);
  717. break;
  718. case Laya.Loader.SOUND:
  719. thisLoader._loadSound(url);
  720. break;
  721. default:
  722. thisLoader._loadResource(type, url);
  723. }
  724. }
  725. _loadSound(url) {
  726. var thisLoader = this;
  727. var fileNativeUrl;
  728. if (MiniFileMgr.isLocalNativeFile(url)) {
  729. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  730. var tempUrl = url;
  731. if (tempStr != "" && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1))
  732. fileNativeUrl = url.split(tempStr)[1];
  733. if (!fileNativeUrl) {
  734. fileNativeUrl = tempUrl;
  735. }
  736. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  737. }
  738. else {
  739. var tempurl = Laya.URL.formatURL(url);
  740. if (!MiniFileMgr.isLocalNativeFile(url) && (tempurl.indexOf("http://") == -1 && tempurl.indexOf("https://") == -1) || (tempurl.indexOf("http://usr/") != -1)) {
  741. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  742. }
  743. else {
  744. MiniFileMgr.downOtherFiles(encodeURI(tempurl), Laya.Handler.create(MiniLoader, MiniLoader.onDownLoadCallBack, [tempurl, thisLoader]), tempurl);
  745. }
  746. }
  747. }
  748. static onDownLoadCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = null) {
  749. if (!errorCode) {
  750. var fileNativeUrl;
  751. if (BMiniAdapter.autoCacheFile) {
  752. if (!tempFilePath) {
  753. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  754. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  755. var tempUrl = sourceUrl;
  756. if (tempStr != "" && (sourceUrl.indexOf("http://") != -1 || sourceUrl.indexOf("https://") != -1))
  757. fileNativeUrl = sourceUrl.split(tempStr)[1];
  758. if (!fileNativeUrl) {
  759. fileNativeUrl = tempUrl;
  760. }
  761. }
  762. else {
  763. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  764. if (fileObj && fileObj.md5) {
  765. var fileMd5Name = fileObj.md5;
  766. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  767. }
  768. else {
  769. fileNativeUrl = sourceUrl;
  770. }
  771. }
  772. }
  773. else {
  774. fileNativeUrl = tempFilePath;
  775. }
  776. }
  777. sourceUrl = fileNativeUrl;
  778. var sound = (new Laya.SoundManager._soundClass());
  779. sound.load(sourceUrl);
  780. thisLoader.onLoaded(sound);
  781. }
  782. else {
  783. thisLoader.event(Laya.Event.ERROR, "Load sound failed");
  784. }
  785. }
  786. static bindToThis(fun, scope) {
  787. var rst = fun;
  788. rst = fun.bind(scope);
  789. return rst;
  790. }
  791. complete(data) {
  792. if (data instanceof Laya.Resource) {
  793. data._setCreateURL(this.sourceUrl);
  794. }
  795. else if ((data instanceof Laya.Texture) && (data.bitmap instanceof Laya.Resource)) {
  796. data.bitmap._setCreateURL(this.sourceUrl);
  797. }
  798. this.originComplete(data);
  799. }
  800. _loadHttpRequestWhat(url, contentType) {
  801. var thisLoader = this;
  802. var encoding = BMiniAdapter.getUrlEncode(url, contentType);
  803. if (Laya.Loader.preLoadedMap[url])
  804. thisLoader.onLoaded(Laya.Loader.preLoadedMap[url]);
  805. else {
  806. var tempurl = Laya.URL.formatURL(url);
  807. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(tempurl) && url.indexOf(BMiniAdapter.window.swan.env.USER_DATA_PATH) == -1 && (tempurl.indexOf("http://") != -1 || tempurl.indexOf("https://") != -1) && !BMiniAdapter.AutoCacheDownFile) {
  808. thisLoader._loadHttpRequest(tempurl, contentType, thisLoader, thisLoader.onLoaded, thisLoader, thisLoader.onProgress, thisLoader, thisLoader.onError);
  809. }
  810. else {
  811. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(url));
  812. if (fileObj) {
  813. fileObj.encoding = fileObj.encoding == null ? "utf8" : fileObj.encoding;
  814. MiniFileMgr.readFile(MiniFileMgr.getFileNativePath(fileObj.md5), encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  815. }
  816. else if (thisLoader.type == "image" || thisLoader.type == "htmlimage") {
  817. thisLoader._transformImgUrl(url, contentType);
  818. }
  819. else {
  820. if (contentType != Laya.Loader.IMAGE && ((tempurl.indexOf("http://") == -1 && tempurl.indexOf("https://") == -1) || MiniFileMgr.isLocalNativeFile(url))) {
  821. MiniFileMgr.readFile(url, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  822. }
  823. else {
  824. MiniFileMgr.downFiles(tempurl, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), tempurl, true);
  825. }
  826. }
  827. }
  828. }
  829. }
  830. static onReadNativeCallBack(url, type = null, thisLoader = null, errorCode = 0, data = null) {
  831. if (!errorCode) {
  832. var tempData;
  833. if (type == Laya.Loader.JSON || type == Laya.Loader.ATLAS || type == Laya.Loader.PREFAB || type == Laya.Loader.PLF) {
  834. tempData = BMiniAdapter.getJson(data.data);
  835. }
  836. else if (type == Laya.Loader.XML) {
  837. tempData = Laya.Utils.parseXMLFromString(data.data);
  838. }
  839. else {
  840. tempData = data.data;
  841. }
  842. if (!BMiniAdapter.isZiYu && BMiniAdapter.isPosMsgYu && type != Laya.Loader.BUFFER && BMiniAdapter.window.swan) {
  843. BMiniAdapter.window.swan.postMessage({ url: url, data: tempData, isLoad: "filedata" });
  844. }
  845. thisLoader.onLoaded(tempData);
  846. }
  847. else if (errorCode == 1) {
  848. thisLoader._loadHttpRequest(url, type, thisLoader, thisLoader.onLoaded, thisLoader, thisLoader.onProgress, thisLoader, thisLoader.onError);
  849. }
  850. }
  851. static _transformImgUrl(url, type, thisLoader) {
  852. if (BMiniAdapter.isZiYu || MiniFileMgr.isLocalNativeFile(url)) {
  853. thisLoader._loadImage(url, false);
  854. return;
  855. }
  856. if (!BMiniAdapter.autoCacheFile) {
  857. thisLoader._loadImage(encodeURI(url));
  858. return;
  859. }
  860. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(Laya.URL.formatURL(url))) {
  861. var tempUrl = Laya.URL.formatURL(url);
  862. if (url.indexOf(BMiniAdapter.window.swan.env.USER_DATA_PATH) == -1 && (tempUrl.indexOf("http://") != -1 || tempUrl.indexOf("https://") != -1)) {
  863. if (BMiniAdapter.isZiYu) {
  864. thisLoader._loadImage(url);
  865. }
  866. else {
  867. MiniFileMgr.downOtherFiles(tempUrl, new Laya.Handler(MiniLoader, MiniLoader.onDownImgCallBack, [url, thisLoader]), tempUrl);
  868. }
  869. }
  870. else
  871. thisLoader._loadImage(url);
  872. }
  873. else {
  874. MiniLoader.onCreateImage(url, thisLoader);
  875. }
  876. }
  877. static onDownImgCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = "") {
  878. if (!errorCode)
  879. MiniLoader.onCreateImage(sourceUrl, thisLoader, false, tempFilePath);
  880. else {
  881. thisLoader.onError(null);
  882. }
  883. }
  884. static onCreateImage(sourceUrl, thisLoader, isLocal = false, tempFilePath = "") {
  885. var fileNativeUrl;
  886. if (BMiniAdapter.autoCacheFile) {
  887. if (!isLocal) {
  888. if (tempFilePath != "") {
  889. fileNativeUrl = tempFilePath;
  890. }
  891. else {
  892. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(sourceUrl));
  893. var fileMd5Name = fileObj.md5;
  894. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  895. }
  896. }
  897. else if (BMiniAdapter.isZiYu) {
  898. var tempUrl = Laya.URL.formatURL(sourceUrl);
  899. if (MiniFileMgr.ziyuFileTextureData[tempUrl]) {
  900. fileNativeUrl = MiniFileMgr.ziyuFileTextureData[tempUrl];
  901. }
  902. else
  903. fileNativeUrl = sourceUrl;
  904. }
  905. else
  906. fileNativeUrl = sourceUrl;
  907. }
  908. else {
  909. if (!isLocal)
  910. fileNativeUrl = tempFilePath;
  911. else
  912. fileNativeUrl = sourceUrl;
  913. }
  914. thisLoader._loadImage(fileNativeUrl, false);
  915. }
  916. }
  917. class MiniLocalStorage {
  918. constructor() {
  919. }
  920. static __init__() {
  921. MiniLocalStorage.items = MiniLocalStorage;
  922. }
  923. static setItem(key, value) {
  924. try {
  925. BMiniAdapter.window.swan.setStorageSync(key, value);
  926. }
  927. catch (error) {
  928. BMiniAdapter.window.swan.setStorage({
  929. key: key,
  930. data: value
  931. });
  932. }
  933. }
  934. static getItem(key) {
  935. return BMiniAdapter.window.swan.getStorageSync(key);
  936. }
  937. static setJSON(key, value) {
  938. MiniLocalStorage.setItem(key, value);
  939. }
  940. static getJSON(key) {
  941. return MiniLocalStorage.getItem(key);
  942. }
  943. static removeItem(key) {
  944. BMiniAdapter.window.swan.removeStorageSync(key);
  945. }
  946. static clear() {
  947. BMiniAdapter.window.swan.clearStorageSync();
  948. }
  949. static getStorageInfoSync() {
  950. try {
  951. var res = BMiniAdapter.window.swan.getStorageInfoSync();
  952. console.log(res.keys);
  953. console.log(res.currentSize);
  954. console.log(res.limitSize);
  955. return res;
  956. }
  957. catch (e) {
  958. }
  959. return null;
  960. }
  961. }
  962. MiniLocalStorage.support = true;
  963. class BMiniAdapter {
  964. static getJson(data) {
  965. return JSON.parse(data);
  966. }
  967. static enable() {
  968. BMiniAdapter.init(Laya.Laya.isWXPosMsg, Laya.Laya.isWXOpenDataContext);
  969. }
  970. static init(isPosMsg = false, isSon = false) {
  971. if (BMiniAdapter._inited)
  972. return;
  973. BMiniAdapter._inited = true;
  974. BMiniAdapter.window = window;
  975. if (!BMiniAdapter.window.hasOwnProperty("swan"))
  976. return;
  977. if (BMiniAdapter.window.navigator.userAgent.indexOf('SwanGame') < 0)
  978. return;
  979. BMiniAdapter.isZiYu = isSon;
  980. BMiniAdapter.isPosMsgYu = isPosMsg;
  981. BMiniAdapter.EnvConfig = {};
  982. if (!BMiniAdapter.isZiYu) {
  983. MiniFileMgr.setNativeFileDir("/layaairGame");
  984. MiniFileMgr.existDir(MiniFileMgr.fileNativeDir, Laya.Handler.create(BMiniAdapter, BMiniAdapter.onMkdirCallBack));
  985. }
  986. BMiniAdapter.systemInfo = BMiniAdapter.window.swan.getSystemInfoSync();
  987. BMiniAdapter.window.focus = function () {
  988. };
  989. Laya.Laya['_getUrlPath'] = function () {
  990. return "";
  991. };
  992. BMiniAdapter.window.logtime = function (str) {
  993. };
  994. BMiniAdapter.window.alertTimeLog = function (str) {
  995. };
  996. BMiniAdapter.window.resetShareInfo = function () {
  997. };
  998. BMiniAdapter.window.CanvasRenderingContext2D = function () {
  999. };
  1000. BMiniAdapter.window.CanvasRenderingContext2D.prototype = BMiniAdapter.window.swan.createCanvas().getContext('2d').__proto__;
  1001. BMiniAdapter.window.document.body.appendChild = function () {
  1002. };
  1003. BMiniAdapter.EnvConfig.pixelRatioInt = 0;
  1004. Laya.Browser["_pixelRatio"] = BMiniAdapter.pixelRatio();
  1005. BMiniAdapter._preCreateElement = Laya.Browser.createElement;
  1006. Laya.Browser["createElement"] = BMiniAdapter.createElement;
  1007. Laya.RunDriver.createShaderCondition = BMiniAdapter.createShaderCondition;
  1008. Laya.Utils['parseXMLFromString'] = BMiniAdapter.parseXMLFromString;
  1009. Laya.Input['_createInputElement'] = MiniInput['_createInputElement'];
  1010. Laya.Loader.prototype._loadResourceFilter = MiniLoader.prototype._loadResourceFilter;
  1011. Laya.Loader.prototype._loadSound = MiniLoader.prototype._loadSound;
  1012. Laya.Loader.prototype.originComplete = Laya.Loader.prototype.complete;
  1013. Laya.Loader.prototype.complete = MiniLoader.prototype.complete;
  1014. Laya.Loader.prototype._loadHttpRequestWhat = MiniLoader.prototype._loadHttpRequestWhat;
  1015. Laya.LocalStorage._baseClass = MiniLocalStorage;
  1016. MiniLocalStorage.__init__();
  1017. BMiniAdapter.window.swan.onMessage && BMiniAdapter.window.swan.onMessage(BMiniAdapter._onMessage);
  1018. }
  1019. static _onMessage(data) {
  1020. switch (data.type) {
  1021. case "changeMatrix":
  1022. Laya.Laya.stage.transform.identity();
  1023. Laya.Laya.stage._width = data.w;
  1024. Laya.Laya.stage._height = data.h;
  1025. Laya.Laya.stage._canvasTransform = new Laya.Matrix(data.a, data.b, data.c, data.d, data.tx, data.ty);
  1026. break;
  1027. case "display":
  1028. Laya.Laya.stage.frameRate = data.rate || Laya.Stage.FRAME_FAST;
  1029. break;
  1030. case "undisplay":
  1031. Laya.Laya.stage.frameRate = Laya.Stage.FRAME_SLEEP;
  1032. break;
  1033. }
  1034. if (data['isLoad'] == "opendatacontext") {
  1035. if (data.url) {
  1036. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1037. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1038. }
  1039. }
  1040. else if (data['isLoad'] == "openJsondatacontext") {
  1041. if (data.url) {
  1042. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1043. }
  1044. }
  1045. else if (data['isLoad'] == "openJsondatacontextPic") {
  1046. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1047. }
  1048. }
  1049. static getUrlEncode(url, type) {
  1050. if (type == "arraybuffer")
  1051. return "";
  1052. return "utf8";
  1053. }
  1054. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "utf8") {
  1055. var fileObj = MiniFileMgr.getFileInfo(fileUrl);
  1056. if (!fileObj)
  1057. MiniFileMgr.downLoadFile(fileUrl, fileType, callBack, encoding);
  1058. else {
  1059. callBack != null && callBack.runWith([0]);
  1060. }
  1061. }
  1062. static remove(fileUrl, callBack = null) {
  1063. MiniFileMgr.deleteFile("", fileUrl, callBack, "", 0);
  1064. }
  1065. static removeAll() {
  1066. MiniFileMgr.deleteAll();
  1067. }
  1068. static hasNativeFile(fileUrl) {
  1069. return MiniFileMgr.isLocalNativeFile(fileUrl);
  1070. }
  1071. static getFileInfo(fileUrl) {
  1072. return MiniFileMgr.getFileInfo(fileUrl);
  1073. }
  1074. static getFileList() {
  1075. return MiniFileMgr.filesListObj;
  1076. }
  1077. static onMkdirCallBack(errorCode, data) {
  1078. if (!errorCode) {
  1079. MiniFileMgr.filesListObj = JSON.parse(data.data);
  1080. MiniFileMgr.fakeObj = JSON.parse(data.data) || {};
  1081. }
  1082. else {
  1083. MiniFileMgr.fakeObj = {};
  1084. MiniFileMgr.filesListObj = {};
  1085. }
  1086. MiniFileMgr.fs.readdir({
  1087. dirPath: MiniFileMgr.fileNativeDir,
  1088. success: function (data) {
  1089. var tempMd5ListObj = {};
  1090. var fileObj;
  1091. for (let key in MiniFileMgr.filesListObj) {
  1092. if (key != "fileUsedSize") {
  1093. fileObj = MiniFileMgr.filesListObj[key];
  1094. tempMd5ListObj[fileObj.md5] = true;
  1095. }
  1096. }
  1097. var files = data.files;
  1098. var fileName;
  1099. for (let i = 0, sz = files.length; i < sz; i++) {
  1100. fileName = files[i];
  1101. if (fileName == MiniFileMgr.fileListName)
  1102. continue;
  1103. var deleteFileUrl = MiniFileMgr.getFileNativePath(fileName);
  1104. if (tempMd5ListObj[deleteFileUrl])
  1105. continue;
  1106. if (tempMd5ListObj[fileName])
  1107. continue;
  1108. MiniFileMgr.fs.unlink({
  1109. filePath: deleteFileUrl,
  1110. success: function (data) {
  1111. console.log("删除无引用的磁盘文件:" + fileName);
  1112. }
  1113. });
  1114. }
  1115. }
  1116. });
  1117. }
  1118. static pixelRatio() {
  1119. if (!BMiniAdapter.EnvConfig.pixelRatioInt) {
  1120. try {
  1121. BMiniAdapter.EnvConfig.pixelRatioInt = BMiniAdapter.systemInfo.pixelRatio;
  1122. return BMiniAdapter.systemInfo.pixelRatio;
  1123. }
  1124. catch (error) {
  1125. }
  1126. }
  1127. return BMiniAdapter.EnvConfig.pixelRatioInt;
  1128. }
  1129. static createElement(type) {
  1130. if (type == "canvas") {
  1131. var _source;
  1132. if (BMiniAdapter.idx == 1) {
  1133. if (BMiniAdapter.isZiYu) {
  1134. _source = BMiniAdapter.window.sharedCanvas;
  1135. }
  1136. else {
  1137. _source = BMiniAdapter.window.canvas;
  1138. }
  1139. }
  1140. else {
  1141. _source = BMiniAdapter.window.swan.createCanvas();
  1142. }
  1143. _source.style = {};
  1144. BMiniAdapter.idx++;
  1145. return _source;
  1146. }
  1147. else if (type == "textarea" || type == "input") {
  1148. return BMiniAdapter.onCreateInput(type);
  1149. }
  1150. else if (type == "div") {
  1151. var node = BMiniAdapter._preCreateElement(type);
  1152. node.contains = function (value) {
  1153. return null;
  1154. };
  1155. node.removeChild = function (value) {
  1156. };
  1157. return node;
  1158. }
  1159. else {
  1160. return BMiniAdapter._preCreateElement(type);
  1161. }
  1162. }
  1163. static onCreateInput(type) {
  1164. var node = BMiniAdapter._preCreateElement(type);
  1165. node.focus = MiniInput.wxinputFocus;
  1166. node.blur = MiniInput.wxinputblur;
  1167. node.style = {};
  1168. node.value = 0;
  1169. if (!node.parentElement)
  1170. node.parentElement = {};
  1171. node.placeholder = {};
  1172. node.type = {};
  1173. node.setColor = function (value) {
  1174. };
  1175. node.setType = function (value) {
  1176. };
  1177. node.setFontFace = function (value) {
  1178. };
  1179. node.addEventListener = function (value) {
  1180. };
  1181. node.contains = function (value) {
  1182. return null;
  1183. };
  1184. node.removeChild = function (value) {
  1185. };
  1186. return node;
  1187. }
  1188. static createShaderCondition(conditionScript) {
  1189. var func = function () {
  1190. return this[conditionScript.replace("this.", "")];
  1191. };
  1192. return func;
  1193. }
  1194. static sendAtlasToOpenDataContext(url) {
  1195. if (!BMiniAdapter.isZiYu) {
  1196. var atlasJson = Laya.Loader.getRes(Laya.URL.formatURL(url));
  1197. if (atlasJson) {
  1198. var textureArr = atlasJson.meta.image.split(",");
  1199. if (atlasJson.meta && atlasJson.meta.image) {
  1200. var toloadPics = atlasJson.meta.image.split(",");
  1201. var split = url.indexOf("/") >= 0 ? "/" : "\\";
  1202. var idx = url.lastIndexOf(split);
  1203. var folderPath = idx >= 0 ? url.substr(0, idx + 1) : "";
  1204. for (var i = 0, len = toloadPics.length; i < len; i++) {
  1205. toloadPics[i] = folderPath + toloadPics[i];
  1206. }
  1207. }
  1208. else {
  1209. toloadPics = [url.replace(".json", ".png")];
  1210. }
  1211. for (i = 0; i < toloadPics.length; i++) {
  1212. var tempAtlasPngUrl = toloadPics[i];
  1213. BMiniAdapter.postInfoToContext(url, tempAtlasPngUrl, atlasJson);
  1214. }
  1215. }
  1216. else {
  1217. throw "传递的url没有获取到对应的图集数据信息,请确保图集已经过!";
  1218. }
  1219. }
  1220. }
  1221. static postInfoToContext(url, atlaspngUrl, atlasJson) {
  1222. var postData = { "frames": atlasJson.frames, "meta": atlasJson.meta };
  1223. var textureUrl = atlaspngUrl;
  1224. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(atlaspngUrl));
  1225. if (fileObj) {
  1226. var fileMd5Name = fileObj.md5;
  1227. var fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  1228. }
  1229. else {
  1230. fileNativeUrl = textureUrl;
  1231. }
  1232. if (fileNativeUrl) {
  1233. BMiniAdapter.window.swan.postMessage({ url: url, atlasdata: postData, imgNativeUrl: fileNativeUrl, imgReadyUrl: textureUrl, isLoad: "opendatacontext" });
  1234. }
  1235. else {
  1236. throw "获取图集的磁盘url路径不存在!";
  1237. }
  1238. }
  1239. static sendSinglePicToOpenDataContext(url) {
  1240. var tempTextureUrl = Laya.URL.formatURL(url);
  1241. var fileObj = MiniFileMgr.getFileInfo(tempTextureUrl);
  1242. if (fileObj) {
  1243. var fileMd5Name = fileObj.md5;
  1244. var fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  1245. url = tempTextureUrl;
  1246. }
  1247. else {
  1248. fileNativeUrl = url;
  1249. }
  1250. if (fileNativeUrl) {
  1251. BMiniAdapter.window.swan.postMessage({ url: url, imgNativeUrl: fileNativeUrl, imgReadyUrl: url, isLoad: "openJsondatacontextPic" });
  1252. }
  1253. else {
  1254. throw "获取图集的磁盘url路径不存在!";
  1255. }
  1256. }
  1257. static sendJsonDataToDataContext(url) {
  1258. if (!BMiniAdapter.isZiYu) {
  1259. var atlasJson = Laya.Loader.getRes(url);
  1260. if (atlasJson) {
  1261. BMiniAdapter.window.swan.postMessage({ url: url, atlasdata: atlasJson, isLoad: "openJsondatacontext" });
  1262. }
  1263. else {
  1264. throw "传递的url没有获取到对应的图集数据信息,请确保图集已经过!";
  1265. }
  1266. }
  1267. }
  1268. }
  1269. BMiniAdapter._inited = false;
  1270. BMiniAdapter.autoCacheFile = true;
  1271. BMiniAdapter.minClearSize = (5 * 1024 * 1024);
  1272. BMiniAdapter.nativefiles = ["layaNativeDir", "wxlocal"];
  1273. BMiniAdapter.subNativeFiles = [];
  1274. BMiniAdapter.subNativeheads = [];
  1275. BMiniAdapter.subMaps = [];
  1276. BMiniAdapter.AutoCacheDownFile = false;
  1277. BMiniAdapter.parseXMLFromString = function (value) {
  1278. var rst;
  1279. value = value.replace(/>\s+</g, '><');
  1280. try {
  1281. rst = (new BMiniAdapter.window.Parser.DOMParser()).parseFromString(value, 'text/xml');
  1282. }
  1283. catch (error) {
  1284. throw "需要引入xml解析库文件";
  1285. }
  1286. return rst;
  1287. };
  1288. BMiniAdapter.idx = 1;
  1289. class MiniAccelerator extends Laya.EventDispatcher {
  1290. constructor() {
  1291. super();
  1292. }
  1293. static __init__() {
  1294. try {
  1295. var Acc;
  1296. Acc = Laya.Accelerator;
  1297. if (!Acc)
  1298. return;
  1299. Acc["prototype"]["on"] = MiniAccelerator["prototype"]["on"];
  1300. Acc["prototype"]["off"] = MiniAccelerator["prototype"]["off"];
  1301. }
  1302. catch (e) {
  1303. }
  1304. }
  1305. static startListen(callBack) {
  1306. MiniAccelerator._callBack = callBack;
  1307. if (MiniAccelerator._isListening)
  1308. return;
  1309. MiniAccelerator._isListening = true;
  1310. try {
  1311. BMiniAdapter.window.swan.onAccelerometerChange(MiniAccelerator.onAccelerometerChange);
  1312. }
  1313. catch (e) { }
  1314. }
  1315. static stopListen() {
  1316. MiniAccelerator._isListening = false;
  1317. try {
  1318. BMiniAdapter.window.swan.stopAccelerometer({});
  1319. }
  1320. catch (e) { }
  1321. }
  1322. static onAccelerometerChange(res) {
  1323. var e;
  1324. e = {};
  1325. e.acceleration = res;
  1326. e.accelerationIncludingGravity = res;
  1327. e.rotationRate = {};
  1328. if (MiniAccelerator._callBack != null) {
  1329. MiniAccelerator._callBack(e);
  1330. }
  1331. }
  1332. on(type, caller, listener, args = null) {
  1333. super.on(type, caller, listener, args);
  1334. MiniAccelerator.startListen(this["onDeviceOrientationChange"]);
  1335. return this;
  1336. }
  1337. off(type, caller, listener, onceOnly = false) {
  1338. if (!this.hasListener(type))
  1339. MiniAccelerator.stopListen();
  1340. return super.off(type, caller, listener, onceOnly);
  1341. }
  1342. }
  1343. MiniAccelerator._isListening = false;
  1344. class MiniImage {
  1345. _loadImage(url) {
  1346. var thisLoader = this;
  1347. if (BMiniAdapter.isZiYu) {
  1348. MiniImage.onCreateImage(url, thisLoader, true);
  1349. return;
  1350. }
  1351. var isTransformUrl;
  1352. if (!MiniFileMgr.isLocalNativeFile(url)) {
  1353. isTransformUrl = true;
  1354. url = Laya.URL.formatURL(url);
  1355. }
  1356. else {
  1357. if (url.indexOf("http://usr/") == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  1358. if (MiniFileMgr.loadPath != "") {
  1359. url = url.split(MiniFileMgr.loadPath)[1];
  1360. }
  1361. else {
  1362. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  1363. var tempUrl = url;
  1364. if (tempStr != "")
  1365. url = url.split(tempStr)[1];
  1366. if (!url) {
  1367. url = tempUrl;
  1368. }
  1369. }
  1370. }
  1371. if (BMiniAdapter.subNativeFiles && BMiniAdapter.subNativeheads.length == 0) {
  1372. for (var key in BMiniAdapter.subNativeFiles) {
  1373. var tempArr = BMiniAdapter.subNativeFiles[key];
  1374. BMiniAdapter.subNativeheads = BMiniAdapter.subNativeheads.concat(tempArr);
  1375. for (var aa = 0; aa < tempArr.length; aa++) {
  1376. BMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  1377. }
  1378. }
  1379. }
  1380. if (BMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  1381. var curfileHead = url.split("/")[0] + "/";
  1382. if (curfileHead && BMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  1383. var newfileHead = BMiniAdapter.subMaps[curfileHead];
  1384. url = url.replace(curfileHead, newfileHead);
  1385. }
  1386. }
  1387. }
  1388. if (!MiniFileMgr.getFileInfo(url)) {
  1389. if (url.indexOf('http://usr/') == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  1390. if (BMiniAdapter.isZiYu) {
  1391. MiniImage.onCreateImage(url, thisLoader, true);
  1392. }
  1393. else {
  1394. MiniFileMgr.downOtherFiles(url, new Laya.Handler(MiniImage, MiniImage.onDownImgCallBack, [url, thisLoader]), url);
  1395. }
  1396. }
  1397. else
  1398. MiniImage.onCreateImage(url, thisLoader, true);
  1399. }
  1400. else {
  1401. MiniImage.onCreateImage(url, thisLoader, !isTransformUrl);
  1402. }
  1403. }
  1404. static onDownImgCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = "") {
  1405. if (!errorCode)
  1406. MiniImage.onCreateImage(sourceUrl, thisLoader, false, tempFilePath);
  1407. else {
  1408. thisLoader.onError(null);
  1409. }
  1410. }
  1411. static onCreateImage(sourceUrl, thisLoader, isLocal = false, tempFilePath = "") {
  1412. var fileNativeUrl;
  1413. if (BMiniAdapter.autoCacheFile) {
  1414. if (!isLocal) {
  1415. if (tempFilePath != "") {
  1416. fileNativeUrl = tempFilePath;
  1417. }
  1418. else {
  1419. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  1420. var fileMd5Name = fileObj.md5;
  1421. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  1422. }
  1423. }
  1424. else if (BMiniAdapter.isZiYu) {
  1425. var tempUrl = Laya.URL.formatURL(sourceUrl);
  1426. if (MiniFileMgr.ziyuFileTextureData[tempUrl]) {
  1427. fileNativeUrl = MiniFileMgr.ziyuFileTextureData[tempUrl];
  1428. }
  1429. else
  1430. fileNativeUrl = sourceUrl;
  1431. }
  1432. else
  1433. fileNativeUrl = sourceUrl;
  1434. }
  1435. else {
  1436. if (!isLocal)
  1437. fileNativeUrl = tempFilePath;
  1438. else
  1439. fileNativeUrl = sourceUrl;
  1440. }
  1441. if (thisLoader._imgCache == null)
  1442. thisLoader._imgCache = {};
  1443. var image;
  1444. function clear() {
  1445. var img = thisLoader._imgCache[fileNativeUrl];
  1446. if (img) {
  1447. img.onload = null;
  1448. img.onerror = null;
  1449. delete thisLoader._imgCache[fileNativeUrl];
  1450. }
  1451. }
  1452. var onerror = function () {
  1453. clear();
  1454. delete MiniFileMgr.fakeObj[sourceUrl];
  1455. delete MiniFileMgr.filesListObj[sourceUrl];
  1456. thisLoader.event(Laya.Event.ERROR, "Load image failed");
  1457. };
  1458. if (thisLoader._type == "nativeimage") {
  1459. var onload = function () {
  1460. clear();
  1461. thisLoader.onLoaded(image);
  1462. };
  1463. image = new Laya.Browser.window.Image();
  1464. image.crossOrigin = "";
  1465. image.onload = onload;
  1466. image.onerror = onerror;
  1467. image.src = fileNativeUrl;
  1468. thisLoader._imgCache[fileNativeUrl] = image;
  1469. }
  1470. else {
  1471. var imageSource = new Laya.Browser.window.Image();
  1472. onload = function () {
  1473. image = Laya.HTMLImage.create(imageSource.width, imageSource.height);
  1474. image.loadImageSource(imageSource, true);
  1475. image._setCreateURL(fileNativeUrl);
  1476. clear();
  1477. thisLoader.onLoaded(image);
  1478. };
  1479. imageSource.crossOrigin = "";
  1480. imageSource.onload = onload;
  1481. imageSource.onerror = onerror;
  1482. imageSource.src = fileNativeUrl;
  1483. thisLoader._imgCache[fileNativeUrl] = imageSource;
  1484. }
  1485. }
  1486. }
  1487. class MiniLocation {
  1488. constructor() {
  1489. }
  1490. static __init__() {
  1491. BMiniAdapter.window.navigator.geolocation.getCurrentPosition = MiniLocation.getCurrentPosition;
  1492. BMiniAdapter.window.navigator.geolocation.watchPosition = MiniLocation.watchPosition;
  1493. BMiniAdapter.window.navigator.geolocation.clearWatch = MiniLocation.clearWatch;
  1494. }
  1495. static getCurrentPosition(success = null, error = null, options = null) {
  1496. var paramO;
  1497. paramO = {};
  1498. paramO.success = getSuccess;
  1499. paramO.fail = error;
  1500. BMiniAdapter.window.swan.getLocation(paramO);
  1501. function getSuccess(res) {
  1502. if (success != null) {
  1503. success(res);
  1504. }
  1505. }
  1506. }
  1507. static watchPosition(success = null, error = null, options = null) {
  1508. MiniLocation._curID++;
  1509. var curWatchO;
  1510. curWatchO = {};
  1511. curWatchO.success = success;
  1512. curWatchO.error = error;
  1513. MiniLocation._watchDic[MiniLocation._curID] = curWatchO;
  1514. Laya.Laya.systemTimer.loop(1000, null, MiniLocation._myLoop);
  1515. return MiniLocation._curID;
  1516. }
  1517. static clearWatch(id) {
  1518. delete MiniLocation._watchDic[id];
  1519. if (!MiniLocation._hasWatch()) {
  1520. Laya.Laya.systemTimer.clear(null, MiniLocation._myLoop);
  1521. }
  1522. }
  1523. static _hasWatch() {
  1524. var key;
  1525. for (key in MiniLocation._watchDic) {
  1526. if (MiniLocation._watchDic[key])
  1527. return true;
  1528. }
  1529. return false;
  1530. }
  1531. static _myLoop() {
  1532. MiniLocation.getCurrentPosition(MiniLocation._mySuccess, MiniLocation._myError);
  1533. }
  1534. static _mySuccess(res) {
  1535. var rst = {};
  1536. rst.coords = res;
  1537. rst.timestamp = Laya.Browser.now();
  1538. var key;
  1539. for (key in MiniLocation._watchDic) {
  1540. if (MiniLocation._watchDic[key].success) {
  1541. MiniLocation._watchDic[key].success(rst);
  1542. }
  1543. }
  1544. }
  1545. static _myError(res) {
  1546. var key;
  1547. for (key in MiniLocation._watchDic) {
  1548. if (MiniLocation._watchDic[key].error) {
  1549. MiniLocation._watchDic[key].error(res);
  1550. }
  1551. }
  1552. }
  1553. }
  1554. MiniLocation._watchDic = {};
  1555. MiniLocation._curID = 0;
  1556. class MiniVideo {
  1557. constructor(width = 320, height = 240) {
  1558. this.videoend = false;
  1559. this.videourl = "";
  1560. this.videoElement = BMiniAdapter.window.swan.createVideo({ width: width, height: height, autoplay: true });
  1561. }
  1562. static __init__() {
  1563. }
  1564. on(eventType, ths, callBack) {
  1565. if (eventType == "loadedmetadata") {
  1566. this.onPlayFunc = callBack.bind(ths);
  1567. this.videoElement.onPlay = this.onPlayFunction.bind(this);
  1568. }
  1569. else if (eventType == "ended") {
  1570. this.onEndedFunC = callBack.bind(ths);
  1571. this.videoElement.onEnded = this.onEndedFunction.bind(this);
  1572. }
  1573. this.videoElement.onTimeUpdate = this.onTimeUpdateFunc.bind(this);
  1574. }
  1575. onTimeUpdateFunc(data) {
  1576. this.position = data.position;
  1577. this._duration = data.duration;
  1578. }
  1579. get duration() {
  1580. return this._duration;
  1581. }
  1582. onPlayFunction() {
  1583. if (this.videoElement)
  1584. this.videoElement.readyState = 200;
  1585. console.log("=====视频加载完成========");
  1586. this.onPlayFunc != null && this.onPlayFunc();
  1587. }
  1588. onEndedFunction() {
  1589. if (!this.videoElement)
  1590. return;
  1591. this.videoend = true;
  1592. console.log("=====视频播放完毕========");
  1593. this.onEndedFunC != null && this.onEndedFunC();
  1594. }
  1595. off(eventType, ths, callBack) {
  1596. if (eventType == "loadedmetadata") {
  1597. this.onPlayFunc = callBack.bind(ths);
  1598. this.videoElement.offPlay = this.onPlayFunction.bind(this);
  1599. }
  1600. else if (eventType == "ended") {
  1601. this.onEndedFunC = callBack.bind(ths);
  1602. this.videoElement.offEnded = this.onEndedFunction.bind(this);
  1603. }
  1604. }
  1605. load(url) {
  1606. if (!this.videoElement)
  1607. return;
  1608. this.videoElement.src = url;
  1609. }
  1610. play() {
  1611. if (!this.videoElement)
  1612. return;
  1613. this.videoend = false;
  1614. this.videoElement.play();
  1615. }
  1616. pause() {
  1617. if (!this.videoElement)
  1618. return;
  1619. this.videoend = true;
  1620. this.videoElement.pause();
  1621. }
  1622. get currentTime() {
  1623. if (!this.videoElement)
  1624. return 0;
  1625. return this.videoElement.initialTime;
  1626. }
  1627. set currentTime(value) {
  1628. if (!this.videoElement)
  1629. return;
  1630. this.videoElement.initialTime = value;
  1631. }
  1632. get videoWidth() {
  1633. if (!this.videoElement)
  1634. return 0;
  1635. return this.videoElement.width;
  1636. }
  1637. get videoHeight() {
  1638. if (!this.videoElement)
  1639. return 0;
  1640. return this.videoElement.height;
  1641. }
  1642. get ended() {
  1643. return this.videoend;
  1644. }
  1645. get loop() {
  1646. if (!this.videoElement)
  1647. return false;
  1648. return this.videoElement.loop;
  1649. }
  1650. set loop(value) {
  1651. if (!this.videoElement)
  1652. return;
  1653. this.videoElement.loop = value;
  1654. }
  1655. get playbackRate() {
  1656. if (!this.videoElement)
  1657. return 0;
  1658. return this.videoElement.playbackRate;
  1659. }
  1660. set playbackRate(value) {
  1661. if (!this.videoElement)
  1662. return;
  1663. this.videoElement.playbackRate = value;
  1664. }
  1665. get muted() {
  1666. if (!this.videoElement)
  1667. return false;
  1668. return this.videoElement.muted;
  1669. }
  1670. set muted(value) {
  1671. if (!this.videoElement)
  1672. return;
  1673. this.videoElement.muted = value;
  1674. }
  1675. get paused() {
  1676. if (!this.videoElement)
  1677. return false;
  1678. return this.videoElement.paused;
  1679. }
  1680. size(width, height) {
  1681. if (!this.videoElement)
  1682. return;
  1683. this.videoElement.width = width;
  1684. this.videoElement.height = height;
  1685. }
  1686. get x() {
  1687. if (!this.videoElement)
  1688. return 0;
  1689. return this.videoElement.x;
  1690. }
  1691. set x(value) {
  1692. if (!this.videoElement)
  1693. return;
  1694. this.videoElement.x = value;
  1695. }
  1696. get y() {
  1697. if (!this.videoElement)
  1698. return 0;
  1699. return this.videoElement.y;
  1700. }
  1701. set y(value) {
  1702. if (!this.videoElement)
  1703. return;
  1704. this.videoElement.y = value;
  1705. }
  1706. get currentSrc() {
  1707. return this.videoElement.src;
  1708. }
  1709. destroy() {
  1710. if (this.videoElement)
  1711. this.videoElement.destroy();
  1712. this.videoElement = null;
  1713. this.onEndedFunC = null;
  1714. this.onPlayFunc = null;
  1715. this.videoend = false;
  1716. this.videourl = null;
  1717. }
  1718. reload() {
  1719. if (!this.videoElement)
  1720. return;
  1721. this.videoElement.src = this.videourl;
  1722. }
  1723. }
  1724. exports.BMiniAdapter = BMiniAdapter;
  1725. exports.MiniAccelerator = MiniAccelerator;
  1726. exports.MiniFileMgr = MiniFileMgr;
  1727. exports.MiniImage = MiniImage;
  1728. exports.MiniInput = MiniInput;
  1729. exports.MiniLoader = MiniLoader;
  1730. exports.MiniLocalStorage = MiniLocalStorage;
  1731. exports.MiniLocation = MiniLocation;
  1732. exports.MiniSound = MiniSound;
  1733. exports.MiniSoundChannel = MiniSoundChannel;
  1734. exports.MiniVideo = MiniVideo;
  1735. }