Files

289 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2026-05-23 22:10:14 +08:00
/*******************************************************************************
*
* Nx 资源缓存
*
*
*
* 2021.12.10
******************************************************************************/
const NxObject = require( "nx.object" );
// 常驻目录:基础
const ResidentBase = [
"coms"
];
// 常驻目录:图片
const ResidentImages = [
"resDB/icons",
];
// 常驻目录:动画
const ResidentSpines = [
];
// 常驻目录:预制体
const ResidentPrefabs = [
"prefab/bag",
"prefab/battle",
"prefab/chat",
"prefab/home",
"prefab/mainui",
"prefab/partner",
"prefab/popup",
"prefab/push",
"prefab/shop",
"prefab/summon",
"prefab/task",
];
const NxAssets = cc.Class( {
extends: NxObject,
name: "NxAssets",
// 初始化
initialize: function( _args ) {
// USPER
if( !this._super( _args ) ) {
return false;
}
// 全局服务开启
nx.assets = this;
// 资产缓存表
this.caches = {};
// 常驻资产表
this.residents = {};
return true;
},
// 销毁
uninitialize: function() {
// 全局服务关闭
nx.assets = null;
// USPER
return this._super();
},
/**************************************************************
* 资产管理
/*************************************************************/
// 资产查询
queryAsset: function( _uid ) {
return this.caches[ _uid ];
},
// 资产缓存
cacheAsset: function( _asset, _skey ) {
// 无效资产
if( !( _asset instanceof cc.Asset ) ) {
return;
}
// 资产ID无效
let uid = _skey || _asset._uuid;
if( nx.dt.strEmpty( uid ) ) {
nx.error( "[资产]缓存失败,资产ID无效!" );
return;
}
// 缓存+1
_asset.addRef();
this.caches[ uid ] = _asset;
//nx.debug( "[资产]缓存资产[REF:%d]:{%s}", _asset.refCount, uid );
// 压入常驻
this.pushResident( _skey, _asset );
},
// 资产释放
uncacheAsset: function( _assetOrUid ) {
// UUID
if( nx.dt.strNEmpty( _assetOrUid ) ) {
let key = _assetOrUid;
let asset = this.caches[ key ];
if( !asset ) {
for( let k in this.caches ) {
if( this.caches[ k ]._uuid == key ) {
asset = this.caches[ k ];
key = k;
break;
}
}
return;
}
// 释放-1
asset.decRef();
//nx.debug( "[资产]资产释放[REF:%d]:{%s}", asset.refCount, key );
if( asset.refCount == 0 ) {
delete this.caches[ key ];
delete this.residents[ key ];
}
return;
}
// 资产
if( _assetOrUid instanceof cc.Asset ) {
// 资产ID无效
let key = _assetOrUid._uuid;
for( let k in this.caches ) {
if( this.caches[ k ]._uuid == key ) {
key = k;
break;
}
}
if( nx.dt.strEmpty( key ) ) {
nx.error( "[资产]释放失败,资产ID无效!" );
return;
}
// 释放-1
_assetOrUid.decRef();
if( _assetOrUid.refCount == 0 ) {
delete this.caches[ key ];
delete this.residents[ key ];
}
//nx.debug( "[资产]释放资产[REF:%d]:{%s}", _assetOrUid.refCount, key );
}
},
// 资源全释放
uncacheAssets: function( _resident = false ) {
// 常驻清理
if( _resident ) {
this.clearResidents();
}
// 其余清理
for( let key in this.caches ) {
let asset = this.caches[ key ];
if( !asset ) {
continue;
}
asset.decRef();
if( asset.refCount > 0 ) {
nx.warn( "[资产]释放未完全[REF:%d]:{%s}", asset.refCount, key )
}
}
this.caches = {};
},
/**************************************************************
* 常驻资产管理
/*************************************************************/
// 获取常驻
queryResident: function( _skey ) {
let res = this.residents[ _skey ];
return res;
},
// 是否可常驻
canResident: function( _skey, _asset ) {
// 通用目录
for( let i in ResidentBase ) {
if( _skey.indexOf( ResidentBase[ i ] ) >= 0 ) {
return true;
}
}
// 类型目录
let list = null;
if( _asset instanceof cc.SpriteFrame ) {
list = ResidentImages;
} else if( _asset instanceof sp.SkeletonData ) {
list = ResidentSpines;
} else if( _asset instanceof cc.Prefab ) {
list = ResidentPrefabs;
}
for( let i in list ) {
if( _skey.indexOf( list[ i ] ) >= 0 ) {
return true;
}
}
return false;
},
// 压入常驻
pushResident: function( _skey, _asset ) {
// 已经存在
let old = this.queryResident( _skey );
if( old ) {
return true;
}
// 无效
if( nx.dt.strEmpty( _skey ) || !_asset ) {
return false;
}
// 加入常驻表
let good = this.canResident( _skey, _asset );
if( good ) {
_asset.addRef();
this.residents[ _skey ] = _asset;
}
return good;
},
// 常驻清理
clearResidents: function() {
for( let key in this.residents ) {
let res = this.residents[ key ];
if( res ) {
res.decRef();
}
}
this.residents = {};
},
/**************************************************************
* 资产调试
/*************************************************************/
// 缓存打印
dumpAssets: function() {
//nx.debug( "[资产]当前缓存资产:" )
for( let uuid in this.caches ) {
let asset = this.caches[ uuid ];
if( asset ) {
//nx.debug( "\t\t[REF:%d]:%s", asset.refCount, uuid );
}
}
},
} );
// 模块导出
module.exports = NxAssets;