diff --git a/packages/napcat-core/index.ts b/packages/napcat-core/index.ts index fef9718f5..d5fd604e2 100644 --- a/packages/napcat-core/index.ts +++ b/packages/napcat-core/index.ts @@ -59,12 +59,30 @@ export enum NapCatCoreWorkingEnv { Framework = 2, } +function dlopenWrapper (wrapperPath: string): WrapperNodeApi { + const nativemodule: { exports: WrapperNodeApi; } = { exports: {} as WrapperNodeApi }; + try { + process.dlopen(nativemodule, wrapperPath); + } catch (error) { + // QQNT.dll 依赖媒体基础(mfplat.dll 等),Windows Server 默认不装这个组件。 + // 缺了它系统只会报 The specified module could not be found,看不出缺的是什么 + const mfplatPath = path.join(process.env['SystemRoot'] || 'C:\\Windows', 'System32', 'mfplat.dll'); + if (os.platform() === 'win32' && !fs.existsSync(mfplatPath)) { + throw new Error( + '加载 wrapper.node 失败:系统缺少「媒体基础」组件(找不到 mfplat.dll),Windows Server 默认不安装它。' + + '请在服务器管理器的「添加角色和功能」里勾选「媒体基础」,' + + '或在管理员 PowerShell 中执行 Install-WindowsFeature Server-Media-Foundation,重启后再启动。' + + `原始错误:${(error as Error).message}` + ); + } + throw error; + } + return nativemodule.exports; +} + export function loadQQWrapper (execPath: string | undefined, QQVersion: string): WrapperNodeApi { if (process.env['NAPCAT_WRAPPER_PATH']) { - const wrapperPath = process.env['NAPCAT_WRAPPER_PATH']; - const nativemodule: { exports: WrapperNodeApi; } = { exports: {} as WrapperNodeApi }; - process.dlopen(nativemodule, wrapperPath); - return nativemodule.exports; + return dlopenWrapper(process.env['NAPCAT_WRAPPER_PATH']); } if (!execPath) { throw new Error('无法加载Wrapper,execPath未定义'); @@ -85,10 +103,9 @@ export function loadQQWrapper (execPath: string | undefined, QQVersion: string): if (!fs.existsSync(wrapperNodePath)) { wrapperNodePath = path.join(path.dirname(execPath), `./resources/app/versions/${QQVersion}/wrapper.node`); } - const nativemodule: { exports: WrapperNodeApi; } = { exports: {} as WrapperNodeApi }; - process.dlopen(nativemodule, wrapperNodePath); + const exports = dlopenWrapper(wrapperNodePath); process.env['NAPCAT_WRAPPER_PATH'] = wrapperNodePath; - return nativemodule.exports; + return exports; } export function getMajorPath (execPath: string, QQVersion: string): string { // major.node diff --git a/packages/napcat-test/loadQQWrapper.test.ts b/packages/napcat-test/loadQQWrapper.test.ts new file mode 100644 index 000000000..7e0a84c90 --- /dev/null +++ b/packages/napcat-test/loadQQWrapper.test.ts @@ -0,0 +1,46 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; +import fs from 'node:fs'; +import os from 'node:os'; +import { loadQQWrapper } from '../napcat-core/index'; + +describe('loadQQWrapper', () => { + const originalWrapperPath = process.env['NAPCAT_WRAPPER_PATH']; + + beforeEach(() => { + process.env['NAPCAT_WRAPPER_PATH'] = 'E:\\NapCat\\wrapper.node'; + vi.spyOn(process, 'dlopen').mockImplementation(() => { + throw new Error('The specified module could not be found.\r\n\\\\?\\E:\\NapCat\\wrapper.node'); + }); + }); + + afterEach(() => { + vi.restoreAllMocks(); + if (originalWrapperPath === undefined) { + delete process.env['NAPCAT_WRAPPER_PATH']; + } else { + process.env['NAPCAT_WRAPPER_PATH'] = originalWrapperPath; + } + }); + + test('explains the missing Media Foundation feature on Windows Server', () => { + vi.spyOn(os, 'platform').mockReturnValue('win32'); + vi.spyOn(fs, 'existsSync').mockReturnValue(false); + + expect(() => loadQQWrapper(undefined, '9.9.33-52230')) + .toThrow(/媒体基础.*Install-WindowsFeature Server-Media-Foundation.*The specified module could not be found/s); + }); + + test('keeps the original error when Media Foundation is installed', () => { + vi.spyOn(os, 'platform').mockReturnValue('win32'); + vi.spyOn(fs, 'existsSync').mockReturnValue(true); + + expect(() => loadQQWrapper(undefined, '9.9.33-52230')).toThrow(/^The specified module could not be found/); + }); + + test('keeps the original error on other platforms', () => { + vi.spyOn(os, 'platform').mockReturnValue('linux'); + vi.spyOn(fs, 'existsSync').mockReturnValue(false); + + expect(() => loadQQWrapper(undefined, '3.2.32-52194')).toThrow(/^The specified module could not be found/); + }); +});