138 lines
3.7 KiB
JavaScript
138 lines
3.7 KiB
JavaScript
|
|
"use strict";
|
||
|
|
cc._RF.push(module, '9ffcbuDoiFNUqcT4RzLA0B5', 'nx.factory');
|
||
|
|
// Scripts/nx/kernel/nx.factory.js
|
||
|
|
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/*******************************************************************************
|
||
|
|
*
|
||
|
|
* Nx对象工厂
|
||
|
|
*
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* 2021.12.10
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
var NxFactory = cc.Class({
|
||
|
|
name: "NxFactory",
|
||
|
|
// 创建
|
||
|
|
ctor: function ctor() {
|
||
|
|
this.counter = 1000; // 计数器
|
||
|
|
this.instances = {}; // 实例列表
|
||
|
|
},
|
||
|
|
|
||
|
|
// 销毁
|
||
|
|
destroy: function destroy() {
|
||
|
|
var count = 0;
|
||
|
|
for (var k in this.instances) {
|
||
|
|
this._deleteObject(k);
|
||
|
|
++count;
|
||
|
|
}
|
||
|
|
this.instances = {};
|
||
|
|
if (CC_DEBUG && count > 0) {
|
||
|
|
nx.logger.warn("[对象工厂]有残留:%d个", count);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 创建
|
||
|
|
create: function create(_jsfile, _args) {
|
||
|
|
return this._createObject(nx.dt.newUUID(), _jsfile, _args);
|
||
|
|
},
|
||
|
|
// 创建单例服务
|
||
|
|
createService: function createService(_name, _jsfile, _args) {
|
||
|
|
_jsfile = _jsfile || _name;
|
||
|
|
return this._createObject(_name, _jsfile, _args);
|
||
|
|
},
|
||
|
|
// 删除
|
||
|
|
remove: function remove(_idOrNameOrObject) {
|
||
|
|
return this._deleteObject(_idOrNameOrObject);
|
||
|
|
},
|
||
|
|
// 获取实例
|
||
|
|
query: function query(_idOrName) {
|
||
|
|
var inst = this.instances[_idOrName];
|
||
|
|
if (!inst) {
|
||
|
|
nx.logger.warn("[对象工厂]查找失败:" + _idOrName);
|
||
|
|
}
|
||
|
|
return inst;
|
||
|
|
},
|
||
|
|
// 获取对象的UUID
|
||
|
|
getUUID: function getUUID(_binder, _create) {
|
||
|
|
// 如果binder是字符串,直接返回保持不变
|
||
|
|
if (nx.dt.strNEmpty(_binder)) {
|
||
|
|
return _binder;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果binder是对象,并且已经有UUID,直接返回
|
||
|
|
if (nx.dt.objGood(_binder) && nx.dt.strNEmpty(_binder.nxUUID)) {
|
||
|
|
return _binder.nxUUID;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 新建
|
||
|
|
if (_create) {
|
||
|
|
_binder.nxUUID = nx.dt.newUUID(_binder.__classname__);
|
||
|
|
return _binder.nxUUID;
|
||
|
|
}
|
||
|
|
return "";
|
||
|
|
},
|
||
|
|
// 创建实例
|
||
|
|
_createObject: function _createObject(_uid, _jsfile, _args) {
|
||
|
|
// 参数合法检测
|
||
|
|
if (nx.dt.strEmpty(_uid) || nx.dt.strEmpty(_jsfile)) {
|
||
|
|
nx.logger.error("[对象工厂]创建失败,参数不合法!");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 重复判定
|
||
|
|
if (nx.dt.objGood(this.instances[_uid])) {
|
||
|
|
nx.logger.error("[对象工厂]创建失败,重复创建");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 脚本读取
|
||
|
|
var mod = require(_jsfile);
|
||
|
|
if (!mod) {
|
||
|
|
nx.logger.error("[对象工厂]创建失败,脚本无效:" + _jsfile);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 实例化
|
||
|
|
var inst = new mod();
|
||
|
|
inst.nxUUID = _uid;
|
||
|
|
inst._jsfile = _jsfile;
|
||
|
|
if (!inst.initialize(_args || {})) {
|
||
|
|
nx.logger.error("[对象工厂]创建初始化失败: %s:%s.js", _uid, _jsfile);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
this.instances[_uid] = inst;
|
||
|
|
|
||
|
|
// nx.logger.debug( "[对象工厂]创建对象: %s:%s.js", _uid, _jsfile );
|
||
|
|
return inst;
|
||
|
|
},
|
||
|
|
// 销毁实例
|
||
|
|
_deleteObject: function _deleteObject(_idOrNameOrObject) {
|
||
|
|
// 如果是对象,那么必须存在nxUUID属性
|
||
|
|
var _uid = _idOrNameOrObject;
|
||
|
|
if (nx.dt.objGood(_idOrNameOrObject) && nx.dt.strNEmpty(_idOrNameOrObject.nxUUID)) {
|
||
|
|
_uid = _idOrNameOrObject.nxUUID;
|
||
|
|
}
|
||
|
|
if (nx.dt.strEmpty(_uid)) {
|
||
|
|
nx.logger.warn("[对象工厂]删除失败,参数非法");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
var object = this.instances[_uid];
|
||
|
|
if (!object) {
|
||
|
|
nx.logger.warn("[对象工厂]删除失败,不存在:" + _uid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 对象删除
|
||
|
|
// nx.logger.debug( "[对象工厂]删除对象:", _uid, object.__classname__ || object._jsfile );
|
||
|
|
if (nx.dt.fnGood(object.uninitialize)) {
|
||
|
|
object.uninitialize();
|
||
|
|
}
|
||
|
|
delete this.instances[_uid];
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = NxFactory;
|
||
|
|
|
||
|
|
cc._RF.pop();
|