398 lines
11 KiB
JavaScript
398 lines
11 KiB
JavaScript
|
|
"use strict";
|
|||
|
|
cc._RF.push(module, '45852p8PQxJYYWzEyhldn15', 'nx.res');
|
|||
|
|
// Scripts/nx/kernel/nx.res.js
|
|||
|
|
|
|||
|
|
"use strict";
|
|||
|
|
|
|||
|
|
/*******************************************************************************
|
|||
|
|
*
|
|||
|
|
* Nx 资源相关方法
|
|||
|
|
*
|
|||
|
|
*
|
|||
|
|
*
|
|||
|
|
* 2021.12.10
|
|||
|
|
******************************************************************************/
|
|||
|
|
|
|||
|
|
var ERR = require("error");
|
|||
|
|
var NxObject = require("nx.object");
|
|||
|
|
var nxResource = cc.Class({
|
|||
|
|
"extends": NxObject,
|
|||
|
|
name: "nxResource",
|
|||
|
|
// 初始化
|
|||
|
|
initialize: function initialize(_args) {
|
|||
|
|
// USPER
|
|||
|
|
if (!this._super(_args)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 全局服务开启
|
|||
|
|
nx.res = this;
|
|||
|
|
|
|||
|
|
// 原生目录创建
|
|||
|
|
this._buildFolder();
|
|||
|
|
return true;
|
|||
|
|
},
|
|||
|
|
// 销毁
|
|||
|
|
uninitialize: function uninitialize() {
|
|||
|
|
// 全局服务关闭
|
|||
|
|
nx.res = null;
|
|||
|
|
|
|||
|
|
// USPER
|
|||
|
|
return this._super();
|
|||
|
|
},
|
|||
|
|
// ---------------------------------------------------------------------------------------------
|
|||
|
|
// ---------------------------------------------------------------------------------------------
|
|||
|
|
// 本地化资源格式统配
|
|||
|
|
|
|||
|
|
// 通配符格式化
|
|||
|
|
// {lang}本地化语种
|
|||
|
|
// {chnn}渠道
|
|||
|
|
// {com}全局通用路径
|
|||
|
|
// {skin}皮肤路径
|
|||
|
|
fmtPath: function fmtPath(_fmt) {
|
|||
|
|
var rt = _fmt.trim();
|
|||
|
|
|
|||
|
|
// 全局通配
|
|||
|
|
rt = rt.replace("{lang}", nx.getLocLanguage());
|
|||
|
|
rt = rt.replace("{com}", "coms");
|
|||
|
|
|
|||
|
|
// 渠道通配
|
|||
|
|
var 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 isBindLang(_fmt) {
|
|||
|
|
return nx.dt.strNEmpty(_fmt) && _fmt.indexof("{lang}") >= 0;
|
|||
|
|
},
|
|||
|
|
// ---------------------------------------------------------------------------------------------
|
|||
|
|
// ---------------------------------------------------------------------------------------------
|
|||
|
|
// 各种加载
|
|||
|
|
|
|||
|
|
// 加载资源
|
|||
|
|
loadRes: function loadRes(_path, _type, _cb) {
|
|||
|
|
// 常驻缓存
|
|||
|
|
var asset = nx.assets.queryAsset(_path);
|
|||
|
|
if (asset) {
|
|||
|
|
nx.dt.fnInvoke(_cb, null, asset);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 包读取
|
|||
|
|
cc.loader.loadRes(_path, _type, function (_err, _data) {
|
|||
|
|
// 失败
|
|||
|
|
if (_err) {
|
|||
|
|
nx.error("[资源]资源加载失败!", _path);
|
|||
|
|
nx.dt.fnInvoke(_cb, _err);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
nx.dt.fnInvoke(_cb, null, _data);
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
// 读取JSON
|
|||
|
|
loadJson: function loadJson(_path, _cb) {
|
|||
|
|
this.loadRes(_path, cc.JsonAsset, function (_err, _data) {
|
|||
|
|
nx.dt.fnInvoke(_cb, _err, _data ? _data.json : {});
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
// 读取字体
|
|||
|
|
loadFont: function loadFont(_path, _cb) {
|
|||
|
|
this.loadRes(_path, cc.Font, _cb);
|
|||
|
|
},
|
|||
|
|
// 读取纹理
|
|||
|
|
loadSpriteFrame: function loadSpriteFrame(_path, _cb) {
|
|||
|
|
this.loadRes(_path, cc.SpriteFrame, _cb);
|
|||
|
|
},
|
|||
|
|
// 读取图集
|
|||
|
|
loadSpriteAtlas: function loadSpriteAtlas(_path, _cb) {
|
|||
|
|
this.loadRes(_path, cc.SpriteAtlas, _cb);
|
|||
|
|
},
|
|||
|
|
// 读取Texture
|
|||
|
|
loadTexture2D: function loadTexture2D(_path, _cb) {
|
|||
|
|
this.loadRes(_path, cc.Texture2D, _cb);
|
|||
|
|
},
|
|||
|
|
// 读取图集子图
|
|||
|
|
loadAtlasFrame: function loadAtlasFrame(_path, _cb) {
|
|||
|
|
var pos = _path.lastIndexOf("\/");
|
|||
|
|
if (pos == -1 || pos == _path.length - 1) {
|
|||
|
|
nx.warn("[资源]AtlasFrame读取失败,无效的路径!");
|
|||
|
|
nx.dt.trycatch(function () {
|
|||
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.COM.EmptyPath));
|
|||
|
|
});
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 拆分
|
|||
|
|
var path = _path.substr(0, pos);
|
|||
|
|
var frmName = _path.substr(pos + 1, _path.length);
|
|||
|
|
var done = function done(_err, _data) {
|
|||
|
|
// 错误
|
|||
|
|
if (_err) {
|
|||
|
|
nx.dt.fnInvoke(_cb, _err, _data);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var _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, function (_err, _data) {
|
|||
|
|
nx.dt.trycatch(function () {
|
|||
|
|
done(_err, _data);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
// 读取音乐
|
|||
|
|
loadAudio: function loadAudio(_path, _cb) {
|
|||
|
|
this.loadRes(_path, cc.AudioClip, _cb);
|
|||
|
|
},
|
|||
|
|
// 读取SPINE
|
|||
|
|
loadSpine: function loadSpine(_path, _cb) {
|
|||
|
|
this.loadRes(_path, sp.SkeletonData, _cb);
|
|||
|
|
},
|
|||
|
|
// 初始化Prefab
|
|||
|
|
loadPrefab: function loadPrefab(_path, _cb, _showWait) {
|
|||
|
|
if (_showWait === void 0) {
|
|||
|
|
_showWait = true;
|
|||
|
|
}
|
|||
|
|
this.loadRes(_path, cc.Prefab, function (_err, _data) {
|
|||
|
|
nx.dt.fnInvoke(_cb, _err, _data);
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
// 初始化资源目录
|
|||
|
|
loadResDir: function loadResDir(_path, _cb) {
|
|||
|
|
cc.loader.loadResDir(_path, function (_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 loadRemote(_url, _cb) {
|
|||
|
|
// 具体执行
|
|||
|
|
var fire = function fire() {
|
|||
|
|
// 判空
|
|||
|
|
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, function (_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(function () {
|
|||
|
|
fire();
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
// ------------------------------------------------------------
|
|||
|
|
// 資源预加载
|
|||
|
|
// ------------------------------------------------------------
|
|||
|
|
|
|||
|
|
// 预加载指定资源
|
|||
|
|
preload: function preload(_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 preloadDir(_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 nextPreload() {
|
|||
|
|
// 空队列
|
|||
|
|
if (nx.dt.arrEmpty(this.preQueue)) {
|
|||
|
|
this.preLoading = false;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 进行中
|
|||
|
|
if (this.preLoading) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 预加载
|
|||
|
|
var self = this;
|
|||
|
|
var task = this.preQueue.shift();
|
|||
|
|
var done = function done(_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 dumpCacheTextures() {
|
|||
|
|
var cnt = 0;
|
|||
|
|
var out = {};
|
|||
|
|
for (var k in cc.assetManager.assets._map) {
|
|||
|
|
var 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 dumpCacheSpines() {
|
|||
|
|
var cnt = 0;
|
|||
|
|
var out = {};
|
|||
|
|
for (var k in cc.assetManager.assets._map) {
|
|||
|
|
var 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 dumpCachePrefab() {
|
|||
|
|
var cnt = 0;
|
|||
|
|
var out = {};
|
|||
|
|
for (var k in cc.assetManager.assets._map) {
|
|||
|
|
var 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 addDownLoader(_type, _handler) {
|
|||
|
|
if (nx.dt.strEmpty(_type) || !nx.dt.fnGood(_handler)) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
cc.assetManager.addDownloadHandlers({
|
|||
|
|
_type: _handler
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
// 原生目录创建
|
|||
|
|
_buildFolder: function _buildFolder() {
|
|||
|
|
if (!window.jsb || !window.jsb.fileUtils) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 原生目录创建
|
|||
|
|
var _build = function _build(_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;
|
|||
|
|
|
|||
|
|
cc._RF.pop();
|