laya.hwmini.js 56 KB

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