import { AudioInterface } from "../audio/interface/AudioInterface" ; import { ResManager } from "../core/ResManager" ; import { Log } from "../log/Log" ; import { ResDestroyArray } from "./ResDestroyArray" ; /**
 * 资源加载管理器
 */ export class ResLoadGroupManager { /**
     * res 所对应的  group
     */ private static mResGroupMap : Map < string , Array < string > > = new Map ( ) ; private static _noDestroyTypes : ResDestroyArray < string > = new ResDestroyArray ( ) ; private static _noDestroyRess : ResDestroyArray < { type : string ; res : string ; } > = new ResDestroyArray ( ) ; /**
     * 不销毁的类型
     *
     * 如  noDestroyTypes里有  spine  则不会销毁spine
     */ public static get noDestroyTypes ( ) : ResDestroyArray < string > { return ResLoadGroupManager . _noDestroyTypes ; } param res public static getGroupByRes ( res ) { let arr = new Array ( ) ; let group = ResLoadGroupManager . mResGroupMap . get ( res ) ; if ( group ) { arr . push ( ... group ) ; } return arr ; } /**
     * 获取当前全部的组
     */ public static getGroups ( ) { let arr = new Array ( ) ; this . mResGroupMap . forEach ( ( gs ) => gs . forEach ( ( g ) => ( arr . includes ( g ) ? undefined : arr . push ( g ) ) ) ) ; return arr ; } param group public static getResByGroup ( group ) { let arr = new Array ( ) ; ResLoadGroupManager . mResGroupMap . forEach ( ( v , k ) => { if ( arr . includes ( k ) ) { return ; } if ( v . includes ( group ) ) { arr . push ( k ) ; } } ) ; return arr ; } param res param group public static isExistGroup ( res , group ) { return ResLoadGroupManager . getGroupByRes ( res ) . includes ( group ) ; } param res param group public static pushResGroup ( res , ... group ) { let groups = ResLoadGroupManager . mResGroupMap . get ( res ) ; if ( ! Array . isArray ( groups ) ) { groups = new Array ( ) ; ResLoadGroupManager . mResGroupMap . set ( res , groups ) ; } group . forEach ( ( v ) => { if ( ! groups ) { return ; } if ( ! groups . includes ( v ) ) { groups . push ( v ) ; } } ) ; } param name param url param type param group param callback public static loadRes ( name , url , type , group , callback ) { ResLoadGroupManager . pushResGroup ( name , group ) ; ResManager . loadRes ( name , url , type , callback ) ; } param res param group param onProcess param onComplete public static loadResArr ( res , group , onProcess ? , onComplete ? ) { res . forEach ( ( v ) => { ResLoadGroupManager . pushResGroup ( v . name , group ) ; } ) ; ResManager . loadResArr ( res , onProcess , onComplete ) ; } param name param group param callback public static loadNameRes ( name , group , callback ) { ResLoadGroupManager . pushResGroup ( name , group ) ; if ( ResManager . isExist ( name ) ) { callback ( ) ; return ; } let resObj = ResManager . getResInfo ( name ) ; if ( resObj == undefined ) { throw new Error ( "没有名为：【" + name + "】的配置资源表" ) ; } ResManager . asyncLoad ( resObj . name , resObj . url , resObj . type , callback ) ; } param res param onProcess param onComplete public static loadResNameArr ( res , group , onProcess ? , onComplete ? ) { let resArr = new Array ( ) ; res . forEach ( ( name ) => { let resObj = ResManager . getResInfo ( name ) ; if ( resObj == undefined ) { throw new Error ( "没有名为：【" + name + "】的配置资源表" ) ; } resArr . push ( resObj ) ; ResLoadGroupManager . pushResGroup ( name , group ) ; } ) ; if ( resArr . length == 0 ) { onComplete && onComplete ( ) ; return ; } ResManager . loadResArr ( resArr , onProcess , onComplete ) ; } param res public static loadErrorRes ( res ? ) { ResManager . loadErrorRes ( res ) ; } param name public static getRes ( name ) { return ResManager . getRes ( name ) ; } param name param url param type param param public static putResInfo ( name , url , type , param ? ) { ResManager . putResInfo ( name , url , type , param ) ; } param name public static hasResInfo ( name ) { return ResManager . hasResInfo ( name ) ; } param name public static getResInfo ( name ) { return ResManager . getResInfo ( name ) ; } param type public static destroyRes ( res ) { let groups = ResLoadGroupManager . mResGroupMap . get ( res ) ; ResLoadGroupManager . mResGroupMap . delete ( res ) ; groups ?. clear ( ) ; let arr = ResLoadGroupManager . _noDestroyRess . toArray ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] . res == res ) { ResLoadGroupManager . _noDestroyRess . remove ( arr [ i ] ) ; break ; } } ResManager . removeResource ( res ) ; } param type param plug public static destroyResType ( type , plug ? ) { ResLoadGroupManager . mResGroupMap . forEach ( ( _ , res ) => { let objInfo = ResLoadGroupManager . getResInfo ( res ) ; if ( ! objInfo ) { return ; } if ( objInfo . type != type ) { return ; } let isDestroy = plug == undefined ? true : plug ( res , type , ResLoadGroupManager . getGroupByRes ( res ) ) ; if ( ! isDestroy ) { return ; } ResLoadGroupManager . destroyRes ( res ) ; } ) ; } param type public static destroyOtherRes ( type ? ) { ResLoadGroupManager . _noDestroyRess . toArray ( ) . forEach ( ( obj ) => { let isDestroy = false ; if ( type == undefined || type == obj . type ) { isDestroy = true ; } let groups = ResLoadGroupManager . mResGroupMap . get ( obj . res ) ; ResLoadGroupManager . mResGroupMap . delete ( obj . res ) ; groups ?. clear ( ) ; ResLoadGroupManager . _noDestroyRess . remove ( obj ) ; ResManager . removeResource ( obj . res ) ; } ) ; } param group param isGDALL public static destroyGroup ( group , isGDALL ? ) { let ress = ResLoadGroupManager . getResByGroup ( group ) ; ress . forEach ( ( res ) => { let groups = ResLoadGroupManager . mResGroupMap . get ( res ) ; if ( ! groups ) { ResLoadGroupManager . mResGroupMap . delete ( res ) ; return ; } if ( isGDALL ) { groups . clear ( ) ; groups . push ( group ) ; } if ( groups . length == 1 && groups . includes ( group ) ) { let objInfo = ResLoadGroupManager . getResInfo ( res ) ; if ( objInfo && ResLoadGroupManager . noDestroyTypes . includes ( objInfo . type ) ) { ResLoadGroupManager . _noDestroyRess . push ( { res , type : objInfo . type } ) ; return ; } ResLoadGroupManager . mResGroupMap . delete ( res ) ; ResManager . removeResource ( res ) ; groups . clear ( ) ; } else { groups . remove ( group ) ; } } ) ; } }