26 包 monorepo(TS 全源码): - snowcat-core: NT 内核 API 封装 + listener(含 9.9.37 六回调适配)+ Packet 发包 - snowcat-onebot: OneBot v11 全动作 + MCP Streamable HTTP 适配器 + 稳定线 - snowcat-webui-frontend/backend: 雪猫控制台(React+HeroUI) - snowcat-shell: 单文件 bundle 出口(vite inlineDynamicImports) - snowcat-native: napi2native / MoeHoo / ffmpeg / pty 运行时 - 56 项 SnowLuma 独门动作移植(qzone 全家桶/群相册/装扮/闪照/收藏) - 工具件: 解包器/启动器/便携制作器/doctor GitHub 关联已全量指向 DandanLLab/SnowCatQQ
105 lines
3.4 KiB
TypeScript
105 lines
3.4 KiB
TypeScript
import {
|
|
InstanceContext,
|
|
SnowCatCore,
|
|
} from 'snowcat-core';
|
|
import { SnowCatProtocolConfigLoader, SnowCatProtocolConfig } from '@/snowcat-protocol/config';
|
|
import { SnowCatPathWrapper } from 'snowcat-common/src/path';
|
|
import {
|
|
SnowCatProtocolNetworkManager,
|
|
} from '@/snowcat-protocol/network';
|
|
import {
|
|
SnowCatProtocolMsgApi,
|
|
SnowCatProtocolUserApi,
|
|
SnowCatProtocolGroupApi,
|
|
SnowCatProtocolFriendApi,
|
|
} from '@/snowcat-protocol/api';
|
|
import { ActionMap, createActionMap } from '@/snowcat-protocol/action';
|
|
|
|
interface ApiListType {
|
|
MsgApi: SnowCatProtocolMsgApi;
|
|
UserApi: SnowCatProtocolUserApi;
|
|
GroupApi: SnowCatProtocolGroupApi;
|
|
FriendApi: SnowCatProtocolFriendApi;
|
|
}
|
|
|
|
// NapCat Protocol 适配器 - NapCat 私有 Bot 协议实现
|
|
export class SnowCatProtocolAdapter {
|
|
readonly core: SnowCatCore;
|
|
readonly context: InstanceContext;
|
|
|
|
configLoader: SnowCatProtocolConfigLoader;
|
|
public apis: ApiListType;
|
|
networkManager: SnowCatProtocolNetworkManager;
|
|
actions: ActionMap;
|
|
|
|
constructor (core: SnowCatCore, context: InstanceContext, pathWrapper: SnowCatPathWrapper) {
|
|
this.core = core;
|
|
this.context = context;
|
|
this.configLoader = new SnowCatProtocolConfigLoader(core, pathWrapper.configPath);
|
|
this.apis = {
|
|
MsgApi: new SnowCatProtocolMsgApi(this, core),
|
|
UserApi: new SnowCatProtocolUserApi(this, core),
|
|
GroupApi: new SnowCatProtocolGroupApi(this, core),
|
|
FriendApi: new SnowCatProtocolFriendApi(this, core),
|
|
} as const;
|
|
this.actions = createActionMap(this, core);
|
|
this.networkManager = new SnowCatProtocolNetworkManager();
|
|
}
|
|
|
|
// 检查协议是否启用
|
|
isEnabled (): boolean {
|
|
return this.configLoader.configData.enable;
|
|
}
|
|
|
|
async createProtocolLog (config: SnowCatProtocolConfig) {
|
|
let log = '[SnowCat Protocol] 配置加载\n';
|
|
log += `协议状态: ${config.enable ? '已启用' : '已禁用'}\n`;
|
|
|
|
if (config.enable) {
|
|
for (const key of config.network.httpServers) {
|
|
log += `HTTP服务: ${key.host}:${key.port}, : ${key.enable ? '已启动' : '未启动'}\n`;
|
|
}
|
|
for (const key of config.network.websocketServers) {
|
|
log += `WebSocket服务: ${key.host}:${key.port}, : ${key.enable ? '已启动' : '未启动'}\n`;
|
|
}
|
|
for (const key of config.network.websocketClients) {
|
|
log += `WebSocket客户端: ${key.url}, : ${key.enable ? '已启动' : '未启动'}\n`;
|
|
}
|
|
}
|
|
return log;
|
|
}
|
|
|
|
async initProtocol () {
|
|
const config = this.configLoader.configData;
|
|
|
|
// 如果协议未启用,直接返回
|
|
if (!config.enable) {
|
|
this.context.logger.log('[SnowCat Protocol] 协议未启用,跳过初始化');
|
|
return;
|
|
}
|
|
|
|
const selfInfo = this.core.selfInfo;
|
|
const serviceInfo = await this.createProtocolLog(config);
|
|
this.context.logger.log(`[Notice] ${serviceInfo}`);
|
|
|
|
// 注册网络适配器
|
|
// 这里可以根据配置注册不同的网络适配器
|
|
// 例如: WebSocket Server, WebSocket Client, HTTP Server 等
|
|
|
|
await this.networkManager.openAllAdapters();
|
|
|
|
this.context.logger.log(`[SnowCat Protocol] 初始化完成,Bot: ${selfInfo.uin}`);
|
|
}
|
|
|
|
async close () {
|
|
await this.networkManager.closeAllAdapters();
|
|
this.context.logger.log('[SnowCat Protocol] 已关闭所有网络适配器');
|
|
}
|
|
}
|
|
|
|
export * from './types/index';
|
|
export * from './api/index';
|
|
export * from './event/index';
|
|
export * from './config/index';
|
|
export * from './network/index';
|