From 435cb7cf5278b632ceaa171a5f8c7b58cd24da21 Mon Sep 17 00:00:00 2001 From: sj817 <74231782+sj817@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:37:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20Windows=20Server=20=E7=BC=BA?= =?UTF-8?q?=E5=B0=91=E5=AA=92=E4=BD=93=E5=9F=BA=E7=A1=80=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E7=BB=99=E5=87=BA=E6=98=8E=E7=A1=AE=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QQNT.dll 静态依赖 mf.dll / mfplat.dll / mfreadwrite.dll,Windows Server 默认不安装 「媒体基础」,加载 wrapper.node 时系统只报 The specified module could not be found(#2069)。 现在 Windows 下加载失败且找不到 mfplat.dll 时,直接提示安装媒体基础的方法。 --- packages/napcat-core/index.ts | 31 +++++++++++---- packages/napcat-test/loadQQWrapper.test.ts | 46 ++++++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 packages/napcat-test/loadQQWrapper.test.ts 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/); + }); +});