🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Common模块 [![](https://img.shields.io/badge/simple--robot--module-debugger-green)](https://github.com/ForteScarlet/simple-robot-core) [![](https://img.shields.io/maven-central/v/love.forte.simple-robot-module/simple-robot-module-debugger-common)](https://search.maven.org/artifact/love.forte.simple-robot-module/simple-robot-module-debugger-common) Common模块是debugger中提供的基础的模块,也是后续章节中client与server的依赖模块。Common模块中提供了一些能够快速获取测试用假的`MsgGet`对象的Mock工具、序列化工具、Debug启动器等一些用于debug测试用的东西。 <br> ## **使用** 以maven为例: (版本参考maven仓库或者此文档上方的版本号图标) ```xml <!-- https://search.maven.org/artifact/love.forte.simple-robot-module/simple-robot-module-debugger-common --> <dependency> <groupId>love.forte.simple-robot-module</groupId> <artifactId>simple-robot-module-debugger-common</artifactId> <version>${project.version}</version> </dependency> ``` <br> ## **DebuggerMsgGet** Common模组针对`MsgGet`的各个类型(例如`PrivateMsg`或者`GroupMsg`)提供了其对应的Debug封装类,他们的命名方式是在原本的类型前面加上`Debugger`,例如`DebuggerPrivateMsg`。他们在下文会统一称为`DebuggerMsgGet`。 <br> 当然,一般情况下你不需要直接手动去创建他们,你可以使用`DebugMocker`来获取他们。关于`DebugMocker`会在后续讲到。 <br> 所有的`DebuggerMsgGet`中应有的字段都有`getter/setter`,他们大部分命名为`dgXxx`, 例如`PrivateMsg`中的`msg`属性,其对应的`getter/setter`为`getDgMsg()/setDgMsg(...)` 当然,除了`DebuggerMsgGet`中提供的字段,原本接口的其他方法也是可用的,例如`getMsg()` <br> ## **Debug启动器** Common模组提供了一个Debug启动器`com.simbot.modules.debugger.common.DebugApplication` 即 `DebugApplication`,你可以使用此启动器来启动程序。debug启动器会默认注册一些随机的账号,因此当你在使用它进行测试的时候尽量不要使用一些其他模组(例如redis-botManager) 你可以像如下代码所示的那样来测试你的程序: ```java /** * 启动器,启动器所在的位置至少需要一层包路径。 **/ // 如果不用接口启动,则记得标注注解。默认的配置文件的文件名为:simple-robot-conf.properties @SimpleRobotApplication public class DebugRunApplication { public static void main(String[] args) throws InterruptedException { // 使用debug启动 DebugContext context = new DebugApplication().run(DebugRunApplication.class, args); // 获取依赖中心 DependCenter center = context.getDependCenter(); // 获取Bot管理器 BotManager botManager = context.getBotManager(); // 获取监听触发器 MsgProcessor msgProcessor = context.getMsgProcessor(); // 查看debug注册的随机bot账号信息 for (BotInfo bot : botManager.bots()) { System.out.println(bot.getSender().GETTER.getLoginQQInfo()); } // 获取DebugMocker DebugMocker mocker = DebugMocker.getInstance(); // 每2秒“接收”一条私信消息。 while(true){ // 获取debug用的私信消息 DebuggerPrivateMsg privateMsg = mocker.getPrivateMsg(); // 触发私信监听 msgProcessor.onMsg(privateMsg); // 休息2秒 Thread.sleep(2000); } } } ``` <br> ## **DebugMocker** 上面的代码中提到了`DebugMocker`这个类,那么就先介绍一下这个类。 `DebugMocker`是单例类,可以通过`INSTANCE`或`getInstance()`获取。 `DebugMocker`可以快速获取各种类型的`MsgGet`的debugger实例,例如一个`privateMsg`或者一个`GroupMsg`,就像下述示例代码那样: ```java // 获取Mocker DebugMocker mocker = DebugMocker.getInstance(); // 获取一个debug私信消息 DebuggerPrivateMsg privateMsg = mocker.getPrivateMsg(); // 获取5个 List<DebuggerPrivateMsg> privateMsgList = mocker.getPrivateMsg(5); // 获取debug群消息 DebuggerGroupMsg groupMsg = mocker.getGroupMsg(); // ...依此类推 // 根据MsgGetType类型来获取一个Debug消息封装类 DebuggerBaseMsgGet debugMsgGet = mocker.typeBy(MsgGetTypes.groupBan); ``` > DebugMocker依赖于`Mock.java`,此项目详情可查看: gitee : https://gitee.com/ForteScarlet/Mock.java github: https://github.com/ForteScarlet/Mock.java ~~(广告费一条5毛)~~ <br> ## **SerializationHelper** `SerializationHelper`是一个接口,其提供了两个方法: - **byte[] serialization(? extends DebuggerBaseMsgGet)** 即将一个Debug消息序列化为byte数组 <br> - **DebuggerBaseMsgGet deserialization(byte[])** 即将序列化后的Debug消息字节数组转化为Debug消息封装类 <br> 此接口主要为后续介绍的`client`模块与`server`模块之间的通讯使用,其默认的实现方式为使用`Protostuff`来实现序列化。`ProtostuffSerializationHelper`即为其实现类,此类为单例,可使用`INSTANCE`或`getInstance()`得到。 此接口规定同一个实现类所得到的`byte[]`一定可以在此实现类中实现反序列化,但是不必保证不同的序列化实现类之间的相互转化。 <br> ## **DebugController** debug控制器为Common模组提供的一个接口,其主要为后续介绍的`client`模块与`server`模块之间的通讯使用。 在单独使用Common模组的时候,不存在此接口的实现类,因此需要配合其他对此接口做了实现的模块或模组来搭配使用,例如debugger模组的`eclient`模块或`server`模块 接口定义了如下方法: (`[...]`中的内容代表此参数存在默认值重载) - **void sendRandom([Consumer<DebuggerBaseMsgGet> debugMsgConsumer])** 发送一个随机类型的随机消息给远程debug端或者本地debug端,参数为对随机生成的消息做一些前置处理,例如修改一下此消息的Bot账号。默认无操作。 <br> - **void send(MsgGetTypes type [, Consumer<DebuggerBaseMsgGet> debugMsgConsumer])** 发送指定类型的随机消息。可对此消息做一些前置处理,默认无操作。 <br> - **void send(MsgGet msgGet)** 发送一个消息。**注意:此消息无论类型,最终均会转化为`DebuggerMsgGet`对应类型的封装类** <br> - **boolean isActive()** 此debug控制器是否依旧活跃。以进行远程调试的模块为例,此方法的返回值客户端中代表了是否处于连接状态,服务端中代表了服务是否存活。 <br> ## HexUtils 普通的工具类,将`byte[]`转化为16进制字符串或将16进制字符串转化为`byte[]` 此类为单例。