refactor: import native .node

This commit is contained in:
linyuchen 2024-04-29 16:25:27 +08:00
parent 58f544862b
commit 91089cdb9e
3 changed files with 27 additions and 6 deletions

19
src/ntqqapi/external/cpmodule.ts vendored Normal file
View File

@ -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) {
}
}

View File

@ -1,5 +1,6 @@
import {log} from "../../../common/utils"; import {log} from "../../../common/utils";
import {NTQQApi} from "../../ntcall"; import {NTQQApi} from "../../ntcall";
import {cpModule} from "../cpmodule";
type PokeHandler = (id: string, isGroup: boolean) => void type PokeHandler = (id: string, isGroup: boolean) => void
type CrychicHandler = (event: string, id: string, isGroup: boolean) => void type CrychicHandler = (event: string, id: string, isGroup: boolean) => void
@ -12,12 +13,12 @@ class Crychic{
loadNode(){ loadNode(){
if (!this.crychic){ if (!this.crychic){
try { try {
this.crychic = require("./crychic-win32-x64.node") cpModule('crychic');
this.crychic = require("./crychic.node")
this.crychic.init() this.crychic.init()
}catch (e) { }catch (e) {
log("crychic加载失败", e) log("crychic加载失败", e)
} }
} }
} }
registerPokeHandler(fn: PokeHandler){ registerPokeHandler(fn: PokeHandler){

View File

@ -1,5 +1,7 @@
import {log} from "../../../common/utils";
import * as os from "os"; import * as os from "os";
import fs from "fs";
import path from "node:path";
import {cpModule} from "../cpmodule";
interface MoeHook { interface MoeHook {
GetRkey: () => string, // Return '&rkey=xxx' GetRkey: () => string, // Return '&rkey=xxx'
@ -11,10 +13,9 @@ class HookApi {
private readonly moeHook: MoeHook | null = null; private readonly moeHook: MoeHook | null = null;
constructor() { constructor() {
cpModule('MoeHoo');
try { try {
const systemPlatform = os.platform(); this.moeHook = require('./MoeHoo.node');
const cpuArch = os.arch();
this.moeHook = require(`./MoeHoo-${systemPlatform}-${cpuArch}.node`);
console.log("hook rkey地址", this.moeHook!.HookRkey()); console.log("hook rkey地址", this.moeHook!.HookRkey());
} catch (e) { } catch (e) {
console.log('加载 moehoo 失败', e); console.log('加载 moehoo 失败', e);