chore: refactor path config

This commit is contained in:
Hao Guan 2024-09-03 20:14:35 +08:00
parent f59248cc5a
commit 709a0744bd
No known key found for this signature in database
GPG Key ID: E58E5B9DEDB1C425
6 changed files with 56 additions and 28 deletions

View File

@ -22,10 +22,15 @@ export abstract class ConfigBase<T> {
} }
getConfigPath(pathName: string | undefined): string { getConfigPath(pathName: string | undefined): string {
const suffix = pathName ? `_${pathName}` : ''; if (!pathName) {
const filename = `${this.name}${suffix}.json`; 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); return path.join(this.configPath, filename);
} }
}
read(): T { read(): T {
const logger = this.core.context.logger; const logger = this.core.context.logger;

View File

@ -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 { export function getQQVersionConfigPath(exePath: string = ''): string | undefined {
let configVersionInfoPath; let configVersionInfoPath;
if (os.platform() !== 'linux') { if (os.platform() === 'win32') {
configVersionInfoPath = path.join(path.dirname(exePath), 'resources', 'app', 'versions', 'config.json'); configVersionInfoPath = path.join(path.dirname(exePath), 'resources', 'app', 'versions', 'config.json');
} else { } else {
const userPath = os.homedir(); const userPath = os.homedir();

View File

@ -1,6 +1,7 @@
import path, { dirname } from 'path'; import path, { dirname } from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import fs from 'fs'; import fs from 'fs';
import os from 'os';
export class NapCatPathWrapper { export class NapCatPathWrapper {
binaryPath: string; binaryPath: string;
@ -11,17 +12,19 @@ export class NapCatPathWrapper {
constructor(mainPath: string = dirname(fileURLToPath(import.meta.url))) { constructor(mainPath: string = dirname(fileURLToPath(import.meta.url))) {
this.binaryPath = mainPath; this.binaryPath = mainPath;
this.logsPath = path.join(this.binaryPath, 'logs'); let writePath: string;
this.configPath = path.join(this.binaryPath, 'config'); writePath = this.binaryPath;
this.cachePath = path.join(this.binaryPath, 'cache'); 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'); this.staticPath = path.join(this.binaryPath, 'static');
if (fs.existsSync(this.logsPath)) { if (!fs.existsSync(this.logsPath)) {
fs.mkdirSync(this.logsPath, { recursive: true }); fs.mkdirSync(this.logsPath, { recursive: true });
} }
if (fs.existsSync(this.configPath)) { if (!fs.existsSync(this.configPath)) {
fs.mkdirSync(this.configPath, { recursive: true }); fs.mkdirSync(this.configPath, { recursive: true });
} }
if (fs.existsSync(this.cachePath)) { if (!fs.existsSync(this.cachePath)) {
fs.mkdirSync(this.cachePath, { recursive: true }); fs.mkdirSync(this.cachePath, { recursive: true });
} }
} }

View File

@ -1,7 +1,6 @@
import path from 'node:path';
import fs from 'node:fs'; import fs from 'node:fs';
import { systemPlatform } from '@/common/system'; import { systemPlatform } from '@/common/system';
import { getDefaultQQVersionConfigInfo, getQQVersionConfigPath } from './helper'; import { getDefaultQQVersionConfigInfo, getQQPackageInfoPath, getQQVersionConfigPath } from './helper';
import AppidTable from '@/core/external/appid.json'; import AppidTable from '@/core/external/appid.json';
import { LogWrapper } from './log'; import { LogWrapper } from './log';
@ -20,7 +19,7 @@ export class QQBasicInfoWrapper {
//基础目录获取 //基础目录获取
this.context = context; this.context = context;
this.QQMainPath = process.execPath; 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); this.QQVersionConfigPath = getQQVersionConfigPath(this.QQMainPath);
//基础信息获取 无快更则启用默认模板填充 //基础信息获取 无快更则启用默认模板填充
@ -53,9 +52,21 @@ export class QQBasicInfoWrapper {
//此方法不要直接使用 //此方法不要直接使用
getQUAInternal() { getQUAInternal() {
return systemPlatform === 'linux' switch (systemPlatform) {
? `V1_LNX_NQ_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B` case 'linux':
: `V1_WIN_NQ_${this.getFullQQVesion()}_${this.getQQBuildStr()}_GW_B`; 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 } { getAppidV2(): { appid: string; qua: string } {
@ -71,6 +82,6 @@ export class QQBasicInfoWrapper {
// else // else
this.context.logger.log(`[QQ版本兼容性检测] 获取Appid异常 请检测NapCat/QQNT是否正常`); this.context.logger.log(`[QQ版本兼容性检测] 获取Appid异常 请检测NapCat/QQNT是否正常`);
this.context.logger.log(`[QQ版本兼容性检测] ${fullVersion} 版本兼容性不佳,可能会导致一些功能无法正常使用`,); this.context.logger.log(`[QQ版本兼容性检测] ${fullVersion} 版本兼容性不佳,可能会导致一些功能无法正常使用`,);
return { appid: systemPlatform === 'linux' ? '537243600' : '537243441', qua: this.getQUAInternal() }; return { appid: this.getAppidInternal(), qua: this.getQUAInternal() };
} }
} }

View File

@ -42,9 +42,11 @@ export enum NapCatCoreWorkingEnv {
} }
export function loadQQWrapper(QQVersion: string): WrapperNodeApi { 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)) { 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: {} }; const nativemodule: any = { exports: {} };
process.dlopen(nativemodule, wrapperNodePath); process.dlopen(nativemodule, wrapperNodePath);

View File

@ -49,12 +49,15 @@ export async function NCoreInitShell() {
const session = new wrapper.NodeIQQNTWrapperSession(); const session = new wrapper.NodeIQQNTWrapperSession();
// from get dataPath // from get dataPath
const [dataPath, dataPathGlobal] = (() => {
let dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig(); let dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig();
if (!dataPath) { if (!dataPath) {
dataPath = path.resolve(os.homedir(), './.config/QQ'); dataPath = path.resolve(os.homedir(), './.config/QQ');
fs.mkdirSync(dataPath, { recursive: true }); fs.mkdirSync(dataPath, { recursive: true });
} }
const dataPathGlobal = path.resolve(dataPath, './nt_qq/global'); const dataPathGlobal = path.resolve(dataPath, './nt_qq/global');
return [dataPath, dataPathGlobal];
})();
// from initConfig // from initConfig
engine.initWithDeskTopConfig( engine.initWithDeskTopConfig(
@ -115,7 +118,7 @@ export async function NCoreInitShell() {
const realBase64 = pngBase64QrcodeData.replace(/^data:image\/\w+;base64,/, ''); const realBase64 = pngBase64QrcodeData.replace(/^data:image\/\w+;base64,/, '');
const buffer = Buffer.from(realBase64, 'base64'); const buffer = Buffer.from(realBase64, 'base64');
logger.logWarn('请扫描下面的二维码然后在手Q上授权登录'); 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) => { qrcode.generate(qrcodeUrl, { small: true }, (res) => {
logger.logWarn([ logger.logWarn([
'\n', '\n',