mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor
This commit is contained in:
parent
a1298b1c93
commit
da0ebd3f80
@ -1,6 +1,6 @@
|
|||||||
import { invoke, NTClass, NTMethod } from '../ntcall'
|
import { invoke, NTClass, NTMethod } from '../ntcall'
|
||||||
import { GeneralCallResult } from '../services'
|
import { GeneralCallResult } from '../services'
|
||||||
import { ReceiveCmd } from '../hook'
|
import { ReceiveCmdS } from '../hook'
|
||||||
import { BrowserWindow } from 'electron'
|
import { BrowserWindow } from 'electron'
|
||||||
import { Service, Context } from 'cordis'
|
import { Service, Context } from 'cordis'
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ export class NTQQWindowApi extends Service {
|
|||||||
async openWindow<R = GeneralCallResult>(
|
async openWindow<R = GeneralCallResult>(
|
||||||
ntQQWindow: NTQQWindow,
|
ntQQWindow: NTQQWindow,
|
||||||
args: unknown[],
|
args: unknown[],
|
||||||
cbCmd: ReceiveCmd | undefined,
|
cbCmd: ReceiveCmdS | undefined,
|
||||||
autoCloseSeconds: number = 2,
|
autoCloseSeconds: number = 2,
|
||||||
) {
|
) {
|
||||||
const result = await invoke<R>(
|
const result = await invoke<R>(
|
||||||
|
@ -6,48 +6,46 @@ import { Dict } from 'cosmokit'
|
|||||||
|
|
||||||
export const hookApiCallbacks: Record<string, (res: any) => void> = {}
|
export const hookApiCallbacks: Record<string, (res: any) => void> = {}
|
||||||
|
|
||||||
export const ReceiveCmdS = {
|
export enum ReceiveCmdS {
|
||||||
RECENT_CONTACT: 'nodeIKernelRecentContactListener/onRecentContactListChangedVer2',
|
RECENT_CONTACT = 'nodeIKernelRecentContactListener/onRecentContactListChangedVer2',
|
||||||
UPDATE_MSG: 'nodeIKernelMsgListener/onMsgInfoListUpdate',
|
UPDATE_MSG = 'nodeIKernelMsgListener/onMsgInfoListUpdate',
|
||||||
UPDATE_ACTIVE_MSG: 'nodeIKernelMsgListener/onActiveMsgInfoUpdate',
|
UPDATE_ACTIVE_MSG = 'nodeIKernelMsgListener/onActiveMsgInfoUpdate',
|
||||||
NEW_MSG: `nodeIKernelMsgListener/onRecvMsg`,
|
NEW_MSG = `nodeIKernelMsgListener/onRecvMsg`,
|
||||||
NEW_ACTIVE_MSG: `nodeIKernelMsgListener/onRecvActiveMsg`,
|
NEW_ACTIVE_MSG = `nodeIKernelMsgListener/onRecvActiveMsg`,
|
||||||
SELF_SEND_MSG: 'nodeIKernelMsgListener/onAddSendMsg',
|
SELF_SEND_MSG = 'nodeIKernelMsgListener/onAddSendMsg',
|
||||||
USER_INFO: 'nodeIKernelProfileListener/onProfileSimpleChanged',
|
USER_INFO = 'nodeIKernelProfileListener/onProfileSimpleChanged',
|
||||||
USER_DETAIL_INFO: 'nodeIKernelProfileListener/onProfileDetailInfoChanged',
|
USER_DETAIL_INFO = 'nodeIKernelProfileListener/onProfileDetailInfoChanged',
|
||||||
GROUPS: 'nodeIKernelGroupListener/onGroupListUpdate',
|
GROUPS = 'nodeIKernelGroupListener/onGroupListUpdate',
|
||||||
GROUPS_STORE: 'onGroupListUpdate',
|
GROUPS_STORE = 'onGroupListUpdate',
|
||||||
GROUP_MEMBER_INFO_UPDATE: 'nodeIKernelGroupListener/onMemberInfoChange',
|
GROUP_MEMBER_INFO_UPDATE = 'nodeIKernelGroupListener/onMemberInfoChange',
|
||||||
FRIENDS: 'onBuddyListChange',
|
FRIENDS = 'onBuddyListChange',
|
||||||
MEDIA_DOWNLOAD_COMPLETE: 'nodeIKernelMsgListener/onRichMediaDownloadComplete',
|
MEDIA_DOWNLOAD_COMPLETE = 'nodeIKernelMsgListener/onRichMediaDownloadComplete',
|
||||||
UNREAD_GROUP_NOTIFY: 'nodeIKernelGroupListener/onGroupNotifiesUnreadCountUpdated',
|
UNREAD_GROUP_NOTIFY = 'nodeIKernelGroupListener/onGroupNotifiesUnreadCountUpdated',
|
||||||
GROUP_NOTIFY: 'nodeIKernelGroupListener/onGroupSingleScreenNotifies',
|
GROUP_NOTIFY = 'nodeIKernelGroupListener/onGroupSingleScreenNotifies',
|
||||||
FRIEND_REQUEST: 'nodeIKernelBuddyListener/onBuddyReqChange',
|
FRIEND_REQUEST = 'nodeIKernelBuddyListener/onBuddyReqChange',
|
||||||
SELF_STATUS: 'nodeIKernelProfileListener/onSelfStatusChanged',
|
SELF_STATUS = 'nodeIKernelProfileListener/onSelfStatusChanged',
|
||||||
CACHE_SCAN_FINISH: 'nodeIKernelStorageCleanListener/onFinishScan',
|
CACHE_SCAN_FINISH = 'nodeIKernelStorageCleanListener/onFinishScan',
|
||||||
MEDIA_UPLOAD_COMPLETE: 'nodeIKernelMsgListener/onRichMediaUploadComplete',
|
MEDIA_UPLOAD_COMPLETE = 'nodeIKernelMsgListener/onRichMediaUploadComplete',
|
||||||
SKEY_UPDATE: 'onSkeyUpdate',
|
SKEY_UPDATE = 'onSkeyUpdate',
|
||||||
} as const
|
}
|
||||||
|
|
||||||
export type ReceiveCmd = string
|
type NTReturnData = [
|
||||||
|
{
|
||||||
interface NTQQApiReturnData extends Array<unknown> {
|
|
||||||
0: {
|
|
||||||
type: 'request'
|
type: 'request'
|
||||||
eventName: NTClass
|
eventName: NTClass
|
||||||
callbackId?: string
|
callbackId?: string
|
||||||
}
|
},
|
||||||
1: {
|
{
|
||||||
cmdName: ReceiveCmd
|
cmdName: ReceiveCmdS
|
||||||
cmdType: 'event'
|
cmdType: 'event'
|
||||||
payload: unknown
|
payload: unknown
|
||||||
}[]
|
}[]
|
||||||
}
|
]
|
||||||
|
|
||||||
const logHook = false
|
const logHook = false
|
||||||
|
|
||||||
const receiveHooks: Array<{
|
const receiveHooks: Array<{
|
||||||
method: ReceiveCmd[]
|
method: ReceiveCmdS[]
|
||||||
hookFunc: (payload: any) => void | Promise<void>
|
hookFunc: (payload: any) => void | Promise<void>
|
||||||
id: string
|
id: string
|
||||||
}> = []
|
}> = []
|
||||||
@ -58,62 +56,45 @@ const callHooks: Array<{
|
|||||||
}> = []
|
}> = []
|
||||||
|
|
||||||
export function hookNTQQApiReceive(window: BrowserWindow, onlyLog: boolean) {
|
export function hookNTQQApiReceive(window: BrowserWindow, onlyLog: boolean) {
|
||||||
const originalSend = window.webContents.send
|
window.webContents.send = new Proxy(window.webContents.send, {
|
||||||
const patchSend = (channel: string, ...args: NTQQApiReturnData) => {
|
apply(target, thisArg, args: [channel: string, ...args: NTReturnData]) {
|
||||||
try {
|
try {
|
||||||
const isLogger = args[0]?.eventName?.startsWith('ns-LoggerApi')
|
if (logHook && !args[1]?.eventName?.startsWith('ns-LoggerApi')) {
|
||||||
if (logHook && !isLogger) {
|
log(`received ntqq api message: ${args[0]}`, args)
|
||||||
log(`received ntqq api message: ${channel}`, args)
|
}
|
||||||
}
|
} catch { }
|
||||||
} catch { }
|
if (!onlyLog) {
|
||||||
if (!onlyLog) {
|
if (args[2] instanceof Array) {
|
||||||
if (args?.[1] instanceof Array) {
|
for (const receiveData of args[2]) {
|
||||||
for (const receiveData of args[1]) {
|
const ntMethodName = receiveData.cmdName
|
||||||
const ntQQApiMethodName = receiveData.cmdName
|
for (const hook of receiveHooks) {
|
||||||
// log(`received ntqq api message: ${channel} ${ntQQApiMethodName}`, JSON.stringify(receiveData))
|
if (hook.method.includes(ntMethodName)) {
|
||||||
for (const hook of receiveHooks) {
|
Promise.resolve(hook.hookFunc(receiveData.payload))
|
||||||
if (hook.method.includes(ntQQApiMethodName)) {
|
}
|
||||||
new Promise(resolve => {
|
|
||||||
try {
|
|
||||||
hook.hookFunc(receiveData.payload)
|
|
||||||
} catch (e) {
|
|
||||||
log('hook error', ntQQApiMethodName, (e as Error).stack?.toString())
|
|
||||||
}
|
|
||||||
resolve(undefined)
|
|
||||||
}).then()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (args[1]?.callbackId) {
|
||||||
if (args[0]?.callbackId) {
|
const callbackId = args[1].callbackId
|
||||||
// log("hookApiCallback", hookApiCallbacks, args)
|
if (hookApiCallbacks[callbackId]) {
|
||||||
const callbackId = args[0].callbackId
|
Promise.resolve(hookApiCallbacks[callbackId](args[2]))
|
||||||
if (hookApiCallbacks[callbackId]) {
|
delete hookApiCallbacks[callbackId]
|
||||||
new Promise(resolve => {
|
}
|
||||||
hookApiCallbacks[callbackId](args[1])
|
|
||||||
resolve(undefined)
|
|
||||||
}).then()
|
|
||||||
delete hookApiCallbacks[callbackId]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
const ret = target.apply(thisArg, args)
|
||||||
originalSend.call(window.webContents, channel, ...args)
|
return ret
|
||||||
}
|
},
|
||||||
window.webContents.send = patchSend
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hookNTQQApiCall(window: BrowserWindow, onlyLog: boolean) {
|
export function hookNTQQApiCall(window: BrowserWindow, onlyLog: boolean) {
|
||||||
// 监听调用NTQQApi
|
|
||||||
const webContents = window.webContents as Dict
|
const webContents = window.webContents as Dict
|
||||||
const ipc_message_proxy = webContents._events['-ipc-message']?.[0] || webContents._events['-ipc-message']
|
const ipc_message_proxy = webContents._events['-ipc-message']?.[0] || webContents._events['-ipc-message']
|
||||||
|
|
||||||
const proxyIpcMsg = new Proxy(ipc_message_proxy, {
|
const proxyIpcMsg = new Proxy(ipc_message_proxy, {
|
||||||
apply(target, thisArg, args) {
|
apply(target, thisArg, args) {
|
||||||
// console.log(thisArg, args);
|
const isLogger = args[3]?.[0]?.eventName?.startsWith('ns-LoggerApi')
|
||||||
let isLogger = false
|
|
||||||
try {
|
|
||||||
isLogger = args[3][0].eventName.startsWith('ns-LoggerApi')
|
|
||||||
} catch (e) { }
|
|
||||||
if (!isLogger) {
|
if (!isLogger) {
|
||||||
try {
|
try {
|
||||||
logHook && log('call NTQQ api', thisArg, args)
|
logHook && log('call NTQQ api', thisArg, args)
|
||||||
@ -125,17 +106,10 @@ export function hookNTQQApiCall(window: BrowserWindow, onlyLog: boolean) {
|
|||||||
const callParams = _args.slice(1)
|
const callParams = _args.slice(1)
|
||||||
callHooks.forEach((hook) => {
|
callHooks.forEach((hook) => {
|
||||||
if (hook.method.includes(cmdName)) {
|
if (hook.method.includes(cmdName)) {
|
||||||
new Promise(resolve => {
|
Promise.resolve(hook.hookFunc(callParams))
|
||||||
try {
|
|
||||||
hook.hookFunc(callParams)
|
|
||||||
} catch (e) {
|
|
||||||
log('hook call error', e, _args)
|
|
||||||
}
|
|
||||||
resolve(undefined)
|
|
||||||
}).then()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} catch (e) { }
|
} catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return target.apply(thisArg, args)
|
return target.apply(thisArg, args)
|
||||||
@ -147,7 +121,7 @@ export function hookNTQQApiCall(window: BrowserWindow, onlyLog: boolean) {
|
|||||||
webContents._events['-ipc-message'] = proxyIpcMsg
|
webContents._events['-ipc-message'] = proxyIpcMsg
|
||||||
}
|
}
|
||||||
|
|
||||||
const ipc_invoke_proxy = webContents._events['-ipc-invoke']?.[0] || webContents._events['-ipc-invoke']
|
/*const ipc_invoke_proxy = webContents._events['-ipc-invoke']?.[0] || webContents._events['-ipc-invoke']
|
||||||
const proxyIpcInvoke = new Proxy(ipc_invoke_proxy, {
|
const proxyIpcInvoke = new Proxy(ipc_invoke_proxy, {
|
||||||
apply(target, thisArg, args) {
|
apply(target, thisArg, args) {
|
||||||
//HOOK_LOG && log('call NTQQ invoke api', thisArg, args)
|
//HOOK_LOG && log('call NTQQ invoke api', thisArg, args)
|
||||||
@ -157,9 +131,7 @@ export function hookNTQQApiCall(window: BrowserWindow, onlyLog: boolean) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
const ret = target.apply(thisArg, args)
|
const ret = target.apply(thisArg, args)
|
||||||
/*try {
|
//HOOK_LOG && log('call NTQQ invoke api return', ret)
|
||||||
HOOK_LOG && log('call NTQQ invoke api return', ret)
|
|
||||||
} catch (e) { }*/
|
|
||||||
return ret
|
return ret
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -167,11 +139,11 @@ export function hookNTQQApiCall(window: BrowserWindow, onlyLog: boolean) {
|
|||||||
webContents._events['-ipc-invoke'][0] = proxyIpcInvoke
|
webContents._events['-ipc-invoke'][0] = proxyIpcInvoke
|
||||||
} else {
|
} else {
|
||||||
webContents._events['-ipc-invoke'] = proxyIpcInvoke
|
webContents._events['-ipc-invoke'] = proxyIpcInvoke
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
export function registerReceiveHook<PayloadType>(
|
export function registerReceiveHook<PayloadType>(
|
||||||
method: ReceiveCmd | ReceiveCmd[],
|
method: ReceiveCmdS | ReceiveCmdS[],
|
||||||
hookFunc: (payload: PayloadType) => void,
|
hookFunc: (payload: PayloadType) => void,
|
||||||
): string {
|
): string {
|
||||||
const id = randomUUID()
|
const id = randomUUID()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user