UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

437 lines (436 loc) 18.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoaderManager = void 0; const ILaya_1 = require("../../ILaya"); const Event_1 = require("../events/Event"); const EventDispatcher_1 = require("../events/EventDispatcher"); const URL_1 = require("../net/URL"); const Resource_1 = require("../resource/Resource"); const Texture_1 = require("../resource/Texture"); const Handler_1 = require("../utils/Handler"); const Utils_1 = require("../utils/Utils"); const AtlasInfoManager_1 = require("./AtlasInfoManager"); const Loader_1 = require("./Loader"); class LoaderManager extends EventDispatcher_1.EventDispatcher { getProgress() { return this._statInfo.loaded / this._statInfo.count; } resetProgress() { this._statInfo.count = this._statInfo.loaded = 1; } constructor() { super(); this.retryNum = 1; this.retryDelay = 0; this.maxLoader = 5; this._loaders = []; this._loaderCount = 0; this._resInfos = []; this._infoPool = []; this._maxPriority = 5; this._failRes = {}; this._statInfo = { count: 1, loaded: 1 }; for (var i = 0; i < this._maxPriority; i++) this._resInfos[i] = []; } create(url, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) { this._create(url, true, complete, progress, type, constructParams, propertyParams, priority, cache); } _create(url, mainResou, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) { if (url instanceof Array) { var allScuess = true; var items = url; var itemCount = items.length; var loadedCount = 0; if (progress) { var progress2 = Handler_1.Handler.create(progress.caller, progress ? progress.method : null, progress.args, false); } for (var i = 0; i < itemCount; i++) { var item = items[i]; if (typeof (item) == 'string') item = items[i] = { url: item }; item.progress = 0; } for (i = 0; i < itemCount; i++) { item = items[i]; var progressHandler = progress ? Handler_1.Handler.create(null, function (item, value) { item.progress = value; var num = 0; for (var j = 0; j < itemCount; j++) { var item1 = items[j]; num += item1.progress; } var v = num / itemCount; progress2.runWith(v); }, [item], false) : null; var completeHandler = (progress || complete) ? Handler_1.Handler.create(null, function (item, content = null) { loadedCount++; item.progress = 1; content || (allScuess = false); if (loadedCount === itemCount && complete) { complete.runWith(allScuess); } }, [item]) : null; this._createOne(item.url, mainResou, completeHandler, progressHandler, item.type || type, item.constructParams || constructParams, item.propertyParams || propertyParams, item.priority || priority, cache); } } else { this._createOne(url, mainResou, complete, progress, type, constructParams, propertyParams, priority, cache); } } _createOne(url, mainResou, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) { var item = this.getRes(url); if (!item) { var extension = (LoaderManager.createMap[Utils_1.Utils.getFilecompatibleExtension(url)]) ? Utils_1.Utils.getFilecompatibleExtension(url) : Utils_1.Utils.getFileExtension(url); (type) || (type = LoaderManager.createMap[extension] ? LoaderManager.createMap[extension][0] : null); if (!type) { this.load(url, complete, progress, type, priority, cache); return; } var parserMap = Loader_1.Loader.parserMap; if (!parserMap[type]) { this.load(url, complete, progress, type, priority, cache); return; } this._createLoad(url, Handler_1.Handler.create(null, function (createRes) { if (createRes) { if (!mainResou && createRes instanceof Resource_1.Resource) createRes._addReference(); createRes._setCreateURL(url); } complete && complete.runWith(createRes); ILaya_1.ILaya.loader.event(url); }), progress, type, constructParams, propertyParams, priority, cache, true); } else { if (!mainResou && item instanceof Resource_1.Resource) item._addReference(); progress && progress.runWith(1); complete && complete.runWith(item); } } load(url, complete = null, progress = null, type = null, priority = 1, cache = true, group = null, ignoreCache = false, useWorkerLoader = ILaya_1.ILaya.WorkerLoader.enable) { if (url instanceof Array) { return this._loadAssets(url, complete, progress, type, priority, cache, group); } if (!type) { if (url.indexOf("data:image") === 0) type = Loader_1.Loader.IMAGE; else type = Loader_1.Loader.getTypeFromUrl(url); } var content; if (type === Loader_1.Loader.IMAGE) { content = Loader_1.Loader.textureMap[URL_1.URL.formatURL(url)]; if (content && content.bitmap && content.bitmap.destroyed) { content = null; } } else content = Loader_1.Loader.loadedMap[URL_1.URL.formatURL(url)]; if (!ignoreCache && content != null) { ILaya_1.ILaya.systemTimer.callLater(this, function () { progress && progress.runWith(1); complete && complete.runWith(content instanceof Array ? [content] : content); this._loaderCount || this.event(Event_1.Event.COMPLETE); }); } else { var original; original = url; url = AtlasInfoManager_1.AtlasInfoManager.getFileLoadPath(url); if (url != original && type !== "nativeimage") { type = Loader_1.Loader.ATLAS; } else { original = null; } var info = LoaderManager._resMap[url]; if (!info) { info = this._infoPool.length ? this._infoPool.pop() : new ResInfo(); info.url = url; info.type = type; info.cache = cache; info.group = group; info.ignoreCache = ignoreCache; info.useWorkerLoader = useWorkerLoader; info.originalUrl = original; complete && info.on(Event_1.Event.COMPLETE, complete.caller, complete.method, complete.args); progress && info.on(Event_1.Event.PROGRESS, progress.caller, progress.method, progress.args); LoaderManager._resMap[url] = info; priority = priority < this._maxPriority ? priority : this._maxPriority - 1; this._resInfos[priority].push(info); this._statInfo.count++; this.event(Event_1.Event.PROGRESS, this.getProgress()); this._next(); } else { if (complete) { if (original) { complete && info._createListener(Event_1.Event.COMPLETE, this, this._resInfoLoaded, [original, complete], false, false); } else { complete && info._createListener(Event_1.Event.COMPLETE, complete.caller, complete.method, complete.args, false, false); } } progress && info._createListener(Event_1.Event.PROGRESS, progress.caller, progress.method, progress.args, false, false); } } return this; } _resInfoLoaded(original, complete) { complete.runWith(Loader_1.Loader.getRes(original)); } _createLoad(url, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true, ignoreCache = false) { if (url instanceof Array) return this._loadAssets(url, complete, progress, type, priority, cache); var content = Loader_1.Loader.getRes(url); if (content != null) { ILaya_1.ILaya.systemTimer.frameOnce(1, this, function () { progress && progress.runWith(1); complete && complete.runWith(content); this._loaderCount || this.event(Event_1.Event.COMPLETE); }); } else { var info = LoaderManager._resMap[url]; if (!info) { info = this._infoPool.length ? this._infoPool.pop() : new ResInfo(); info.url = url; info.type = type; info.cache = false; info.ignoreCache = ignoreCache; info.originalUrl = null; info.group = null; info.createCache = cache; info.createConstructParams = constructParams; info.createPropertyParams = propertyParams; complete && info.on(Event_1.Event.COMPLETE, complete.caller, complete.method, complete.args); progress && info.on(Event_1.Event.PROGRESS, progress.caller, progress.method, progress.args); LoaderManager._resMap[url] = info; priority = priority < this._maxPriority ? priority : this._maxPriority - 1; this._resInfos[priority].push(info); this._statInfo.count++; this.event(Event_1.Event.PROGRESS, this.getProgress()); this._next(); } else { complete && info._createListener(Event_1.Event.COMPLETE, complete.caller, complete.method, complete.args, false, false); progress && info._createListener(Event_1.Event.PROGRESS, progress.caller, progress.method, progress.args, false, false); } } return this; } _next() { if (this._loaderCount >= this.maxLoader) return; for (var i = 0; i < this._maxPriority; i++) { var infos = this._resInfos[i]; while (infos.length > 0) { var info = infos.shift(); if (info) return this._doLoad(info); } } this._loaderCount || this.event(Event_1.Event.COMPLETE); } _doLoad(resInfo) { this._loaderCount++; var loader = this._loaders.length ? this._loaders.pop() : new Loader_1.Loader(); loader.on(Event_1.Event.COMPLETE, null, onLoaded); loader.on(Event_1.Event.PROGRESS, null, function (num) { resInfo.event(Event_1.Event.PROGRESS, num); }); loader.on(Event_1.Event.ERROR, null, function (msg) { onLoaded(null); }); var _me = this; function onLoaded(data = null) { loader.offAll(); loader._data = null; loader._customParse = false; _me._loaders.push(loader); _me._endLoad(resInfo, data instanceof Array ? [data] : data); _me._loaderCount--; _me._next(); } loader._constructParams = resInfo.createConstructParams; loader._propertyParams = resInfo.createPropertyParams; loader._createCache = resInfo.createCache; loader.load(resInfo.url, resInfo.type, resInfo.cache, resInfo.group, resInfo.ignoreCache, resInfo.useWorkerLoader); } _endLoad(resInfo, content) { var url = resInfo.url; if (content == null) { var errorCount = this._failRes[url] || 0; if (errorCount < this.retryNum) { console.warn("[warn]Retry to load:", url); this._failRes[url] = errorCount + 1; ILaya_1.ILaya.systemTimer.once(this.retryDelay, this, this._addReTry, [resInfo], false); return; } else { Loader_1.Loader.clearRes(url); console.warn("[error]Failed to load:", url); this.event(Event_1.Event.ERROR, url); } } if (this._failRes[url]) this._failRes[url] = 0; delete LoaderManager._resMap[url]; if (resInfo.originalUrl) { content = Loader_1.Loader.getRes(resInfo.originalUrl); } resInfo.event(Event_1.Event.COMPLETE, content); resInfo.offAll(); this._infoPool.push(resInfo); this._statInfo.loaded++; this.event(Event_1.Event.PROGRESS, this.getProgress()); } _addReTry(resInfo) { this._resInfos[this._maxPriority - 1].push(resInfo); this._next(); } clearRes(url) { Loader_1.Loader.clearRes(url); } clearTextureRes(url) { Loader_1.Loader.clearTextureRes(url); } getRes(url) { return Loader_1.Loader.getRes(url); } cacheRes(url, data) { Loader_1.Loader.cacheRes(url, data); } setGroup(url, group) { Loader_1.Loader.setGroup(url, group); } clearResByGroup(group) { Loader_1.Loader.clearResByGroup(group); } static cacheRes(url, data) { Loader_1.Loader.cacheRes(url, data); } clearUnLoaded() { for (var i = 0; i < this._maxPriority; i++) { var infos = this._resInfos[i]; for (var j = infos.length - 1; j > -1; j--) { var info = infos[j]; if (info) { info.offAll(); this._infoPool.push(info); } } infos.length = 0; } this._loaderCount = 0; LoaderManager._resMap = {}; } cancelLoadByUrls(urls) { if (!urls) return; for (var i = 0, n = urls.length; i < n; i++) { this.cancelLoadByUrl(urls[i]); } } cancelLoadByUrl(url) { for (var i = 0; i < this._maxPriority; i++) { var infos = this._resInfos[i]; for (var j = infos.length - 1; j > -1; j--) { var info = infos[j]; if (info && info.url === url) { infos[j] = null; info.offAll(); this._infoPool.push(info); } } } if (LoaderManager._resMap[url]) delete LoaderManager._resMap[url]; } _loadAssets(arr, complete = null, progress = null, type = null, priority = 1, cache = true, group = null) { var itemCount = arr.length; var loadedCount = 0; var totalSize = 0; var items = []; var success = true; for (var i = 0; i < itemCount; i++) { let url = arr[i]; let item; if (typeof (url) == 'string') item = { url: url, type: type, size: 1, priority: priority }; else item = url; if (!item.size) item.size = 1; item.progress = 0; totalSize += item.size; items.push(item); var progressHandler = progress ? Handler_1.Handler.create(null, loadProgress, [item], false) : null; var completeHandler = (complete || progress) ? Handler_1.Handler.create(null, loadComplete, [item]) : null; this.load(item.url, completeHandler, progressHandler, item.type, item.priority || 1, cache, item.group || group, false, item.useWorkerLoader); } function loadComplete(item, content = null) { loadedCount++; item.progress = 1; if (!content) success = false; if (loadedCount === itemCount && complete) { complete.runWith(success); } } function loadProgress(item, value) { if (progress != null) { item.progress = value; var num = 0; for (var j = 0; j < items.length; j++) { var item1 = items[j]; if (item1) { let prog = item1.progress == undefined ? 0 : item1.progress; num += item1.size == undefined ? 0 : item1.size * prog; } } var v = num / totalSize; progress.runWith(v); } } return this; } decodeBitmaps(urls) { var i, len = urls.length; var ctx; ctx = ILaya_1.ILaya.Render._context; for (i = 0; i < len; i++) { var atlas; atlas = Loader_1.Loader.getAtlas(urls[i]); if (atlas) { this._decodeTexture(atlas[0], ctx); } else { var tex; tex = this.getRes(urls[i]); if (tex && tex instanceof Texture_1.Texture) { this._decodeTexture(tex, ctx); } } } } _decodeTexture(tex, ctx) { var bitmap = tex.bitmap; if (!tex || !bitmap) return; var tImg = bitmap.source || bitmap.image; if (!tImg) return; if (tImg instanceof HTMLImageElement) { ctx.drawImage(tImg, 0, 0, 1, 1); var info = ctx.getImageData(0, 0, 1, 1); } } } exports.LoaderManager = LoaderManager; LoaderManager._resMap = {}; LoaderManager.createMap = { atlas: [null, Loader_1.Loader.ATLAS] }; class ResInfo extends EventDispatcher_1.EventDispatcher { }