/******************************************************************************* * * Nx插件管理器 * * * * 2021.12.10 ******************************************************************************/ const NxObject = require( "nx.object" ); const PE = require( "nx.plugin.event" ); const PV = require( "nx.plugin.view" ); const PVB = require( "nx.plugin.view.binder" ); var NxPluginManager = cc.Class( { extends: NxObject, name: "NxPluginManager", // 初始化 initialize: function( _args ) { // USPER if( !this._super( _args ) ) { return false; } // 注册插件库 this.plugins = { "event" : PE, // 事件相关 "view" : PV, // 视图相关 "vbinder" : PVB, // 视图监听 }; // 全局服务开启 nx.plugin = this; return true; }, // 销毁 uninitialize: function() { // 全局服务关闭 nx.plugin = null; // USPER return this._super(); }, // 添加插件 add: function( _inst, _keys ) { // 对象非法 if( nx.dt.objEmpty( _inst ) ) { nx.error( "[插件]添加失败,对象非法!" ); return false; } // 空插件 if( nx.dt.arrEmpty( _keys ) ) { nx.error( "[插件]添加失败,插件无效!" ); return false; } // 安装 _inst._plugins = _keys; for( let i = 0; i < _keys.length; ++i ) { let key = _keys[i]; let handle = this.plugins[key]; if( nx.dt.objEmpty( handle ) ) { nx.error( "[插件]添加失败,插件无效:", key ); continue; } handle.install( _inst ); } return true; }, // 删除插件 remove: function( _inst ) { // 对象非法 if( nx.dt.objEmpty( _inst ) ) { nx.error( "[插件]删除失败,对象非法!" ); return false; } // 空插件 if( nx.dt.arrEmpty( _inst._plugins ) ) { return true; } // 删除 for( let i = 0; i < _inst._plugins.length; ++i ) { let key = _inst._plugins[i]; let handle = this.plugins[key]; if( nx.dt.objEmpty( handle ) ) { nx.error( "[插件]删除失败,插件无效:", key ); continue; } handle.uninstall( _inst ); } delete _inst._plugins; return true; }, } ); // 模块导出 module.exports = NxPluginManager;