Files

465 lines
9.9 KiB
JavaScript
Raw Permalink Normal View History

2026-05-23 22:10:14 +08:00
/*******************************************************************************
*
* Nx 资源相关方法
*
*
*
* 2021.12.10
******************************************************************************/
const ERR = require( "error" );
const NxObject = require( "nx.object" );
var nxResource = cc.Class( {
extends: NxObject,
name: "nxResource",
// 初始化
initialize: function( _args ) {
// USPER
if( !this._super( _args ) ) {
return false;
}
// 全局服务开启
nx.res = this;
// 原生目录创建
this._buildFolder();
return true;
},
// 销毁
uninitialize: function() {
// 全局服务关闭
nx.res = null;
// USPER
return this._super();
},
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// 本地化资源格式统配
// 通配符格式化
// {lang}本地化语种
// {chnn}渠道
// {com}全局通用路径
// {skin}皮肤路径
fmtPath: function( _fmt ) {
let rt = _fmt.trim();
// 全局通配
rt = rt.replace( "{lang}", nx.getLocLanguage() );
rt = rt.replace( "{com}", "coms" );
// 渠道通配
let channelName = nx.bridge ? nx.bridge.vget( "channelName" ) : "gf";
rt = rt.replace( "{chnn}", channelName );
// let theme = nx.frame.vget( "Theme" );
// if( nx.dt.arrNEmpty( theme ) && theme.length == 2 ) {
// let path = cc.path.join( "skins", theme[ 0 ] );
// rt = rt.replace( "{skin}", path );
// }
return rt.replace( "//", "/" );
},
// 是否语言相关
isBindLang: function( _fmt ) {
return nx.dt.strNEmpty( _fmt ) && _fmt.indexof( "{lang}" ) >= 0;
},
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// 各种加载
// 加载资源
loadRes: function( _path, _type, _cb ) {
// 常驻缓存
let asset = nx.assets.queryAsset( _path );
if( asset ) {
nx.dt.fnInvoke( _cb, null, asset );
return;
}
// 包读取
cc.loader.loadRes( _path, _type, ( _err, _data ) => {
// 失败
if( _err ) {
nx.error( "[资源]资源加载失败!", _path );
nx.dt.fnInvoke( _cb, _err );
return;
}
nx.dt.fnInvoke( _cb, null, _data );
} );
},
// 读取JSON
loadJson: function( _path, _cb ) {
this.loadRes( _path, cc.JsonAsset, ( _err, _data ) => {
nx.dt.fnInvoke( _cb, _err, _data ? _data.json : {} );
} );
},
// 读取字体
loadFont: function( _path, _cb ) {
this.loadRes( _path, cc.Font, _cb );
},
// 读取纹理
loadSpriteFrame: function( _path, _cb ) {
this.loadRes( _path, cc.SpriteFrame, _cb );
},
// 读取图集
loadSpriteAtlas: function( _path, _cb ) {
this.loadRes( _path, cc.SpriteAtlas, _cb );
},
// 读取Texture
loadTexture2D: function( _path, _cb ) {
this.loadRes( _path, cc.Texture2D, _cb );
},
// 读取图集子图
loadAtlasFrame: function( _path, _cb ) {
let pos = _path.lastIndexOf( "\/" );
if( pos == -1 || pos == ( _path.length - 1 ) ) {
nx.warn( "[资源]AtlasFrame读取失败,无效的路径!" );
nx.dt.trycatch( () => {
nx.dt.fnInvoke( _cb, nx.genError( ERR.COM.EmptyPath ) );
} );
return;
}
// 拆分
let path = _path.substr( 0, pos );
let frmName = _path.substr( pos + 1, _path.length );
let done = function( _err, _data ) {
// 错误
if( _err ) {
nx.dt.fnInvoke( _cb, _err, _data );
return;
}
let _sframe = _data.getSpriteFrame( frmName );
if( _sframe ) {
nx.dt.fnInvoke( _cb, null, _sframe );
return;
}
nx.warn( "[资源]AtlasFrame读取失败,无效的子图:%s", frmName );
nx.dt.fnInvoke( _cb, nx.genError( ERR.COM.InvalidAsset ) );
}
this.loadRes( path, cc.SpriteAtlas, ( _err, _data ) => {
nx.dt.trycatch( () => {
done( _err, _data );
} );
} );
},
// 读取音乐
loadAudio: function( _path, _cb ) {
this.loadRes( _path, cc.AudioClip, _cb );
},
// 读取SPINE
loadSpine: function( _path, _cb ) {
this.loadRes( _path, sp.SkeletonData, _cb );
},
// 初始化Prefab
loadPrefab: function( _path, _cb, _showWait = true ) {
this.loadRes( _path, cc.Prefab, ( _err, _data ) => {
nx.dt.fnInvoke( _cb, _err, _data );
} );
},
// 初始化资源目录
loadResDir: function( _path, _cb ) {
cc.loader.loadResDir( _path, ( _err, _assets ) => {
if( !_err ) {
nx.dt.fnInvoke( _cb, null, _asset );
return;
}
// 失败
nx.warn( "[资源]loadResDir加载失败:", _path );
nx.dt.fnInvoke( _cb, nx.genError( ERR.CLI.FailedLoadAsset ) );
} );
},
// 远程加载
loadRemote: function( _url, _cb ) {
// 具体执行
let fire = function() {
// 判空
if( nx.dt.strEmpty( _url ) ) {
nx.warn( "[远程]loadRemote加载失败,无效的路径!" );
nx.dt.fnInvoke( _cb, nx.genError( ERR.COM.EmptyPath ) );
return;
}
// 加载
_url += "?t=" + cc.sys.now();
nx.debug( "[远程]loadRemote:%s", _url );
cc.assetManager.loadRemote( _url, ( _err, _asset ) => {
// 成功
if( !_err ) {
nx.dt.fnInvoke( _cb, null, _asset );
return;
}
nx.error( "[远程]loadRemote加载失败:" + _err.message );
nx.dt.fnInvoke( _cb, nx.genError( ERR.CLI.FailedLoadAsset ) );
} );
};
nx.dt.trycatch( () => { fire(); } );
},
// ------------------------------------------------------------
// 資源预加载
// ------------------------------------------------------------
// 预加载指定资源
preload: function( _path, _type, _cb ) {
this.preQueue = this.preQueue || [];
this.preQueue.push( {
dir: false,
path: _path,
type: _type,
doneCb: _cb || nx.dt.fnEmpty
} );
// 队列触发
if( !this.preLoading ) {
this.nextPreload();
}
},
// 预加载指定目录
preloadDir: function( _path, _type, _progCB, _doneCB ) {
this.preQueue = this.preQueue || [];
this.preQueue.push( {
dir: true,
path: _path,
type: _type,
doneCb: _doneCB || nx.dt.fnEmpty,
progCb: _progCB || nx.dt.fnEmpty
} );
// 队列触发
if( !this.preLoading ) {
this.nextPreload();
}
},
// 下波预加载
nextPreload: function() {
// 空队列
if( nx.dt.arrEmpty( this.preQueue ) ) {
this.preLoading = false;
return;
}
// 进行中
if( this.preLoading ) {
return;
}
// 预加载
let self = this;
let task = this.preQueue.shift();
let done = function( _err, _data ) {
self.preLoading = false;
if( _err ) {
task.doneCb( _err );
return;
}
task.doneCb( null, _params );
};
this.preLoading = true;
if( task.dir ) {
cc.loader.preloadDir( task.path, done );
} else {
cc.loader.preload( task.path, done );
}
},
// ------------------------------------------------------------
// 缓存相关
// ------------------------------------------------------------
// 输出当前缓存图片
dumpCacheTextures: function() {
let cnt = 0;
let out = {};
for( let k in cc.assetManager.assets._map ) {
let item = cc.assetManager.assets._map[ k ];
if( item instanceof cc.Texture2D ||
item instanceof cc.SpriteFrame ||
item instanceof cc.SpriteAtlas ) {
out[ k ] = item;
++cnt;
}
}
console.log( "缓存图片数量:", cnt );
console.log( out );
},
// 输出当前缓存Spine
dumpCacheSpines: function() {
let cnt = 0;
let out = {};
for( let k in cc.assetManager.assets._map ) {
let item = cc.assetManager.assets._map[ k ];
if( item instanceof sp.Skeleton ||
item instanceof sp.SkeletonData ||
item instanceof sp.SkeletonTexture ) {
out[ k ] = item;
++cnt;
}
}
console.log( "缓存Spine数量:", cnt );
console.log( out );
},
// 输出当前缓存Prefab
dumpCachePrefab: function() {
let cnt = 0;
let out = {};
for( let k in cc.assetManager.assets._map ) {
let item = cc.assetManager.assets._map[ k ];
if( item instanceof cc.Prefab ) {
out[ k ] = item;
++cnt;
}
}
console.log( "缓存Prefab数量:", cnt );
console.log( out );
},
// 给cc.loader增加新的扩展文件下载映射
/*
// 定义
nx.res.addDownLoader( "zip", function(item, callback ) {
var url = item.url;
var xhr = cc.Pipeline.getXMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";
xhr.onload = function (oEvent) {
var arrayBuffer = xhr.response;
if (arrayBuffer) {
var result = new Uint8Array(arrayBuffer);
// 任何需要的处理
callback(null, result);
}
else {
callback(message); // 第一个参数需要传递错误信息
}
}
// 错误处理
xhr.onerror = function( onEvent ) { // 同样需要调用 callback 返回错误信息
console.log( onEvent );
}
xhr.send(null);
} );
// 使用
cc.assetManager.load( "http://..../coms.zip",
function( completedCount, totalCount, item ) {
console.log( " prog:" + completedCount + ":" + totalCount );
},
function( err, zip ) {
console.log( err );
console.log( " size:" + zip.length );
})
*/
addDownLoader: function( _type, _handler ) {
if( nx.dt.strEmpty( _type ) ||
!nx.dt.fnGood( _handler ) ) {
return;
}
cc.assetManager.addDownloadHandlers( { _type: _handler } );
},
// 原生目录创建
_buildFolder: function() {
if( !window.jsb || !window.jsb.fileUtils ) {
return;
}
// 原生目录创建
var _build = function( _folder ) {
var prefix = jsb.fileUtils.getWritablePath() + _folder + "/";
if( !jsb.fileUtils.isDirectoryExist( prefix ) ) {
jsb.fileUtils.createDirectory( prefix );
}
return prefix;
};
nx.folder = {};
nx.folder.games = _build( "assets/games" );
nx.folder.log = _build( "assets/log" );
nx.folder.photos = _build( "assets/photos" );
nx.folder.voices = _build( "assets/voices" );
nx.folder.shots = _build( "assets/shots" );
nx.folder.other = _build( "assets/other" );
},
} );
// 模块导出
module.exports = nxResource;