HJ_Server/src/patch.ts
2023-12-25 13:50:28 +08:00

68 lines
2.0 KiB
TypeScript

import { extendType } from "./extends";
import { ctor } from "./global";
import { initIORedis } from "./ioredis";
import { initGcType } from "./jsonType";
import { createLng } from "./lng";
import { initMongoDB } from "./setMongodb";
import { initRedis } from "./setRedis";
var path = require('path');
var scriptName = path.basename(__filename);
export function patchFun(target: any, key: string, descriptor: PropertyDescriptor) {
let scriptName;
try {
throw new Error();
} catch (e) {
let caller = (e.stack.split("\n").slice(2)[0].split("\\").slice(-2));
scriptName = caller[1].split(':')[0];
}
if(!scriptName){
console.log('获取不到脚本名');
return;
}
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
//@ts-ignore
G.mongodb.collection("_patch_run_log").findOne({ fileName: scriptName, funName: key }).then(async runed=>{
if(runed){
console.log(`脚本${scriptName}已经执行过${key}方法,跳过`);
return (null as any);
}
console.log(`调用了${scriptName}脚本里的${key}方法,参数为:${args}`);
const result = await originalMethod.apply(this, args);
console.log(`执行结果为:${result}`);
//@ts-ignore
await G.mongodb.collection("_patch_run_log").insertOne({
"fileName": scriptName,
"funName": key as any,
"ctime":Date.now(),
"res":result
});
return (result as any);
});
}
}
export async function patchInit() {
await patchStart();
}
async function patchStart() {
//扩展框架
extendType();
//游戏配置提示文件
initGcType();
//连接mongodb
await initMongoDB();
//连接redis
await initRedis();
await initIORedis();
}
// //定义全局变量
ctor();
//创建语言包
createLng();