This repository was archived by the owner on Nov 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibEchoLiveMsgAPIConvert.cpp
More file actions
340 lines (313 loc) · 11 KB
/
libEchoLiveMsgAPIConvert.cpp
File metadata and controls
340 lines (313 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* @file libEchoLiveMsgAPIConvert.cpp
* @brief Echo-Live消息API处理函数
* @details 处理Echo-Live API与字符串变量的转换
* @author 星羽筱夜<AuraElicase>
* @date 2024-09-25
* @version 1.0.0
* @copyright Copyright (c) 2024 AuraElicase
*/
#include <complex>
#include <iostream>
#include <string>
#include <vector>
#include "nlohmann/json.hpp"
#include "echoLiveWsAPIStruct.h"
#include "libEchoLiveMsgAPIConvert.h"
using json = nlohmann::json;
/**
* @brief 序列化结构体为Json
* @param jsonStr 需要生成的Json数据
* @param style 需要序列化json的结构体
*/
void marshalJson(json &jsonStr, const echoLiveAPIStyle &style)
{
try
{
json styleData;
if (style.style.bold) styleData["bold"] = true;
if (style.style.italic) styleData["italic"] = true;
if (style.style.underline) styleData["underline"] = true;
if (style.style.strikethrough) styleData["strikethrough"] = true;
if (style.style.emphasis) styleData["emphasis"] = true;
if (!style.style.color.empty()) styleData["color"] = style.style.color;
if (!style.style.backgroundColor.empty()) styleData["backgroundColor"] = style.style.backgroundColor;
if (!style.style.size.empty()) styleData["size"] = style.style.size;
if (!style.style.weight.empty()) styleData["weight"] = style.style.weight;
if (!style.style.letterSpacing.empty()) styleData["letterSpacing"] = style.style.letterSpacing;
if (!style.style.stretch.empty()) styleData["stretch"] = style.style.stretch;
if (!style.style.css.empty()) styleData["style"] = style.style.css;
jsonStr["text"] = style.text;
if (!styleData.empty()) jsonStr["style"] = styleData;
if (style.data.has_value())
{
json externalData;
externalData["image"]["url"] = style.data->image.url;
jsonStr["data"] = externalData;
}
} catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
/**
* @brief 分割普通字符串与格式字符串
* @param text 需要分割的原始文本
* @param marker 格式字符串匹配前缀
* @param markerEnd 格式字符串匹配后缀
* @return 拆分后的普通字符串与格式字符串数组
*/
std::vector<std::string> splitOriginString(const std::string& text, const std::string& marker, const std::string& markerEnd)
{
std::vector<std::string> results;
size_t start = 0;
while (true) {
// 查找下一个标记的开始位置
const size_t markerPos = text.find(marker, start);
if (markerPos == std::string::npos) {
// 如果没有找到标记,输出剩余部分
results.push_back(text.substr(start));
break;
}
// 输出标记之前的部分
if (markerPos > start) {
results.push_back(text.substr(start, markerPos - start));
}
// 找到标记的结束位置
const size_t endPos = text.find(markerEnd, markerPos);
if (endPos == std::string::npos) {
// 如果没有找到结束标记,输出剩余部分并退出
results.push_back(text.substr(markerPos));
break;
}
// 输出匹配到的部分
results.push_back(text.substr(markerPos, endPos - markerPos + 1));
// 更新开始位置
start = endPos + 1;
}
return results;
}
/**
* @brief 拆分格式字符串
* @param text 需要拆分的格式字符串
* @param split 分隔符
* @return 拆分后的模板内容数组
*/
std::vector<std::string> splitTemplateString(const std::string& text, const char split)
{
std::vector<std::string> results;
try
{
if (text.empty())
{
throw std::invalid_argument("empty string");
}
std::string text2 = text + split;
size_t pos = text2.find(split);
while (pos != std::string::npos)
{
std::string tmp = text2.substr(0, pos);
results.push_back(tmp);
text2 = text2.substr(pos + 1, text2.length());
pos = text2.find(split);
}
} catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return results;
}
/**
* @brief 分析模板字符串
* @param text 原始模板字符串
* @param marker 模板字符串匹配前缀
* @return 拆分后的模板内容数组
*/
std::vector<std::string> analysisTemplateString(const std::string& text, const std::string& marker)
{
if (const size_t markerPos = text.find(marker, 0); markerPos == std::string::npos) {
throw std::invalid_argument("no string type template");
}
// substr拆除非必要内容
const std::string validString = text.substr(marker.size(), text.size() - 1 - marker.size());
std::vector<std::string> results = splitTemplateString(validString, ',');
return results;
}
/**
* @brief 格式化消息结构体
* @param originText 需要格式化的文本
* @param args 格式化参数数组
* @param marker 格式化参数匹配前缀
* @return 格式化后的结构体
*/
echoLiveAPIStyle generateMsgJsonStruct(const std::string& originText, const std::vector<std::string>& args, const std::string& marker)
{
echoLiveAPIStyle style;
style.text = originText;
for (const auto& arg : args)
{
std::vector<std::string> styleList = analysisTemplateString(arg, marker);
if (styleList[0] == "Format")
{
if (styleList[1] == "Boldface")
{
style.style.bold = true;
} else if (styleList[1] == "Italics")
{
style.style.italic = true;
} else if (styleList[1] == "Underline")
{
style.style.underline = true;
} else if (styleList[1] == "Strikethrough")
{
style.style.strikethrough = true;
} else if (styleList[1] == "Emphasis")
{
style.style.emphasis = true;
} else if (styleList[1] == "Custom")
{
style.style.css = styleList[2];
} else if (styleList[1] == "Reset")
{
style.style.bold = false;
style.style.italic = false;
style.style.underline = false;
style.style.strikethrough = false;
style.style.emphasis = false;
style.style.css = "";
style.style.color = "";
style.style.backgroundColor = "";
style.style.size = "";
style.style.weight = "";
style.style.letterSpacing = "";
style.style.stretch = "";
style.data = std::nullopt;
} else
{
throw std::invalid_argument("invalid argument");
}
} else if (styleList[0] == "TypeSize")
{
style.style.size = styleList[1];
} else if (styleList[0] == "TypeWeight")
{
style.style.weight = styleList[1];
} else if (styleList[0] == "FontColor")
{
if (styleList[1] == "hex")
{
style.style.color = styleList[2];
} else if (styleList[1] == "rgba")
{
std::string rgbaColor = "rgba(" + styleList[2] + "," + styleList[3] + "," + styleList[4] + ")";
style.style.color = rgbaColor;
} else
{
throw std::invalid_argument("invalid color type");
}
} else if (styleList[0] == "BgColor")
{
if (styleList[1] == "hex")
{
style.style.backgroundColor = styleList[2];
} else if (styleList[1] == "rgba")
{
std::string rgbaColor = "rgba(" + styleList[2] + "," + styleList[3] + "," + styleList[4] + ")";
style.style.backgroundColor = rgbaColor;
} else
{
throw std::invalid_argument("invalid color type");
}
} else if (styleList[0] == "Image")
{
echoLiveExternalData eDataImage;
eDataImage.image.url = styleList[1];
// eDataImage.image.margin.left = "0em";
// eDataImage.image.margin.right = "0em";
// eDataImage.image.size.height.max = "5em";
// eDataImage.image.size.height.min = "0em";
// eDataImage.image.size.width.max = "5em";
// eDataImage.image.size.width.min = "0em";
style.data = eDataImage;
} else if (styleList[0] == "Emoji")
{
echoLiveExternalData eDataEmoji;
eDataEmoji.image.url = styleList[1];
// eDataEmoji.image.margin.left = "0em";
// eDataEmoji.image.margin.right = "0em";
// eDataEmoji.image.size.height.max = "5em";
// eDataEmoji.image.size.height.min = "0em";
// eDataEmoji.image.size.width.max = "5em";
// eDataEmoji.image.size.width.min = "0em";
style.data = eDataEmoji;
} else
{
throw std::invalid_argument("find unhandled expression:" + styleList[0]);
}
}
return style;
}
/**
* @brief 格式字符串列表处理Json
* @param msgList 初次拆分后的普通字符串与格式字符串列表
* @param msgsJson 需要返回的json
* @param marker 格式字符串匹配前缀
*/
void generateMsgList(const std::vector<std::string>& msgList, json &msgsJson, const std::string& marker)
{
msgsJson = {
{"message", json::array()}
};
std::vector<std::string> markerTempData;
// 循环读取拆分出来的数据
for (const auto& arg : msgList)
{
if (arg.find(marker) != std::string::npos)
{
markerTempData.push_back(arg);
}
else
{
json msgJson;
echoLiveAPIStyle styleData = generateMsgJsonStruct(arg, markerTempData, marker);
marshalJson(msgJson, styleData);
msgsJson["message"].push_back(msgJson);
}
}
}
/**
* @brief C++字符串格式化Json
* @param origin 需要处理的原始字符串
* @return 处理后的Json字符串
*/
std::string generateMsgs(const std::string& origin)
{
json a;
const std::string marker = "[CHAT:";
const std::string markerEnd = "]";
const std::vector<std::string> result = splitOriginString(origin, marker, markerEnd);
generateMsgList(result, a, marker);
return to_string(a);
}
extern "C" {
/**
* @brief C字符串格式化Json
* @param origin 需要处理的原始字符串
* @return 处理后的Json字符串
*/
__declspec(dllexport) const char *generateMsgsC(const char *origin)
{
const std::string result = generateMsgs(origin);
char* cstr = new char[result.length() + 1];
strcpy_s(cstr,strlen(result.c_str()) + 1, result.c_str());
return cstr;
}
/**
* @brief 消息内存回收
* @param msg 需要释放的消息地址
*/
__declspec(dllexport) void freeMsg(const char* msg)
{
delete[] msg; // 释放内存
}
}