mm_machine 3.1.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/manager.js +186 -222
- package/package.json +1 -1
- package/registry.js +140 -158
package/manager.js
CHANGED
|
@@ -86,7 +86,9 @@ class Manager extends Mod {
|
|
|
86
86
|
* 作用域
|
|
87
87
|
* @type {string}
|
|
88
88
|
*/
|
|
89
|
-
scope: $.val && $.val.scope ? $.val.scope + '' : 'server'
|
|
89
|
+
scope: $.val && $.val.scope ? $.val.scope + '' : 'server',
|
|
90
|
+
// 触发字段,添加到注册表时注入此字段,可以避免先加载模块,满足触发条件后再加载
|
|
91
|
+
trigger_key: []
|
|
90
92
|
};
|
|
91
93
|
|
|
92
94
|
/**
|
|
@@ -106,18 +108,12 @@ class Manager extends Mod {
|
|
|
106
108
|
);
|
|
107
109
|
|
|
108
110
|
this.scanner = new Scanner();
|
|
109
|
-
// 模块信息列表 { name: '', sort: 0, available: true, enabled: true }
|
|
110
|
-
this.infos = [];
|
|
111
111
|
// 模块
|
|
112
112
|
this.mods = mods;
|
|
113
113
|
if (Drive) {
|
|
114
114
|
this.Drive = Drive;
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
get list() {
|
|
119
|
-
return this.getList();
|
|
120
|
-
}
|
|
121
117
|
}
|
|
122
118
|
|
|
123
119
|
/**
|
|
@@ -128,52 +124,16 @@ Manager.prototype.getEventer = function () {
|
|
|
128
124
|
return $.eventer;
|
|
129
125
|
};
|
|
130
126
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
* @returns {object[]} 模块信息数组
|
|
134
|
-
*/
|
|
135
|
-
Manager.prototype.getInfos = function () {
|
|
136
|
-
return this.infos;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 创建模块状态模型
|
|
141
|
-
* @param {string} name 模块名
|
|
142
|
-
* @param {number} sort 排序值
|
|
143
|
-
* @param {boolean} state 状态
|
|
144
|
-
* @param {string} file 文件名
|
|
145
|
-
* @returns {object} 模块状态模型
|
|
146
|
-
*/
|
|
147
|
-
Manager.prototype.newInfo = function (name, sort, state, file = '') {
|
|
148
|
-
return {
|
|
149
|
-
name,
|
|
150
|
-
sort,
|
|
151
|
-
state,
|
|
152
|
-
enabled: true,
|
|
153
|
-
file: file || '',
|
|
154
|
-
get available() {
|
|
155
|
-
return this.state == true && this.enabled;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
127
|
+
Manager.prototype.getParentKey = function () {
|
|
128
|
+
return this.constructor.name + '_' + this.name;
|
|
158
129
|
};
|
|
159
130
|
|
|
160
131
|
/**
|
|
161
|
-
*
|
|
162
|
-
* @
|
|
132
|
+
* 获取所有模块信息
|
|
133
|
+
* @returns {object[]} 模块配置数组
|
|
163
134
|
*/
|
|
164
|
-
Manager.prototype.
|
|
165
|
-
|
|
166
|
-
for (let i = 0; i < this.infos.length; i++) {
|
|
167
|
-
let o = this.infos[i];
|
|
168
|
-
if (o.name == info.name) {
|
|
169
|
-
this.infos[i] = info;
|
|
170
|
-
has = true;
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if (!has) {
|
|
175
|
-
this.infos.push(info);
|
|
176
|
-
}
|
|
135
|
+
Manager.prototype.getInfos = function () {
|
|
136
|
+
return this._getSortedEntries();
|
|
177
137
|
};
|
|
178
138
|
|
|
179
139
|
/**
|
|
@@ -182,21 +142,20 @@ Manager.prototype.setInfo = function (info) {
|
|
|
182
142
|
* @returns {object[]} 模块对象数组
|
|
183
143
|
*/
|
|
184
144
|
Manager.prototype.getList = function (keyword) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
let mod = this.getMod(info.name);
|
|
189
|
-
if (!mod) {
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
mod.config.state = info.state;
|
|
193
|
-
list.push(mod);
|
|
194
|
-
}
|
|
145
|
+
var entries = this._getSortedEntries();
|
|
146
|
+
|
|
147
|
+
// 先用 entry 做关键词过滤,避免无效实例化
|
|
195
148
|
if (keyword) {
|
|
196
|
-
|
|
197
|
-
(o)
|
|
198
|
-
|
|
199
|
-
|
|
149
|
+
entries = entries.filter(function (o) {
|
|
150
|
+
return o.name.includes(keyword) || o.title.includes(keyword) || o.description.includes(keyword);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
var list = [];
|
|
155
|
+
for (var i = 0; i < entries.length; i++) {
|
|
156
|
+
var mod = this.getMod(entries[i].name);
|
|
157
|
+
if (!mod) continue;
|
|
158
|
+
list.push(mod);
|
|
200
159
|
}
|
|
201
160
|
return list;
|
|
202
161
|
};
|
|
@@ -226,33 +185,15 @@ Manager.prototype.Drive = Drive;
|
|
|
226
185
|
* 更新模块信息
|
|
227
186
|
*/
|
|
228
187
|
Manager.prototype.updateInfo = function () {
|
|
229
|
-
|
|
230
|
-
let mods = this.getMods();
|
|
231
|
-
for (let name in mods) {
|
|
232
|
-
let o = mods[name];
|
|
233
|
-
let info = this.newInfo(name, o.config.sort, o.config.state, o.config_file);
|
|
234
|
-
this.infos.push(info);
|
|
235
|
-
}
|
|
236
|
-
this.sort();
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* 排序
|
|
241
|
-
*/
|
|
242
|
-
Manager.prototype._sort = function () {
|
|
243
|
-
var sort_key = this.config.sort_key || 'sort';
|
|
244
|
-
this.infos.sort((o1, o2) => {
|
|
245
|
-
let p1 = o1[sort_key] || 100;
|
|
246
|
-
let p2 = o2[sort_key] || 100;
|
|
247
|
-
return p1 - p2;
|
|
248
|
-
});
|
|
188
|
+
// 由 registry 管理模块信息,无需额外操作
|
|
249
189
|
};
|
|
250
190
|
|
|
251
191
|
/**
|
|
252
|
-
*
|
|
192
|
+
* 从 registry 获取按排序后的模块配置列表
|
|
193
|
+
* @returns {Array} 排序后的模块配置数组
|
|
253
194
|
*/
|
|
254
|
-
Manager.prototype.
|
|
255
|
-
this.
|
|
195
|
+
Manager.prototype._getSortedEntries = function () {
|
|
196
|
+
return $.registry.getMods(this.getParentKey());
|
|
256
197
|
};
|
|
257
198
|
|
|
258
199
|
// ==== 模块管理接口 ====
|
|
@@ -291,11 +232,34 @@ Manager.prototype.setMod = function (name, mod) {
|
|
|
291
232
|
|
|
292
233
|
/**
|
|
293
234
|
* 获取模块
|
|
235
|
+
* 内存不存在时,若启用注册表则从注册表配置按需实例化
|
|
294
236
|
* @param {string} name 模块名
|
|
295
|
-
* @returns {object|null}
|
|
237
|
+
* @returns {object|null} 模块对象
|
|
296
238
|
*/
|
|
297
239
|
Manager.prototype.getMod = function (name) {
|
|
298
|
-
|
|
240
|
+
var mod = this.mods[name];
|
|
241
|
+
if (mod) return mod;
|
|
242
|
+
|
|
243
|
+
// 注册表回退:通过 _getSortedEntries 查找配置并按需实例化
|
|
244
|
+
if (this.config.use_registry && $.registry) {
|
|
245
|
+
var entries = this._getSortedEntries();
|
|
246
|
+
for (var i = 0; i < entries.length; i++) {
|
|
247
|
+
if (entries[i].name === name) {
|
|
248
|
+
var entry = entries[i];
|
|
249
|
+
var ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
|
|
250
|
+
mod = new ModClass(entry, this);
|
|
251
|
+
mod.mode = this.config.mode;
|
|
252
|
+
mod.config_file = entry.config_file;
|
|
253
|
+
if (mod._preset) {
|
|
254
|
+
mod._preset();
|
|
255
|
+
}
|
|
256
|
+
this.mods[name] = mod;
|
|
257
|
+
return mod;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return null;
|
|
299
263
|
};
|
|
300
264
|
|
|
301
265
|
/**
|
|
@@ -394,13 +358,8 @@ Manager.prototype.registerMod = function (config_file, config = {}, script = {})
|
|
|
394
358
|
|
|
395
359
|
// 启用注册表时,自动写入 $.registry 供后续实例复用
|
|
396
360
|
if (this.config.use_registry && $.registry) {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
return o.name === mod.config.name;
|
|
400
|
-
});
|
|
401
|
-
if (!has) {
|
|
402
|
-
// 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
|
|
403
|
-
var parent_name = mod.config.parent || this.config.name;
|
|
361
|
+
// 直接通过 registry 的 getMod 判断是否已存在,避免 _getSortedEntries 重复
|
|
362
|
+
if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
|
|
404
363
|
var entry = {
|
|
405
364
|
name: mod.config.name,
|
|
406
365
|
title: mod.config.title,
|
|
@@ -409,14 +368,18 @@ Manager.prototype.registerMod = function (config_file, config = {}, script = {})
|
|
|
409
368
|
state: mod.config.state,
|
|
410
369
|
config_file: config_file,
|
|
411
370
|
main: mod.config.main || '',
|
|
412
|
-
|
|
413
|
-
class_name: mod.config.class_name || 'Drive',
|
|
414
|
-
parent: parent_name,
|
|
415
|
-
dir: this.config.dir || ''
|
|
371
|
+
class_name: this.config.class_name || 'Drive'
|
|
416
372
|
};
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
373
|
+
// 获取触发键
|
|
374
|
+
let trigger = this.config.trigger;
|
|
375
|
+
if (trigger && trigger.length > 0) {
|
|
376
|
+
for (let t of trigger) {
|
|
377
|
+
entry.trigger[t] = mod.config[t];
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
// 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
|
|
381
|
+
// 同步 registry 的内部缓存,确保 Index.getMod() 可查
|
|
382
|
+
$.registry.setMod(this.getParentKey(), mod.config.name, entry);
|
|
420
383
|
}
|
|
421
384
|
}
|
|
422
385
|
|
|
@@ -529,12 +492,12 @@ Manager.prototype.loadScripts = async function () {
|
|
|
529
492
|
/**
|
|
530
493
|
* 更新配置前的钩子函数
|
|
531
494
|
*/
|
|
532
|
-
Manager.prototype.updateBefore = function () {};
|
|
495
|
+
Manager.prototype.updateBefore = function () { };
|
|
533
496
|
|
|
534
497
|
/**
|
|
535
498
|
* 更新配置前的钩子函数
|
|
536
499
|
*/
|
|
537
|
-
Manager.prototype.updateAfter = function () {};
|
|
500
|
+
Manager.prototype.updateAfter = function () { };
|
|
538
501
|
|
|
539
502
|
/**
|
|
540
503
|
* 更新配置
|
|
@@ -545,13 +508,11 @@ Manager.prototype.update = async function (dir, clear = true) {
|
|
|
545
508
|
try {
|
|
546
509
|
if (clear) {
|
|
547
510
|
this.clearMods();
|
|
548
|
-
this.infos = [];
|
|
549
511
|
}
|
|
550
512
|
|
|
551
513
|
var search_dir = dir || this.config.dir;
|
|
552
514
|
var files = await this._findFiles(search_dir);
|
|
553
515
|
this.registerMods(files);
|
|
554
|
-
this.updateInfo();
|
|
555
516
|
|
|
556
517
|
if (!this.config.lazy_load) {
|
|
557
518
|
await this.loadScripts();
|
|
@@ -585,62 +546,38 @@ Manager.prototype._getRegistryConfig = function (config_file) {
|
|
|
585
546
|
};
|
|
586
547
|
|
|
587
548
|
/**
|
|
588
|
-
* 从 registry 缓存加载模块(按 parent
|
|
589
|
-
*
|
|
590
|
-
* @param {string} parent 父级标识,空或不传时加载所有模块
|
|
591
|
-
* @param {string} dir 目录路径,用于区分不同目录下的同名管理器(如 agent 的 user/contact/record)
|
|
549
|
+
* 从 registry 缓存加载模块(按 parent 字段匹配)
|
|
550
|
+
* 从 registry.mod_dict 中按 parent 过滤并创建模块实例
|
|
592
551
|
* @returns {boolean} 是否加载到模块
|
|
593
552
|
*/
|
|
594
|
-
Manager.prototype._loadFromRegistry = function (
|
|
553
|
+
Manager.prototype._loadFromRegistry = function () {
|
|
595
554
|
var registry = $.registry;
|
|
596
|
-
if (!registry || !registry.
|
|
555
|
+
if (!registry || !registry.mod_dict) return false;
|
|
556
|
+
|
|
557
|
+
var parent = this.getParentKey();
|
|
558
|
+
var lt = registry.getMods(parent);
|
|
559
|
+
if (!lt.length) return false;
|
|
597
560
|
|
|
598
561
|
var ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
|
|
599
562
|
var mode = this.config.mode;
|
|
600
|
-
var sort_key = this.config.sort_key || 'sort';
|
|
601
|
-
var list = registry.module_list;
|
|
602
563
|
var mods = this.mods;
|
|
603
|
-
var infos = this.infos;
|
|
604
|
-
var count = 0;
|
|
605
|
-
|
|
606
|
-
for (var i = 0; i < list.length; i++) {
|
|
607
|
-
var entry = list[i];
|
|
608
|
-
// 按 parent 字段匹配,不传或空时加载所有
|
|
609
|
-
if (parent && entry.parent !== parent) continue;
|
|
610
|
-
// 按 dir 字段匹配,确保不同目录的管理器互不干扰
|
|
611
|
-
if (dir && entry.dir !== dir) continue;
|
|
612
|
-
|
|
613
|
-
var config = entry;
|
|
614
|
-
var mod = new ModClass(config, this);
|
|
615
|
-
mod.mode = mode;
|
|
616
|
-
mod.config_file = entry.config_file;
|
|
617
|
-
if (mod._preset) {
|
|
618
|
-
mod._preset();
|
|
619
|
-
}
|
|
620
|
-
mods[config.name] = mod;
|
|
621
|
-
infos.push({
|
|
622
|
-
name: config.name,
|
|
623
|
-
sort: config.sort || 100,
|
|
624
|
-
state: config.state,
|
|
625
|
-
enabled: true,
|
|
626
|
-
file: entry.config_file || '',
|
|
627
|
-
get available() {
|
|
628
|
-
return this.state == true && this.enabled;
|
|
629
|
-
}
|
|
630
|
-
});
|
|
631
|
-
count++;
|
|
632
|
-
}
|
|
633
564
|
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
565
|
+
for (var i = 0; i < lt.length; i++) {
|
|
566
|
+
var entry = lt[i];
|
|
567
|
+
|
|
568
|
+
if (!this.config.lazy_load) {
|
|
569
|
+
// 非懒加载模式:立即实例化
|
|
570
|
+
var mod = new ModClass(entry, this);
|
|
571
|
+
mod.mode = mode;
|
|
572
|
+
mod.config_file = entry.config_file;
|
|
573
|
+
if (mod._preset) {
|
|
574
|
+
mod._preset();
|
|
575
|
+
}
|
|
576
|
+
mods[entry.name] = mod;
|
|
577
|
+
}
|
|
641
578
|
}
|
|
642
579
|
|
|
643
|
-
return
|
|
580
|
+
return true;
|
|
644
581
|
};
|
|
645
582
|
|
|
646
583
|
/**
|
|
@@ -674,8 +611,21 @@ Manager.prototype._findFiles = async function (dir) {
|
|
|
674
611
|
Manager.prototype.main = async function (name, method, ...params) {
|
|
675
612
|
var result = null;
|
|
676
613
|
if (name) {
|
|
677
|
-
|
|
678
|
-
|
|
614
|
+
var mod = this.mods[name];
|
|
615
|
+
// 注册表模式下一次遍历查 entry,避免 getMod 重复 _getSortedEntries
|
|
616
|
+
if (!mod && this.config.use_registry && $.registry) {
|
|
617
|
+
var entries = this._getSortedEntries();
|
|
618
|
+
var entry = entries.find(function (o) { return o.name === name; });
|
|
619
|
+
if (entry && entry.state === 1) {
|
|
620
|
+
var ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
|
|
621
|
+
mod = new ModClass(entry, this);
|
|
622
|
+
mod.mode = this.config.mode;
|
|
623
|
+
mod.config_file = entry.config_file;
|
|
624
|
+
if (mod._preset) mod._preset();
|
|
625
|
+
this.mods[name] = mod;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (mod) {
|
|
679
629
|
result = await this._runMod(mod, method, ...params);
|
|
680
630
|
}
|
|
681
631
|
} else if (name === null) {
|
|
@@ -693,22 +643,26 @@ Manager.prototype.main = async function (name, method, ...params) {
|
|
|
693
643
|
*/
|
|
694
644
|
Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
695
645
|
var result;
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
646
|
+
var entries = this._getSortedEntries();
|
|
647
|
+
for (var i = 0; i < entries.length; i++) {
|
|
648
|
+
var entry = entries[i];
|
|
649
|
+
// 直接用 entry.state 过滤,避免无效实例化
|
|
650
|
+
if (entry.state !== 1) continue;
|
|
651
|
+
|
|
652
|
+
var mod = this.getMod(entry.name);
|
|
653
|
+
if (!mod) continue;
|
|
654
|
+
|
|
655
|
+
var ret = null;
|
|
656
|
+
try {
|
|
657
|
+
ret = await this._runMod(mod, method, ...params);
|
|
658
|
+
} catch (err) {
|
|
659
|
+
this.log('error', `执行模块方法失败: `, err);
|
|
660
|
+
}
|
|
661
|
+
if (ret !== null && ret !== undefined) {
|
|
662
|
+
result = ret;
|
|
663
|
+
// 如果结果不为空且有结束标志,则停止迭代
|
|
664
|
+
if (mod.config?.end) {
|
|
665
|
+
break;
|
|
712
666
|
}
|
|
713
667
|
}
|
|
714
668
|
}
|
|
@@ -752,7 +706,7 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
|
|
|
752
706
|
/**
|
|
753
707
|
* 初始化管理器
|
|
754
708
|
*/
|
|
755
|
-
Manager.prototype._initManager = function () {};
|
|
709
|
+
Manager.prototype._initManager = function () { };
|
|
756
710
|
|
|
757
711
|
/**
|
|
758
712
|
* 获取二次开发模块目录
|
|
@@ -765,37 +719,43 @@ Manager.prototype.getDir = function () {
|
|
|
765
719
|
|
|
766
720
|
/**
|
|
767
721
|
* 加载资源
|
|
768
|
-
*
|
|
722
|
+
* 启用注册表时优先从注册表读取,快速启动;
|
|
723
|
+
* 同时在后台异步扫描文件系统,确保注册表数据为物理最新。
|
|
769
724
|
*/
|
|
770
725
|
Manager.prototype._loadSources = async function () {
|
|
771
|
-
var base_dir = this.getBaseDir();
|
|
772
|
-
var dir = this.getDir();
|
|
773
|
-
|
|
774
726
|
// 启用注册表且有缓存数据时,走内存快路径
|
|
775
727
|
// 按管理器自身 name 过滤,多 Manager 共享 $.registry 时互不污染
|
|
776
|
-
if (
|
|
777
|
-
this.config.use_registry &&
|
|
778
|
-
$.registry &&
|
|
779
|
-
$.registry.module_list &&
|
|
780
|
-
$.registry.module_list.length > 0
|
|
781
|
-
) {
|
|
728
|
+
if (this.config.use_registry && $.registry && $.registry.mod_dict) {
|
|
782
729
|
// 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
|
|
783
|
-
if (this._loadFromRegistry(
|
|
784
|
-
|
|
730
|
+
if (this._loadFromRegistry()) {
|
|
731
|
+
// 后台异步扫描文件系统,确保物理最新
|
|
732
|
+
setImmediate(() => this._scanForUpdate());
|
|
785
733
|
return;
|
|
786
734
|
}
|
|
787
735
|
}
|
|
788
736
|
|
|
789
|
-
//
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
737
|
+
// 注册表无匹配,走文件系统扫描
|
|
738
|
+
await this._scanForUpdate();
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* 后台扫描文件系统并同步到注册表
|
|
743
|
+
* 不阻塞启动,让注册表始终与磁盘保持同步
|
|
744
|
+
*/
|
|
745
|
+
Manager.prototype._scanForUpdate = async function () {
|
|
746
|
+
var base_dir = this.getBaseDir();
|
|
747
|
+
var dir = this.getDir();
|
|
748
|
+
try {
|
|
749
|
+
if (base_dir) {
|
|
750
|
+
await Promise.all([this.call('update', base_dir, true), this.call('update', dir, false)]);
|
|
751
|
+
} else {
|
|
752
|
+
await this.call('update', dir, true);
|
|
753
|
+
}
|
|
754
|
+
if (this.config.use_registry && $.registry) {
|
|
755
|
+
$.registry.save();
|
|
756
|
+
}
|
|
757
|
+
} catch (err) {
|
|
758
|
+
this.log('error', '后台扫描同步失败', err);
|
|
799
759
|
}
|
|
800
760
|
};
|
|
801
761
|
|
|
@@ -882,14 +842,11 @@ Manager.prototype._ensureModLoaded = async function (name) {
|
|
|
882
842
|
* @returns {Promise} 加载完成
|
|
883
843
|
*/
|
|
884
844
|
Manager.prototype._ensureAllLoaded = async function () {
|
|
885
|
-
var infos = this.getInfos();
|
|
886
845
|
var promises = [];
|
|
846
|
+
var entries = this._getSortedEntries();
|
|
887
847
|
|
|
888
|
-
for (var i = 0; i <
|
|
889
|
-
|
|
890
|
-
promises.push(
|
|
891
|
-
this._ensureModLoaded(info.name)
|
|
892
|
-
);
|
|
848
|
+
for (var i = 0; i < entries.length; i++) {
|
|
849
|
+
promises.push(this._ensureModLoaded(entries[i].name));
|
|
893
850
|
}
|
|
894
851
|
await Promise.all(promises);
|
|
895
852
|
};
|
|
@@ -906,11 +863,10 @@ Manager.prototype.runWait = async function (method, ...params) {
|
|
|
906
863
|
}
|
|
907
864
|
|
|
908
865
|
let result;
|
|
909
|
-
|
|
866
|
+
var entries = this._getSortedEntries();
|
|
910
867
|
|
|
911
|
-
for (
|
|
912
|
-
let
|
|
913
|
-
let mod = await this._ensureModLoaded(info.name);
|
|
868
|
+
for (var i = 0; i < entries.length; i++) {
|
|
869
|
+
let mod = await this._ensureModLoaded(entries[i].name);
|
|
914
870
|
if (!mod) continue;
|
|
915
871
|
|
|
916
872
|
try {
|
|
@@ -919,7 +875,11 @@ Manager.prototype.runWait = async function (method, ...params) {
|
|
|
919
875
|
result = ret;
|
|
920
876
|
}
|
|
921
877
|
} catch (error) {
|
|
922
|
-
this.log(
|
|
878
|
+
this.log(
|
|
879
|
+
'error',
|
|
880
|
+
'\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
|
|
881
|
+
error
|
|
882
|
+
);
|
|
923
883
|
}
|
|
924
884
|
}
|
|
925
885
|
|
|
@@ -940,15 +900,18 @@ Manager.prototype.runAsync = async function (method, ...params) {
|
|
|
940
900
|
// 确保所有模块都已加载
|
|
941
901
|
await this._ensureAllLoaded();
|
|
942
902
|
|
|
943
|
-
|
|
903
|
+
var entries = this._getSortedEntries();
|
|
944
904
|
|
|
945
|
-
for (
|
|
946
|
-
|
|
947
|
-
let mod = this.getMod(info.name);
|
|
905
|
+
for (var i = 0; i < entries.length; i++) {
|
|
906
|
+
var mod = this.mods[entries[i].name];
|
|
948
907
|
if (!mod) continue;
|
|
949
908
|
|
|
950
909
|
mod.do(method, ...params).catch((error) => {
|
|
951
|
-
this.log(
|
|
910
|
+
this.log(
|
|
911
|
+
'error',
|
|
912
|
+
'\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
|
|
913
|
+
error
|
|
914
|
+
);
|
|
952
915
|
});
|
|
953
916
|
}
|
|
954
917
|
|
|
@@ -970,11 +933,10 @@ Manager.prototype.runAll = async function (method, ...params) {
|
|
|
970
933
|
await this._ensureAllLoaded();
|
|
971
934
|
|
|
972
935
|
const promises = [];
|
|
973
|
-
|
|
936
|
+
var entries = this._getSortedEntries();
|
|
974
937
|
|
|
975
|
-
for (
|
|
976
|
-
|
|
977
|
-
let mod = this.getMod(info.name);
|
|
938
|
+
for (var i = 0; i < entries.length; i++) {
|
|
939
|
+
var mod = this.mods[entries[i].name];
|
|
978
940
|
if (!mod) continue;
|
|
979
941
|
|
|
980
942
|
promises.push(mod.do(method, ...params));
|
|
@@ -998,11 +960,10 @@ Manager.prototype.runRace = async function (method, ...params) {
|
|
|
998
960
|
await this._ensureAllLoaded();
|
|
999
961
|
|
|
1000
962
|
const promises = [];
|
|
1001
|
-
|
|
963
|
+
var entries = this._getSortedEntries();
|
|
1002
964
|
|
|
1003
|
-
for (
|
|
1004
|
-
|
|
1005
|
-
let mod = this.getMod(info.name);
|
|
965
|
+
for (var i = 0; i < entries.length; i++) {
|
|
966
|
+
var mod = this.mods[entries[i].name];
|
|
1006
967
|
if (!mod) continue;
|
|
1007
968
|
|
|
1008
969
|
promises.push(
|
|
@@ -1027,11 +988,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
|
1027
988
|
throw new TypeError('方法名称必须是字符串');
|
|
1028
989
|
}
|
|
1029
990
|
|
|
1030
|
-
|
|
991
|
+
var entries = this._getSortedEntries();
|
|
1031
992
|
|
|
1032
|
-
for (
|
|
1033
|
-
|
|
1034
|
-
let mod = await this._ensureModLoaded(info.name);
|
|
993
|
+
for (var i = 0; i < entries.length; i++) {
|
|
994
|
+
var mod = await this._ensureModLoaded(entries[i].name);
|
|
1035
995
|
if (!mod) continue;
|
|
1036
996
|
|
|
1037
997
|
try {
|
|
@@ -1040,7 +1000,11 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
|
1040
1000
|
result = ret;
|
|
1041
1001
|
}
|
|
1042
1002
|
} catch (error) {
|
|
1043
|
-
this.log(
|
|
1003
|
+
this.log(
|
|
1004
|
+
'error',
|
|
1005
|
+
'\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
|
|
1006
|
+
error
|
|
1007
|
+
);
|
|
1044
1008
|
}
|
|
1045
1009
|
}
|
|
1046
1010
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
package/registry.js
CHANGED
|
@@ -14,14 +14,10 @@ class Registry {
|
|
|
14
14
|
...config
|
|
15
15
|
};
|
|
16
16
|
this.class_dict = this.getDefaultClassDict();
|
|
17
|
-
this.
|
|
17
|
+
this.mod_dict = {};
|
|
18
18
|
|
|
19
19
|
// 注册表缓存:类名 -> 类构造函数
|
|
20
|
-
this.
|
|
21
|
-
// 注册表缓存:模块名 -> { config, instance }
|
|
22
|
-
this._module_cache = {};
|
|
23
|
-
// 类名索引:类名 -> [模块名]
|
|
24
|
-
this._class_index = {};
|
|
20
|
+
this.class_cache = {};
|
|
25
21
|
this._preset();
|
|
26
22
|
}
|
|
27
23
|
}
|
|
@@ -81,7 +77,7 @@ Registry.prototype.registerClass = function (class_name, class_ref) {
|
|
|
81
77
|
if (!class_name || !class_ref) {
|
|
82
78
|
throw new TypeError('模块类名和类引用不能为空');
|
|
83
79
|
}
|
|
84
|
-
this.
|
|
80
|
+
this.class_cache[class_name] = class_ref;
|
|
85
81
|
};
|
|
86
82
|
|
|
87
83
|
/**
|
|
@@ -90,60 +86,41 @@ Registry.prototype.registerClass = function (class_name, class_ref) {
|
|
|
90
86
|
* @returns {Function} 模块类构造函数
|
|
91
87
|
*/
|
|
92
88
|
Registry.prototype.getClass = function (class_name) {
|
|
93
|
-
return this.
|
|
89
|
+
return this.class_cache[class_name];
|
|
94
90
|
};
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 注册模块配置到缓存(不实例化)
|
|
98
|
-
* @param {object} config 模块配置项
|
|
99
|
-
* @returns {object} 注册的配置项
|
|
100
|
-
* @throws {Error} 模块配置项错误
|
|
101
|
-
*/
|
|
102
|
-
Registry.prototype.registerModule = function (config) {
|
|
103
|
-
if (!config) {
|
|
104
|
-
throw new TypeError('模块配置不能为空');
|
|
105
|
-
}
|
|
106
|
-
if (!config.name) {
|
|
107
|
-
throw new TypeError('模块名称不能为空');
|
|
108
|
-
}
|
|
109
|
-
var class_name = config.class_name || 'Drive';
|
|
110
|
-
this._module_cache[config.name] = {
|
|
111
|
-
config: config,
|
|
112
|
-
instance: null
|
|
113
|
-
};
|
|
114
|
-
// 按类名建立索引
|
|
115
|
-
if (!this._class_index[class_name]) {
|
|
116
|
-
this._class_index[class_name] = [];
|
|
117
|
-
}
|
|
118
|
-
this._class_index[class_name].push(config.name);
|
|
119
|
-
return config;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
91
|
/**
|
|
123
92
|
* 获取模块实例(懒加载)
|
|
124
93
|
* 首次调用时根据 config 和已注册的类构造函数实例化
|
|
125
94
|
* @param {string} name 模块名
|
|
126
95
|
* @returns {object|null} 模块实例
|
|
127
96
|
*/
|
|
128
|
-
Registry.prototype.
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
var
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
try {
|
|
140
|
-
entry.instance = new CS(entry.config);
|
|
141
|
-
} catch (err) {
|
|
142
|
-
console.error('实例化模块 ' + name + ' 失败: ' + err.message);
|
|
143
|
-
return null;
|
|
97
|
+
Registry.prototype.getMod = function (name) {
|
|
98
|
+
// 从 mod_dict 中查找配置
|
|
99
|
+
var config = null;
|
|
100
|
+
for (var parent in this.mod_dict) {
|
|
101
|
+
var lt = this.mod_dict[parent];
|
|
102
|
+
for (var i = 0; i < lt.length; i++) {
|
|
103
|
+
if (lt[i].name === name) {
|
|
104
|
+
config = lt[i];
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
144
107
|
}
|
|
108
|
+
if (config) break;
|
|
109
|
+
}
|
|
110
|
+
if (!config) return null;
|
|
111
|
+
|
|
112
|
+
var class_name = config.class_name || 'Drive';
|
|
113
|
+
var CS = this.class_cache[class_name];
|
|
114
|
+
if (!CS) {
|
|
115
|
+
console.error('模块类 ' + class_name + ' 未注册');
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
return new CS(config);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.error('实例化模块 ' + name + ' 失败: ' + err.message);
|
|
122
|
+
return null;
|
|
145
123
|
}
|
|
146
|
-
return entry.instance;
|
|
147
124
|
};
|
|
148
125
|
|
|
149
126
|
/**
|
|
@@ -151,31 +128,21 @@ Registry.prototype.getModule = function (name) {
|
|
|
151
128
|
* @param {string} class_name 模块类名
|
|
152
129
|
* @returns {Array} 模块实例数组
|
|
153
130
|
*/
|
|
154
|
-
Registry.prototype.
|
|
155
|
-
var
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
Registry.prototype.unregisterModule = function (name) {
|
|
167
|
-
var entry = this._module_cache[name];
|
|
168
|
-
if (!entry) return;
|
|
169
|
-
|
|
170
|
-
var class_name = entry.config.class_name || 'Drive';
|
|
171
|
-
var names = this._class_index[class_name];
|
|
172
|
-
if (names) {
|
|
173
|
-
var idx = names.indexOf(name);
|
|
174
|
-
if (idx !== -1) {
|
|
175
|
-
names.splice(idx, 1);
|
|
131
|
+
Registry.prototype.getModsFromClass = function (class_name) {
|
|
132
|
+
var result = [];
|
|
133
|
+
for (var parent in this.mod_dict) {
|
|
134
|
+
var lt = this.mod_dict[parent];
|
|
135
|
+
for (var i = 0; i < lt.length; i++) {
|
|
136
|
+
var cfg = lt[i];
|
|
137
|
+
if ((cfg.class_name || 'Drive') === class_name) {
|
|
138
|
+
var instance = this.getMod(cfg.name);
|
|
139
|
+
if (instance) {
|
|
140
|
+
result.push(instance);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
176
143
|
}
|
|
177
144
|
}
|
|
178
|
-
|
|
145
|
+
return result;
|
|
179
146
|
};
|
|
180
147
|
|
|
181
148
|
/**
|
|
@@ -197,11 +164,12 @@ Registry.prototype.load = function () {
|
|
|
197
164
|
}
|
|
198
165
|
}
|
|
199
166
|
|
|
200
|
-
//
|
|
201
|
-
for (var
|
|
202
|
-
var
|
|
203
|
-
|
|
204
|
-
|
|
167
|
+
// 注册模块配置
|
|
168
|
+
for (var parent in this.mod_dict) {
|
|
169
|
+
var lt = this.mod_dict[parent];
|
|
170
|
+
for (var i = 0; i < lt.length; i++) {
|
|
171
|
+
var mod_cfg = lt[i];
|
|
172
|
+
this.setMod(parent, mod_cfg.name, mod_cfg);
|
|
205
173
|
}
|
|
206
174
|
}
|
|
207
175
|
};
|
|
@@ -221,60 +189,77 @@ Registry.prototype.newModel = function () {
|
|
|
221
189
|
version: '1.0.0',
|
|
222
190
|
dependencies: {},
|
|
223
191
|
sort: 100,
|
|
224
|
-
|
|
192
|
+
state: 1,
|
|
225
193
|
auto_start: 'auto',
|
|
226
194
|
class_name: 'Drive',
|
|
227
195
|
config_file: '',
|
|
228
|
-
main: ''
|
|
229
|
-
values: {}
|
|
196
|
+
main: ''
|
|
230
197
|
};
|
|
231
198
|
};
|
|
232
199
|
|
|
233
200
|
/**
|
|
234
201
|
* 设置模块
|
|
202
|
+
* @param {string} parent 父级名称
|
|
203
|
+
* @param {string} name 模块名称
|
|
235
204
|
* @param {object} config 模块配置项
|
|
236
205
|
* @returns {object} 模块配置项
|
|
237
206
|
*/
|
|
238
|
-
Registry.prototype.
|
|
207
|
+
Registry.prototype.setMod = function (parent, name, config) {
|
|
208
|
+
if (!parent) {
|
|
209
|
+
throw new TypeError('父级名称(parent)不能为空');
|
|
210
|
+
}
|
|
211
|
+
else if (typeof parent !== 'string') {
|
|
212
|
+
throw new TypeError('父级名称(parent)必须为字符串');
|
|
213
|
+
}
|
|
214
|
+
if (!name) {
|
|
215
|
+
throw new TypeError('模块名称(name)不能为空');
|
|
216
|
+
}
|
|
217
|
+
else if (typeof name !== 'string') {
|
|
218
|
+
throw new TypeError('模块名称(name)必须为字符串');
|
|
219
|
+
}
|
|
220
|
+
if (!config) {
|
|
221
|
+
throw new TypeError('模块配置项不能为空');
|
|
222
|
+
}
|
|
223
|
+
else if (typeof config !== 'object') {
|
|
224
|
+
throw new TypeError('模块配置项必须为对象');
|
|
225
|
+
}
|
|
239
226
|
let cg = this.checkConfig(config);
|
|
227
|
+
cg.parent = parent;
|
|
228
|
+
|
|
229
|
+
// 按 parent 分组存储到 mod_dict
|
|
230
|
+
if (!this.mod_dict[parent]) {
|
|
231
|
+
this.mod_dict[parent] = [];
|
|
232
|
+
}
|
|
233
|
+
var lt = this.mod_dict[parent];
|
|
234
|
+
|
|
240
235
|
let has = false;
|
|
241
|
-
for (let i = 0; i <
|
|
242
|
-
let o =
|
|
243
|
-
if (o.name ===
|
|
244
|
-
|
|
245
|
-
throw new Error('模块类名与配置项类名不一致');
|
|
246
|
-
}
|
|
247
|
-
this.module_list[i] = cg;
|
|
236
|
+
for (let i = 0; i < lt.length; i++) {
|
|
237
|
+
let o = lt[i];
|
|
238
|
+
if (o.name === name) {
|
|
239
|
+
lt[i] = cg;
|
|
248
240
|
has = true;
|
|
249
241
|
break;
|
|
250
242
|
}
|
|
251
243
|
}
|
|
252
244
|
if (!has) {
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
// 同步更新 _module_cache,避免 get() 读到脏数据
|
|
256
|
-
if (this._module_cache[cg.name]) {
|
|
257
|
-
this._module_cache[cg.name].config = cg;
|
|
258
|
-
} else {
|
|
259
|
-
this.registerModule(cg);
|
|
245
|
+
lt.push(cg);
|
|
260
246
|
}
|
|
261
|
-
// 清除外部缓存索引
|
|
262
|
-
this._config_map = null;
|
|
263
247
|
return cg;
|
|
264
248
|
};
|
|
265
249
|
|
|
266
250
|
/**
|
|
267
251
|
* 获取模块
|
|
252
|
+
* @param {string} parent 父级名称
|
|
268
253
|
* @param {string} name 模块名称
|
|
269
254
|
* @returns {object} 模块配置项
|
|
270
255
|
*/
|
|
271
|
-
Registry.prototype.
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
256
|
+
Registry.prototype.getMod = function (parent, name) {
|
|
257
|
+
var lt = this.mod_dict[parent];
|
|
258
|
+
if (lt) {
|
|
259
|
+
for (let i = 0; i < lt.length; i++) {
|
|
260
|
+
if (lt[i].name === name) {
|
|
261
|
+
return lt[i];
|
|
262
|
+
}
|
|
278
263
|
}
|
|
279
264
|
}
|
|
280
265
|
return null;
|
|
@@ -282,41 +267,35 @@ Registry.prototype.get = function (name) {
|
|
|
282
267
|
|
|
283
268
|
/**
|
|
284
269
|
* 删除模块
|
|
270
|
+
* @param {string} parent 父级名称
|
|
285
271
|
* @param {string} name 模块名称
|
|
286
272
|
* @returns {object} 模块配置项
|
|
287
273
|
*/
|
|
288
|
-
Registry.prototype.
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
274
|
+
Registry.prototype.delMod = function (parent, name) {
|
|
275
|
+
var lt = this.mod_dict[parent];
|
|
276
|
+
if (!lt) return null;
|
|
277
|
+
|
|
278
|
+
var obj = null;
|
|
279
|
+
for (let i = 0; i < lt.length; i++) {
|
|
280
|
+
if (lt[i].name === name) {
|
|
281
|
+
obj = lt[i];
|
|
282
|
+
lt.splice(i, 1);
|
|
294
283
|
break;
|
|
295
284
|
}
|
|
296
285
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
var class_name = obj.class_name || 'Drive';
|
|
300
|
-
var names = this._class_index[class_name];
|
|
301
|
-
if (names) {
|
|
302
|
-
var idx = names.indexOf(name);
|
|
303
|
-
if (idx !== -1) {
|
|
304
|
-
names.splice(idx, 1);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
delete this._module_cache[name];
|
|
286
|
+
if (lt.length === 0) {
|
|
287
|
+
delete this.mod_dict[parent];
|
|
308
288
|
}
|
|
309
|
-
// 清除外部缓存索引
|
|
310
|
-
this._config_map = null;
|
|
311
289
|
return obj;
|
|
312
290
|
};
|
|
313
291
|
|
|
314
292
|
/**
|
|
315
293
|
* 获取模块列表
|
|
294
|
+
* @param {string} parent 父级名称
|
|
316
295
|
* @returns {Array} 模块配置项数组
|
|
317
296
|
*/
|
|
318
|
-
Registry.prototype.
|
|
319
|
-
return this.
|
|
297
|
+
Registry.prototype.getMods = function (parent) {
|
|
298
|
+
return this.mod_dict[parent] || [];
|
|
320
299
|
};
|
|
321
300
|
|
|
322
301
|
/**
|
|
@@ -328,21 +307,17 @@ Registry.prototype.getList = function () {
|
|
|
328
307
|
Registry.prototype.checkConfig = function (config) {
|
|
329
308
|
let cg = { ...this.newModel(), ...config };
|
|
330
309
|
let {
|
|
331
|
-
parent,
|
|
332
310
|
name,
|
|
333
311
|
title,
|
|
334
312
|
description,
|
|
335
313
|
version,
|
|
336
|
-
|
|
314
|
+
state,
|
|
337
315
|
auto_start,
|
|
338
316
|
class_name,
|
|
339
317
|
config_file,
|
|
340
318
|
main,
|
|
341
319
|
dependencies
|
|
342
320
|
} = cg;
|
|
343
|
-
if (!parent) {
|
|
344
|
-
cg.parent = '';
|
|
345
|
-
}
|
|
346
321
|
if (!name) {
|
|
347
322
|
throw new TypeError('模块名称不能为空');
|
|
348
323
|
}
|
|
@@ -359,34 +334,42 @@ Registry.prototype.checkConfig = function (config) {
|
|
|
359
334
|
if (valid_auto.indexOf(auto_start) === -1 && ![true, false].includes(auto_start)) {
|
|
360
335
|
throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
|
|
361
336
|
}
|
|
362
|
-
if (!this.
|
|
337
|
+
if (!this.class_cache[class_name]) {
|
|
363
338
|
throw new TypeError('模块类不存在');
|
|
364
339
|
}
|
|
365
|
-
if (!enabled) {
|
|
366
|
-
cg.enabled = false;
|
|
367
|
-
} else if (enabled === 'true') {
|
|
368
|
-
cg.enabled = true;
|
|
369
|
-
}
|
|
370
340
|
if (!version) {
|
|
371
341
|
cg.version = '1.0.0';
|
|
372
342
|
}
|
|
373
343
|
if (!main) {
|
|
374
344
|
cg.main = '';
|
|
375
345
|
}
|
|
346
|
+
if (state) {
|
|
347
|
+
cg.state = 1;
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
cg.state = 0;
|
|
351
|
+
}
|
|
376
352
|
if (!dependencies) {
|
|
377
353
|
cg.dependencies = {};
|
|
378
354
|
}
|
|
379
355
|
return cg;
|
|
380
356
|
};
|
|
381
357
|
|
|
358
|
+
/**
|
|
359
|
+
* 获取注册表目录
|
|
360
|
+
* @returns {string} 注册表目录
|
|
361
|
+
*/
|
|
362
|
+
Registry.prototype.getDir = function () {
|
|
363
|
+
return this.config.dir.fullname();
|
|
364
|
+
};
|
|
365
|
+
|
|
382
366
|
/**
|
|
383
367
|
* 获取模块类文件
|
|
384
|
-
* @param {string} class_name 模块类名
|
|
385
368
|
* @returns {string} 模块类文件路径
|
|
386
369
|
*/
|
|
387
|
-
Registry.prototype.getClassFile = function (
|
|
370
|
+
Registry.prototype.getClassFile = function () {
|
|
388
371
|
let file_name = this.config.name + '_class.json';
|
|
389
|
-
let file = file_name.fullname(this.
|
|
372
|
+
let file = file_name.fullname(this.getDir());
|
|
390
373
|
// file.addDir();
|
|
391
374
|
return file;
|
|
392
375
|
};
|
|
@@ -395,9 +378,9 @@ Registry.prototype.getClassFile = function (class_name) {
|
|
|
395
378
|
* 获取模块文件
|
|
396
379
|
* @returns {string} 模块文件路径
|
|
397
380
|
*/
|
|
398
|
-
Registry.prototype.
|
|
381
|
+
Registry.prototype.getModFile = function () {
|
|
399
382
|
let file_name = this.config.name + '_module.json';
|
|
400
|
-
let file = file_name.fullname(this.
|
|
383
|
+
let file = file_name.fullname(this.getDir());
|
|
401
384
|
// file.addDir();
|
|
402
385
|
return file;
|
|
403
386
|
};
|
|
@@ -408,7 +391,7 @@ Registry.prototype.getModuleFile = function () {
|
|
|
408
391
|
*/
|
|
409
392
|
Registry.prototype.read = function () {
|
|
410
393
|
this.class_dict = this.getClassFile().loadJson() || this.getDefaultClassDict();
|
|
411
|
-
this.
|
|
394
|
+
this.mod_dict = this.getModFile().loadJson() || {};
|
|
412
395
|
};
|
|
413
396
|
|
|
414
397
|
/**
|
|
@@ -419,7 +402,7 @@ Registry.prototype.read = function () {
|
|
|
419
402
|
Registry.prototype.save = function () {
|
|
420
403
|
try {
|
|
421
404
|
this.getClassFile().saveJson(this.class_dict);
|
|
422
|
-
this.
|
|
405
|
+
this.getModFile().saveJson(this.mod_dict);
|
|
423
406
|
} catch (err) {
|
|
424
407
|
console.error('保存注册数据失败: ' + err.message);
|
|
425
408
|
}
|
|
@@ -429,18 +412,17 @@ Registry.prototype.save = function () {
|
|
|
429
412
|
* 排序模块
|
|
430
413
|
*/
|
|
431
414
|
Registry.prototype.sort = function () {
|
|
432
|
-
this.
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
return
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
});
|
|
415
|
+
for (var parent in this.mod_dict) {
|
|
416
|
+
this.mod_dict[parent].sort((a, b) => {
|
|
417
|
+
if (a.sort !== b.sort) {
|
|
418
|
+
return a.sort - b.sort;
|
|
419
|
+
}
|
|
420
|
+
if (a.name !== b.name) {
|
|
421
|
+
return a.name.localeCompare(b.name);
|
|
422
|
+
}
|
|
423
|
+
return 0;
|
|
424
|
+
});
|
|
425
|
+
}
|
|
444
426
|
};
|
|
445
427
|
|
|
446
428
|
module.exports = {
|