From 91089cdb9ea78da2b8a784c972938397c8043ca5 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Mon, 29 Apr 2024 16:25:27 +0800 Subject: [PATCH] refactor: import native .node --- src/ntqqapi/external/cpmodule.ts | 19 +++++++++++++++++++ src/ntqqapi/external/crychic/index.ts | 5 +++-- src/ntqqapi/external/moehook/hook.ts | 9 +++++---- 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/ntqqapi/external/cpmodule.ts diff --git a/src/ntqqapi/external/cpmodule.ts b/src/ntqqapi/external/cpmodule.ts new file mode 100644 index 0000000..ab0d24d --- /dev/null +++ b/src/ntqqapi/external/cpmodule.ts @@ -0,0 +1,19 @@ +import * as os from "os"; +import path from "node:path"; +import fs from "fs"; + +export function getModuleWithArchName(moduleName: string) { + const systemPlatform = os.platform + const cpuArch = os.arch + return `${moduleName}-${systemPlatform}-${cpuArch}.node` +} + +export function cpModule(moduleName: string) { + const currentDir = path.resolve(__dirname); + const fileName = `./${getModuleWithArchName(moduleName)}` + try { + fs.copyFileSync(path.join(currentDir, fileName), path.join(currentDir, `${moduleName}.node`)); + } catch (e) { + + } +} \ No newline at end of file diff --git a/src/ntqqapi/external/crychic/index.ts b/src/ntqqapi/external/crychic/index.ts index f090c01..6de1418 100644 --- a/src/ntqqapi/external/crychic/index.ts +++ b/src/ntqqapi/external/crychic/index.ts @@ -1,5 +1,6 @@ import {log} from "../../../common/utils"; import {NTQQApi} from "../../ntcall"; +import {cpModule} from "../cpmodule"; type PokeHandler = (id: string, isGroup: boolean) => void type CrychicHandler = (event: string, id: string, isGroup: boolean) => void @@ -12,12 +13,12 @@ class Crychic{ loadNode(){ if (!this.crychic){ try { - this.crychic = require("./crychic-win32-x64.node") + cpModule('crychic'); + this.crychic = require("./crychic.node") this.crychic.init() }catch (e) { log("crychic加载失败", e) } - } } registerPokeHandler(fn: PokeHandler){ diff --git a/src/ntqqapi/external/moehook/hook.ts b/src/ntqqapi/external/moehook/hook.ts index c877b65..a1dd5fb 100644 --- a/src/ntqqapi/external/moehook/hook.ts +++ b/src/ntqqapi/external/moehook/hook.ts @@ -1,5 +1,7 @@ -import {log} from "../../../common/utils"; import * as os from "os"; +import fs from "fs"; +import path from "node:path"; +import {cpModule} from "../cpmodule"; interface MoeHook { GetRkey: () => string, // Return '&rkey=xxx' @@ -11,10 +13,9 @@ class HookApi { private readonly moeHook: MoeHook | null = null; constructor() { + cpModule('MoeHoo'); try { - const systemPlatform = os.platform(); - const cpuArch = os.arch(); - this.moeHook = require(`./MoeHoo-${systemPlatform}-${cpuArch}.node`); + this.moeHook = require('./MoeHoo.node'); console.log("hook rkey地址", this.moeHook!.HookRkey()); } catch (e) { console.log('加载 moehoo 失败', e);