Files
ZYZ/game-server/app/components/EvalComponent.ts
2026-03-13 01:38:40 +00:00

44 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}