Cyrus Mod Loader
Loading...
Searching...
No Matches
ChatStyle.h
1#pragma once
2#include <Cyrus/Classes/ControlText.h>
3#include <Cyrus/Classes/Window.h>
4#include <string>
5#include <vector>
6
7std::vector<std::wstring> split(std::wstring s, wchar_t delimiter) {
8 std::vector<std::wstring> parts;
9 std::wstringstream wss(s);
10 std::wstring temp = L"";
11 while (std::getline(wss, temp, delimiter)) {
12 parts.push_back(temp);
13 }
14 return parts;
15}
16
17int countSubstring(const std::wstring &str, const std::wstring &sub) {
18 if (sub.length() == 0)
19 return 0;
20 int count = 0;
21 for (size_t offset = str.find(sub); offset != std::string::npos;
22 offset = str.find(sub, offset + sub.length())) {
23 ++count;
24 }
25 return count;
26}
27
29 ControlText *chatLog;
30
31 struct class_msg_t {
32 bool valid;
33 struct msg_t {
34 std::wstring color = L"";
35 std::wstring message = L"";
36 } msg;
37
38 struct player_t {
39 std::wstring GID = L"";
40 std::wstring name = L"";
41 std::wstring level = L"";
42 std::wstring link = L"";
43 } player;
44
45 struct img_t {
46 std::wstring path = L"";
47 std::wstring size_x = L"";
48 std::wstring size_y = L"";
49 std::wstring color = L"";
50 } img;
51
52 std::wstring serialize() {
53 std::wstring ret = L"";
54 ret += L"<color;" + msg.color + L"><image;" + img.path + L";" +
55 img.size_x + L";" + img.size_y + L";" + img.color + L">";
56 if (!player.GID.empty()) {
57 ret += L"<link;GID:" + player.GID + L"," + player.name + L"," +
58 player.level + L">" + player.link + L"</link>";
59 }
60 ret += msg.message + L"</color>";
61 return ret;
62 }
63 };
64
65 std::vector<class_msg_t> messages;
66
67public:
68 class_msg_t process_line(std::wstring line) {
69 class_msg_t ret = {};
70 if (line.size() == 0) {
71 ret.valid = false;
72 return ret;
73 }
74
75 auto splitted = split(line, L';');
76 if (splitted.size() < 6) {
77 ret.valid = false;
78 return ret;
79 }
80 ret.valid = true;
81 ret.msg.color = splitted[1].substr(0, splitted[1].find(L">"));
82 ret.img.size_x = splitted[3];
83 ret.img.size_y = splitted[4];
84 ret.img.color = splitted[5].substr(0, splitted[5].find(L">"));
85
86 const auto link = line.find(L"<link;");
87 if (link != std::wstring::npos) { // handle chat from another player
88 const auto gid_loc = line.find(L"GID:") + sizeof("GID");
89 const auto first_comma = line.find(L",", gid_loc);
90 ret.player.GID = line.substr(gid_loc, first_comma - gid_loc);
91
92 const auto second_comma = line.find(L",", first_comma + 1);
93 ret.player.name =
94 line.substr(first_comma + 1, second_comma - first_comma - 1);
95
96 auto tag_end = line.find(L">", second_comma + 1);
97 ret.player.level =
98 line.substr(second_comma + 1, tag_end - second_comma - 1);
99
100 tag_end++;
101 auto link_end = line.find(L"</link>", tag_end);
102 ret.player.link = line.substr(tag_end, link_end - tag_end);
103
104 link_end += sizeof("</link>");
105 const auto message = line.substr(
106 link_end - 1, line.find(L"</color>", link_end) - link_end + 1);
107 ret.msg.message = message;
108
109 ret.img.path = L"Art/Art_Chat_Say.dds";
110
111 return ret;
112 }
113
114 std::wstring type = L"none";
115 if (countSubstring(line, L"<image;") == 2) {
116 auto offset = line.find(L"<image;", line.find(L"<image;") + 1);
117 type = line.substr(offset + sizeof("<image"),
118 line.find(L">", offset) - sizeof("<image") - offset);
119 std::wstring::size_type i = line.find(L" <image;" + type + L">");
120 line.erase(i, sizeof("<image;>") + type.length());
121 }
122
123 if (type != L"none") {
124 ret.img.path = L"QuestPage/Art_Quest_" + type + L".dds";
125 } else {
126 ret.img.path = splitted[2];
127 }
128
129 splitted =
130 split(line, L';'); // update because we might have removed an image tag
131 const auto message =
132 splitted[5].substr(splitted[5].find(L">") + 1,
133 splitted[5].find(L"<") - splitted[5].find(L">") - 1);
134 if (ret.msg.color == L"AA00AA") // "You have received %d experience!"
135 {
136 ret.img.path = L"Art/Art_Quest_XP.dds";
137 } else if (ret.msg.color == L"00FFFF") { // received pet to pet tome
138 ret.img.path = L"QuestPage/Art_Quest_Pet.dds";
139 } else if (ret.msg.color == L"D9ABF8") {
140 ret.img.path = L"QuestPage/Art_Quest_Emote.dds";
141 } else if (message.find(L"You have earned") != std::wstring::npos &&
142 message.find(L"gold!") != std::wstring::npos) {
143 ret.msg.color = L"00CCFE";
144 ret.img.path = L"QuestPage/Art_Quest_Gold.dds";
145 }
146 ret.msg.message = message;
147 return ret;
148 }
149
150 chat_msg_parser_t(ControlText *chatLog) : chatLog(chatLog) {
151 const auto split_lines = split(chatLog->getText(), L'\n');
152 for (auto line : split_lines) {
153 auto ret = process_line(line);
154 if (ret.valid) {
155 messages.push_back(ret);
156 }
157 }
158 }
159
160 std::wstring serialize() {
161 std::wstring ret = L"";
162 for (auto i = 0; i < messages.size(); i++) {
163 ret += messages[i].serialize() + (i == messages.size() - 1 ? L"" : L"\n");
164 }
165 return ret;
166 }
167
168 void test() {
169 for (auto i = 0; i < messages.size(); i++) {
170 messages[i].msg.color = L"00CCFE";
171 }
172 }
173
174 void stylize(int padding = 0) {
175 const auto history = chatLog->getText();
176 if (history.size() > 0 && history.find(L"DBGM") == std::wstring::npos &&
177 history.find(L"DBGL") == std::wstring::npos &&
178 history.find(L"WARN") == std::wstring::npos &&
179 history.find(L"ERRO") == std::wstring::npos &&
180 history.find(L"STAT") == std::wstring::npos) {
181 chatLog->setText(serialize());
182 }
183 }
184};
A class that represents a text-based control element in a window.
Definition ControlText.h:22
void setText(const std::wstring &text)
Set the text of the control.
Definition ControlText.h:46
std::wstring getText() const
Get the text of the control.
Definition ControlText.h:39
Definition ChatStyle.h:28
void test()
Definition ChatStyle.h:168
void stylize(int padding=0)
Definition ChatStyle.h:174
std::wstring serialize()
Definition ChatStyle.h:160
class_msg_t process_line(std::wstring line)
Definition ChatStyle.h:68
chat_msg_parser_t(ControlText *chatLog)
Definition ChatStyle.h:150
Definition ChatStyle.h:45
std::wstring path
Definition ChatStyle.h:46
std::wstring size_y
Definition ChatStyle.h:48
std::wstring size_x
Definition ChatStyle.h:47
std::wstring color
Definition ChatStyle.h:49
Definition ChatStyle.h:33
std::wstring message
Definition ChatStyle.h:35
std::wstring color
Definition ChatStyle.h:34
std::wstring name
Definition ChatStyle.h:40
std::wstring level
Definition ChatStyle.h:41
std::wstring link
Definition ChatStyle.h:42
std::wstring GID
Definition ChatStyle.h:39