diff --git a/packages/core/src/hook/hook-manager.ts b/packages/core/src/hook/hook-manager.ts index f13e05ea..78cb701f 100644 --- a/packages/core/src/hook/hook-manager.ts +++ b/packages/core/src/hook/hook-manager.ts @@ -47,6 +47,16 @@ export class HookManager { state.path = proc.path; } + // Probe every freshly-enumerated PID in parallel to detect hooks left over + // from a previous SnowLuma run. Positive hits are reconnected immediately + // so the login-state snapshot can update the UI without a manual load. + await Promise.all( + processes + .map(proc => this.states.get(proc.pid)!) + .filter(state => !state.injected && !this.loadTasks.has(state.pid)) + .map(state => this.probeAndSeed(state)), + ); + const seen = new Set(processes.map(proc => proc.pid)); const result: HookProcessInfo[] = []; for (const proc of processes) { @@ -109,10 +119,18 @@ export class HookManager { try { if (!state.injected) { - state.injectResult = injectHookProcess(pid); - state.injected = true; - state.method = state.injectResult.method; - log.info('SnowLuma loaded into PID=%d via %s', pid, state.method); + // Fast path: if QQ.exe still hosts a hook DLL from a previous SnowLuma + // run, the control pipe is already listed. Skip re-injection on a hit. + if (await QqHookClient.probePipe(pid)) { + state.injected = true; + state.method = 'reconnect'; + log.info('SnowLuma reconnected to existing hook in PID=%d', pid); + } else { + state.injectResult = injectHookProcess(pid); + state.injected = true; + state.method = state.injectResult.method; + log.info('SnowLuma loaded into PID=%d via %s', pid, state.method); + } } state.status = state.connected ? (state.loggedIn ? 'online' : 'loaded') : 'loaded'; this.startClient(state); @@ -248,11 +266,14 @@ export class HookManager { } private handleLoginState(state: HookProcessState, loginState: QqHookLoginState): void { + const wasLoggedIn = state.loggedIn; + const previousUin = state.uin; state.uin = loginState.uin || loginState.uinNumber.toString(); state.loggedIn = loginState.loggedIn && isRealUin(state.uin); state.status = state.loggedIn ? 'online' : (state.connected ? 'loaded' : state.status); if (!state.loggedIn || !state.sender) return; + if (wasLoggedIn && previousUin === state.uin) return; this.bridgeManager.onHookLogin(state.pid, state.uin, state.sender); log.success('login detected: PID=%d UIN=%s', state.pid, state.uin); diff --git a/packages/core/src/hook/qq-hook-client.ts b/packages/core/src/hook/qq-hook-client.ts index f85b8c2f..c938daed 100644 --- a/packages/core/src/hook/qq-hook-client.ts +++ b/packages/core/src/hook/qq-hook-client.ts @@ -1,6 +1,7 @@ import net from 'net'; import os from 'os'; import path from 'path'; +import { promises as fs } from 'fs'; import { EventEmitter } from 'events'; export const PIPE_MAGIC = 0x31504851; @@ -98,7 +99,7 @@ function mojoPipeName(pid: number, suffix: string): string { if (process.platform === 'win32') { return `\\\\.\\pipe\\mojo.${pid}.${suffix}`; } - return path.join(linuxRuntimeDir(), `mojo.${pid}.${suffix}`); + return path.join(linuxRuntimeDir(), `mojo.${pid}.${suffix}.sock`); } function toBuffer(body: Buffer | Uint8Array | string | null | undefined): Buffer { @@ -282,6 +283,27 @@ export class QqHookClient extends EventEmitter { return mojoPipeName(pid, 'recv'); } + /** + * Lightweight liveness check for the control pipe. Used to detect whether an + * existing SnowLuma hook is still hosted inside the QQ.exe process without + * opening the pipe, so probing cannot disturb the first real client connect. + */ + static async probePipe(pid: number): Promise { + try { + const pipeFileName = process.platform === 'win32' + ? `mojo.${pid}.control` + : `mojo.${pid}.control.sock`; + if (process.platform === 'win32') { + const names = await fs.readdir('\\\\.\\pipe\\'); + return names.some(name => name.toLowerCase() === pipeFileName.toLowerCase()); + } + const names = await fs.readdir(linuxRuntimeDir()); + return names.includes(pipeFileName); + } catch { + return false; + } + } + async connect(): Promise { if (this.closed) { throw new Error('qq_hook client is closed'); @@ -592,8 +614,8 @@ export class QqHookClient extends EventEmitter { const previous = this.loginState; const next = { loggedIn, uin, uinNumber }; this.loginState = next; + this.emit('loginState', next); if (previous.loggedIn !== next.loggedIn || previous.uin !== next.uin) { - this.emit('loginState', next); if (next.loggedIn) { const waiters = this.loginWaiters; this.loginWaiters = [];