diff --git a/cocos/assets/CCJsonAsset.ts b/cocos/assets/CCJsonAsset.ts index faa0b5992e..6a4154b153 100644 --- a/cocos/assets/CCJsonAsset.ts +++ b/cocos/assets/CCJsonAsset.ts @@ -27,25 +27,15 @@ import {ccclass, property} from '../core/data/class-decorator'; import { Asset } from './asset'; /** - * !#en - * Class for JSON file. When the JSON file is loaded, this object is returned. - * The parsed JSON object can be accessed through the `json` attribute in it.
- * If you want to get the original JSON text, you should modify the extname to `.txt` - * so that it is loaded as a `TextAsset` instead of a `JsonAsset`. - * - * !#zh - * JSON 资源类。JSON 文件加载后,将会返回该对象。可以通过其中的 `json` 属性访问解析后的 JSON 对象。
- * 如果你想要获得 JSON 的原始文本,那么应该修改源文件的后缀为 `.txt`,这样就会加载为一个 `TextAsset` 而不是 `JsonAsset`。 - * - * @class JsonAsset - * @extends Asset + * Json 资源。 + * Json 资源加载后将直接解析为对象。如果你希望获得 JSON 的原始文本,你需要使用文本资源(使用文件名后缀“.txt”)。 */ @ccclass('cc.JsonAsset') export default class JsonAsset extends Asset { /** - * @property json - The loaded JSON object. + * 解析后的对象。 */ - @property() + @property public json: object | null = null; } diff --git a/cocos/assets/CCSceneAsset.ts b/cocos/assets/CCSceneAsset.ts index 8f87b7dab4..7f92281517 100644 --- a/cocos/assets/CCSceneAsset.ts +++ b/cocos/assets/CCSceneAsset.ts @@ -29,36 +29,21 @@ import { Scene } from '../scene-graph'; import { Asset } from './asset'; /** - * !#en Class for scene handling. - * !#zh 场景资源类。 - * @class SceneAsset - * @extends Asset - * + * 场景资源。 */ @ccclass('cc.SceneAsset') export default class SceneAsset extends Asset { /** - * @property {Scene} scene - * @default null + * 场景结点。 */ @property public scene: Scene | null = null; /** - * !#en Indicates the raw assets of this scene can be load after scene launched. - * !#zh 指示该场景依赖的资源可否在场景切换后再延迟加载。 - * @property {Boolean} asyncLoadAssets - * @default false + * 指示该场景依赖的资源可否在场景切换后再延迟加载。 */ @property public asyncLoadAssets = false; - - //// backup prefab assets in editor - //// {string} assetUuid: {cc.Node} rootInPrefab - // _prefabDatas: { - // default: null, - // editorOnly: true - // } } cc.SceneAsset = SceneAsset; diff --git a/cocos/assets/CCScripts.ts b/cocos/assets/CCScripts.ts index 6ced434e37..bd50154c2e 100644 --- a/cocos/assets/CCScripts.ts +++ b/cocos/assets/CCScripts.ts @@ -28,12 +28,7 @@ import {ccclass} from '../core/data/class-decorator'; import { Asset } from './asset'; /** - * !#en Class for script handling. - * !#zh Script 资源类。 - * @class _Script - * @extends Asset - * - * @private + * 脚本资源基类。 */ @ccclass('cc.Script') export class Script extends Asset { @@ -41,11 +36,7 @@ export class Script extends Asset { cc._Script = Script; /** - * !#en Class for JavaScript handling. - * !#zh JavaScript 资源类。 - * @class _JavaScript - * @extends Asset - * + * JavaScript 脚本资源。 */ @ccclass('cc.JavaScript') export class JavaScript extends Script { @@ -53,11 +44,7 @@ export class JavaScript extends Script { cc._JavaScript = JavaScript; /** - * !#en Class for TypeScript handling. - * !#zh TypeScript 资源类。 - * @class TypeScript - * @extends Asset - * + * Typescript 脚本资源。 */ @ccclass('cc.TypeScript') export class TypeScript extends Script { diff --git a/cocos/assets/CCTextAsset.ts b/cocos/assets/CCTextAsset.ts index f5ad7d1c2f..1f575ddcf3 100644 --- a/cocos/assets/CCTextAsset.ts +++ b/cocos/assets/CCTextAsset.ts @@ -28,26 +28,18 @@ import {ccclass, property} from '../core/data/class-decorator'; import { Asset } from './asset'; /** - * !#en Class for text file. - * !#zh 文本资源类。 - * @class TextAsset - * @extends Asset + * 文本资源。 */ @ccclass('cc.TextAsset') export default class TextAsset extends Asset { /** - * @property {String} text - The text contents of the resource. + * 此资源包含的文本。 */ @property public text = ''; /** - * Returns the text content of the asset. - * - * JavaScript calls the toString() method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation. - * - * @method toString - * @return {String} + * 重载标准的 `toString()` 方法。 */ public toString () { return this.text; diff --git a/cocos/assets/image-asset.ts b/cocos/assets/image-asset.ts index 265830c8ac..bac9f9bdf8 100644 --- a/cocos/assets/image-asset.ts +++ b/cocos/assets/image-asset.ts @@ -29,6 +29,9 @@ import { GFXDevice, GFXFeature } from '../gfx/device'; import { Asset } from './asset'; import { PixelFormat } from './asset-enum'; +/** + * 内存图像源。 + */ export interface IMemoryImageSource { _data: ArrayBufferView | null; _compressed: boolean; @@ -37,6 +40,9 @@ export interface IMemoryImageSource { format: number; } +/** + * 图像资源的原始图像源。可以来源于 HTML 元素也可以来源于内存。 + */ export type ImageSource = HTMLCanvasElement | HTMLImageElement | IMemoryImageSource; function fetchImageSource (imageSource: ImageSource) { @@ -44,7 +50,7 @@ function fetchImageSource (imageSource: ImageSource) { } /** - * Class ImageAsset. + * 图像资源。 */ @ccclass('cc.ImageAsset') export class ImageAsset extends Asset { @@ -59,32 +65,45 @@ export class ImageAsset extends Asset { this.reset(value); } + /** + * 此图像资源的图像数据。 + */ get data () { return '_data' in this._nativeData ? this._nativeData._data : this._nativeData; } + /** + * 此图像资源的像素宽度。 + */ get width () { return this._nativeData.width || this._width; } + /** + * 此图像资源的像素高度。 + */ get height () { return this._nativeData.height || this._height; } + /** + * 此图像资源的像素格式。 + */ get format () { return this._format; } + /** + * 此图像资源是否为压缩像素格式。 + */ get isCompressed () { return this._format >= PixelFormat.RGB_ETC1 && this._format <= PixelFormat.RGBA_PVRTC_4BPPV1; } + /** - * !#en - * The url of the texture, this could be empty if the texture wasn't created via a file. - * !#zh - * 图像文件的 url,当图像不是由文件创建时值可能为空 + * 此图像资源的原始图像源的 URL。当原始图像元不是 HTML 文件时可能为空。 + * @deprecated 请转用 `this.nativeUrl`。 */ - // TODO - use nativeUrl directly get url () { return this._url; } @@ -129,6 +148,10 @@ export class ImageAsset extends Asset { } } + /** + * 重置此图像资源使用的原始图像源。 + * @param data 新的原始图像源。 + */ public reset (data: ImageSource) { if (!(data instanceof HTMLElement)) { // this._nativeData = Object.create(data); diff --git a/cocos/assets/index.ts b/cocos/assets/index.ts index ad14ba5a6a..3aa6c0631d 100644 --- a/cocos/assets/index.ts +++ b/cocos/assets/index.ts @@ -40,3 +40,5 @@ export { TTFFont } from './ttf-font'; export { LabelAtlas } from './label-atlas'; export { BitmapFont } from './bitmap-font'; export { Font } from './font'; +import * as textureUtil from './texture-util'; +export { textureUtil }; diff --git a/cocos/assets/raw-asset.ts b/cocos/assets/raw-asset.ts index e55b092666..7284b8fd76 100644 --- a/cocos/assets/raw-asset.ts +++ b/cocos/assets/raw-asset.ts @@ -29,25 +29,20 @@ import { CCObject } from '../core/data/object'; import { isChildClassOf } from '../core/utils/js'; /** - * !#en - * The base class for registering asset types. - * !#zh - * 注册用的资源基类。 - * - * @class RawAsset - * @extends Object + * 原生资源的基类。内部使用。 + * @private */ @ccclass('cc.RawAsset') export class RawAsset extends CCObject { /** - * For internal usage. + * 内部使用。 */ public static isRawAssetType (ctor: Function) { return isChildClassOf(ctor, cc.RawAsset) && !isChildClassOf(ctor, cc.Asset); } /** - * For internal usage. + * 内部使用。 */ // @ts-ignore public _uuid: string; diff --git a/cocos/assets/texture-util.ts b/cocos/assets/texture-util.ts index f955544eef..09ae5d2143 100644 --- a/cocos/assets/texture-util.ts +++ b/cocos/assets/texture-util.ts @@ -24,50 +24,62 @@ THE SOFTWARE. ****************************************************************************/ +import { assertID, error } from '../core/platform/CCDebug'; +import { LoadCallbackParams, loader } from '../load-pipeline'; import { ImageAsset, ImageSource } from './image-asset'; +export type LoadImageCallback = ( + this: T | undefined, + ...args: LoadCallbackParams + ) => void; + /** - * cc.textureUtil is a singleton object, it can load cc.Texture2D asynchronously - * @class textureUtil - * @static + * 加载指定的图像资源。 + * @param url 图像资源的链接。 + * @param callback 回调函数。 + * @param target 回调函数的 `this` 参数。 + * @returns 图像资源,返回时可能还未完成加载;加载完成或失败时会调用回调函数。 */ +export function loadImage (url: string, callback?: LoadImageCallback, target?: T) { + assertID(!!url, 3103); -export function loadImage (url: string, cb, target) { - cc.assertID(url, 3103); - - let imageAsset = cc.loader.getRes(url); + let imageAsset = loader.getRes(url); if (imageAsset) { if (imageAsset.loaded) { - if (cb){ - cb.call(target, imageAsset); + if (callback){ + callback.call(target, null, imageAsset); } - return imageAsset; - } - else { + } else { imageAsset.once('load', () => { - if (cb) { - cb.call(target, imageAsset); + if (callback) { + callback.call(target, null, imageAsset!); } }, target); return imageAsset; } - } - else { + } else { imageAsset = new ImageAsset(); - imageAsset.url = url; - cc.loader.load({ url, imageAsset }, (err, asset) => { + loader.load({url, imageAsset}, (err, asset) => { if (err) { - return cb && cb.call(target, err || new Error('Unknown error')); + if (callback) { + callback.call(target, err || new Error('Unknown error')); + } + return imageAsset; } - if (cb) { - cb.call(target, null, asset); + if (callback) { + callback.call(target, null, asset); } }); return imageAsset; } } +/** + * 缓存指定的图像源,为它指定链接。此后,可以通过该链接直接加载它。 + * @param url 指定的链接。 + * @param image 缓存的图像源。 + */ export function cacheImage (url: string, image: ImageSource) { if (url && image) { const imageAsset = new ImageAsset(image); @@ -78,11 +90,16 @@ export function cacheImage (url: string, image: ImageSource) { content: imageAsset, complete: false, }; - cc.loader.flowOut(item); + loader.flowOut(item); return imageAsset; } } -// TODO: + +/** + * 尝试加载图像资源的实际数据。 + * @param imageAsset 图像资源。 + * @param callback 回调函数。 + */ export function postLoadImage (imageAsset: ImageAsset, callback?: Function) { if (imageAsset.loaded) { if (callback) { @@ -97,14 +114,14 @@ export function postLoadImage (imageAsset: ImageAsset, callback?: Function) { return; } // load image - cc.loader.load({ + loader.load({ url: imageAsset.nativeUrl, // For image, we should skip loader otherwise it will load a new ImageAsset skips: imageAsset.isCompressed ? undefined : ['Loader'], }, (err, image) => { if (image) { - if (CC_DEBUG && image instanceof cc.ImageAsset) { - return cc.error('internal error: loader handle pipe must be skipped'); + if (CC_DEBUG && image instanceof ImageAsset) { + return error('internal error: loader handle pipe must be skipped'); } if (!imageAsset.loaded) { imageAsset._nativeAsset = image; @@ -115,9 +132,3 @@ export function postLoadImage (imageAsset: ImageAsset, callback?: Function) { } }); } - -cc.textureUtil = { - loadImage, - cacheImage, - postLoadImage, -}; diff --git a/cocos/load-pipeline/CCLoader.ts b/cocos/load-pipeline/CCLoader.ts index 107eeb5cb7..d61c339c6e 100644 --- a/cocos/load-pipeline/CCLoader.ts +++ b/cocos/load-pipeline/CCLoader.ts @@ -1,1159 +1,1159 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -import { Asset, RawAsset } from '../assets'; -import { createMap, getClassName, isChildClassOf } from '../core/utils/js'; -import { callInNextTick } from '../core/utils/misc'; -import AssetLoader from './asset-loader'; -import AssetTable from './asset-table'; -import { getDependsRecursively } from './auto-release-utils'; -import Downloader from './downloader'; -import Loader from './loader'; -import LoadingItems from './loading-items'; -import Pipeline from './pipeline'; -import ReleasedAssetChecker from './released-asset-checker'; - -const assetTables = Object.create(null); -assetTables.assets = new AssetTable(); -assetTables.internal = new AssetTable(); - -function getXMLHttpRequest () { - return window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('MSXML2.XMLHTTP'); -} - -const _info = { url: null, raw: false }; - -/** - * @en - * Convert a resources by finding its real url with uuid, otherwise we will use the uuid or raw url as its url
- * So we gurantee there will be url in result - * @zh - * 通过使用 uuid 查找资源的真实 url 来转换资源,否则将使用 uuid 或原始 url 作为其 url
- * 所以可以保证结果中会有 url - * @param res - */ -function getResWithUrl (res) { - let id; - let result; - let isUuid; - if (typeof res === 'object') { - result = res; - if (res.url) { - return result; - } - else { - id = res.uuid; - } - } - else { - result = {}; - id = res; - } - isUuid = result.type ? result.type === 'uuid' : cc.AssetLibrary._uuidInSettings(id); - cc.AssetLibrary._getAssetInfoInRuntime(id, _info); - result.url = !isUuid ? id : _info.url; - if (_info.url && result.type === 'uuid' && _info.raw) { - result.type = null; - result.isRawAsset = true; - } - else if (!isUuid) { - result.isRawAsset = true; - } - return result; -} - -const _sharedResources: any = []; -const _sharedList: any = []; - -/** - * @en - * Loader for resource loading process. It's a singleton object. - * @zh - * 资源加载程序,这是一个单例对象。 - * @class loader - * @extends Pipeline - * @static - */ -class CCLoader extends Pipeline { - - /** - * @en - * Gets a new XMLHttpRequest instance. - * @zh - * 获取一个新的 XMLHttpRequest 的实例。 - * @method getXMLHttpRequest - * @returns {XMLHttpRequest} - */ - public getXMLHttpRequest: Function; - - /** - * @en - * The asset loader in cc.loader's pipeline, it's by default the first pipe.
- * It's used to identify an asset's type, and determine how to download it. - * @zh - * cc.loader 中的资源加载器,默认情况下是最先加载的。
- * 用于标识资源的类型,并确定如何加载此资源。 - */ - public assetLoader: AssetLoader; - - /** - * @en - * The md5 pipe in cc.loader's pipeline, it could be absent if the project isn't build with md5 option.
- * It's used to modify the url to the real downloadable url with md5 suffix. - * @zh - * cc.loader 中的 md5 加载管道,如果项目没有使用 md5 构建,则此项可能不存在。
- * 用于修改带有 md5 后缀的真实可下载的 URL 。 - */ - public md5Pipe: null; - - /** - * @en - * The downloader in cc.loader's pipeline, it's by default the second pipe.
- * It's used to download files with several handlers: pure text, image, script, audio, font, uuid.
- * You can add your own download function with addDownloadHandlers - * @zh - * cc.loader 中的资源下载程序,默认情况下是第二个加载的。
- * 它用于下载带有多个处理程序的文件:纯文本,图像,脚本,音频,字体,uuid。
- * 您可以使用 addDownloadHandlers 来添加自己的下载函数 - */ - public downloader: Downloader; - - /** - * @en - * The loader in cc.loader's pipeline, it's by default the third pipe.
- * It's used to parse downloaded content with several handlers: JSON, image, plist, fnt, uuid.
- * You can add your own download function with addLoadHandlers - * @zh - * cc.loader 中的资源下载程序,默认情况下是第三个加载的。
- * 它用于解析下载的内容及多个处理程序的文件:纯文本,图像,脚本,音频,字体,uuid。
- * 您可以使用 addLoadHandlers 来添加自己的下载函数 - */ - public loader: Loader; - public onProgress: null; - public _assetTables: any; - - private _autoReleaseSetting: any; - private _releasedAssetChecker_DEBUG: any; - - constructor () { - - const assetLoader = new AssetLoader(); - const downloader = new Downloader(); - // tslint:disable-next-line: no-shadowed-variable - const loader = new Loader(); - - super([ - assetLoader, - downloader, - loader, - ]); - - this.getXMLHttpRequest = getXMLHttpRequest; - - this.assetLoader = assetLoader; - - this.md5Pipe = null; - - this.downloader = downloader; - - this.loader = loader; - - this.onProgress = null; - - this._assetTables = assetTables; - // assets to release automatically - this._autoReleaseSetting = createMap(true); - - if (CC_DEBUG) { - this._releasedAssetChecker_DEBUG = new ReleasedAssetChecker(); - } - } - - public init (director) { - if (CC_DEBUG) { - const self = this; - director.on(cc.Director.EVENT_AFTER_UPDATE, () => { - self._releasedAssetChecker_DEBUG.checkCouldRelease(self._cache); - }); - } - } - - /** - * @en - * Add custom supported types handler or modify existing type handler for download process. - * @zh - * 为下载程序添加自定义支持的类型处理程序或修改现有的类型处理程序。 - * @example - * ```typescript - * cc.loader.addDownloadHandlers({ - * // This will match all url with `.scene` extension or all url with `scene` type - * 'scene' : function (url, callback) {} - * }); - * ``` - * @param extMap 具有相应处理程序的自定义支持类型 - */ - public addDownloadHandlers (extMap: Object) { - this.downloader.addHandlers(extMap); - } - - /** - * @en - * Add custom supported types handler or modify existing type handler for load process. - * @zh - * 为加载程序添加自定义支持的类型处理程序或修改现有的类型处理程序。 - * @example - * ```typescript - * cc.loader.addLoadHandlers({ - * // This will match all url with `.scene` extension or all url with `scene` type - * 'scene' : function (url, callback) {} - * }); - * ``` - * @method addLoadHandlers - * @param extMap 具有相应处理程序的自定义支持类型 - */ - public addLoadHandlers (extMap: Object) { - this.loader.addHandlers(extMap); - } - - // tslint:disable: max-line-length - - // load(resources: string|string[]|{uuid?: string, url?: string, type?: string}, completeCallback?: Function): void - // load(resources: string|string[]|{uuid?: string, url?: string, type?: string}, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: Function|null): void - /** - * @en - * Load resources with a progression callback and a complete callback.
- * The progression callback is the same as Pipeline's [[LoadingItems.onProgress]]
- * The complete callback is almost the same as Pipeline's [[LoadingItems.onComplete]]
- * The only difference is when user pass a single url as resources, the complete callback will set its result directly as the second parameter. - * @zh - * 使用进度回调和完整回调加载资源。
-     * 进度回调与 Pipeline 的 [[LoadingItems.onProgress]] 相同
-     * 完整的回调与 Pipeline 的 [[LoadingItems.onComplete]] 几乎相同
-     * 唯一的区别是当用户将单个 URL 作为资源传递时,完整的回调将其结果直接设置为第二个参数。 - * @example - * ```TypeScript - * cc.loader.load('a.png', function (err, tex) { - * cc.log('Result should be a texture: ' + (tex instanceof cc.Texture2D)); - * }); - * - * cc.loader.load('http://example.com/a.png', function (err, tex) { - * cc.log('Should load a texture from external url: ' + (tex instanceof cc.Texture2D)); - * }); - * - * cc.loader.load({url: 'http://example.com/getImageREST?file=a.png', type: 'png'}, function (err, tex) { - * cc.log('Should load a texture from RESTful API by specify the type: ' + (tex instanceof cc.Texture2D)); - * }); - * - * cc.loader.load(['a.png', 'b.json'], function (errors, results) { - * if (errors) { - * for (var i = 0; i < errors.length; i++) { - * cc.log('Error url [' + errors[i] + ']: ' + results.getError(errors[i])); - * } - * } - * var aTex = results.getContent('a.png'); - * var bJsonObj = results.getContent('b.json'); - * }); - * ``` - * @method load - * @param {String|String[]|Object} resources - Url 列表数组 - * @param {Function} progressCallback - 当进度改变时调用的回调函数 - * @param {Number} progressCallback.completedCount - The number of the items that are already completed - * @param {Number} progressCallback.totalCount - The total number of the items - * @param {Object} progressCallback.item - The latest item which flow out the pipeline - * @param {Function} completeCallback - 当所有资源加载完毕后调用的回调函数 - */ - public load (resources, progressCallback, completeCallback) { - if (CC_DEV && !resources) { - return cc.error('[cc.loader.load] resources must be non-nil.'); - } - - if (completeCallback === undefined) { - completeCallback = progressCallback; - progressCallback = this.onProgress || null; - } - - const self = this; - let singleRes: Boolean = false; - let res; - if (!(resources instanceof Array)) { - if (resources) { - singleRes = true; - resources = [resources]; - } else { - resources = []; - } - } - - _sharedResources.length = 0; - // tslint:disable-next-line: prefer-for-of - for (let i = 0; i < resources.length; ++i) { - const resource = resources[i]; - // Backward compatibility - if (resource && resource.id) { - cc.warnID(4920, resource.id); - if (!resource.uuid && !resource.url) { - resource.url = resource.id; - } - } - res = getResWithUrl(resource); - if (!res.url && !res.uuid) { - continue; - } - const item = this._cache[res.url]; - _sharedResources.push(item || res); - } - - const queue = LoadingItems.create(this, progressCallback, (errors, items) => { - callInNextTick(() => { - if (completeCallback) { - if (singleRes) { - const id = res.url; - completeCallback.call(self, items.getError(id), items.getContent(id)); - } - else { - completeCallback.call(self, errors, items); - } - completeCallback = null; - } - - items.destroy(); - }); - }); - LoadingItems.initQueueDeps(queue); - queue.append(_sharedResources); - _sharedResources.length = 0; - } - - /** - * @en - * See: [[Pipeline.flowInDeps]] - * @zh - * 参考:[[Pipeline.flowInDeps]] - */ - public flowInDeps (owner, urlList, callback) { - _sharedList.length = 0; - // tslint:disable-next-line: prefer-for-of - for (let i = 0; i < urlList.length; ++i) { - const res = getResWithUrl(urlList[i]); - if (!res.url && !res.uuid) { - continue; - } - const item = this._cache[res.url]; - if (item) { - _sharedList.push(item); - } - else { - _sharedList.push(res); - } - } - // @ts-ignore - const queue = LoadingItems.create(this, owner ? (completedCount, totalCount, item) => { - if (queue._ownerQueue && queue._ownerQueue.onProgress) { - queue._ownerQueue._childOnProgress(item); - } - } : null, (errors, items) => { - callback(errors, items); - // Clear deps because it's already done - // Each item will only flowInDeps once, so it's still safe here - if (owner && owner.deps) { - owner.deps.length = 0; - } - items.destroy(); - }); - if (owner) { - const ownerQueue = LoadingItems.getQueue(owner); - // Set the root ownerQueue, if no ownerQueue defined in ownerQueue, it's the root - queue._ownerQueue = ownerQueue._ownerQueue || ownerQueue; - } - const accepted = queue.append(_sharedList, owner); - _sharedList.length = 0; - return accepted; - } - - // @typescript - // loadRes(url: string, type: typeof cc.Asset, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any) => void)|null): void - // loadRes(url: string, type: typeof cc.Asset, completeCallback: (error: Error, resource: any) => void): void - // loadRes(url: string, type: typeof cc.Asset): void - // loadRes(url: string, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any) => void)|null): void - // loadRes(url: string, completeCallback: (error: Error, resource: any) => void): void - // loadRes(url: string): void - - /** - * @en - * Load resources from the "resources" folder inside the "assets" folder of your project.
- *
- * Note: All asset URLs in Creator use forward slashes, URLs using backslashes will not work. - * @zh - * 从项目的 “assets” 文件夹下的 “resources” 文件夹中加载资源
- *
- * 注意:Creator 中的所有资源 URL 都使用正斜杠,使用反斜杠的 URL 将不起作用。 - * @method loadRes - * @param {String} url - 目标资源的 URL
- * URl 相对于 “resources” 文件夹,必须省略文件扩展名。 - * @param {Function} type - 如果提供此参数,则将仅加载此类型的资源。 - * @param {Function} progressCallback - 当进度改变时调用的回调函数 - * @param {Number} progressCallback.completedCount - The number of the items that are already completed. - * @param {Number} progressCallback.totalCount - The total number of the items. - * @param {Object} progressCallback.item - The latest item which flow out the pipeline. - * @param {Function} completeCallback - 当所有资源加载完毕后调用的回调函数 - * @param {Error} completeCallback.error - The error info or null if loaded successfully. - * @param {Object} completeCallback.resource - The loaded resource if it can be found otherwise returns null. - * - * @example - * ```typescript - * // load the prefab (project/assets/resources/misc/character/cocos) from resources folder - * cc.loader.loadRes('misc/character/cocos', function (err, prefab) { - * if (err) { - * cc.error(err.message || err); - * return; - * } - * cc.log('Result should be a prefab: ' + (prefab instanceof cc.Prefab)); - * }); - * - * // load the sprite frame of (project/assets/resources/imgs/cocos.png) from resources folder - * cc.loader.loadRes('imgs/cocos', cc.SpriteFrame, function (err, spriteFrame) { - * if (err) { - * cc.error(err.message || err); - * return; - * } - * cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame)); - * }); - * ``` - */ - public loadRes (url: String, type: Function, mount, progressCallback: Function, completeCallback: Function) { - if (arguments.length !== 5) { - completeCallback = progressCallback; - progressCallback = mount; - mount = 'assets'; - } - const args = this._parseLoadResArgs(type, progressCallback, completeCallback); - type = args.type; - progressCallback = args.onProgress; - completeCallback = args.onComplete; - const self = this; - const uuid = self._getResUuid(url, type, mount, true); - if (uuid) { - this.load( - { - type: 'uuid', - uuid, - }, - progressCallback, - (err, asset) => { - if (asset) { - // should not release these assets, even if they are static referenced in the scene. - self.setAutoReleaseRecursively(uuid, false); - } - if (completeCallback) { - completeCallback(err, asset); - } - }); - } - else { - self._urlNotFound(url, type, completeCallback); - } - } - - // @typescript - // loadResDir(url: string, type: typeof cc.Asset, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[], urls: string[]) => void)|null): void - // loadResDir(url: string, type: typeof cc.Asset, completeCallback: (error: Error, resource: any[], urls: string[]) => void): void - // loadResDir(url: string, type: typeof cc.Asset): void - // loadResDir(url: string, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[], urls: string[]) => void)|null): void - // loadResDir(url: string, completeCallback: (error: Error, resource: any[], urls: string[]) => void): void - // loadResDir(url: string): void - - /** - * @en - * Load all assets in a folder inside the "assets/resources" folder of your project.
- *
- * Note: All asset URLs in Creator use forward slashes, URLs using backslashes will not work. - * @zh - * 将所有资产加载到项目 “assets / resources” 文件夹中 -     *
- * 注意:Creator 中的所有资源 URL 都使用正斜杠,使用反斜杠的 URL 将不起作用。 - * @method loadResDir - * @param {String} url - 目标文件夹的 URL
- * URl 相对于 “resources” 文件夹,必须省略文件扩展名。 - * @param {Function} type - 如果提供此参数,则将仅加载此类型的资源。 - * @param {Function} progressCallback - 当进度改变时调用的回调函数 - * @param {Number} progressCallback.completedCount - The number of the items that are already completed. - * @param {Number} progressCallback.totalCount - The total number of the items. - * @param {Object} progressCallback.item - The latest item which flow out the pipeline. - * @param {Function} completeCallback - 当所有资源加载完毕后或者发生错误时调用的回调函数 - * @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called - * with the error. If all assets are loaded successfully, error will be null. - * @param {Asset[]|Array} completeCallback.assets - An array of all loaded assets. - * If nothing to load, assets will be an empty array. - * @param {String[]} completeCallback.urls - An array that lists all the URLs of loaded assets. - * - * @example - * ```typescript - * // load the texture (resources/imgs/cocos.png) and the corresponding sprite frame - * cc.loader.loadResDir('imgs/cocos', function (err, assets) { - * if (err) { - * cc.error(err); - * return; - * } - * var texture = assets[0]; - * var spriteFrame = assets[1]; - * }); - * - * // load all textures in "resources/imgs/" - * cc.loader.loadResDir('imgs', cc.Texture2D, function (err, textures) { - * var texture1 = textures[0]; - * var texture2 = textures[1]; - * }); - * - * // load all JSONs in "resources/data/" - * cc.loader.loadResDir('data', function (err, objects, urls) { - * var data = objects[0]; - * var url = urls[0]; - * }); - * ``` - */ - public loadResDir (url: String, type?: Function, mount?, progressCallback?: Function, completeCallback?: Function) { - if (arguments.length !== 5) { - completeCallback = progressCallback; - progressCallback = mount; - mount = 'assets'; - } - - if (!assetTables[mount]) { return; } - - const args = this._parseLoadResArgs(type, progressCallback, completeCallback); - type = args.type; - progressCallback = args.onProgress; - completeCallback = args.onComplete; - - const urls = []; - const uuids = assetTables[mount].getUuidArray(url, type, urls); - this._loadResUuids(uuids, progressCallback, (errors, assetRes, urlRes) => { - // The spriteFrame url in spriteAtlas will be removed after build project - // To show users the exact structure in asset panel, we need to return the spriteFrame assets in spriteAtlas - const assetResLength = assetRes.length; - for (let i = 0; i < assetResLength; ++i) { - if (assetRes[i] instanceof cc.SpriteAtlas) { - const spriteFrames = assetRes[i].getSpriteFrames(); - // tslint:disable: forin - for (const k in spriteFrames) { - const sf = spriteFrames[k]; - assetRes.push(sf); - if (urlRes) { - urlRes.push(`${urlRes[i]}/${sf.name}`); - } - } - } - } - if (completeCallback) { - completeCallback(errors, assetRes, urlRes); - } - }, urls); - } - - // * @typescript - // * loadResArray(url: string[], type: typeof cc.Asset, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[]) => void)|null): void - // * loadResArray(url: string[], type: typeof cc.Asset, completeCallback: (error: Error, resource: any[]) => void): void - // * loadResArray(url: string[], type: typeof cc.Asset): void - // * loadResArray(url: string[], progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[]) => void)|null): void - // * loadResArray(url: string[], completeCallback: (error: Error, resource: any[]) => void): void - // * loadResArray(url: string[]): void - /** - * @en - * This method is like [[loadRes]] except that it accepts array of url. - * @zh - * 此方法除了接受 URL 数组参数外,与 [[loadRes]] 方法相同。 - * - * @method loadResArray - * @param {String[]} urls - 目标资源的 URL 数组。 - * URl 为相对于 “resources” 文件夹的,且必须省略文件扩展名。 - * @param {Function} type - 如果提供此参数,则将仅加载此类型的资源。 - * @param {Function} progressCallback - 当进度改变时调用的回调函数 - * @param {Number} progressCallback.completedCount - The number of the items that are already completed. - * @param {Number} progressCallback.totalCount - The total number of the items. - * @param {Object} progressCallback.item - The latest item which flow out the pipeline. - * @param {Function} completeCallback - 当所有资源加载完毕后或者发生错误时调用的回调函数 - * @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called - * with the error. If all assets are loaded successfully, error will be null. - * @param {Asset[]|Array} completeCallback.assets - An array of all loaded assets. - * If nothing to load, assets will be an empty array. - * @example - * ```typescript - * // load the SpriteFrames from resources folder - * let spriteFrames; - * let urls = ['misc/characters/character_01', 'misc/weapons/weapons_01']; - * cc.loader.loadResArray(urls, cc.SpriteFrame, function (err, assets) { - * if (err) { - * cc.error(err); - * return; - * } - * spriteFrames = assets; - * // ... - * }); - * ``` - */ - public loadResArray (urls: String[], type?: Function, mount?, progressCallback?: Function, completeCallback?: Function) { - if (arguments.length !== 5) { - completeCallback = progressCallback; - progressCallback = mount; - mount = 'assets'; - } - - const args = this._parseLoadResArgs(type, progressCallback, completeCallback); - type = args.type; - progressCallback = args.onProgress; - completeCallback = args.onComplete; - - const uuids: any = []; - // tslint:disable: prefer-for-of - for (let i = 0; i < urls.length; i++) { - const url = urls[i]; - const uuid = this._getResUuid(url, type, mount, true); - if (uuid) { - uuids.push(uuid); - } - else { - this._urlNotFound(url, type, completeCallback); - return; - } - } - this._loadResUuids(uuids, progressCallback, completeCallback); - } - - /** - * @en - * Get resource data by id.
- * When you load resources with [[load]] or [[loadRes]], - * the url will be the unique identity of the resource. - * After loaded, you can acquire them by passing the url to this API. - * @zh - * 根据 ID 获取资源数据。
- * 当使用 [[load]] 或 [[loadRes]] 来加载资源时,
- * URL 将是资源的唯一标识。
- * 在完成加载之后,你可以通过将 URL 传递给此 API 来获取它们。 - * @method getRes - * @param {String} url - * @param {Function} type - 如果提供此参数,则将仅返回此类型的资源。 - * @returns {*} - */ - public getRes (url: string, type?: Function) { - let item = this._cache[url]; - if (!item) { - const uuid = this._getResUuid(url, type, null, true); - if (uuid) { - const ref = this._getReferenceKey(uuid); - item = this._cache[ref]; - } - else { - return null; - } - } - if (item && item.alias) { - item = item.alias; - } - return (item && item.complete) ? item.content : null; - } - - /** - * @en - * Get total resources count in loader. - * @zh - * 获取加载的总资源数量 - */ - public getResCount (): Number { - return Object.keys(this._cache).length; - } - - /** - * @en - * Get all resource dependencies of the requested asset in an array, including itself.
- * The owner parameter accept the following types: 1. The asset itself; 2. The resource url; 3. The asset's uuid.
- * The returned array stores the dependencies with their uuids, after retrieve dependencies,
- * you can release them, access dependent assets by passing the uuid to [[getRes]], or other stuffs you want.
- * For release all dependencies of an asset, please refer to [[release]] - * Here is some examples: - * @zh - * 获取一个指定资源的所有依赖资源,包含它自身,并保存在数组中返回。
- * owner 参数接收以下几种类型:1. 资源 asset 对象;2. 资源目录下的 url;3. 资源的 uuid。
- * 返回的数组将仅保存依赖资源的 uuid,获取这些 uuid 后,你可以从 loader 释放这些资源;通过 [[getRes]] 获取某个资源或者进行其他你需要的操作。
- * 想要释放一个资源及其依赖资源,可以参考 [[release]]。
- * 下面是一些示例代码: - * @example - * ```typescript - * // Release all dependencies of a loaded prefab - * let deps = cc.loader.getDependsRecursively(prefab); - * cc.loader.release(deps); - * // Retrieve all dependent textures - * let deps = cc.loader.getDependsRecursively('prefabs/sample'); - * let textures = []; - * for (let i = 0; i < deps.length; ++i) { - * let item = cc.loader.getRes(deps[i]); - * if (item instanceof cc.Texture2D) { - * textures.push(item); - * } - * } - * ``` - * @method getDependsRecursively - * @param {Asset|RawAsset|String} owner - 资源本身或者是资源的 url 或者是资源的 uuid - * @return {Array} - */ - public getDependsRecursively (owner: Asset|RawAsset|String) { - if (owner) { - const key = this._getReferenceKey(owner); - const assets = getDependsRecursively(key); - assets.push(key); - return assets; - } - else { - return []; - } - } - - /** - * @en - * Release the content of an asset or an array of assets by uuid.
- * Start from v1.3, this method will not only remove the cache of the asset in loader, but also clean up its content.
- * For example, if you release a texture, the texture asset and its gl texture data will be freed up.
- * In complexe project, you can use this function with [[getDependsRecursively]] to free up memory in critical circumstances.
- * Notice, this method may cause the texture to be unusable, if there are still other nodes use the same texture, they may turn to black and report gl errors.
- * If you only want to remove the cache of an asset, please use [[pipeline.removeItem]] - * @zh - * 通过 id(通常是资源 url)来释放一个资源或者一个资源数组。
- * 从 v1.3 开始,这个方法不仅会从 loader 中删除资源的缓存引用,还会清理它的资源内容。
- * 比如说,当你释放一个 texture 资源,这个 texture 和它的 gl 贴图数据都会被释放。
- * 在复杂项目中,我们建议你结合 [[getDependsRecursively]] 来使用,便于在设备内存告急的情况下更快地释放不再需要的资源的内存。
- * 注意,这个函数可能会导致资源贴图或资源所依赖的贴图不可用,如果场景中存在节点仍然依赖同样的贴图,它们可能会变黑并报 GL 错误。
- * 如果你只想删除一个资源的缓存引用,请使用 [[pipeline.removeItem]] - * - * @example - * ```typescript - * // Release a texture which is no longer need - * cc.loader.release(texture); - * // Release all dependencies of a loaded prefab - * let deps = cc.loader.getDependsRecursively('prefabs/sample'); - * cc.loader.release(deps); - * // If there is no instance of this prefab in the scene, the prefab and its dependencies like textures, sprite frames, etc, will be freed up. - * // If you have some other nodes share a texture in this prefab, you can skip it in two ways: - * // 1. Forbid auto release a texture before release - * cc.loader.setAutoRelease(texture2d, false); - * // 2. Remove it from the dependencies array - * let deps = cc.loader.getDependsRecursively('prefabs/sample'); - * let index = deps.indexOf(texture2d._uuid); - * if (index !== -1) - * deps.splice(index, 1); - * cc.loader.release(deps); - * ``` - * @method release - * @param {Asset|RawAsset|String|Array} asset - */ - public release (asset) { - if (Array.isArray(asset)) { - for (let i = 0; i < asset.length; i++) { - const key = asset[i]; - this.release(key); - } - } - else if (asset) { - const id = this._getReferenceKey(asset); - const item = this.getItem(id); - if (item) { - const removed = this.removeItem(id); - asset = item.content; - if (asset instanceof cc.Asset) { - const nativeUrl = asset.nativeUrl; - if (nativeUrl) { - this.release(nativeUrl); // uncache loading item of native asset - } - asset.destroy(); - } - if (CC_DEBUG && removed) { - this._releasedAssetChecker_DEBUG.setReleased(item, id); - } - } - } - } - - /** - * @en Release the asset by its object. Refer to [[release]] for detailed informations. - * @zh 通过资源对象自身来释放资源。详细信息请参考 [[release]] - * - * @method releaseAsset - * @param {Asset} asset - */ - public releaseAsset (asset: Asset) { - const uuid = asset._uuid; - if (uuid) { - this.release(uuid); - } - } - - /** - * @en - * Release the asset loaded by [[loadRes]]. Refer to [[release]] for detailed informations. - * @zh - * 释放通过 [[loadRes]] 加载的资源。详细信息请参考 [[release]] - * - * @method releaseRes - * @param {String} url - * @param {Function} type - 如果提供此参数,则将仅释放此类型的资源。 - */ - public releaseRes (url: String, type?: Function, mount?) { - const uuid = this._getResUuid(url, type, mount, true); - if (uuid) { - this.release(uuid); - } - else { - cc.errorID(4914, url); - } - } - - /** - * @en - * Release the all assets loaded by [[loadResDir]]. Refer to [[release]] for detailed informations. - * @zh - * 释放通过 [[loadResDir]] 加载的资源。详细信息请参考 [[release]] - * - * @method releaseResDir - * @param {String} url - * @param {Function} type - 如果提供此参数,则将仅释放此类型的资源。 - */ - public releaseResDir (url: String, type?: Function, mount?) { - mount = mount || 'assets'; - if (!assetTables[mount]) { return; } - - const uuids = assetTables[mount].getUuidArray(url, type); - for (let i = 0; i < uuids.length; i++) { - const uuid = uuids[i]; - this.release(uuid); - } - } - - /** - * @en Resource all assets. Refer to [[release]] for detailed informations. - * @zh 释放所有资源。详细信息请参考 [[release]] - * - * @method releaseAll - */ - public releaseAll () { - for (const id in this._cache) { - this.release(id); - } - } - - // AUTO RELEASE - - // override - public removeItem (key) { - const removed = Pipeline.prototype.removeItem.call(this, key); - delete this._autoReleaseSetting[key]; - return removed; - } - - /** - * @en - * Indicates whether to release the asset when loading a new scene.
- * By default, when loading a new scene, all assets in the previous scene will be released or preserved
- * according to whether the previous scene checked the "Auto Release Assets" option.
- * On the other hand, assets dynamically loaded by using `cc.loader.loadRes` or `cc.loader.loadResDir`
- * will not be affected by that option, remain not released by default.
- * Use this API to change the default behavior on a single asset, to force preserve or release specified asset when scene switching.
- *
- * See: [[setAutoReleaseRecursively]], [[isAutoRelease]] - * @zh - * 设置当场景切换时是否自动释放资源。
- * 默认情况下,当加载新场景时,旧场景的资源根据旧场景是否勾选“Auto Release Assets”,将会被释放或者保留。
- * 而使用 `cc.loader.loadRes` 或 `cc.loader.loadResDir` 动态加载的资源,则不受场景设置的影响,默认不自动释放。
- * 使用这个 API 可以在单个资源上改变这个默认行为,强制在切换场景时保留或者释放指定资源。
- *
- * 参考:[[setAutoReleaseRecursively]],[[isAutoRelease]] - * - * @example - * ```typescript - * // auto release the texture event if "Auto Release Assets" disabled in current scene - * cc.loader.setAutoRelease(texture2d, true); - * // don't release the texture even if "Auto Release Assets" enabled in current scene - * cc.loader.setAutoRelease(texture2d, false); - * // first parameter can be url - * cc.loader.setAutoRelease(audioUrl, false); - * ``` - * @method setAutoRelease - * @param {Asset|String} assetOrUrlOrUuid - 资源对象或原始资源的 URL 或是 UUID - * @param {Boolean} autoRelease - 表示是否自动释放 - */ - public setAutoRelease (assetOrUrlOrUuid: Asset|String, autoRelease: Boolean) { - const key = this._getReferenceKey(assetOrUrlOrUuid); - if (key) { - this._autoReleaseSetting[key] = !!autoRelease; - } - else if (CC_DEV) { - cc.warnID(4902); - } - } - - /** - * @en - * Indicates whether to release the asset and its referenced other assets when loading a new scene.
- * By default, when loading a new scene, all assets in the previous scene will be released or preserved
- * according to whether the previous scene checked the "Auto Release Assets" option.
- * On the other hand, assets dynamically loaded by using `cc.loader.loadRes` or `cc.loader.loadResDir`
- * will not be affected by that option, remain not released by default.
- * Use this API to change the default behavior on the specified asset and its recursively referenced assets, to force preserve or release specified asset when scene switching.
- *
- * See: [[setAutoRelease]], [[isAutoRelease]] - * @zh - * 设置当场景切换时是否自动释放资源及资源引用的其它资源。
- * 默认情况下,当加载新场景时,旧场景的资源根据旧场景是否勾选“Auto Release Assets”,将会被释放或者保留。
- * 而使用 `cc.loader.loadRes` 或 `cc.loader.loadResDir` 动态加载的资源,则不受场景设置的影响,默认不自动释放。
- * 使用这个 API 可以在指定资源及资源递归引用到的所有资源上改变这个默认行为,强制在切换场景时保留或者释放指定资源。
- *
- * 参考:[[setAutoRelease]],[[isAutoRelease]] - * - * @example - * ```typescript - * // auto release the SpriteFrame and its Texture event if "Auto Release Assets" disabled in current scene - * cc.loader.setAutoReleaseRecursively(spriteFrame, true); - * // don't release the SpriteFrame and its Texture even if "Auto Release Assets" enabled in current scene - * cc.loader.setAutoReleaseRecursively(spriteFrame, false); - * // don't release the Prefab and all the referenced assets - * cc.loader.setAutoReleaseRecursively(prefab, false); - * ``` - * @method setAutoReleaseRecursively - * @param {Asset|String} assetOrUrlOrUuid - 资源对象或原始资源的 URL 或是 UUID - * @param {Boolean} autoRelease - 表示是否自动释放 - */ - public setAutoReleaseRecursively (assetOrUrlOrUuid: Asset|String, autoRelease: Boolean) { - autoRelease = !!autoRelease; - const key = this._getReferenceKey(assetOrUrlOrUuid); - if (key) { - this._autoReleaseSetting[key] = autoRelease; - - const depends = getDependsRecursively(key); - for (let i = 0; i < depends.length; i++) { - const depend = depends[i]; - this._autoReleaseSetting[depend] = autoRelease; - } - } - else if (CC_DEV) { - cc.warnID(4902); - } - } - - /** - * @en - * Returns whether the asset is configured as auto released, despite how "Auto Release Assets" property is set on scene asset.
- *
- * See: [[setAutoRelease]], [[setAutoReleaseRecursively]] - * - * @zh - * 返回指定的资源是否有被设置为自动释放,不论场景的“Auto Release Assets”如何设置。
- *
- * 参考:[[setAutoRelease]],[[setAutoReleaseRecursively]] - * @method isAutoRelease - * @param {Asset|String} assetOrUrl - asset object or the raw asset's url - * @returns {Boolean} - */ - public isAutoRelease (assetOrUrl: Asset|String): Boolean{ - const key = this._getReferenceKey(assetOrUrl); - if (key) { - return !!this._autoReleaseSetting[key]; - } - return false; - } - - /** - * @zh - * 获取资源的 uuid - */ - public _getResUuid (url, type, mount, quiet) { - mount = mount || 'assets'; - const assetTable = assetTables[mount]; - if (!url || !assetTable) { - return null; - } - // Ignore parameter - const index = url.indexOf('?'); - if (index !== -1) { - url = url.substr(0, index); - } - let uuid = assetTable.getUuid(url, type); - if (!uuid) { - const extname = cc.path.extname(url); - if (extname) { - // strip extname - url = url.slice(0, - extname.length); - uuid = assetTable.getUuid(url, type); - if (uuid && !quiet) { - cc.warnID(4901, url, extname); - } - } - } - return uuid; - } - - /** - * @en - * Find the asset's reference id in loader, asset could be asset object, asset uuid or asset url - * @zh - * 在 laoder 中找到资源的引用 id ,参数可以是资源对象、资源的 uuid 或者是资源的 url - */ - public _getReferenceKey (assetOrUrlOrUuid) { - let key; - if (typeof assetOrUrlOrUuid === 'object') { - key = assetOrUrlOrUuid._uuid || null; - } - else if (typeof assetOrUrlOrUuid === 'string') { - key = this._getResUuid(assetOrUrlOrUuid, null, null, true) || assetOrUrlOrUuid; - } - if (!key) { - cc.warnID(4800, assetOrUrlOrUuid); - return key; - } - cc.AssetLibrary._getAssetInfoInRuntime(key, _info); - return this._cache[_info.url!] ? _info.url : key; - } - - /** - * @zh - * 当 url 未找到时的操作 - */ - private _urlNotFound (url, type, completeCallback) { - callInNextTick(() => { - url = cc.url.normalize(url); - const info = `${type ? getClassName(type) : 'Asset'} in "resources/${url}" does not exist.`; - if (completeCallback) { - completeCallback(new Error(info), []); - } - }); - } - - /** - * @param {Function} [type] - * @param {Function} [onProgress] - * @param {Function} onComplete - * @returns {Object} arguments - * @returns {Function} arguments.type - * @returns {Function} arguments.onProgress - * @returns {Function} arguments.onComplete - */ - private _parseLoadResArgs (type, onProgress, onComplete) { - if (onComplete === undefined) { - const isValidType = isChildClassOf(type, cc.RawAsset); - if (onProgress) { - onComplete = onProgress; - if (isValidType) { - onProgress = this.onProgress || null; - } - } - else if (onProgress === undefined && !isValidType) { - onComplete = type; - onProgress = this.onProgress || null; - type = null; - } - if (onProgress !== undefined && !isValidType) { - onProgress = type; - type = null; - } - } - return { - type, - onProgress, - onComplete, - }; - } - - /** - * @zh - * 通过uuids 加载资源 - */ - private _loadResUuids (uuids, progressCallback, completeCallback, urls ?) { - if (uuids.length > 0) { - const self = this; - const res = uuids.map((uuid) => { - return { - type: 'uuid', - uuid, - }; - }); - this.load(res, progressCallback, (errors, items) => { - if (completeCallback) { - const assetRes: any = []; - const urlRes: any = urls && []; - for (let i = 0; i < res.length; ++i) { - const uuid = res[i].uuid; - const id = self._getReferenceKey(uuid); - const item = items.getContent(id); - if (item) { - // should not release these assets, even if they are static referenced in the scene. - self.setAutoReleaseRecursively(uuid, false); - assetRes.push(item); - if (urlRes) { - urlRes.push(urls[i]); - } - } - } - if (urls) { - completeCallback(errors, assetRes, urlRes); - } - else { - completeCallback(errors, assetRes); - } - } - }); - } - else { - if (completeCallback) { - callInNextTick( () => { - if (urls) { - completeCallback(null, [], []); - } - else { - completeCallback(null, []); - } - }); - } - } - } -} - -const loader = cc.loader = new CCLoader(); - -if (CC_EDITOR) { - cc.loader.refreshUrl = function (uuid, oldUrl, newUrl) { - let item = this._cache[uuid]; - if (item) { - item.url = newUrl; - } - - item = this._cache[oldUrl]; - if (item) { - item.id = newUrl; - item.url = newUrl; - this._cache[newUrl] = item; - delete this._cache[oldUrl]; - } - }; -} - -export default loader; +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +import { Asset, RawAsset } from '../assets'; +import { createMap, getClassName, isChildClassOf } from '../core/utils/js'; +import { callInNextTick } from '../core/utils/misc'; +import AssetLoader from './asset-loader'; +import AssetTable from './asset-table'; +import { getDependsRecursively } from './auto-release-utils'; +import Downloader from './downloader'; +import Loader from './loader'; +import LoadingItems from './loading-items'; +import Pipeline from './pipeline'; +import ReleasedAssetChecker from './released-asset-checker'; + +const assetTables = Object.create(null); +assetTables.assets = new AssetTable(); +assetTables.internal = new AssetTable(); + +function getXMLHttpRequest () { + return window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('MSXML2.XMLHTTP'); +} + +const _info = { url: null, raw: false }; + +/** + * @en + * Convert a resources by finding its real url with uuid, otherwise we will use the uuid or raw url as its url
+ * So we gurantee there will be url in result + * @zh + * 通过使用 uuid 查找资源的真实 url 来转换资源,否则将使用 uuid 或原始 url 作为其 url
+ * 所以可以保证结果中会有 url + * @param res + */ +function getResWithUrl (res) { + let id; + let result; + let isUuid; + if (typeof res === 'object') { + result = res; + if (res.url) { + return result; + } + else { + id = res.uuid; + } + } + else { + result = {}; + id = res; + } + isUuid = result.type ? result.type === 'uuid' : cc.AssetLibrary._uuidInSettings(id); + cc.AssetLibrary._getAssetInfoInRuntime(id, _info); + result.url = !isUuid ? id : _info.url; + if (_info.url && result.type === 'uuid' && _info.raw) { + result.type = null; + result.isRawAsset = true; + } + else if (!isUuid) { + result.isRawAsset = true; + } + return result; +} + +const _sharedResources: any = []; +const _sharedList: any = []; + +/** + * @en + * Loader for resource loading process. It's a singleton object. + * @zh + * 资源加载程序,这是一个单例对象。 + * @class loader + * @extends Pipeline + * @static + */ +class CCLoader extends Pipeline { + + /** + * @en + * Gets a new XMLHttpRequest instance. + * @zh + * 获取一个新的 XMLHttpRequest 的实例。 + * @method getXMLHttpRequest + * @returns {XMLHttpRequest} + */ + public getXMLHttpRequest: Function; + + /** + * @en + * The asset loader in cc.loader's pipeline, it's by default the first pipe.
+ * It's used to identify an asset's type, and determine how to download it. + * @zh + * cc.loader 中的资源加载器,默认情况下是最先加载的。
+ * 用于标识资源的类型,并确定如何加载此资源。 + */ + public assetLoader: AssetLoader; + + /** + * @en + * The md5 pipe in cc.loader's pipeline, it could be absent if the project isn't build with md5 option.
+ * It's used to modify the url to the real downloadable url with md5 suffix. + * @zh + * cc.loader 中的 md5 加载管道,如果项目没有使用 md5 构建,则此项可能不存在。
+ * 用于修改带有 md5 后缀的真实可下载的 URL 。 + */ + public md5Pipe: null; + + /** + * @en + * The downloader in cc.loader's pipeline, it's by default the second pipe.
+ * It's used to download files with several handlers: pure text, image, script, audio, font, uuid.
+ * You can add your own download function with addDownloadHandlers + * @zh + * cc.loader 中的资源下载程序,默认情况下是第二个加载的。
+ * 它用于下载带有多个处理程序的文件:纯文本,图像,脚本,音频,字体,uuid。
+ * 您可以使用 addDownloadHandlers 来添加自己的下载函数 + */ + public downloader: Downloader; + + /** + * @en + * The loader in cc.loader's pipeline, it's by default the third pipe.
+ * It's used to parse downloaded content with several handlers: JSON, image, plist, fnt, uuid.
+ * You can add your own download function with addLoadHandlers + * @zh + * cc.loader 中的资源下载程序,默认情况下是第三个加载的。
+ * 它用于解析下载的内容及多个处理程序的文件:纯文本,图像,脚本,音频,字体,uuid。
+ * 您可以使用 addLoadHandlers 来添加自己的下载函数 + */ + public loader: Loader; + public onProgress: null; + public _assetTables: any; + + private _autoReleaseSetting: any; + private _releasedAssetChecker_DEBUG: any; + + constructor () { + + const assetLoader = new AssetLoader(); + const downloader = new Downloader(); + // tslint:disable-next-line: no-shadowed-variable + const loader = new Loader(); + + super([ + assetLoader, + downloader, + loader, + ]); + + this.getXMLHttpRequest = getXMLHttpRequest; + + this.assetLoader = assetLoader; + + this.md5Pipe = null; + + this.downloader = downloader; + + this.loader = loader; + + this.onProgress = null; + + this._assetTables = assetTables; + // assets to release automatically + this._autoReleaseSetting = createMap(true); + + if (CC_DEBUG) { + this._releasedAssetChecker_DEBUG = new ReleasedAssetChecker(); + } + } + + public init (director) { + if (CC_DEBUG) { + const self = this; + director.on(cc.Director.EVENT_AFTER_UPDATE, () => { + self._releasedAssetChecker_DEBUG.checkCouldRelease(self._cache); + }); + } + } + + /** + * @en + * Add custom supported types handler or modify existing type handler for download process. + * @zh + * 为下载程序添加自定义支持的类型处理程序或修改现有的类型处理程序。 + * @example + * ```typescript + * cc.loader.addDownloadHandlers({ + * // This will match all url with `.scene` extension or all url with `scene` type + * 'scene' : function (url, callback) {} + * }); + * ``` + * @param extMap 具有相应处理程序的自定义支持类型 + */ + public addDownloadHandlers (extMap: Object) { + this.downloader.addHandlers(extMap); + } + + /** + * @en + * Add custom supported types handler or modify existing type handler for load process. + * @zh + * 为加载程序添加自定义支持的类型处理程序或修改现有的类型处理程序。 + * @example + * ```typescript + * cc.loader.addLoadHandlers({ + * // This will match all url with `.scene` extension or all url with `scene` type + * 'scene' : function (url, callback) {} + * }); + * ``` + * @method addLoadHandlers + * @param extMap 具有相应处理程序的自定义支持类型 + */ + public addLoadHandlers (extMap: Object) { + this.loader.addHandlers(extMap); + } + + // tslint:disable: max-line-length + + // load(resources: string|string[]|{uuid?: string, url?: string, type?: string}, completeCallback?: Function): void + // load(resources: string|string[]|{uuid?: string, url?: string, type?: string}, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: Function|null): void + /** + * @en + * Load resources with a progression callback and a complete callback.
+ * The progression callback is the same as Pipeline's [[LoadingItems.onProgress]]
+ * The complete callback is almost the same as Pipeline's [[LoadingItems.onComplete]]
+ * The only difference is when user pass a single url as resources, the complete callback will set its result directly as the second parameter. + * @zh + * 使用进度回调和完整回调加载资源。
+     * 进度回调与 Pipeline 的 [[LoadingItems.onProgress]] 相同
+     * 完整的回调与 Pipeline 的 [[LoadingItems.onComplete]] 几乎相同
+     * 唯一的区别是当用户将单个 URL 作为资源传递时,完整的回调将其结果直接设置为第二个参数。 + * @example + * ```TypeScript + * cc.loader.load('a.png', function (err, tex) { + * cc.log('Result should be a texture: ' + (tex instanceof cc.Texture2D)); + * }); + * + * cc.loader.load('http://example.com/a.png', function (err, tex) { + * cc.log('Should load a texture from external url: ' + (tex instanceof cc.Texture2D)); + * }); + * + * cc.loader.load({url: 'http://example.com/getImageREST?file=a.png', type: 'png'}, function (err, tex) { + * cc.log('Should load a texture from RESTful API by specify the type: ' + (tex instanceof cc.Texture2D)); + * }); + * + * cc.loader.load(['a.png', 'b.json'], function (errors, results) { + * if (errors) { + * for (var i = 0; i < errors.length; i++) { + * cc.log('Error url [' + errors[i] + ']: ' + results.getError(errors[i])); + * } + * } + * var aTex = results.getContent('a.png'); + * var bJsonObj = results.getContent('b.json'); + * }); + * ``` + * @method load + * @param {String|String[]|Object} resources - Url 列表数组 + * @param {Function} progressCallback - 当进度改变时调用的回调函数 + * @param {Number} progressCallback.completedCount - The number of the items that are already completed + * @param {Number} progressCallback.totalCount - The total number of the items + * @param {Object} progressCallback.item - The latest item which flow out the pipeline + * @param {Function} completeCallback - 当所有资源加载完毕后调用的回调函数 + */ + public load (resources, progressCallback, completeCallback?) { + if (CC_DEV && !resources) { + return cc.error('[cc.loader.load] resources must be non-nil.'); + } + + if (completeCallback === undefined) { + completeCallback = progressCallback; + progressCallback = this.onProgress || null; + } + + const self = this; + let singleRes: Boolean = false; + let res; + if (!(resources instanceof Array)) { + if (resources) { + singleRes = true; + resources = [resources]; + } else { + resources = []; + } + } + + _sharedResources.length = 0; + // tslint:disable-next-line: prefer-for-of + for (let i = 0; i < resources.length; ++i) { + const resource = resources[i]; + // Backward compatibility + if (resource && resource.id) { + cc.warnID(4920, resource.id); + if (!resource.uuid && !resource.url) { + resource.url = resource.id; + } + } + res = getResWithUrl(resource); + if (!res.url && !res.uuid) { + continue; + } + const item = this._cache[res.url]; + _sharedResources.push(item || res); + } + + const queue = LoadingItems.create(this, progressCallback, (errors, items) => { + callInNextTick(() => { + if (completeCallback) { + if (singleRes) { + const id = res.url; + completeCallback.call(self, items.getError(id), items.getContent(id)); + } + else { + completeCallback.call(self, errors, items); + } + completeCallback = null; + } + + items.destroy(); + }); + }); + LoadingItems.initQueueDeps(queue); + queue.append(_sharedResources); + _sharedResources.length = 0; + } + + /** + * @en + * See: [[Pipeline.flowInDeps]] + * @zh + * 参考:[[Pipeline.flowInDeps]] + */ + public flowInDeps (owner, urlList, callback) { + _sharedList.length = 0; + // tslint:disable-next-line: prefer-for-of + for (let i = 0; i < urlList.length; ++i) { + const res = getResWithUrl(urlList[i]); + if (!res.url && !res.uuid) { + continue; + } + const item = this._cache[res.url]; + if (item) { + _sharedList.push(item); + } + else { + _sharedList.push(res); + } + } + // @ts-ignore + const queue = LoadingItems.create(this, owner ? (completedCount, totalCount, item) => { + if (queue._ownerQueue && queue._ownerQueue.onProgress) { + queue._ownerQueue._childOnProgress(item); + } + } : null, (errors, items) => { + callback(errors, items); + // Clear deps because it's already done + // Each item will only flowInDeps once, so it's still safe here + if (owner && owner.deps) { + owner.deps.length = 0; + } + items.destroy(); + }); + if (owner) { + const ownerQueue = LoadingItems.getQueue(owner); + // Set the root ownerQueue, if no ownerQueue defined in ownerQueue, it's the root + queue._ownerQueue = ownerQueue._ownerQueue || ownerQueue; + } + const accepted = queue.append(_sharedList, owner); + _sharedList.length = 0; + return accepted; + } + + // @typescript + // loadRes(url: string, type: typeof cc.Asset, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any) => void)|null): void + // loadRes(url: string, type: typeof cc.Asset, completeCallback: (error: Error, resource: any) => void): void + // loadRes(url: string, type: typeof cc.Asset): void + // loadRes(url: string, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any) => void)|null): void + // loadRes(url: string, completeCallback: (error: Error, resource: any) => void): void + // loadRes(url: string): void + + /** + * @en + * Load resources from the "resources" folder inside the "assets" folder of your project.
+ *
+ * Note: All asset URLs in Creator use forward slashes, URLs using backslashes will not work. + * @zh + * 从项目的 “assets” 文件夹下的 “resources” 文件夹中加载资源
+ *
+ * 注意:Creator 中的所有资源 URL 都使用正斜杠,使用反斜杠的 URL 将不起作用。 + * @method loadRes + * @param {String} url - 目标资源的 URL
+ * URl 相对于 “resources” 文件夹,必须省略文件扩展名。 + * @param {Function} type - 如果提供此参数,则将仅加载此类型的资源。 + * @param {Function} progressCallback - 当进度改变时调用的回调函数 + * @param {Number} progressCallback.completedCount - The number of the items that are already completed. + * @param {Number} progressCallback.totalCount - The total number of the items. + * @param {Object} progressCallback.item - The latest item which flow out the pipeline. + * @param {Function} completeCallback - 当所有资源加载完毕后调用的回调函数 + * @param {Error} completeCallback.error - The error info or null if loaded successfully. + * @param {Object} completeCallback.resource - The loaded resource if it can be found otherwise returns null. + * + * @example + * ```typescript + * // load the prefab (project/assets/resources/misc/character/cocos) from resources folder + * cc.loader.loadRes('misc/character/cocos', function (err, prefab) { + * if (err) { + * cc.error(err.message || err); + * return; + * } + * cc.log('Result should be a prefab: ' + (prefab instanceof cc.Prefab)); + * }); + * + * // load the sprite frame of (project/assets/resources/imgs/cocos.png) from resources folder + * cc.loader.loadRes('imgs/cocos', cc.SpriteFrame, function (err, spriteFrame) { + * if (err) { + * cc.error(err.message || err); + * return; + * } + * cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame)); + * }); + * ``` + */ + public loadRes (url: String, type: Function, mount, progressCallback: Function, completeCallback: Function) { + if (arguments.length !== 5) { + completeCallback = progressCallback; + progressCallback = mount; + mount = 'assets'; + } + const args = this._parseLoadResArgs(type, progressCallback, completeCallback); + type = args.type; + progressCallback = args.onProgress; + completeCallback = args.onComplete; + const self = this; + const uuid = self._getResUuid(url, type, mount, true); + if (uuid) { + this.load( + { + type: 'uuid', + uuid, + }, + progressCallback, + (err, asset) => { + if (asset) { + // should not release these assets, even if they are static referenced in the scene. + self.setAutoReleaseRecursively(uuid, false); + } + if (completeCallback) { + completeCallback(err, asset); + } + }); + } + else { + self._urlNotFound(url, type, completeCallback); + } + } + + // @typescript + // loadResDir(url: string, type: typeof cc.Asset, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[], urls: string[]) => void)|null): void + // loadResDir(url: string, type: typeof cc.Asset, completeCallback: (error: Error, resource: any[], urls: string[]) => void): void + // loadResDir(url: string, type: typeof cc.Asset): void + // loadResDir(url: string, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[], urls: string[]) => void)|null): void + // loadResDir(url: string, completeCallback: (error: Error, resource: any[], urls: string[]) => void): void + // loadResDir(url: string): void + + /** + * @en + * Load all assets in a folder inside the "assets/resources" folder of your project.
+ *
+ * Note: All asset URLs in Creator use forward slashes, URLs using backslashes will not work. + * @zh + * 将所有资产加载到项目 “assets / resources” 文件夹中 +     *
+ * 注意:Creator 中的所有资源 URL 都使用正斜杠,使用反斜杠的 URL 将不起作用。 + * @method loadResDir + * @param {String} url - 目标文件夹的 URL
+ * URl 相对于 “resources” 文件夹,必须省略文件扩展名。 + * @param {Function} type - 如果提供此参数,则将仅加载此类型的资源。 + * @param {Function} progressCallback - 当进度改变时调用的回调函数 + * @param {Number} progressCallback.completedCount - The number of the items that are already completed. + * @param {Number} progressCallback.totalCount - The total number of the items. + * @param {Object} progressCallback.item - The latest item which flow out the pipeline. + * @param {Function} completeCallback - 当所有资源加载完毕后或者发生错误时调用的回调函数 + * @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called + * with the error. If all assets are loaded successfully, error will be null. + * @param {Asset[]|Array} completeCallback.assets - An array of all loaded assets. + * If nothing to load, assets will be an empty array. + * @param {String[]} completeCallback.urls - An array that lists all the URLs of loaded assets. + * + * @example + * ```typescript + * // load the texture (resources/imgs/cocos.png) and the corresponding sprite frame + * cc.loader.loadResDir('imgs/cocos', function (err, assets) { + * if (err) { + * cc.error(err); + * return; + * } + * var texture = assets[0]; + * var spriteFrame = assets[1]; + * }); + * + * // load all textures in "resources/imgs/" + * cc.loader.loadResDir('imgs', cc.Texture2D, function (err, textures) { + * var texture1 = textures[0]; + * var texture2 = textures[1]; + * }); + * + * // load all JSONs in "resources/data/" + * cc.loader.loadResDir('data', function (err, objects, urls) { + * var data = objects[0]; + * var url = urls[0]; + * }); + * ``` + */ + public loadResDir (url: String, type?: Function, mount?, progressCallback?: Function, completeCallback?: Function) { + if (arguments.length !== 5) { + completeCallback = progressCallback; + progressCallback = mount; + mount = 'assets'; + } + + if (!assetTables[mount]) { return; } + + const args = this._parseLoadResArgs(type, progressCallback, completeCallback); + type = args.type; + progressCallback = args.onProgress; + completeCallback = args.onComplete; + + const urls = []; + const uuids = assetTables[mount].getUuidArray(url, type, urls); + this._loadResUuids(uuids, progressCallback, (errors, assetRes, urlRes) => { + // The spriteFrame url in spriteAtlas will be removed after build project + // To show users the exact structure in asset panel, we need to return the spriteFrame assets in spriteAtlas + const assetResLength = assetRes.length; + for (let i = 0; i < assetResLength; ++i) { + if (assetRes[i] instanceof cc.SpriteAtlas) { + const spriteFrames = assetRes[i].getSpriteFrames(); + // tslint:disable: forin + for (const k in spriteFrames) { + const sf = spriteFrames[k]; + assetRes.push(sf); + if (urlRes) { + urlRes.push(`${urlRes[i]}/${sf.name}`); + } + } + } + } + if (completeCallback) { + completeCallback(errors, assetRes, urlRes); + } + }, urls); + } + + // * @typescript + // * loadResArray(url: string[], type: typeof cc.Asset, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[]) => void)|null): void + // * loadResArray(url: string[], type: typeof cc.Asset, completeCallback: (error: Error, resource: any[]) => void): void + // * loadResArray(url: string[], type: typeof cc.Asset): void + // * loadResArray(url: string[], progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[]) => void)|null): void + // * loadResArray(url: string[], completeCallback: (error: Error, resource: any[]) => void): void + // * loadResArray(url: string[]): void + /** + * @en + * This method is like [[loadRes]] except that it accepts array of url. + * @zh + * 此方法除了接受 URL 数组参数外,与 [[loadRes]] 方法相同。 + * + * @method loadResArray + * @param {String[]} urls - 目标资源的 URL 数组。 + * URl 为相对于 “resources” 文件夹的,且必须省略文件扩展名。 + * @param {Function} type - 如果提供此参数,则将仅加载此类型的资源。 + * @param {Function} progressCallback - 当进度改变时调用的回调函数 + * @param {Number} progressCallback.completedCount - The number of the items that are already completed. + * @param {Number} progressCallback.totalCount - The total number of the items. + * @param {Object} progressCallback.item - The latest item which flow out the pipeline. + * @param {Function} completeCallback - 当所有资源加载完毕后或者发生错误时调用的回调函数 + * @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called + * with the error. If all assets are loaded successfully, error will be null. + * @param {Asset[]|Array} completeCallback.assets - An array of all loaded assets. + * If nothing to load, assets will be an empty array. + * @example + * ```typescript + * // load the SpriteFrames from resources folder + * let spriteFrames; + * let urls = ['misc/characters/character_01', 'misc/weapons/weapons_01']; + * cc.loader.loadResArray(urls, cc.SpriteFrame, function (err, assets) { + * if (err) { + * cc.error(err); + * return; + * } + * spriteFrames = assets; + * // ... + * }); + * ``` + */ + public loadResArray (urls: String[], type?: Function, mount?, progressCallback?: Function, completeCallback?: Function) { + if (arguments.length !== 5) { + completeCallback = progressCallback; + progressCallback = mount; + mount = 'assets'; + } + + const args = this._parseLoadResArgs(type, progressCallback, completeCallback); + type = args.type; + progressCallback = args.onProgress; + completeCallback = args.onComplete; + + const uuids: any = []; + // tslint:disable: prefer-for-of + for (let i = 0; i < urls.length; i++) { + const url = urls[i]; + const uuid = this._getResUuid(url, type, mount, true); + if (uuid) { + uuids.push(uuid); + } + else { + this._urlNotFound(url, type, completeCallback); + return; + } + } + this._loadResUuids(uuids, progressCallback, completeCallback); + } + + /** + * @en + * Get resource data by id.
+ * When you load resources with [[load]] or [[loadRes]], + * the url will be the unique identity of the resource. + * After loaded, you can acquire them by passing the url to this API. + * @zh + * 根据 ID 获取资源数据。
+ * 当使用 [[load]] 或 [[loadRes]] 来加载资源时,
+ * URL 将是资源的唯一标识。
+ * 在完成加载之后,你可以通过将 URL 传递给此 API 来获取它们。 + * @method getRes + * @param {String} url + * @param {Function} type - 如果提供此参数,则将仅返回此类型的资源。 + * @returns {*} + */ + public getRes (url: string, type?: Function): T | null { + let item = this._cache[url]; + if (!item) { + const uuid = this._getResUuid(url, type, null, true); + if (uuid) { + const ref = this._getReferenceKey(uuid); + item = this._cache[ref]; + } + else { + return null; + } + } + if (item && item.alias) { + item = item.alias; + } + return (item && item.complete) ? item.content : null; + } + + /** + * @en + * Get total resources count in loader. + * @zh + * 获取加载的总资源数量 + */ + public getResCount (): Number { + return Object.keys(this._cache).length; + } + + /** + * @en + * Get all resource dependencies of the requested asset in an array, including itself.
+ * The owner parameter accept the following types: 1. The asset itself; 2. The resource url; 3. The asset's uuid.
+ * The returned array stores the dependencies with their uuids, after retrieve dependencies,
+ * you can release them, access dependent assets by passing the uuid to [[getRes]], or other stuffs you want.
+ * For release all dependencies of an asset, please refer to [[release]] + * Here is some examples: + * @zh + * 获取一个指定资源的所有依赖资源,包含它自身,并保存在数组中返回。
+ * owner 参数接收以下几种类型:1. 资源 asset 对象;2. 资源目录下的 url;3. 资源的 uuid。
+ * 返回的数组将仅保存依赖资源的 uuid,获取这些 uuid 后,你可以从 loader 释放这些资源;通过 [[getRes]] 获取某个资源或者进行其他你需要的操作。
+ * 想要释放一个资源及其依赖资源,可以参考 [[release]]。
+ * 下面是一些示例代码: + * @example + * ```typescript + * // Release all dependencies of a loaded prefab + * let deps = cc.loader.getDependsRecursively(prefab); + * cc.loader.release(deps); + * // Retrieve all dependent textures + * let deps = cc.loader.getDependsRecursively('prefabs/sample'); + * let textures = []; + * for (let i = 0; i < deps.length; ++i) { + * let item = cc.loader.getRes(deps[i]); + * if (item instanceof cc.Texture2D) { + * textures.push(item); + * } + * } + * ``` + * @method getDependsRecursively + * @param {Asset|RawAsset|String} owner - 资源本身或者是资源的 url 或者是资源的 uuid + * @return {Array} + */ + public getDependsRecursively (owner: Asset|RawAsset|String) { + if (owner) { + const key = this._getReferenceKey(owner); + const assets = getDependsRecursively(key); + assets.push(key); + return assets; + } + else { + return []; + } + } + + /** + * @en + * Release the content of an asset or an array of assets by uuid.
+ * Start from v1.3, this method will not only remove the cache of the asset in loader, but also clean up its content.
+ * For example, if you release a texture, the texture asset and its gl texture data will be freed up.
+ * In complexe project, you can use this function with [[getDependsRecursively]] to free up memory in critical circumstances.
+ * Notice, this method may cause the texture to be unusable, if there are still other nodes use the same texture, they may turn to black and report gl errors.
+ * If you only want to remove the cache of an asset, please use [[pipeline.removeItem]] + * @zh + * 通过 id(通常是资源 url)来释放一个资源或者一个资源数组。
+ * 从 v1.3 开始,这个方法不仅会从 loader 中删除资源的缓存引用,还会清理它的资源内容。
+ * 比如说,当你释放一个 texture 资源,这个 texture 和它的 gl 贴图数据都会被释放。
+ * 在复杂项目中,我们建议你结合 [[getDependsRecursively]] 来使用,便于在设备内存告急的情况下更快地释放不再需要的资源的内存。
+ * 注意,这个函数可能会导致资源贴图或资源所依赖的贴图不可用,如果场景中存在节点仍然依赖同样的贴图,它们可能会变黑并报 GL 错误。
+ * 如果你只想删除一个资源的缓存引用,请使用 [[pipeline.removeItem]] + * + * @example + * ```typescript + * // Release a texture which is no longer need + * cc.loader.release(texture); + * // Release all dependencies of a loaded prefab + * let deps = cc.loader.getDependsRecursively('prefabs/sample'); + * cc.loader.release(deps); + * // If there is no instance of this prefab in the scene, the prefab and its dependencies like textures, sprite frames, etc, will be freed up. + * // If you have some other nodes share a texture in this prefab, you can skip it in two ways: + * // 1. Forbid auto release a texture before release + * cc.loader.setAutoRelease(texture2d, false); + * // 2. Remove it from the dependencies array + * let deps = cc.loader.getDependsRecursively('prefabs/sample'); + * let index = deps.indexOf(texture2d._uuid); + * if (index !== -1) + * deps.splice(index, 1); + * cc.loader.release(deps); + * ``` + * @method release + * @param {Asset|RawAsset|String|Array} asset + */ + public release (asset) { + if (Array.isArray(asset)) { + for (let i = 0; i < asset.length; i++) { + const key = asset[i]; + this.release(key); + } + } + else if (asset) { + const id = this._getReferenceKey(asset); + const item = this.getItem(id); + if (item) { + const removed = this.removeItem(id); + asset = item.content; + if (asset instanceof cc.Asset) { + const nativeUrl = asset.nativeUrl; + if (nativeUrl) { + this.release(nativeUrl); // uncache loading item of native asset + } + asset.destroy(); + } + if (CC_DEBUG && removed) { + this._releasedAssetChecker_DEBUG.setReleased(item, id); + } + } + } + } + + /** + * @en Release the asset by its object. Refer to [[release]] for detailed informations. + * @zh 通过资源对象自身来释放资源。详细信息请参考 [[release]] + * + * @method releaseAsset + * @param {Asset} asset + */ + public releaseAsset (asset: Asset) { + const uuid = asset._uuid; + if (uuid) { + this.release(uuid); + } + } + + /** + * @en + * Release the asset loaded by [[loadRes]]. Refer to [[release]] for detailed informations. + * @zh + * 释放通过 [[loadRes]] 加载的资源。详细信息请参考 [[release]] + * + * @method releaseRes + * @param {String} url + * @param {Function} type - 如果提供此参数,则将仅释放此类型的资源。 + */ + public releaseRes (url: String, type?: Function, mount?) { + const uuid = this._getResUuid(url, type, mount, true); + if (uuid) { + this.release(uuid); + } + else { + cc.errorID(4914, url); + } + } + + /** + * @en + * Release the all assets loaded by [[loadResDir]]. Refer to [[release]] for detailed informations. + * @zh + * 释放通过 [[loadResDir]] 加载的资源。详细信息请参考 [[release]] + * + * @method releaseResDir + * @param {String} url + * @param {Function} type - 如果提供此参数,则将仅释放此类型的资源。 + */ + public releaseResDir (url: String, type?: Function, mount?) { + mount = mount || 'assets'; + if (!assetTables[mount]) { return; } + + const uuids = assetTables[mount].getUuidArray(url, type); + for (let i = 0; i < uuids.length; i++) { + const uuid = uuids[i]; + this.release(uuid); + } + } + + /** + * @en Resource all assets. Refer to [[release]] for detailed informations. + * @zh 释放所有资源。详细信息请参考 [[release]] + * + * @method releaseAll + */ + public releaseAll () { + for (const id in this._cache) { + this.release(id); + } + } + + // AUTO RELEASE + + // override + public removeItem (key) { + const removed = Pipeline.prototype.removeItem.call(this, key); + delete this._autoReleaseSetting[key]; + return removed; + } + + /** + * @en + * Indicates whether to release the asset when loading a new scene.
+ * By default, when loading a new scene, all assets in the previous scene will be released or preserved
+ * according to whether the previous scene checked the "Auto Release Assets" option.
+ * On the other hand, assets dynamically loaded by using `cc.loader.loadRes` or `cc.loader.loadResDir`
+ * will not be affected by that option, remain not released by default.
+ * Use this API to change the default behavior on a single asset, to force preserve or release specified asset when scene switching.
+ *
+ * See: [[setAutoReleaseRecursively]], [[isAutoRelease]] + * @zh + * 设置当场景切换时是否自动释放资源。
+ * 默认情况下,当加载新场景时,旧场景的资源根据旧场景是否勾选“Auto Release Assets”,将会被释放或者保留。
+ * 而使用 `cc.loader.loadRes` 或 `cc.loader.loadResDir` 动态加载的资源,则不受场景设置的影响,默认不自动释放。
+ * 使用这个 API 可以在单个资源上改变这个默认行为,强制在切换场景时保留或者释放指定资源。
+ *
+ * 参考:[[setAutoReleaseRecursively]],[[isAutoRelease]] + * + * @example + * ```typescript + * // auto release the texture event if "Auto Release Assets" disabled in current scene + * cc.loader.setAutoRelease(texture2d, true); + * // don't release the texture even if "Auto Release Assets" enabled in current scene + * cc.loader.setAutoRelease(texture2d, false); + * // first parameter can be url + * cc.loader.setAutoRelease(audioUrl, false); + * ``` + * @method setAutoRelease + * @param {Asset|String} assetOrUrlOrUuid - 资源对象或原始资源的 URL 或是 UUID + * @param {Boolean} autoRelease - 表示是否自动释放 + */ + public setAutoRelease (assetOrUrlOrUuid: Asset|String, autoRelease: Boolean) { + const key = this._getReferenceKey(assetOrUrlOrUuid); + if (key) { + this._autoReleaseSetting[key] = !!autoRelease; + } + else if (CC_DEV) { + cc.warnID(4902); + } + } + + /** + * @en + * Indicates whether to release the asset and its referenced other assets when loading a new scene.
+ * By default, when loading a new scene, all assets in the previous scene will be released or preserved
+ * according to whether the previous scene checked the "Auto Release Assets" option.
+ * On the other hand, assets dynamically loaded by using `cc.loader.loadRes` or `cc.loader.loadResDir`
+ * will not be affected by that option, remain not released by default.
+ * Use this API to change the default behavior on the specified asset and its recursively referenced assets, to force preserve or release specified asset when scene switching.
+ *
+ * See: [[setAutoRelease]], [[isAutoRelease]] + * @zh + * 设置当场景切换时是否自动释放资源及资源引用的其它资源。
+ * 默认情况下,当加载新场景时,旧场景的资源根据旧场景是否勾选“Auto Release Assets”,将会被释放或者保留。
+ * 而使用 `cc.loader.loadRes` 或 `cc.loader.loadResDir` 动态加载的资源,则不受场景设置的影响,默认不自动释放。
+ * 使用这个 API 可以在指定资源及资源递归引用到的所有资源上改变这个默认行为,强制在切换场景时保留或者释放指定资源。
+ *
+ * 参考:[[setAutoRelease]],[[isAutoRelease]] + * + * @example + * ```typescript + * // auto release the SpriteFrame and its Texture event if "Auto Release Assets" disabled in current scene + * cc.loader.setAutoReleaseRecursively(spriteFrame, true); + * // don't release the SpriteFrame and its Texture even if "Auto Release Assets" enabled in current scene + * cc.loader.setAutoReleaseRecursively(spriteFrame, false); + * // don't release the Prefab and all the referenced assets + * cc.loader.setAutoReleaseRecursively(prefab, false); + * ``` + * @method setAutoReleaseRecursively + * @param {Asset|String} assetOrUrlOrUuid - 资源对象或原始资源的 URL 或是 UUID + * @param {Boolean} autoRelease - 表示是否自动释放 + */ + public setAutoReleaseRecursively (assetOrUrlOrUuid: Asset|String, autoRelease: Boolean) { + autoRelease = !!autoRelease; + const key = this._getReferenceKey(assetOrUrlOrUuid); + if (key) { + this._autoReleaseSetting[key] = autoRelease; + + const depends = getDependsRecursively(key); + for (let i = 0; i < depends.length; i++) { + const depend = depends[i]; + this._autoReleaseSetting[depend] = autoRelease; + } + } + else if (CC_DEV) { + cc.warnID(4902); + } + } + + /** + * @en + * Returns whether the asset is configured as auto released, despite how "Auto Release Assets" property is set on scene asset.
+ *
+ * See: [[setAutoRelease]], [[setAutoReleaseRecursively]] + * + * @zh + * 返回指定的资源是否有被设置为自动释放,不论场景的“Auto Release Assets”如何设置。
+ *
+ * 参考:[[setAutoRelease]],[[setAutoReleaseRecursively]] + * @method isAutoRelease + * @param {Asset|String} assetOrUrl - asset object or the raw asset's url + * @returns {Boolean} + */ + public isAutoRelease (assetOrUrl: Asset|String): Boolean{ + const key = this._getReferenceKey(assetOrUrl); + if (key) { + return !!this._autoReleaseSetting[key]; + } + return false; + } + + /** + * @zh + * 获取资源的 uuid + */ + public _getResUuid (url, type, mount, quiet) { + mount = mount || 'assets'; + const assetTable = assetTables[mount]; + if (!url || !assetTable) { + return null; + } + // Ignore parameter + const index = url.indexOf('?'); + if (index !== -1) { + url = url.substr(0, index); + } + let uuid = assetTable.getUuid(url, type); + if (!uuid) { + const extname = cc.path.extname(url); + if (extname) { + // strip extname + url = url.slice(0, - extname.length); + uuid = assetTable.getUuid(url, type); + if (uuid && !quiet) { + cc.warnID(4901, url, extname); + } + } + } + return uuid; + } + + /** + * @en + * Find the asset's reference id in loader, asset could be asset object, asset uuid or asset url + * @zh + * 在 laoder 中找到资源的引用 id ,参数可以是资源对象、资源的 uuid 或者是资源的 url + */ + public _getReferenceKey (assetOrUrlOrUuid) { + let key; + if (typeof assetOrUrlOrUuid === 'object') { + key = assetOrUrlOrUuid._uuid || null; + } + else if (typeof assetOrUrlOrUuid === 'string') { + key = this._getResUuid(assetOrUrlOrUuid, null, null, true) || assetOrUrlOrUuid; + } + if (!key) { + cc.warnID(4800, assetOrUrlOrUuid); + return key; + } + cc.AssetLibrary._getAssetInfoInRuntime(key, _info); + return this._cache[_info.url!] ? _info.url : key; + } + + /** + * @zh + * 当 url 未找到时的操作 + */ + private _urlNotFound (url, type, completeCallback) { + callInNextTick(() => { + url = cc.url.normalize(url); + const info = `${type ? getClassName(type) : 'Asset'} in "resources/${url}" does not exist.`; + if (completeCallback) { + completeCallback(new Error(info), []); + } + }); + } + + /** + * @param {Function} [type] + * @param {Function} [onProgress] + * @param {Function} onComplete + * @returns {Object} arguments + * @returns {Function} arguments.type + * @returns {Function} arguments.onProgress + * @returns {Function} arguments.onComplete + */ + private _parseLoadResArgs (type, onProgress, onComplete) { + if (onComplete === undefined) { + const isValidType = isChildClassOf(type, cc.RawAsset); + if (onProgress) { + onComplete = onProgress; + if (isValidType) { + onProgress = this.onProgress || null; + } + } + else if (onProgress === undefined && !isValidType) { + onComplete = type; + onProgress = this.onProgress || null; + type = null; + } + if (onProgress !== undefined && !isValidType) { + onProgress = type; + type = null; + } + } + return { + type, + onProgress, + onComplete, + }; + } + + /** + * @zh + * 通过uuids 加载资源 + */ + private _loadResUuids (uuids, progressCallback, completeCallback, urls ?) { + if (uuids.length > 0) { + const self = this; + const res = uuids.map((uuid) => { + return { + type: 'uuid', + uuid, + }; + }); + this.load(res, progressCallback, (errors, items) => { + if (completeCallback) { + const assetRes: any = []; + const urlRes: any = urls && []; + for (let i = 0; i < res.length; ++i) { + const uuid = res[i].uuid; + const id = self._getReferenceKey(uuid); + const item = items.getContent(id); + if (item) { + // should not release these assets, even if they are static referenced in the scene. + self.setAutoReleaseRecursively(uuid, false); + assetRes.push(item); + if (urlRes) { + urlRes.push(urls[i]); + } + } + } + if (urls) { + completeCallback(errors, assetRes, urlRes); + } + else { + completeCallback(errors, assetRes); + } + } + }); + } + else { + if (completeCallback) { + callInNextTick( () => { + if (urls) { + completeCallback(null, [], []); + } + else { + completeCallback(null, []); + } + }); + } + } + } +} + +const loader = cc.loader = new CCLoader(); + +if (CC_EDITOR) { + cc.loader.refreshUrl = function (uuid, oldUrl, newUrl) { + let item = this._cache[uuid]; + if (item) { + item.url = newUrl; + } + + item = this._cache[oldUrl]; + if (item) { + item.id = newUrl; + item.url = newUrl; + this._cache[newUrl] = item; + delete this._cache[oldUrl]; + } + }; +} + +export default loader; diff --git a/cocos/load-pipeline/callback-params.ts b/cocos/load-pipeline/callback-params.ts new file mode 100644 index 0000000000..dfe153e019 --- /dev/null +++ b/cocos/load-pipeline/callback-params.ts @@ -0,0 +1,5 @@ +type LoadSuccessParams = Parameters<(error: null, asset: T) => void>; + +type LoadErrorParams = Parameters<(error?: Error) => void>; + +export type LoadCallbackParams = LoadSuccessParams | LoadErrorParams; diff --git a/cocos/load-pipeline/index.js b/cocos/load-pipeline/index.ts similarity index 95% rename from cocos/load-pipeline/index.js rename to cocos/load-pipeline/index.ts index 410f3b6f8e..9dd4c95b98 100644 --- a/cocos/load-pipeline/index.js +++ b/cocos/load-pipeline/index.ts @@ -29,4 +29,5 @@ export {default as Downloader} from './downloader'; export {default as Loader} from './loader'; export {default as LoadingItems} from './loading-items'; export {default as Pipeline} from './pipeline'; -export {default as loader} from './CCLoader'; \ No newline at end of file +export {default as loader} from './CCLoader'; +export * from './callback-params';