Files
oops-framework/assets/script/core/utils/ResLoader.ts
2021-07-03 16:38:47 +08:00

189 lines
8.9 KiB
TypeScript

import { Asset, AssetManager, assetManager, Constructor, error, js, resources, __private } from "cc";
export type ProgressCallback = __private.cocos_core_asset_manager_shared_ProgressCallback;
export type CompleteCallback<T = any> = __private.cocos_core_asset_manager_shared_CompleteCallbackWithData;
export type IRemoteOptions = __private.cocos_core_asset_manager_shared_IRemoteOptions;
export type AssetType<T = Asset> = Constructor<T>;
interface ILoadResArgs<T extends Asset> {
bundle?: string;
dir?: string;
paths: string | string[];
type: AssetType<T> | null;
onProgress: ProgressCallback | null;
onComplete: CompleteCallback<T> | null;
}
export default class ResLoader {
/**
* 加载资源包
* @param url 资源地址
* @param complete 完成事件
* @param v 资源MD5版本号
*/
public loadBundle(url: string, v?: string) {
return new Promise<AssetManager.Bundle>((resolve, reject) => {
assetManager.loadBundle(url, { version: v }, (err, bundle: AssetManager.Bundle) => {
if (err) {
return error(err);
}
resolve(bundle);
});
});
}
public parseLoadResArgs<T extends Asset>(
paths: string | string[],
type?: AssetType<T> | ProgressCallback | CompleteCallback | null,
onProgress?: AssetType<T> | ProgressCallback | CompleteCallback | null,
onComplete?: ProgressCallback | CompleteCallback | null
) {
let pathsOut: any = paths;
let typeOut: any = type;
let onProgressOut: any = onProgress;
let onCompleteOut: any = onComplete;
if (onComplete === undefined) {
const isValidType = js.isChildClassOf(type as AssetType, Asset);
if (onProgress) {
onCompleteOut = onProgress as CompleteCallback;
if (isValidType) {
onProgressOut = null;
}
}
else if (onProgress === undefined && !isValidType) {
onCompleteOut = type as CompleteCallback;
onProgressOut = null;
typeOut = null;
}
if (onProgress !== undefined && !isValidType) {
onProgressOut = type as ProgressCallback;
typeOut = null;
}
}
return { paths: pathsOut, type: typeOut, onProgress: onProgressOut, onComplete: onCompleteOut };
}
private loadByBundleAndArgs<T extends Asset>(bundle: AssetManager.Bundle, args: ILoadResArgs<T>): void {
if (args.dir) {
bundle.loadDir(args.paths as string, args.type, args.onProgress, args.onComplete);
}
else {
if (typeof args.paths == 'string') {
bundle.load(args.paths, args.type, args.onProgress, args.onComplete);
}
else {
bundle.load(args.paths, args.type, args.onProgress, args.onComplete);
}
}
}
private loadByArgs<T extends Asset>(args: ILoadResArgs<T>) {
if (args.bundle) {
if (assetManager.bundles.has(args.bundle)) {
let bundle = assetManager.bundles.get(args.bundle);
this.loadByBundleAndArgs(bundle!, args);
}
else {
// 自动加载bundle
assetManager.loadBundle(args.bundle, (err, bundle) => {
if (!err) {
this.loadByBundleAndArgs(bundle, args);
}
})
}
}
else {
this.loadByBundleAndArgs(resources, args);
}
}
public load<T extends Asset>(bundleName: string, paths: string | string[], type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
public load<T extends Asset>(bundleName: string, paths: string | string[], onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
public load<T extends Asset>(bundleName: string, paths: string | string[], onComplete?: CompleteCallback<T> | null): void;
public load<T extends Asset>(bundleName: string, paths: string | string[], type: AssetType<T> | null, onComplete?: CompleteCallback<T> | null): void;
public load<T extends Asset>(paths: string | string[], type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
public load<T extends Asset>(paths: string | string[], onProgress: ProgressCallback | null, onComplete: CompleteCallback<T> | null): void;
public load<T extends Asset>(paths: string | string[], onComplete?: CompleteCallback<T> | null): void;
public load<T extends Asset>(paths: string | string[], type: AssetType<T> | null, onComplete?: CompleteCallback<T> | null): void;
public load<T extends Asset>(
bundleName: string,
paths?: string | string[] | AssetType<T> | ProgressCallback | CompleteCallback | null,
type?: AssetType<T> | ProgressCallback | CompleteCallback | null,
onProgress?: ProgressCallback | CompleteCallback | null,
onComplete?: CompleteCallback | null,
) {
let args: ILoadResArgs<T> | null = null;
if (typeof paths === "string" || paths instanceof Array) {
args = this.parseLoadResArgs(paths, type, onProgress, onComplete);
args.bundle = bundleName;
}
else {
args = this.parseLoadResArgs(bundleName, paths, type, onProgress);
}
this.loadByArgs(args);
}
public loadDir<T extends Asset>(bundleName: string, dir: string, type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(bundleName: string, dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(bundleName: string, dir: string, onComplete?: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(bundleName: string, dir: string, type: AssetType<T> | null, onComplete?: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(dir: string, type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(dir: string, onComplete?: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(dir: string, type: AssetType<T> | null, onComplete?: CompleteCallback<T[]> | null): void;
public loadDir<T extends Asset>(
bundleName: string,
dir?: string | AssetType<T> | ProgressCallback | CompleteCallback | null,
type?: AssetType<T> | ProgressCallback | CompleteCallback | null,
onProgress?: ProgressCallback | CompleteCallback | null,
onComplete?: CompleteCallback | null,
) {
let args: ILoadResArgs<T> | null = null;
if (typeof dir === "string") {
args = this.parseLoadResArgs(dir, type, onProgress, onComplete);
args.bundle = bundleName;
}
else {
args = this.parseLoadResArgs(bundleName, dir, type, onProgress);
}
args.dir = args.paths as string;
this.loadByArgs(args);
}
public loadRemote<T extends Asset>(url: string, options: IRemoteOptions | null, onComplete?: CompleteCallback<T> | null): void;
public loadRemote<T extends Asset>(url: string, onComplete?: CompleteCallback<T> | null): void;
public loadRemote(url: string, ...args: any): void {
assetManager.loadRemote(url, args);
}
public release(path: string, bundleName: string = "resources") {
var bundle = assetManager.getBundle(bundleName);
bundle?.release(path);
}
public releaseDir(path: string, bundleName: string = "resources") {
var bundle: AssetManager.Bundle | null = assetManager.getBundle(bundleName);
var infos = bundle?.getDirWithPath(path);
infos?.map(function (info) {
bundle?.release(info.path);
});
if (path == "" && bundleName != "resources" && bundle) {
assetManager.removeBundle(bundle);
}
}
public get<T extends Asset>(path: string, type?: __private.cocos_core_asset_manager_shared_AssetType<T> | null, bundleName: string = "resources"): T | null {
var bundle: AssetManager.Bundle | null = assetManager.getBundle(bundleName);
return bundle!.get(path, type);
}
public dump() {
assetManager.assets.forEach((value: Asset, key: string) => {
console.log(key);
})
console.log(`当前资源总数:${assetManager.assets.count}`);
}
}
export let resLoader = new ResLoader();