Skip to content
This repository was archived by the owner on Jan 7, 2020. It is now read-only.

Commit 912253f

Browse files
committed
Update build system
1 parent df0784f commit 912253f

File tree

7 files changed

+71
-63
lines changed

7 files changed

+71
-63
lines changed

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@ include(FixDebugLibraryLookup)
1414
include_directories(${VCPKG_INCLUDE_DIR})
1515
include_directories(src)
1616

17-
add_definitions(-DBOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE
18-
-D_WIN32_WINNT=0x0501
19-
-DWIN32_LEAN_AND_MEAN
20-
-DNOMINMAX)
17+
add_compile_definitions(BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE
18+
_WIN32_WINNT=0x0501
19+
WIN32_LEAN_AND_MEAN
20+
NOMINMAX)
21+
22+
# read app id from app_id.txt
23+
file(READ "app_id.txt" APP_ID)
24+
string(STRIP "${APP_ID}" APP_ID)
25+
add_compile_definitions(APP_ID=${APP_ID})
2126

2227
find_package(unofficial-iconv CONFIG REQUIRED)
2328

2429
include(FixLinkConflict)
2530

2631
file(GLOB_RECURSE SOURCE_FILES src/*.cpp)
27-
set(LIB_NAME "com.example.demo")
32+
set(LIB_NAME "app")
2833
add_library(${LIB_NAME} SHARED ${SOURCE_FILES})
2934

3035
target_link_libraries(${LIB_NAME} PRIVATE unofficial::iconv::libiconv unofficial::iconv::libcharset)
@@ -34,4 +39,4 @@ cotire(${LIB_NAME})
3439
add_custom_command(TARGET ${LIB_NAME}
3540
POST_BUILD
3641
COMMAND
37-
powershell -ExecutionPolicy Bypass -NoProfile -File "${PROJECT_SOURCE_DIR}/scripts/post_build.ps1" ${LIB_NAME} "$<TARGET_FILE_DIR:${LIB_NAME}>")
42+
powershell -ExecutionPolicy Bypass -NoProfile -File "${PROJECT_SOURCE_DIR}/scripts/post_build.ps1" ${APP_ID} ${LIB_NAME} "$<TARGET_FILE_DIR:${LIB_NAME}>")

README.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,45 @@ CoolQ C++ SDK 封装了跟 DLL 接口相关的底层逻辑,包括:
2323
```cpp
2424
#include "cqsdk/cqsdk.h"
2525

26-
namespace app = cq::app; // 插件本身的生命周期事件和管理
27-
namespace event = cq::event; // 用于注册 QQ 相关的事件处理函数
28-
namespace api = cq::api; // 用于调用酷 Q 提供的接口
29-
namespace logging = cq::logging; // 用于日志
30-
namespace message = cq::message; // 提供封装了的 Message 等类
31-
32-
// 初始化 App Id
33-
CQ_INITIALIZE("com.example.demo");
26+
// namespace cq::app 包含插件本身的生命周期事件和管理
27+
// namespace cq::event 用于注册 QQ 相关的事件处理函数
28+
// namespace cq::api 用于调用酷 Q 提供的接口
29+
// namespace cq::logging 用于日志
30+
// namespace cq::message 提供封装了的 Message 等类
3431

3532
// 插件入口,在静态成员初始化之后,app::on_initialize 事件发生之前被执行,用于配置 SDK 和注册事件回调
3633
CQ_MAIN {
37-
app::on_enable = [] {
38-
// logging、api、dir 等命名空间下的函数只能在事件回调函数内部调用,而不能直接在 CQ_MAIN 中调用
39-
logging::debug(u8"启用", u8"插件已启动");
34+
cq::app::on_enable = [] {
35+
// cq::logging、cq::api、cq::dir 等命名空间下的函数只能在事件回调函数内部调用,而不能直接在 CQ_MAIN 中调用
36+
cq::logging::debug(u8"启用", u8"插件已启动");
4037
};
4138

42-
event::on_private_msg = [](const cq::PrivateMessageEvent &e) {
43-
logging::debug(u8"消息", u8"收到私聊消息:" + e.message + u8",发送者:" + std::to_string(e.user_id));
39+
cq::event::on_private_msg = [](const cq::PrivateMessageEvent &e) {
40+
cq::logging::debug(u8"消息", u8"收到私聊消息:" + e.message + u8",发送者:" + std::to_string(e.user_id));
41+
42+
if (e.user_id != 1002647525) return;
4443

4544
try {
46-
api::send_private_msg(e.user_id, e.message); // echo 回去
45+
cq::api::send_private_msg(e.user_id, e.message); // echo 回去
4746

48-
api::send_msg(e.target, e.message); // 使用 e.target 指定发送目标
47+
cq::api::send_msg(e.target, e.message); // 使用 e.target 指定发送目标
4948

5049
// MessageSegment 类提供一些静态成员函数以快速构造消息段
5150
cq::Message msg = cq::MessageSegment::contact(cq::MessageSegment::ContactType::GROUP, 201865589);
5251
msg.send(e.target); // 使用 Message 类的 send 成员函数
5352
} catch (const cq::exception::ApiError &err) {
5453
// API 调用失败
55-
logging::debug(u8"API", u8"调用失败,错误码:" + std::to_string(err.code));
54+
cq::logging::debug(u8"API", u8"调用失败,错误码:" + std::to_string(err.code));
5655
}
5756

5857
e.block(); // 阻止事件继续传递给其它插件
5958
};
6059

61-
event::on_group_msg = [](const auto &e /* 使用 C++ 的 auto 关键字 */) {
62-
const auto memlist = api::get_group_member_list(e.group_id); // 获取数据接口
60+
cq::event::on_group_msg = [](const auto &e /* 使用 C++ 的 auto 关键字 */) {
61+
const auto memlist = cq::api::get_group_member_list(e.group_id); // 获取数据接口
6362
cq::Message msg = u8"本群一共有 "; // string 到 Message 自动转换
6463
msg += std::to_string(memlist.size()) + u8" 个成员"; // Message 类可以进行加法运算
65-
message::send(e.target, msg); // 使用 message 命名空间的 send 函数
64+
cq::message::send(e.target, msg); // 使用 message 命名空间的 send 函数
6665
};
6766
}
6867
```
@@ -102,13 +101,13 @@ powershell .\scripts\prepare.ps1
102101

103102
### 修改 App Id 和相关信息
104103

105-
修改 [`CMakeLists.txt`](CMakeLists.txt) `LIB_NAME` 变量为你的 App Id,例如 `set(LIB_NAME "com.company.my-awesome-app")`
104+
修改 [`app_id.txt`](app_id.txt) 的内容为你的 App Id,例如 `com.company.my-awesome-app`
106105

107-
复制 [`com.example.demo.json`](com.example.demo.json) 文件,并命名为 `<AppId>.json`,例如 `com.company.my-awesome-app.json`。按需修改其中的信息,通常需要修改 `name``version``version_id``author``description`,根据实际功能可能还需要修改 `menu``auth`。请不要修改 JSON 描述文件的 `event` 字段,因为事件处理函数的名字已经写死在了 SDK 中。另外,这里 JSON 描述文件使用 **UTF-8 编码**,将会在构建时**自动转换成 GB18030 编码**,如果你曾经使用过其它 SDK,可能需要注意一下。
106+
按需修改 [`app.json`](app.json) 文件中的信息,通常需要修改 `name``version``version_id``author``description`,根据实际功能可能还需要修改 `menu``auth`。请不要修改 JSON 描述文件的 `event` 字段,因为事件处理函数的名字已经写死在了 SDK 中。另外,这里 JSON 描述文件使用 **UTF-8 编码**,将会在构建时**自动转换成 GB18030 编码**,如果你曾经使用过其它 SDK,可能需要注意一下。
108107

109108
### 编写功能
110109

111-
移除 [`src/demo.cpp`](src/demo.cpp) 或在其基础上修改,实现自己的功能。如果直接在 `src/demo.cpp` 上修改,请注意修改调用 `CQ_INITIALIZE` 宏时传入的 App Id。
110+
移除 [`src/demo.cpp`](src/demo.cpp) 或在其基础上修改,实现自己的功能。
112111

113112
具体 API 请参考 `src/demo.cpp`,或顺着 [`src/cqsdk/cqsdk.h`](src/cqsdk/cqsdk.h) 头文件找进去,IDE 的自动补全帮助会很大。除此之外,还可以参考 [richardchien/coolq-http-api](https://github.com/richardchien/coolq-http-api) 项目。
114113

@@ -127,23 +126,27 @@ powershell .\scripts\build.ps1 Debug
127126

128127
### 安装插件到 酷Q
129128

130-
复制 `build/Debug/Debug`(如果是 release 编译则是 `build/Release/Release`中的 DLL 和 JSON 文件到 酷Q 的 `app` 目录下,重启 酷Q 即可(注意需要开启 酷Q 的开发模式)。
129+
复制 `build/Debug/Debug`(如果是 release 编译则是 `build/Release/Release`中和你的 App Id 名字相同的文件夹到 酷Q 的 `dev` 目录下, 酷Q 中重载应用即可(注意需要开启 酷Q 的开发模式)。
131130

132131
如果不想每次构建后都手动安装插件,可以添加 `scripts/install.ps1` 文件(使用 UTF-16 LE 编码)如下:
133132

134133
```ps1
135-
$libName = $args[0]
136-
$outDir = $args[1]
134+
$coolqRoot = "C:\Users\Richard\Lab\酷Q Pro" # 注意修改 酷Q 目录
135+
136+
$appId = $args[0]
137+
$libName = $args[1]
138+
$appOutDir = $args[2]
137139
140+
$coolqAppDevDir = "$coolqRoot\dev\$appId"
138141
$dllName = "$libName.dll"
139-
$dllPath = "$outDir\$dllName"
142+
$dllPath = "$appOutDir\$dllName"
140143
$jsonName = "$libName.json"
141-
$jsonPath = "$outDir\$jsonName"
144+
$jsonPath = "$appOutDir\$jsonName"
142145
143146
Write-Host "正在拷贝插件到 酷Q 应用文件夹……"
144147
145-
Copy-Item -Force $dllPath "C:\path\to\coolq\app\$dllName" # 注意修改 酷Q 目录
146-
Copy-Item -Force $jsonPath "C:\path\to\coolq\app\$jsonName"
148+
Copy-Item -Force $dllPath "$coolqAppDevDir\$dllName"
149+
Copy-Item -Force $jsonPath "$coolqAppDevDir\$jsonName"
147150
148151
Write-Host "拷贝完成" -ForegroundColor Green
149152
```
File renamed without changes.

app_id.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.example.demo

scripts/post_build.ps1

556 Bytes
Binary file not shown.

src/cqsdk

Submodule cqsdk updated from 76122cd to 5e1aad4

src/demo.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
#include "cqsdk/cqsdk.h"
22

3-
namespace app = cq::app; // 插件本身的生命周期事件和管理
4-
namespace event = cq::event; // 用于注册 QQ 相关的事件处理函数
5-
namespace api = cq::api; // 用于调用酷 Q 提供的接口
6-
namespace logging = cq::logging; // 用于日志
7-
namespace message = cq::message; // 提供封装了的 Message 等类
8-
9-
// 初始化 App Id
10-
CQ_INITIALIZE("com.example.demo");
3+
// namespace cq::app 包含插件本身的生命周期事件和管理
4+
// namespace cq::event 用于注册 QQ 相关的事件处理函数
5+
// namespace cq::api 用于调用酷 Q 提供的接口
6+
// namespace cq::logging 用于日志
7+
// namespace cq::message 提供封装了的 Message 等类
118

129
// 插件入口,在静态成员初始化之后,app::on_initialize 事件发生之前被执行,用于配置 SDK 和注册事件回调
1310
CQ_MAIN {
14-
cq::config.convert_unicode_emoji = true; // 配置 SDK 自动转换 Emoji 到 Unicode(默认就是 true)
15-
16-
app::on_enable = [] {
17-
// logging、api、dir 等命名空间下的函数只能在事件回调函数内部调用,而不能直接在 CQ_MAIN 中调用
18-
logging::debug(u8"启用", u8"插件已启动");
11+
cq::app::on_enable = [] {
12+
// cq::logging、cq::api、cq::dir 等命名空间下的函数只能在事件回调函数内部调用,而不能直接在 CQ_MAIN 中调用
13+
cq::logging::debug(u8"启用", u8"插件已启动");
1914
};
2015

21-
event::on_private_msg = [](const cq::PrivateMessageEvent &e) {
22-
logging::debug(u8"消息", u8"收到私聊消息:" + e.message + u8",发送者:" + std::to_string(e.user_id));
16+
cq::event::on_private_msg = [](const cq::PrivateMessageEvent &e) {
17+
cq::logging::debug(u8"消息", u8"收到私聊消息:" + e.message + u8",发送者:" + std::to_string(e.user_id));
18+
19+
if (e.user_id != 1002647525) return;
2320

2421
try {
25-
api::send_private_msg(e.user_id, e.message); // echo 回去
22+
cq::api::send_private_msg(e.user_id, e.message); // echo 回去
2623

27-
api::send_msg(e.target, e.message); // 使用 e.target 指定发送目标
24+
cq::api::send_msg(e.target, e.message); // 使用 e.target 指定发送目标
2825

2926
// MessageSegment 类提供一些静态成员函数以快速构造消息段
3027
cq::Message msg = cq::MessageSegment::contact(cq::MessageSegment::ContactType::GROUP, 201865589);
3128
msg.send(e.target); // 使用 Message 类的 send 成员函数
3229
} catch (const cq::exception::ApiError &err) {
3330
// API 调用失败
34-
logging::debug(u8"API", u8"调用失败,错误码:" + std::to_string(err.code));
31+
cq::logging::debug(u8"API", u8"调用失败,错误码:" + std::to_string(err.code));
3532
}
3633

3734
e.block(); // 阻止事件继续传递给其它插件
3835
};
3936

40-
event::on_group_msg = [](const auto &e /* 使用 C++ 的 auto 关键字 */) {
41-
const auto memlist = api::get_group_member_list(e.group_id); // 获取数据接口
37+
cq::event::on_group_msg = [](const auto &e /* 使用 C++ 的 auto 关键字 */) {
38+
const auto memlist = cq::api::get_group_member_list(e.group_id); // 获取数据接口
4239
cq::Message msg = u8"本群一共有 "; // string 到 Message 自动转换
4340
msg += std::to_string(memlist.size()) + u8" 个成员"; // Message 类可以进行加法运算
44-
message::send(e.target, msg); // 使用 message 命名空间的 send 函数
41+
cq::message::send(e.target, msg); // 使用 message 命名空间的 send 函数
4542
};
4643
}
4744

4845
// 添加菜单项,需要同时在 <appid>.json 文件的 menu 字段添加相应的条目,function 字段为 menu_demo_1
4946
CQ_MENU(menu_demo_1) {
50-
logging::info(u8"菜单", u8"点击了示例菜单1");
47+
cq::logging::info(u8"菜单", u8"点击了示例菜单1");
5148
try {
52-
api::send_private_msg(10000, "hello");
49+
cq::api::send_private_msg(10000, "hello");
5350
} catch (const cq::exception::ApiError &) {
54-
logging::warning(u8"菜单", u8"发送失败");
51+
cq::logging::warning(u8"菜单", u8"发送失败");
5552
}
5653
}
5754

58-
// 不像 CQ_INITIALIZE 和 CQ_MAIN,CQ_MENU 可以多次调用来添加多个菜单
55+
#include <Windows.h>
56+
57+
// 不像 CQ_MAIN,CQ_MENU 可以多次调用来添加多个菜单
5958
CQ_MENU(menu_demo_2) {
60-
logging::info(u8"菜单", u8"点击了示例菜单2");
61-
cq::config.convert_unicode_emoji = !cq::config.convert_unicode_emoji;
59+
cq::logging::info(u8"菜单", u8"点击了示例菜单2");
60+
MessageBoxW(nullptr, L"这是一个提示", L"提示", MB_OK | MB_SETFOREGROUND | MB_TASKMODAL | MB_TOPMOST);
6261
}

0 commit comments

Comments
 (0)