mm_machine 2.2.4 → 2.2.6
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/README.md +7 -7
- package/index.js +488 -641
- package/package.json +10 -8
- package/item.js +0 -837
package/index.js
CHANGED
|
@@ -1,641 +1,488 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
*
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
this.
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 默认驱动
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
*
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
*/
|
|
177
|
-
|
|
178
|
-
this.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
*
|
|
309
|
-
* @param {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
if (
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
this.
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
if (item.config?.state === 1) {
|
|
490
|
-
await item.exec('load');
|
|
491
|
-
}
|
|
492
|
-
return item;
|
|
493
|
-
} catch (err) {
|
|
494
|
-
$.log.error(`[${this.type}] 加载插件 ${name} 失败: `, err);
|
|
495
|
-
return null;
|
|
496
|
-
}
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
/**
|
|
500
|
-
* 卸载插件
|
|
501
|
-
* @param {string} name 插件名称
|
|
502
|
-
* @returns {Promise<boolean>} 是否卸载成功
|
|
503
|
-
*/
|
|
504
|
-
Index.prototype.unload = async function (name) {
|
|
505
|
-
if (!name) return false;
|
|
506
|
-
|
|
507
|
-
let item = this.get(name);
|
|
508
|
-
if (!item) return false;
|
|
509
|
-
|
|
510
|
-
try {
|
|
511
|
-
await item.exec('unload');
|
|
512
|
-
return true;
|
|
513
|
-
} catch (err) {
|
|
514
|
-
$.log.error(`[${this.type}] 卸载插件 ${name} 失败: `, err);
|
|
515
|
-
return false;
|
|
516
|
-
}
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* 重新加载插件
|
|
521
|
-
* @param {string} name 插件名称
|
|
522
|
-
* @returns {Promise<object | null>} 重新加载的插件对象
|
|
523
|
-
*/
|
|
524
|
-
Index.prototype.reload = async function (name) {
|
|
525
|
-
if (!name) return null;
|
|
526
|
-
|
|
527
|
-
let item = this.get(name);
|
|
528
|
-
if (!item) return null;
|
|
529
|
-
|
|
530
|
-
try {
|
|
531
|
-
await item.exec('reload');
|
|
532
|
-
return item;
|
|
533
|
-
} catch (err) {
|
|
534
|
-
$.log.error(`[${this.type}] 重新加载插件 ${name} 失败: `, err);
|
|
535
|
-
return null;
|
|
536
|
-
}
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* 通过文件加载配置
|
|
541
|
-
* @param {string} file 文件名
|
|
542
|
-
* @param {boolean} create 不存在则进行创建
|
|
543
|
-
* @returns {Promise<object | null | string>} 加载的驱动对象、null或错误信息
|
|
544
|
-
*/
|
|
545
|
-
Index.prototype.loadFile = async function (file, create = false) {
|
|
546
|
-
try {
|
|
547
|
-
let dir = file.dirname();
|
|
548
|
-
// 载入文件
|
|
549
|
-
let obj = file.loadJson();
|
|
550
|
-
if (obj) {
|
|
551
|
-
if (Array.isArray(obj)) {
|
|
552
|
-
// 使用函数式编程处理数组
|
|
553
|
-
await obj.reduce(async (prev_promise, o) => {
|
|
554
|
-
await prev_promise;
|
|
555
|
-
// 实例化一个驱动
|
|
556
|
-
return this.loadDrive(dir, o, file);
|
|
557
|
-
}, Promise.resolve());
|
|
558
|
-
} else {
|
|
559
|
-
return await this.loadDrive(dir, null, file);
|
|
560
|
-
}
|
|
561
|
-
} else if (create) {
|
|
562
|
-
return await this.loadDrive(dir, null, file);
|
|
563
|
-
} else {
|
|
564
|
-
return `${file}文件不存在`;
|
|
565
|
-
}
|
|
566
|
-
return null;
|
|
567
|
-
} catch (err) {
|
|
568
|
-
$.log.error(`[${this.type}] 加载文件失败: `, err);
|
|
569
|
-
return `加载文件失败: ${err.message}`;
|
|
570
|
-
}
|
|
571
|
-
};
|
|
572
|
-
|
|
573
|
-
/**
|
|
574
|
-
* 调用函数
|
|
575
|
-
* @param {string} name 模块名
|
|
576
|
-
* @param {string} method 函数名
|
|
577
|
-
* @param {object} params 参数集合
|
|
578
|
-
* @returns {Promise<any>} 执行结果
|
|
579
|
-
*/
|
|
580
|
-
Index.prototype.run = async function (name, method, ...params) {
|
|
581
|
-
if (name) {
|
|
582
|
-
let module = await this.get(name);
|
|
583
|
-
if (module && module.config?.state === 1) {
|
|
584
|
-
return await this._exec(module, method, params);
|
|
585
|
-
}
|
|
586
|
-
return null;
|
|
587
|
-
} else if (name === null) {
|
|
588
|
-
let result = null;
|
|
589
|
-
// 修复:使用for循环替代some,正确处理异步迭代
|
|
590
|
-
for (let module of this.list) {
|
|
591
|
-
if (module.config?.state === 1) {
|
|
592
|
-
result = await this._exec(module, method, params);
|
|
593
|
-
// 如果结果不为空且有结束标志,则停止迭代
|
|
594
|
-
if (result && module.config?.end) {
|
|
595
|
-
break;
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
return result;
|
|
600
|
-
}
|
|
601
|
-
return null;
|
|
602
|
-
};
|
|
603
|
-
|
|
604
|
-
/**
|
|
605
|
-
* 执行方法
|
|
606
|
-
* @param {string} name 插件名称
|
|
607
|
-
* @param {string} method 方法名称
|
|
608
|
-
* @param {...any} params 方法参数
|
|
609
|
-
* @returns {Promise<any>} 执行结果
|
|
610
|
-
*/
|
|
611
|
-
Index.prototype.exec = async function (name, method, ...params) {
|
|
612
|
-
if (name) {
|
|
613
|
-
let module = await this.get(name);
|
|
614
|
-
if (module) {
|
|
615
|
-
return await this._exec(module, method, params);
|
|
616
|
-
}
|
|
617
|
-
return null;
|
|
618
|
-
} else if (name === null) {
|
|
619
|
-
let result = null;
|
|
620
|
-
// 使用函数式编程处理列表,使用reduce模拟break逻辑
|
|
621
|
-
await this.list.reduce(async (prev_promise, module) => {
|
|
622
|
-
await prev_promise;
|
|
623
|
-
// 如果已经得到结果且有结束标志,则跳过后续处理
|
|
624
|
-
if (result && module.config?.end) {
|
|
625
|
-
return;
|
|
626
|
-
}
|
|
627
|
-
let current = await this._exec(module, method, params);
|
|
628
|
-
if (current) {
|
|
629
|
-
result = current;
|
|
630
|
-
}
|
|
631
|
-
}, Promise.resolve());
|
|
632
|
-
return result;
|
|
633
|
-
}
|
|
634
|
-
return null;
|
|
635
|
-
};
|
|
636
|
-
|
|
637
|
-
/**
|
|
638
|
-
* @module 导出Index类
|
|
639
|
-
*/
|
|
640
|
-
exports.Index = Index;
|
|
641
|
-
exports.Item = Item;
|
|
1
|
+
const { Mod } = require('./mod');
|
|
2
|
+
const { Drive } = require('./drive.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 管理器类
|
|
6
|
+
*/
|
|
7
|
+
class Manager extends Mod {
|
|
8
|
+
static config = {
|
|
9
|
+
// 管理器名称
|
|
10
|
+
name: 'manager',
|
|
11
|
+
// 管理器描述
|
|
12
|
+
description: '管理模块',
|
|
13
|
+
// 基础目录,加载模块内置资源
|
|
14
|
+
base_dir: '../common/manager'.fullname(__dirname),
|
|
15
|
+
// 自定义目录,加载项目自定义资源
|
|
16
|
+
dir: './ai/manager'.fullname(),
|
|
17
|
+
// 文件拓展名
|
|
18
|
+
filename: 'manager.json',
|
|
19
|
+
// 懒加载
|
|
20
|
+
lazy_load: true,
|
|
21
|
+
// 作用域(同时作为检索的后缀名)
|
|
22
|
+
scope: ($.val && $.val.scope) ? $.val.scope + '' : 'server',
|
|
23
|
+
// 排序项
|
|
24
|
+
sort_key: 'sort',
|
|
25
|
+
// 模式 script脚本 | json配置 | drive驱动
|
|
26
|
+
type: 'drive',
|
|
27
|
+
// 模式 1.生产模式 2.热更新模式 3.热重载模式 4.热更新+重载模式 5.重载模式
|
|
28
|
+
mode: 3
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 构造函数
|
|
33
|
+
* @param {object} config 配置项
|
|
34
|
+
* @param {object} parent 父项对象
|
|
35
|
+
*/
|
|
36
|
+
constructor(config, parent) {
|
|
37
|
+
super({
|
|
38
|
+
...Manager.config,
|
|
39
|
+
...config
|
|
40
|
+
}, parent);
|
|
41
|
+
|
|
42
|
+
// 模块信息列表 { name: '', sort: 0, available: true, enabled: true }
|
|
43
|
+
this.infos = [];
|
|
44
|
+
// 模块
|
|
45
|
+
this.mods = {};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 创建模块状态模型
|
|
51
|
+
* @param {string} name 模块名
|
|
52
|
+
* @param {number} sort 排序值
|
|
53
|
+
* @param {boolean} state 状态
|
|
54
|
+
* @returns {object} 模块状态模型
|
|
55
|
+
*/
|
|
56
|
+
Manager.prototype.newInfo = function (name, sort, state) {
|
|
57
|
+
return {
|
|
58
|
+
name,
|
|
59
|
+
sort,
|
|
60
|
+
available: state == true,
|
|
61
|
+
enabled: true
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 获取基础目录
|
|
67
|
+
* @returns {string} 基础目录
|
|
68
|
+
*/
|
|
69
|
+
Manager.prototype.getBaseDir = function () {
|
|
70
|
+
return this.config.base_dir;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 默认驱动
|
|
75
|
+
*/
|
|
76
|
+
Manager.prototype.Drive = Drive;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 更新模块信息
|
|
80
|
+
*/
|
|
81
|
+
Manager.prototype.updateInfo = function () {
|
|
82
|
+
this.infos = [];
|
|
83
|
+
for (let name in this.mods) {
|
|
84
|
+
let o = this.mods[name];
|
|
85
|
+
let info = this.newInfo(name, o.config.sort, o.config.state);
|
|
86
|
+
this.infos.push(info);
|
|
87
|
+
}
|
|
88
|
+
this.sort();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 排序
|
|
93
|
+
*/
|
|
94
|
+
Manager.prototype.sort = function () {
|
|
95
|
+
var sort_key = this.config.sort_key || 'sort';
|
|
96
|
+
this.infos.sort((o1, o2) => {
|
|
97
|
+
let p1 = o1[sort_key] || 0;
|
|
98
|
+
let p2 = o2[sort_key] || 0;
|
|
99
|
+
return p1 - p2;
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// ==== 模块管理接口 ====
|
|
104
|
+
/**
|
|
105
|
+
* 删除模块
|
|
106
|
+
* @param {string} name 模块名
|
|
107
|
+
* @returns {object|null} 删除的模块对象
|
|
108
|
+
*/
|
|
109
|
+
Manager.prototype.delMod = function (name) {
|
|
110
|
+
let mod = this.mods[name];
|
|
111
|
+
if (mod) {
|
|
112
|
+
delete this.mods[name];
|
|
113
|
+
}
|
|
114
|
+
return mod;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 设置模块
|
|
119
|
+
* @param {string} name 模块名
|
|
120
|
+
* @param {object} mod 模块对象
|
|
121
|
+
* @returns {object|null} 设置的模块对象
|
|
122
|
+
*/
|
|
123
|
+
Manager.prototype.setMod = function (name, mod) {
|
|
124
|
+
this.mods[name] = mod;
|
|
125
|
+
return this.getMod(name);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 获取模块
|
|
130
|
+
* @param {string} name 模块名
|
|
131
|
+
* @returns {object|null} 模块对象,若模块则返回模块对象,否则返回null
|
|
132
|
+
*/
|
|
133
|
+
Manager.prototype.getMod = function (name) {
|
|
134
|
+
return this.mods[name];
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 获取所有模块
|
|
139
|
+
* @returns {object[]} 模块对象数组
|
|
140
|
+
*/
|
|
141
|
+
Manager.prototype.getMods = function () {
|
|
142
|
+
return this.mods;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 创建模块
|
|
147
|
+
* @param {string} config_file 配置文件路径
|
|
148
|
+
* @param {object} config 配置项
|
|
149
|
+
* @param {object} script 模块对象
|
|
150
|
+
* @returns {object} 模块对象
|
|
151
|
+
*/
|
|
152
|
+
Manager.prototype.newMod = function (config_file, config, script) {
|
|
153
|
+
let mod;
|
|
154
|
+
switch (this.config.type) {
|
|
155
|
+
case 'script':
|
|
156
|
+
mod = new Mod(config, this);
|
|
157
|
+
break;
|
|
158
|
+
default:
|
|
159
|
+
// 创建模块
|
|
160
|
+
mod = new this.Drive(config, this);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
mod.mode = this.config.mode;
|
|
164
|
+
mod.loadConfig(config_file);
|
|
165
|
+
mod.setMethods(script);
|
|
166
|
+
mod.setConfig(config);
|
|
167
|
+
return mod;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 注册模块
|
|
172
|
+
* @param {string} config_file 配置文件路径
|
|
173
|
+
* @param {object} config 配置项
|
|
174
|
+
* @param {object} script 模块对象
|
|
175
|
+
* @returns {object} 配置项
|
|
176
|
+
*/
|
|
177
|
+
Manager.prototype.register = function (config_file, config = {}, script = {}) {
|
|
178
|
+
let mod = this.newMod(config_file, config, script);
|
|
179
|
+
this.setMod(mod.config.name, mod);
|
|
180
|
+
return mod;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 加载模块
|
|
185
|
+
* @param {string} name 模块名
|
|
186
|
+
* @returns {Promise<any>} 加载结果
|
|
187
|
+
*/
|
|
188
|
+
Manager.prototype.load = async function (name) {
|
|
189
|
+
let mod = this.getMod(name);
|
|
190
|
+
if (mod) {
|
|
191
|
+
return await mod.exec('load');
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 卸载模块
|
|
198
|
+
* @param {string} name 模块名
|
|
199
|
+
* @returns {Promise<any>} 卸载结果
|
|
200
|
+
*/
|
|
201
|
+
Manager.prototype.unload = async function (name) {
|
|
202
|
+
let mod = this.getMod(name);
|
|
203
|
+
if (mod) {
|
|
204
|
+
return await mod.exec('unload');
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 重载模块
|
|
211
|
+
* @param {string} name 模块名
|
|
212
|
+
* @returns {Promise<any>} 重载结果
|
|
213
|
+
*/
|
|
214
|
+
Manager.prototype.reload = async function (name) {
|
|
215
|
+
let mod = this.getMod(name);
|
|
216
|
+
if (mod) {
|
|
217
|
+
return await mod.exec('reload');
|
|
218
|
+
}
|
|
219
|
+
return null;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 注销模块
|
|
224
|
+
* @param {string} name 模块名
|
|
225
|
+
* @returns {Promise<any>} 注销结果
|
|
226
|
+
*/
|
|
227
|
+
Manager.prototype.unregister = function (name) {
|
|
228
|
+
// 卸载模块
|
|
229
|
+
this.unload(name);
|
|
230
|
+
// 删除模块
|
|
231
|
+
this.delMod(name);
|
|
232
|
+
return mod;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 批量注册模块
|
|
237
|
+
* @param {string[]} files 配置文件路径数组
|
|
238
|
+
* @param {object} config 配置项
|
|
239
|
+
* @param {object} script 模块对象
|
|
240
|
+
*/
|
|
241
|
+
Manager.prototype.batchRegister = function (files, config = {}, script = {}) {
|
|
242
|
+
for (let file of files) {
|
|
243
|
+
this.register(file, config, script);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 加载所有模块
|
|
249
|
+
*/
|
|
250
|
+
Manager.prototype.loads = async function () {
|
|
251
|
+
var promises = [];
|
|
252
|
+
for (let name in this.mods) {
|
|
253
|
+
let mod = this.mods[name];
|
|
254
|
+
promises.push(mod.exec('load'));
|
|
255
|
+
}
|
|
256
|
+
await Promise.all(promises);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 检索目录下所有文件
|
|
261
|
+
* @private
|
|
262
|
+
* @param {string} dir 目录路径
|
|
263
|
+
* @returns {Array} 文件路径数组
|
|
264
|
+
*/
|
|
265
|
+
Manager.prototype._findFiles = function (dir) {
|
|
266
|
+
// 检查目录是否存在
|
|
267
|
+
if (!dir) {
|
|
268
|
+
this.log('error', '目录不能为空');
|
|
269
|
+
return [];
|
|
270
|
+
}
|
|
271
|
+
// 检查目录是否存在
|
|
272
|
+
if (!dir.hasDir()) {
|
|
273
|
+
this.log('warn', '目录不存在', dir);
|
|
274
|
+
return [];
|
|
275
|
+
}
|
|
276
|
+
// 检索目录下所有文件
|
|
277
|
+
return $.file.getAll(dir, this.config.filename);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 加载所有模块的脚本
|
|
282
|
+
*/
|
|
283
|
+
Manager.prototype.loadScripts = async function () {
|
|
284
|
+
var promises = [];
|
|
285
|
+
for (let name in this.mods) {
|
|
286
|
+
let mod = this.mods[name];
|
|
287
|
+
promises.push(mod.exec('loadScript'));
|
|
288
|
+
}
|
|
289
|
+
await Promise.all(promises);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 更新配置前的钩子函数
|
|
294
|
+
*/
|
|
295
|
+
Manager.prototype.updateBefore = function () {
|
|
296
|
+
this.log('info', '更新配置前的钩子函数');
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 更新配置前的钩子函数
|
|
301
|
+
*/
|
|
302
|
+
Manager.prototype.updateAfter = function () {
|
|
303
|
+
this.log('info', '更新配置后钩子函数');
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 更新配置
|
|
308
|
+
* @param {string} dir 检索的路径
|
|
309
|
+
* @param {boolean} clear 是否清除缓存
|
|
310
|
+
*/
|
|
311
|
+
Manager.prototype.update = async function (dir, clear = true) {
|
|
312
|
+
if (clear) {
|
|
313
|
+
// 初始化方法
|
|
314
|
+
this.mods = {};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// 查找所有json文件
|
|
318
|
+
let files = this._findFiles(dir);
|
|
319
|
+
// 批量注册模块
|
|
320
|
+
this.batchRegister(files);
|
|
321
|
+
// 更新配置信息
|
|
322
|
+
this.updateInfo();
|
|
323
|
+
if (!this.config.lazy_load) {
|
|
324
|
+
// 批量加载脚本
|
|
325
|
+
await this.loadScripts();
|
|
326
|
+
}
|
|
327
|
+
this.is_loaded = true;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// ==== 高性能调用接口 ====
|
|
331
|
+
/**
|
|
332
|
+
* 调用模块的方法(高性能版本)
|
|
333
|
+
* @param {string} name 模块名
|
|
334
|
+
* @param {string} method 方法名
|
|
335
|
+
* @param {...any} params 参数集合
|
|
336
|
+
* @returns {Promise<any>} 执行结果
|
|
337
|
+
*/
|
|
338
|
+
Manager.prototype.main = async function (name, method, ...params) {
|
|
339
|
+
var result = null;
|
|
340
|
+
if (name) {
|
|
341
|
+
let mod = this.getMod(name);
|
|
342
|
+
if (mod && mod.config?.state === 1) {
|
|
343
|
+
result = await this._runSub(mod, method, ...params);
|
|
344
|
+
}
|
|
345
|
+
} else if (name === null) {
|
|
346
|
+
result = await this._runAllEnabled(method, ...params);
|
|
347
|
+
}
|
|
348
|
+
return result;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* 私有方法:执行所有模块方法
|
|
353
|
+
* @param {string} method 方法名称
|
|
354
|
+
* @param {...any} params 方法参数集合
|
|
355
|
+
* @returns {Promise<any>} 执行结果
|
|
356
|
+
* @private
|
|
357
|
+
*/
|
|
358
|
+
Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
359
|
+
var result;
|
|
360
|
+
for (let i = 0; i < this.infos.length; i++) {
|
|
361
|
+
let config = this.infos[i];
|
|
362
|
+
let mod = this.getMod(config.name);
|
|
363
|
+
if (mod && mod.config?.state === 1) {
|
|
364
|
+
var ret = await this._runSub(mod, method, ...params);
|
|
365
|
+
if (ret !== null && ret !== undefined) {
|
|
366
|
+
result = ret;
|
|
367
|
+
// 如果结果不为空且有结束标志,则停止迭代
|
|
368
|
+
if (mod.config?.end) {
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return result;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* 执行方法
|
|
379
|
+
* @param {string} name 模块名称
|
|
380
|
+
* @param {string} method 方法名称
|
|
381
|
+
* @param {...any} params 方法参数集合
|
|
382
|
+
* @returns {Promise<any>} 执行结果
|
|
383
|
+
*/
|
|
384
|
+
Manager.prototype.runAsync = async function (name, method, ...params) {
|
|
385
|
+
// 使用mods的顺序(按配置顺序调用)
|
|
386
|
+
var result = null;
|
|
387
|
+
if (name) {
|
|
388
|
+
let mod = await this.getMod(name);
|
|
389
|
+
if (mod) {
|
|
390
|
+
result = await this._runSub(mod, method, ...params);
|
|
391
|
+
}
|
|
392
|
+
} else if (name === null) {
|
|
393
|
+
result = await this._runAll(method, ...params);
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* 私有方法:执行所有模块方法
|
|
400
|
+
* @param {string} method 方法名称
|
|
401
|
+
* @param {...any} params 方法参数集合
|
|
402
|
+
* @returns {Promise<any>} 执行结果
|
|
403
|
+
* @private
|
|
404
|
+
*/
|
|
405
|
+
Manager.prototype._runAll = async function (method, ...params) {
|
|
406
|
+
var result = null;
|
|
407
|
+
for (let i = 0; i < this.infos.length; i++) {
|
|
408
|
+
let config = this.infos[i];
|
|
409
|
+
let mod = this.getMod(config.name);
|
|
410
|
+
if (mod) {
|
|
411
|
+
var ret = await this._runSub(mod, method, ...params);
|
|
412
|
+
if (ret !== null && ret !== undefined) {
|
|
413
|
+
result = ret;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return result;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* 私有方法:执行模块方法
|
|
422
|
+
* @param {object} mod 模块对象
|
|
423
|
+
* @param {string} method 方法名称
|
|
424
|
+
* @param {...any} params 参数集合
|
|
425
|
+
* @returns {Promise<any>} 执行结果
|
|
426
|
+
* @private
|
|
427
|
+
*/
|
|
428
|
+
Manager.prototype._runSub = async function (mod, method, ...params) {
|
|
429
|
+
if (!mod || !method) return null;
|
|
430
|
+
|
|
431
|
+
try {
|
|
432
|
+
// 确保模块已加载
|
|
433
|
+
if (!mod.isLoaded()) {
|
|
434
|
+
await mod.do('loadScript');
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// 执行方法
|
|
438
|
+
let ret = await mod.exec(method, ...params);
|
|
439
|
+
// 根据模式决定是否重载
|
|
440
|
+
if (this.mode >= 4) {
|
|
441
|
+
mod.do('reload');
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return ret;
|
|
445
|
+
} catch (err) {
|
|
446
|
+
$.log.error(`[${this.type}] 执行模块方法失败: `, err);
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* 初始化管理器
|
|
453
|
+
*/
|
|
454
|
+
Manager.prototype._initManager = function () {
|
|
455
|
+
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* 加载资源
|
|
460
|
+
*/
|
|
461
|
+
Manager.prototype._loadSources = function () {
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* 初始化资源
|
|
466
|
+
*/
|
|
467
|
+
Manager.prototype._initSources = function () {
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* 初始化公共属性
|
|
472
|
+
*/
|
|
473
|
+
Manager.prototype._initCore = function () {
|
|
474
|
+
// 初始化管理器
|
|
475
|
+
this._initManager();
|
|
476
|
+
// 加载资源
|
|
477
|
+
this._loadSources();
|
|
478
|
+
// 初始化AI客户端
|
|
479
|
+
this._initSources();
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
module.exports = {
|
|
483
|
+
Manager,
|
|
484
|
+
Drive,
|
|
485
|
+
// 向后兼容
|
|
486
|
+
Index: Manager,
|
|
487
|
+
Item: Drive
|
|
488
|
+
};
|