Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
重构全部代码,用观察者模式
Browse files Browse the repository at this point in the history
Former-commit-id: f0a29ac
  • Loading branch information
Nambers committed Jan 23, 2021
1 parent 4987ab6 commit 5a0f0f9
Show file tree
Hide file tree
Showing 34 changed files with 260 additions and 104 deletions.
4 changes: 2 additions & 2 deletions doc/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MiraiCP是一个[Mirai](https://github.com/mamoe/mirai) 的C++语言的社区SDK

实现原理:

用kotlin调用java的jni包,再用jni调用c++编写dll动态链接库,实现使用c++语言编写mirai机器人
用kotlin调用java的jni包,再用jni调用c++编写dll动态链接库,实现使用c++语言编写mirai机器人(下图已过时,不过大体上是这样的)

![项目流程](https://raw.githubusercontent.com/Nambers/MiraiCP/master/doc/pic/流程.svg?raw=true)

Expand All @@ -26,7 +26,7 @@ mirai-demo: C++SDK

c++版
1. 下载release文件(中包含编译好的.jar)或源代码(需自己编译出.jar)
2. c++sdk(mirai-demo)中`procession.cpp`中编辑你想要的功能
2. c++sdk(mirai-demo)中`procession.cpp`中编辑代码实现你想要的功能
3. 生成dll动态链接库(用x64,release这个配置)
4. 找到生成的.dll文件(通常叫mirai-demo.dll目前不支持改名,除非你改源代码里的名称生成的自定义.jar插件)和.jar文件(如果是release就在根目录,如果源代码要自己生成)
5. 把.dll文件放到mcl(mirai-console-loader)下的data文件夹下的miraiCP里,然后把.jar文件放到plugin文件夹里,如果data下没有没有要先运行一次mcl然后会报错,退出,复制dll进去再运行
Expand Down
Binary file modified mirai-demo/.vs/mirai-demo/v16/.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion mirai-demo/.vs/mirai-demo/v16/Browse.VC.db.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
eb4bdcffbb4edf2d9af251b2aecbded57f4d14b1
606927228cb056176d8a2e1a791251ed63c8c4f7
Original file line number Diff line number Diff line change
@@ -1 +1 @@
eee644334b70921027d186bd936589a8ff6239b5
ee62910962d5c8242b1f44d7cda78d9f66f21010
2 changes: 2 additions & 0 deletions mirai-demo/ClassDiagram.cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram />
58 changes: 41 additions & 17 deletions mirai-demo/Procession.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
#include "pch.h"
class procession: public Procession{
//Ö÷Òª´¦ÀíλÖÃ
public:
void GroupMessage(Group g, Friend f, string message) {
g.SendMsg(message);
logger.Info(message);
}
void PrivateMessage(Friend f, string message) {
f.SendMsg(message);
}
string GroupInvite(Group g, Friend f) {
return accept;
}
string NewFrinedRequest(Friend f, string message) {
return accept;
}
};
void EventRegister() {
/*
注册事件监听-用户自定义
logger - 日志组件
logger.Info(string)发送消息级日志
logger.Warning(string)发送警告级日志
logger.Error(string)发送错误级日志
procession 广播源
procession.registerEvent(lambda) 开始监听事件,可以重复监听
procession.registerEvent([](GroupMessageEvent param){ \*处理*\});是监听群消息
procession.registerEvent([](PrivateMessageEvent param){ \*处理*\});是监听群消息
...
参数都在param变量里,在lambda块中使用param.xxx来调用
*/

procession.registerEvent([](GroupMessageEvent param)->void {
//在这写你自己处理群消息的代码
logger.Info("hi");
param.group.SendMsg("hi");
});
procession.registerEvent([](PrivateMessageEvent param)->void {
//在这写你自己处理私聊消息的代码
logger.Info("hi");
param.sender.SendMsg("hi");
});
procession.registerEvent([](GroupInviteEvent param)->bool{
//处理群邀请,true同意进群,false不同意
if (param.sender.id == 11111) {
return true;
}
return true;
});
procession.registerEvent([](NewFriendRequestEvent param)->bool {
//新好友邀请
logger.Info("新好友申请来自于" + to_string(param.sender.id));
if (param.message == "hhh") {
return false;
}
return true;
});
}
1 change: 1 addition & 0 deletions mirai-demo/mirai-demo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
<ClCompile Include="tools.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram.cd" />
<None Include="cpp.hint" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
1 change: 1 addition & 0 deletions mirai-demo/mirai-demo.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
<None Include="ClassDiagram.cd" />
</ItemGroup>
</Project>
38 changes: 23 additions & 15 deletions mirai-demo/pch.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "pch.h"
#include "Procession.cpp"
Logger logger = Logger();
procession p = procession();
Event procession = Event();
/*
*正文开始
*/
Expand All @@ -12,11 +11,16 @@ procession p = procession();
* 返回值:jstring (用str2jstring把string类型转成jsrting) 发送返回的字符串
*/
JNIEXPORT jstring JNICALL Java_org_example_mirai_plugin_CPP_1lib_Verify(JNIEnv* env, jobject job) {
//初始化日志模块
logger.init(env);
p.init(logger);
//初始化监听
EventRegister();
return tools.str2jstring(env, "2333");//验证机制
}

/*
* 消息解析分流
*/
JNIEXPORT jstring JNICALL Java_org_example_mirai_plugin_CPP_1lib_Event
(JNIEnv* env, jobject job, jstring content) {
string Rcontent = tools.jstring2str(env, content);
Expand All @@ -31,24 +35,28 @@ JNIEXPORT jstring JNICALL Java_org_example_mirai_plugin_CPP_1lib_Event
logger.Error("JSON reader error");
}
switch (root["type"].asInt()) {
/*
1- groupmessage
2- privatemessage
*/
case 1:
//GroupMessage
p.GroupMessage(Group(env,job, root["groupid"].asLargestInt()),
Friend(env, job, root["senderid"].asLargestInt()), root["message"].asCString());
procession.broadcast(GroupMessageEvent(
Group(env, job, root["groupid"].asLargestInt()),
Friend(env, job, root["senderid"].asLargestInt()),
root["message"].asCString()));
return tools.str2jstring(env, "NULL");
case 2:
p.PrivateMessage(Friend(env, job, root["senderid"].asLargestInt()), root["message"].asCString());
//私聊消息
procession.broadcast(PrivateMessageEvent(
Friend(env, job, root["senderid"].asLargestInt()),
root["message"].asCString()));
return tools.str2jstring(env, "NULL");
case 3:
return tools.str2jstring(env, p.GroupInvite(Group(env, job, root["groupid"].asLargestInt()),
Friend(env, job, root["invitorid"].asLargestInt())).c_str());
case 4: return tools.str2jstring(env,
p.NewFriendRequest(
Friend(env, job, root["friendid"].asLargestInt()), root["message"].asCString()).c_str());
//群聊邀请
return tools.str2jstring(env, procession.broadcast(GroupInviteEvent(
Group(env, job, root["groupid"].asLargestInt()),
Friend(env, job, root["invitorid"].asLargestInt()))).c_str());
case 4:
return tools.str2jstring(env, "false");
//return tools.str2jstring(env,p.NewFriendRequest(
//Friend(env, job, root["friendid"].asLargestInt()), root["message"].asCString()).c_str());
}
logger.Error("unknown type");
return tools.str2jstring(env, "ERROR");
Expand Down
12 changes: 7 additions & 5 deletions mirai-demo/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
*/
#ifndef PCH_H
#define PCH_H

// 添加要在此处预编译的标头
#include <iostream>
#include "framework.h"
#include <string>
#include <functional>
#include <json.h>
#include <iostream>
#include "org_example_mirai_plugin_CPP_lib.h"
#include <string>
#include "tools.h"
#include <jni.h>
#include <sstream>
#include <vector>
using namespace std;
#endif //PCH_H
#include "tools.h"
#endif //PCH_H
11 changes: 5 additions & 6 deletions mirai-demo/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ void Logger::Error(string log) {
void Logger::Info(string log) {
this->env->CallStaticVoidMethod(this->javaclass, this->sinfo, tools.str2jstring(env, log.c_str()));
}
string Friend::GetNick() {
/*string Friend::GetNick() {
return tools.jstring2str(this->env, (jstring)this->env->CallStaticObjectMethod(java_first, Send_Msg_id, (jlong)this->id));
}
}*/
Friend::Friend (JNIEnv* env, jobject job,long id){
this->java_first = env->FindClass("org/example/mirai/plugin/CPP_lib");
this->Send_Msg_id = env->GetStaticMethodID(java_first, "SendPrivateMSG", "(Ljava/lang/String;J)V");
Expand All @@ -42,7 +42,7 @@ Group::~Group() {
env->DeleteLocalRef(java_first);

}
string Tools::jstring2str(JNIEnv* env, jstring jstr)
string Tools::jstring2str(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
Expand All @@ -62,8 +62,7 @@ Group::~Group() {
free(rtn);
return stemp;
}

jstring Tools::str2jstring(JNIEnv* env, const char* pat)
jstring Tools::str2jstring(JNIEnv* env, const char* pat)
{
//定义java String类 strClass
jclass strClass = (env)->FindClass("Ljava/lang/String;");
Expand All @@ -78,7 +77,7 @@ Group::~Group() {
//将byte数组转换为java String,并输出
return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);
}
string Tools::JLongToString(jlong qqid) {
string Tools::JLongToString(jlong qqid) {
auto id = [qqid]() -> string {
stringstream stream;
stream << qqid;
Expand Down
Loading

0 comments on commit 5a0f0f9

Please sign in to comment.