From 709a0744bd4b55a7b5d49aabd0fd0dd5de744ae2 Mon Sep 17 00:00:00 2001 From: Hao Guan <10684225+hguandl@users.noreply.github.com> Date: Tue, 3 Sep 2024 20:14:35 +0800 Subject: [PATCH 1/2] chore: refactor path config --- src/common/config-base.ts | 11 ++++++++--- src/common/helper.ts | 8 ++++++-- src/common/path.ts | 15 +++++++++------ src/common/qq-basic-info.ts | 25 ++++++++++++++++++------- src/core/index.ts | 6 ++++-- src/shell/napcat.ts | 19 +++++++++++-------- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/common/config-base.ts b/src/common/config-base.ts index ce4070d1..79e44c85 100644 --- a/src/common/config-base.ts +++ b/src/common/config-base.ts @@ -22,9 +22,14 @@ export abstract class ConfigBase { } getConfigPath(pathName: string | undefined): string { - const suffix = pathName ? `_${pathName}` : ''; - const filename = `${this.name}${suffix}.json`; - return path.join(this.configPath, filename); + if (!pathName) { + const filename = `${this.name}.json`; + const mainPath = this.core.context.pathWrapper.binaryPath; + return path.join(mainPath, 'config', filename); + } else { + const filename = `${this.name}_${pathName}.json`; + return path.join(this.configPath, filename); + } } read(): T { diff --git a/src/common/helper.ts b/src/common/helper.ts index d0ee50a6..f4ad9812 100644 --- a/src/common/helper.ts +++ b/src/common/helper.ts @@ -37,7 +37,7 @@ export class FileNapCatOneBotUUID { if (!uuid.startsWith('NapCatOneBot|ModelIdFile|')) return undefined; const data = uuid.split('|'); if (data.length !== 6) return undefined; - const [, , chatType, peerUid, modelId,fileId] = data; + const [, , chatType, peerUid, modelId, fileId] = data; return { peer: { chatType: chatType as any, @@ -156,9 +156,13 @@ export function getDefaultQQVersionConfigInfo(): QQVersionConfigType { }; } +export function getQQPackageInfoPath(exePath: string = ''): string { + return path.join(path.dirname(exePath), 'resources', 'app', 'package.json'); +} + export function getQQVersionConfigPath(exePath: string = ''): string | undefined { let configVersionInfoPath; - if (os.platform() !== 'linux') { + if (os.platform() === 'win32') { configVersionInfoPath = path.join(path.dirname(exePath), 'resources', 'app', 'versions', 'config.json'); } else { const userPath = os.homedir(); diff --git a/src/common/path.ts b/src/common/path.ts index e154bb46..308bcfd5 100644 --- a/src/common/path.ts +++ b/src/common/path.ts @@ -1,6 +1,7 @@ import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; import fs from 'fs'; +import os from 'os'; export class NapCatPathWrapper { binaryPath: string; @@ -11,17 +12,19 @@ export class NapCatPathWrapper { constructor(mainPath: string = dirname(fileURLToPath(import.meta.url))) { this.binaryPath = mainPath; - this.logsPath = path.join(this.binaryPath, 'logs'); - this.configPath = path.join(this.binaryPath, 'config'); - this.cachePath = path.join(this.binaryPath, 'cache'); + let writePath: string; + writePath = this.binaryPath; + this.logsPath = path.join(writePath, 'logs'); + this.configPath = path.join(writePath, 'config'); + this.cachePath = path.join(writePath, 'cache'); this.staticPath = path.join(this.binaryPath, 'static'); - if (fs.existsSync(this.logsPath)) { + if (!fs.existsSync(this.logsPath)) { fs.mkdirSync(this.logsPath, { recursive: true }); } - if (fs.existsSync(this.configPath)) { + if (!fs.existsSync(this.configPath)) { fs.mkdirSync(this.configPath, { recursive: true }); } - if (fs.existsSync(this.cachePath)) { + if (!fs.existsSync(this.cachePath)) { fs.mkdirSync(this.cachePath, { recursive: true }); } } diff --git a/src/common/qq-basic-info.ts b/src/common/qq-basic-info.ts index 4e063ee2..79556fab 100644 --- a/src/common/qq-basic-info.ts +++ b/src/common/qq-basic-info.ts @@ -1,7 +1,6 @@ -import path from 'node:path'; import fs from 'node:fs'; import { systemPlatform } from '@/common/system'; -import { getDefaultQQVersionConfigInfo, getQQVersionConfigPath } from './helper'; +import { getDefaultQQVersionConfigInfo, getQQPackageInfoPath, getQQVersionConfigPath } from './helper'; import AppidTable from '@/core/external/appid.json'; import { LogWrapper } from './log'; @@ -20,7 +19,7 @@ export class QQBasicInfoWrapper { //基础目录获取 this.context = context; this.QQMainPath = process.execPath; - this.QQPackageInfoPath = path.join(path.dirname(this.QQMainPath), 'resources', 'app', 'package.json'); + this.QQPackageInfoPath = getQQPackageInfoPath(this.QQMainPath); this.QQVersionConfigPath = getQQVersionConfigPath(this.QQMainPath); //基础信息获取 无快更则启用默认模板填充 @@ -53,9 +52,21 @@ export class QQBasicInfoWrapper { //此方法不要直接使用 getQUAInternal() { - return systemPlatform === 'linux' - ? `V1_LNX_NQ_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B` - : `V1_WIN_NQ_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; + switch (systemPlatform) { + case 'linux': + return `V1_LNX_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; + default: + return `V1_WIN_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; + } + } + + getAppidInternal() { + switch (systemPlatform) { + case 'linux': + return '537243600'; + default: + return '537243538'; + } } getAppidV2(): { appid: string; qua: string } { @@ -71,6 +82,6 @@ export class QQBasicInfoWrapper { // else this.context.logger.log(`[QQ版本兼容性检测] 获取Appid异常 请检测NapCat/QQNT是否正常`); this.context.logger.log(`[QQ版本兼容性检测] ${fullVersion} 版本兼容性不佳,可能会导致一些功能无法正常使用`,); - return { appid: systemPlatform === 'linux' ? '537243600' : '537243441', qua: this.getQUAInternal() }; + return { appid: this.getAppidInternal(), qua: this.getQUAInternal() }; } } diff --git a/src/core/index.ts b/src/core/index.ts index adc5e917..5fc2912e 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -42,9 +42,11 @@ export enum NapCatCoreWorkingEnv { } export function loadQQWrapper(QQVersion: string): WrapperNodeApi { - let wrapperNodePath = path.resolve(path.dirname(process.execPath), './resources/app/wrapper.node'); + let appPath; + appPath = path.resolve(path.dirname(process.execPath), './resources/app'); + let wrapperNodePath = path.resolve(appPath, 'wrapper.node'); if (!fs.existsSync(wrapperNodePath)) { - wrapperNodePath = path.join(path.dirname(process.execPath), `resources/app/versions/${QQVersion}/wrapper.node`); + wrapperNodePath = path.join(appPath, `versions/${QQVersion}/wrapper.node`); } const nativemodule: any = { exports: {} }; process.dlopen(nativemodule, wrapperNodePath); diff --git a/src/shell/napcat.ts b/src/shell/napcat.ts index 31857baa..044155df 100644 --- a/src/shell/napcat.ts +++ b/src/shell/napcat.ts @@ -49,12 +49,15 @@ export async function NCoreInitShell() { const session = new wrapper.NodeIQQNTWrapperSession(); // from get dataPath - let dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig(); - if (!dataPath) { - dataPath = path.resolve(os.homedir(), './.config/QQ'); - fs.mkdirSync(dataPath, { recursive: true }); - } - const dataPathGlobal = path.resolve(dataPath, './nt_qq/global'); + const [dataPath, dataPathGlobal] = (() => { + let dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig(); + if (!dataPath) { + dataPath = path.resolve(os.homedir(), './.config/QQ'); + fs.mkdirSync(dataPath, { recursive: true }); + } + const dataPathGlobal = path.resolve(dataPath, './nt_qq/global'); + return [dataPath, dataPathGlobal]; + })(); // from initConfig engine.initWithDeskTopConfig( @@ -115,7 +118,7 @@ export async function NCoreInitShell() { const realBase64 = pngBase64QrcodeData.replace(/^data:image\/\w+;base64,/, ''); const buffer = Buffer.from(realBase64, 'base64'); logger.logWarn('请扫描下面的二维码,然后在手Q上授权登录:'); - const qrcodePath = path.join(pathWrapper.binaryPath, 'qrcode.png'); + const qrcodePath = path.join(pathWrapper.cachePath, 'qrcode.png'); qrcode.generate(qrcodeUrl, { small: true }, (res) => { logger.logWarn([ '\n', @@ -191,7 +194,7 @@ export async function NCoreInitShell() { logger.log(`可用于快速登录的 QQ:\n${historyLoginList .map((u, index) => `${index + 1}. ${u.uin} ${u.nickName}`) .join('\n') - }`); + }`); } loginService.getQRCodePicture(); } From 7e96118cdce1a470baa0adf5f934fb7aafce6085 Mon Sep 17 00:00:00 2001 From: Hao Guan <10684225+hguandl@users.noreply.github.com> Date: Tue, 3 Sep 2024 20:17:10 +0800 Subject: [PATCH 2/2] feat: support macOS --- src/common/helper.ts | 10 +++++++++- src/common/path.ts | 6 +++++- src/common/qq-basic-info.ts | 6 +++++- src/core/external/appid.json | 4 ++++ src/core/index.ts | 6 +++++- src/shell/napcat.ts | 5 +++++ 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/common/helper.ts b/src/common/helper.ts index f4ad9812..693187fb 100644 --- a/src/common/helper.ts +++ b/src/common/helper.ts @@ -157,13 +157,21 @@ export function getDefaultQQVersionConfigInfo(): QQVersionConfigType { } export function getQQPackageInfoPath(exePath: string = ''): string { - return path.join(path.dirname(exePath), 'resources', 'app', 'package.json'); + if (os.platform() === 'darwin') { + return path.join(path.dirname(exePath), '..', 'Resources', 'app', 'package.json'); + } else { + return path.join(path.dirname(exePath), 'resources', 'app', 'package.json'); + } } export function getQQVersionConfigPath(exePath: string = ''): string | undefined { let configVersionInfoPath; if (os.platform() === 'win32') { configVersionInfoPath = path.join(path.dirname(exePath), 'resources', 'app', 'versions', 'config.json'); + } else if (os.platform() === 'darwin') { + const userPath = os.homedir(); + const appDataPath = path.resolve(userPath, './Library/Application Support/QQ'); + configVersionInfoPath = path.resolve(appDataPath, './versions/config.json'); } else { const userPath = os.homedir(); const appDataPath = path.resolve(userPath, './.config/QQ'); diff --git a/src/common/path.ts b/src/common/path.ts index 308bcfd5..b204cb20 100644 --- a/src/common/path.ts +++ b/src/common/path.ts @@ -13,7 +13,11 @@ export class NapCatPathWrapper { constructor(mainPath: string = dirname(fileURLToPath(import.meta.url))) { this.binaryPath = mainPath; let writePath: string; - writePath = this.binaryPath; + if (os.platform() === 'darwin') { + writePath = path.join(os.homedir(), 'Library', 'Application Support', 'QQ', 'NapCat'); + } else { + writePath = this.binaryPath; + } this.logsPath = path.join(writePath, 'logs'); this.configPath = path.join(writePath, 'config'); this.cachePath = path.join(writePath, 'cache'); diff --git a/src/common/qq-basic-info.ts b/src/common/qq-basic-info.ts index 79556fab..555f1195 100644 --- a/src/common/qq-basic-info.ts +++ b/src/common/qq-basic-info.ts @@ -54,7 +54,9 @@ export class QQBasicInfoWrapper { getQUAInternal() { switch (systemPlatform) { case 'linux': - return `V1_LNX_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; + return `V1_LNX_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; + case 'darwin': + return `V1_MAC_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; default: return `V1_WIN_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; } @@ -64,6 +66,8 @@ export class QQBasicInfoWrapper { switch (systemPlatform) { case 'linux': return '537243600'; + case 'darwin': + return '537243441'; default: return '537243538'; } diff --git a/src/core/external/appid.json b/src/core/external/appid.json index 9375e295..3f05c08b 100644 --- a/src/core/external/appid.json +++ b/src/core/external/appid.json @@ -6,5 +6,9 @@ "9.9.15-27597": { "appid": 537243441, "qua": "V1_WIN_NQ_9.9.15_27597_GW_B" + }, + "6.9.53-27597": { + "appid": 537243538, + "qua": "V1_MAC_NQ_6.9.53_27597_GW_B" } } diff --git a/src/core/index.ts b/src/core/index.ts index 5fc2912e..c6e9f0ea 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -43,7 +43,11 @@ export enum NapCatCoreWorkingEnv { export function loadQQWrapper(QQVersion: string): WrapperNodeApi { let appPath; - appPath = path.resolve(path.dirname(process.execPath), './resources/app'); + if (os.platform() === 'darwin') { + appPath = path.resolve(path.dirname(process.execPath), '../Resources/app'); + } else { + appPath = path.resolve(path.dirname(process.execPath), './resources/app'); + } let wrapperNodePath = path.resolve(appPath, 'wrapper.node'); if (!fs.existsSync(wrapperNodePath)) { wrapperNodePath = path.join(appPath, `versions/${QQVersion}/wrapper.node`); diff --git a/src/shell/napcat.ts b/src/shell/napcat.ts index 044155df..4beeded5 100644 --- a/src/shell/napcat.ts +++ b/src/shell/napcat.ts @@ -50,6 +50,11 @@ export async function NCoreInitShell() { // from get dataPath const [dataPath, dataPathGlobal] = (() => { + if (os.platform() === 'darwin') { + const userPath = os.homedir(); + const appDataPath = path.resolve(userPath, './Library/Application Support/QQ'); + return [appDataPath, path.join(appDataPath, 'global')]; + } let dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig(); if (!dataPath) { dataPath = path.resolve(os.homedir(), './.config/QQ');