257 lines
6.1 KiB
JavaScript
257 lines
6.1 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, 'e7c82fAu+VHOpbYkeXslnIe', 'nx.mod');
|
|
// Scripts/nx/kernel/nx.mod.js
|
|
|
|
"use strict";
|
|
|
|
/*******************************************************************************
|
|
*
|
|
* Nx模块基类
|
|
* * 模块嵌套管理
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
var NxObject = require("nx.object");
|
|
var MODBase = cc.Class({
|
|
"extends": NxObject,
|
|
name: "MODBase",
|
|
// 初始化
|
|
initialize: function initialize(_args) {
|
|
// USPER
|
|
this._super(_args);
|
|
|
|
// 子MOD列表
|
|
this.lstMods = [];
|
|
return true;
|
|
},
|
|
// 销毁
|
|
uninitialize: function uninitialize() {
|
|
// 子MOD销毁
|
|
this.modClean();
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
/***
|
|
* *********************************************************************
|
|
* 流程通告
|
|
* *********************************************************************
|
|
*/
|
|
|
|
// 游戏数据准备(启动时)
|
|
doPrepare: function doPrepare(_cb, _args) {
|
|
nx.debug("[Mod]doPrepare:" + this._jsfile);
|
|
var count = this.lstMods.length;
|
|
if (count == 0) {
|
|
nx.dt.fnInvoke(_cb);
|
|
return;
|
|
}
|
|
var check = function check() {
|
|
--count;
|
|
if (count == 0) {
|
|
nx.dt.fnInvoke(_cb);
|
|
}
|
|
};
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.doPrepare(check, _args);
|
|
});
|
|
},
|
|
// 通告: 进入大厅后,发送准备前
|
|
onEnter: function onEnter() {
|
|
// 子模板事件通告
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onEnter();
|
|
});
|
|
},
|
|
// 通告: 登入
|
|
onLogin: function onLogin() {
|
|
// 子模板事件通告
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onLogin();
|
|
});
|
|
},
|
|
// 通告: 登出
|
|
onLogout: function onLogout() {
|
|
// 子模板事件通告
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onLogout();
|
|
});
|
|
},
|
|
// 通告: 切后台
|
|
onBackground: function onBackground() {
|
|
// 子模板事件通告
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onBackground();
|
|
});
|
|
},
|
|
// 通告: 切前台
|
|
onFrontend: function onFrontend(_ms) {
|
|
// 子模板事件通告
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onFrontend(_ms);
|
|
});
|
|
},
|
|
/***
|
|
* *********************************************************************
|
|
* 存档相关
|
|
* *********************************************************************
|
|
*/
|
|
|
|
// 序列化: 初始数据
|
|
onSerializeRaws: function onSerializeRaws(_records) {
|
|
// 子模板序列化
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onSerializeRaws(_records);
|
|
});
|
|
},
|
|
// 序列化: 保存
|
|
onSerialize: function onSerialize(_records) {
|
|
// 子模板序列化
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onSerialize(_records);
|
|
});
|
|
},
|
|
// 反序列化: 读取
|
|
onDeserialize: function onDeserialize(_records) {
|
|
// 子模板序列化
|
|
this.lstMods.forEach(function (_mod) {
|
|
_mod.onDeserialize(_records);
|
|
});
|
|
},
|
|
/***
|
|
* *********************************************************************
|
|
* 子模块操作
|
|
* *********************************************************************
|
|
*/
|
|
|
|
// 创建子MOD
|
|
modCreate: function modCreate(_name, _jsfile, _args) {
|
|
// 参数合法检查
|
|
if (nx.dt.strEmpty(_name) || nx.dt.strEmpty(_jsfile)) {
|
|
nx.error("[Mod]创建MOD失败!");
|
|
return null;
|
|
}
|
|
|
|
// 重复创建检查
|
|
var mod = this.modQuery(_name, false);
|
|
if (mod) {
|
|
nx.warn("[Mod]重复创建MOD:", _name);
|
|
return mod;
|
|
}
|
|
|
|
// 创建
|
|
mod = nx.factory.create(_jsfile, _args || {});
|
|
if (!mod) {
|
|
nx.warn("[Mod]创建MOD失败:", _name, _jsfile);
|
|
return null;
|
|
}
|
|
mod._modKey = _name;
|
|
this.lstMods.push(mod);
|
|
nx.debug("[Mod]创建子MOD:", _name, _jsfile);
|
|
return mod;
|
|
},
|
|
// 添加子MOD
|
|
/**
|
|
* 两种参数格式:
|
|
* 1: 字符串数组[ ModName, ModName, ... ]
|
|
* 2: 信息数组[ [ ModName, ModFile, ModArgs ], ... ]
|
|
*/
|
|
modCreateBatch: function modCreateBatch(_list) {
|
|
var _this = this;
|
|
// 空返回
|
|
if (nx.dt.arrEmpty(_list)) {
|
|
return;
|
|
}
|
|
|
|
// 统计队列
|
|
var infos = [];
|
|
_list.forEach(function (_ifo) {
|
|
// 字符串数组
|
|
if (nx.dt.strNEmpty(_ifo)) {
|
|
infos.push({
|
|
name: _ifo,
|
|
file: _ifo,
|
|
args: {}
|
|
});
|
|
}
|
|
// 信息数组
|
|
else {
|
|
infos.push({
|
|
name: _ifo[0],
|
|
file: _ifo[1],
|
|
args: _ifo[2]
|
|
});
|
|
}
|
|
});
|
|
|
|
// 逐个创建
|
|
infos.forEach(function (_ifo) {
|
|
_this.modCreate(_ifo.name, _ifo.file, _ifo.args);
|
|
});
|
|
},
|
|
// 销毁子MOD
|
|
modDelete: function modDelete(_name) {
|
|
nx.dt.arrDelete(this.lstMods, function (_mod) {
|
|
if (_mod && _mod._modKey == _name) {
|
|
nx.factory.remove(_mod);
|
|
nx.debug("[Mod]销毁子MOD:", _name);
|
|
return true;
|
|
}
|
|
return false;
|
|
}, true);
|
|
},
|
|
// 销毁子MOD
|
|
modDeleteBatch: function modDeleteBatch(_list) {
|
|
var _this2 = this;
|
|
// 空返回
|
|
if (nx.dt.arrEmpty(_list)) {
|
|
return;
|
|
}
|
|
_list.forEach(function (_name) {
|
|
_this2.modDelete(_name);
|
|
});
|
|
},
|
|
// 清理子MOD
|
|
modClean: function modClean() {
|
|
this.lstMods.forEach(function (_mod) {
|
|
nx.factory.remove(_mod);
|
|
});
|
|
this.lstMods = [];
|
|
},
|
|
// 查询
|
|
modQuery: function modQuery(_name, _notify) {
|
|
if (_notify === void 0) {
|
|
_notify = true;
|
|
}
|
|
for (var i in this.lstMods) {
|
|
var mod = this.lstMods[i];
|
|
if (mod && mod._modKey == _name) {
|
|
return mod;
|
|
}
|
|
}
|
|
if (_notify) {
|
|
nx.error("[Mod]查询子MOD失败:", _name || "无效名");
|
|
}
|
|
return null;
|
|
},
|
|
// 模块列表输出
|
|
modDumpTree: function modDumpTree() {
|
|
// 输出
|
|
var _dump = function _dump(_mod, _hs) {
|
|
if (_hs === void 0) {
|
|
_hs = '';
|
|
}
|
|
console.log(_hs + _mod.nxUUID + ':' + _mod._modKey);
|
|
if (_mod.lstMods && _mod.lstMods.length > 0) {
|
|
_mod.lstMods.forEach(function (mod) {
|
|
_dump(mod, _hs + '\t');
|
|
});
|
|
}
|
|
};
|
|
_dump(this);
|
|
}
|
|
});
|
|
module.exports = MODBase;
|
|
|
|
cc._RF.pop(); |