import { Asset } from 'cc'; import { ResPakage } from './ResourcesManager'; /** 加载优先级:critical > interactive > background > silent */ export type LoadPriority = 'critical' | 'interactive' | 'background' | 'silent'; /** local=本地 bundle;network=需联网且失败时展示 loading */ export type LoadPolicyKind = 'local' | 'network'; export interface LoadOptions { priority?: LoadPriority; policy?: LoadPolicyKind; /** 是否启用运行时 loading(实际延迟由 RuntimeLoadingConfig.showAfterMs 统一控制) */ showLoadingAfterMs?: number; onProgress?: (current: number, total: number) => void; } export interface AssetDesc { name: string; bundle: string; path: string; type: typeof Asset; } export const DEFAULT_LOAD_OPTIONS: Required> = { priority: 'interactive', policy: 'local', showLoadingAfterMs: 1, }; export function mergeLoadOptions(opts?: LoadOptions): Required> & LoadOptions { return { ...DEFAULT_LOAD_OPTIONS, ...opts, }; } export type LoadTask = () => Promise; export interface ScheduledTask { id: number; priority: LoadPriority; run: LoadTask; resolve: () => void; reject: (err: unknown) => void; } export function assetKey(name: string, path: string, bundle: string, type: typeof Asset): string { const n = type === Asset ? name : name; return `${bundle}|${path}|${n}|${type.name}`; } export function packageToTasks(pkg: ResPakage, opts?: LoadOptions): Array<{ desc: AssetDesc; options: LoadOptions }> { return pkg.ress.map((r) => ({ desc: { name: r.name, path: r.path, bundle: r.bundle, type: r.type }, options: mergeLoadOptions(opts), })); }