feat(加入热更和eval的管理模块):

This commit is contained in:
liangtongchuan
2023-04-30 23:54:48 +08:00
committed by luying
parent a362a397b5
commit 3a41136722
4 changed files with 176 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import { Application, IComponent } from "pinus";
export class EvalComponent implements IComponent {
name = "EvalComponent";
app: Application;
constructor(app: Application) {
this.app = app;
this.app.set(this.name, this);
}
start(cb: () => void) {
console.log("EvalComponent start", this.app.getServerId());
cb();
}
stop(force: boolean, cb: () => void) {
console.log("EvalComponent stop", force, this.app.getServerId());
cb();
}
eval(script: string, cb: (err, res?) => void) {
try {
// 检查 script 的开头是否由 MagicCode 开头
const MagicCode = "BantuYJZ23:";
if (!script.startsWith(MagicCode)) {
// 没有权限
cb("auth failed");
return;
}
// 去掉 MagicCodesubstr 已经不被支持
const code = script.slice(MagicCode.length);
// ! 执行代码
const result = eval(code);
cb(null, result);
} catch (error) {
console.log("EvalComponent eval", this.app.getServerId(), error)
cb(error);
return;
}
}
}