mm_machine 3.0.1 → 3.0.4
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/drive.js +27 -17
- package/manager.js +56 -43
- package/mod.js +19 -7
- package/package.json +1 -1
- package/registry.js +26 -9
package/drive.js
CHANGED
|
@@ -84,14 +84,19 @@ Drive.prototype._loadConfig = function (file) {
|
|
|
84
84
|
if (file) {
|
|
85
85
|
this.config_file = file;
|
|
86
86
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
try {
|
|
88
|
+
// 加载配置
|
|
89
|
+
let config = $.loadJson(this.config_file);
|
|
90
|
+
if (!config) {
|
|
91
|
+
this.newConfig(this.config_file);
|
|
92
|
+
config = $.loadJson(this.config_file);
|
|
93
|
+
}
|
|
94
|
+
this.setConfig(config);
|
|
95
|
+
return config;
|
|
96
|
+
} catch (err) {
|
|
97
|
+
this.log('error', '加载配置失败', err);
|
|
98
|
+
return null;
|
|
92
99
|
}
|
|
93
|
-
this.setConfig(config);
|
|
94
|
-
return config;
|
|
95
100
|
};
|
|
96
101
|
|
|
97
102
|
/**
|
|
@@ -315,16 +320,21 @@ Drive.prototype._getScriptTpl = function () {
|
|
|
315
320
|
Drive.prototype.loadScript = function () {
|
|
316
321
|
let file = this._getScriptFile();
|
|
317
322
|
if (!file) return null;
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
323
|
+
try {
|
|
324
|
+
if (this.mode >= 3) {
|
|
325
|
+
var cs = $.require(file, (mod) => {
|
|
326
|
+
this._setMainMethod(mod);
|
|
327
|
+
});
|
|
328
|
+
this._setMainMethod(cs);
|
|
329
|
+
return cs;
|
|
330
|
+
} else {
|
|
331
|
+
var cs = require(file);
|
|
332
|
+
this._setMainMethod(cs);
|
|
333
|
+
return cs;
|
|
334
|
+
}
|
|
335
|
+
} catch (err) {
|
|
336
|
+
this.log('error', '加载脚本文件失败', err);
|
|
337
|
+
return null;
|
|
328
338
|
}
|
|
329
339
|
};
|
|
330
340
|
|
package/manager.js
CHANGED
|
@@ -314,38 +314,43 @@ Manager.prototype.getMods = function () {
|
|
|
314
314
|
* @returns {object} 模块对象
|
|
315
315
|
*/
|
|
316
316
|
Manager.prototype.newMod = function (config_file, config, script) {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
317
|
+
try {
|
|
318
|
+
var mod;
|
|
319
|
+
switch (this.config.mod_type) {
|
|
320
|
+
case 'script':
|
|
321
|
+
mod = new Mod(config, this);
|
|
322
|
+
break;
|
|
323
|
+
default:
|
|
324
|
+
mod = new this.Drive(config, this);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
mod.mode = this.config.mode;
|
|
327
328
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
329
|
+
if (config && config.name) {
|
|
330
|
+
// config 已从 registry 缓存获取,跳过磁盘读
|
|
331
|
+
mod.config_file = config_file;
|
|
332
|
+
if (mod._preset) {
|
|
333
|
+
mod._preset();
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
mod.loadConfig(config_file);
|
|
333
337
|
}
|
|
334
|
-
} else {
|
|
335
|
-
mod.loadConfig(config_file);
|
|
336
|
-
}
|
|
337
338
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
339
|
+
if (script) {
|
|
340
|
+
for (var key in script) {
|
|
341
|
+
var obj = script[key];
|
|
342
|
+
if (typeof obj == 'function') {
|
|
343
|
+
mod.setMethod(key, obj);
|
|
344
|
+
} else {
|
|
345
|
+
mod.setData(key, obj);
|
|
346
|
+
}
|
|
345
347
|
}
|
|
346
348
|
}
|
|
349
|
+
return mod;
|
|
350
|
+
} catch (err) {
|
|
351
|
+
this.log('error', '创建模块失败: 配置文件 ' + config_file, err);
|
|
352
|
+
return null;
|
|
347
353
|
}
|
|
348
|
-
return mod;
|
|
349
354
|
};
|
|
350
355
|
|
|
351
356
|
/**
|
|
@@ -517,20 +522,24 @@ Manager.prototype.updateAfter = function () {};
|
|
|
517
522
|
* @param {boolean} clear 是否清除缓存
|
|
518
523
|
*/
|
|
519
524
|
Manager.prototype.update = async function (dir, clear = true) {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
525
|
+
try {
|
|
526
|
+
if (clear) {
|
|
527
|
+
this.clearMods();
|
|
528
|
+
this.infos = [];
|
|
529
|
+
}
|
|
524
530
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
531
|
+
var search_dir = dir || this.config.dir;
|
|
532
|
+
var files = await this._findFiles(search_dir);
|
|
533
|
+
this.registerMods(files);
|
|
534
|
+
this.updateInfo();
|
|
529
535
|
|
|
530
|
-
|
|
531
|
-
|
|
536
|
+
if (!this.config.lazy_load) {
|
|
537
|
+
await this.loadScripts();
|
|
538
|
+
}
|
|
539
|
+
this._is_loaded = true;
|
|
540
|
+
} catch (err) {
|
|
541
|
+
this.log('error', '更新配置失败', err);
|
|
532
542
|
}
|
|
533
|
-
this._is_loaded = true;
|
|
534
543
|
};
|
|
535
544
|
|
|
536
545
|
/**
|
|
@@ -768,12 +777,16 @@ Manager.prototype._initSources = async function () {
|
|
|
768
777
|
* 初始化公共属性
|
|
769
778
|
*/
|
|
770
779
|
Manager.prototype._initCore = async function () {
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
780
|
+
try {
|
|
781
|
+
// 初始化管理器
|
|
782
|
+
this._initManager();
|
|
783
|
+
// 加载资源
|
|
784
|
+
await this._loadSources();
|
|
785
|
+
// 初始化AI客户端
|
|
786
|
+
await this._initSources();
|
|
787
|
+
} catch (err) {
|
|
788
|
+
this.log('error', '管理器初始化失败', err);
|
|
789
|
+
}
|
|
777
790
|
};
|
|
778
791
|
|
|
779
792
|
/**
|
package/mod.js
CHANGED
|
@@ -366,16 +366,24 @@ Mod.prototype._unloadCore = async function () {
|
|
|
366
366
|
* 重载
|
|
367
367
|
*/
|
|
368
368
|
Mod.prototype.reload = async function () {
|
|
369
|
-
|
|
370
|
-
|
|
369
|
+
try {
|
|
370
|
+
await this.do('unload');
|
|
371
|
+
await this.do('load');
|
|
372
|
+
} catch (err) {
|
|
373
|
+
this.log('error', '重载失败', err);
|
|
374
|
+
}
|
|
371
375
|
};
|
|
372
376
|
|
|
373
377
|
/**
|
|
374
378
|
* 重启
|
|
375
379
|
*/
|
|
376
380
|
Mod.prototype.restart = async function () {
|
|
377
|
-
|
|
378
|
-
|
|
381
|
+
try {
|
|
382
|
+
await this.do('stop');
|
|
383
|
+
await this.do('start');
|
|
384
|
+
} catch (err) {
|
|
385
|
+
this.log('error', '重启失败', err);
|
|
386
|
+
}
|
|
379
387
|
};
|
|
380
388
|
|
|
381
389
|
/**
|
|
@@ -390,9 +398,13 @@ Mod.prototype.isRunning = function () {
|
|
|
390
398
|
* 结束
|
|
391
399
|
*/
|
|
392
400
|
Mod.prototype.end = async function () {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
401
|
+
try {
|
|
402
|
+
await this.do('stop');
|
|
403
|
+
await this.do('unload');
|
|
404
|
+
await this.do('destroy');
|
|
405
|
+
} catch (err) {
|
|
406
|
+
this.log('error', '结束失败', err);
|
|
407
|
+
}
|
|
396
408
|
};
|
|
397
409
|
|
|
398
410
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
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
|
@@ -100,8 +100,11 @@ Registry.prototype.getClass = function (class_name) {
|
|
|
100
100
|
* @throws {Error} 模块配置项错误
|
|
101
101
|
*/
|
|
102
102
|
Registry.prototype.registerModule = function (config) {
|
|
103
|
-
if (!config
|
|
104
|
-
throw new TypeError('
|
|
103
|
+
if (!config) {
|
|
104
|
+
throw new TypeError('模块配置不能为空');
|
|
105
|
+
}
|
|
106
|
+
if (!config.name) {
|
|
107
|
+
throw new TypeError('模块名称不能为空');
|
|
105
108
|
}
|
|
106
109
|
var class_name = config.class_name || 'Drive';
|
|
107
110
|
this._module_cache[config.name] = {
|
|
@@ -130,9 +133,15 @@ Registry.prototype.getModule = function (name) {
|
|
|
130
133
|
var class_name = entry.config.class_name || 'Drive';
|
|
131
134
|
const CS = this._class_cache[class_name];
|
|
132
135
|
if (!CS) {
|
|
133
|
-
|
|
136
|
+
console.error('模块类 ' + class_name + ' 未注册');
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
entry.instance = new CS(entry.config);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
console.error('实例化模块 ' + name + ' 失败: ' + err.message);
|
|
143
|
+
return null;
|
|
134
144
|
}
|
|
135
|
-
entry.instance = new CS(entry.config);
|
|
136
145
|
}
|
|
137
146
|
return entry.instance;
|
|
138
147
|
};
|
|
@@ -179,9 +188,13 @@ Registry.prototype.load = function () {
|
|
|
179
188
|
// 注册模块类(幂等-safe,Node require 返回同一引用)
|
|
180
189
|
for (var key in this.class_dict) {
|
|
181
190
|
var file = this.class_dict[key];
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
191
|
+
try {
|
|
192
|
+
var exports = require(file);
|
|
193
|
+
var class_ref = exports[key] || exports;
|
|
194
|
+
this.registerClass(key, class_ref);
|
|
195
|
+
} catch (err) {
|
|
196
|
+
console.error('注册模块类 ' + key + ' 失败: ' + err.message);
|
|
197
|
+
}
|
|
185
198
|
}
|
|
186
199
|
|
|
187
200
|
// 注册模块配置(仅新增不覆盖,保留已有实例)
|
|
@@ -385,8 +398,12 @@ Registry.prototype.read = function () {
|
|
|
385
398
|
* @returns {object} 模块配置项
|
|
386
399
|
*/
|
|
387
400
|
Registry.prototype.save = function () {
|
|
388
|
-
|
|
389
|
-
|
|
401
|
+
try {
|
|
402
|
+
this.getClassFile().saveJson(this.class_dict);
|
|
403
|
+
this.getModuleFile().saveJson(this.module_list);
|
|
404
|
+
} catch (err) {
|
|
405
|
+
console.error('保存注册数据失败: ' + err.message);
|
|
406
|
+
}
|
|
390
407
|
};
|
|
391
408
|
|
|
392
409
|
/**
|