-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMQ2Discord.cpp
More file actions
474 lines (412 loc) · 12.7 KB
/
MQ2Discord.cpp
File metadata and controls
474 lines (412 loc) · 12.7 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#define NONEXISTENT_OPUS
#include "DiscordClient.h"
#include "Config.h"
#include <fstream>
#include <regex>
#include <yaml-cpp\yaml.h>
#include <filesystem>
#include <mq/Plugin.h>
PreSetup("MQ2Discord");
PLUGIN_VERSION(1.1);
// MQ2Main isn't nice enough to export this
unsigned int __stdcall MQ2DataVariableLookup(char* VarName, char* Value, size_t ValueLen)
{
strcpy_s(Value, ValueLen, VarName);
if (!pLocalPlayer)
return (uint32_t)strlen(Value);
return static_cast<unsigned int>(strlen(ParseMacroParameter(Value, ValueLen)));
}
VOID DiscordCmd(PSPAWNINFO pChar, PCHAR szLine);
void Reload();
std::unique_ptr<MQ2Discord::DiscordClient> client;
bool disabled = false;
bool debug = false;
std::queue<std::string> commands;
std::mutex commandsMutex;
std::queue<std::string> messages;
std::mutex messagesMutex;
DWORD mainThreadId;
void OutputMessage(const char * prepend, const char * format, va_list args)
{
char output[MAX_STRING];
strncpy_s(output, prepend, MAX_STRING - 1);
vsprintf_s(&output[strlen(output)], sizeof(output) - strlen(output) - 1, format, args);
// If we're on the main thread write direct, otherwise queue it
if (GetCurrentThreadId() == mainThreadId)
{
disabled = true;
WriteChatf(output);
disabled = false;
}
else
{
std::lock_guard<std::mutex> lock(messagesMutex);
messages.emplace(output);
}
}
void OutputDebug(const char * format, ...)
{
if (debug)
{
va_list args;
va_start(args, format);
OutputMessage("\ag[MQ2Discord] \amDebug: \aw ", format, args);
va_end(args);
}
}
void OutputError(const char * format, ...)
{
va_list args;
va_start(args, format);
OutputMessage("\ag[MQ2Discord] \arError: \aw ", format, args);
va_end(args);
}
void OutputWarning(const char * format, ...)
{
va_list args;
va_start(args, format);
OutputMessage("\ag[MQ2Discord] \ayWarning: \aw ", format, args);
va_end(args);
}
void OutputNormal(const char * format, ...)
{
va_list args;
va_start(args, format);
OutputMessage("\ag[MQ2Discord] \aw ", format, args);
va_end(args);
}
void ProcessMessage(std::string Message)
{
if (client && !disabled && GetGameState() == GAMESTATE_INGAME)
{
char myMessage[MAX_STRING] = { 0 };
strncpy_s(myMessage, Message.c_str(), MAX_STRING - 1);
// Should be okay to modify the message since it's a copy.
StripTextLinks(myMessage);
// Resize the string to match the first null terminator.
//Message.erase(std::find(Message.begin(), Message.end(), '\0'), Message.end());
client->enqueueIfMatch(myMessage);
}
}
std::string ParseMacroDataString(const std::string& input)
{
char buffer[MAX_STRING] = { 0 };
strncpy_s(buffer, input.c_str(), MAX_STRING - 1);
ParseMacroData(buffer, MAX_STRING);
return buffer;
}
void OnCommand(std::string command)
{
OutputDebug("OnCommand: %s", command.c_str());
std::lock_guard<std::mutex> _lock(commandsMutex);
commands.emplace(command);
}
void SetDefaults(DiscordConfig& config)
{
config.token = "YourTokenHere";
config.user_ids.emplace_back("YourUserIdHere");
// A channel for just this character that relays all echoes
ChannelConfig charChannel;
charChannel.name = "CharChannel (This field is ignored)";
charChannel.id = "YourCharacterChannelHere";
charChannel.prefix = "";
charChannel.allow_commands = true;
charChannel.show_command_response = 2000;
config.characters[ParseMacroDataString("${EverQuest.Server}_${Me.Name}")].push_back(charChannel);
// A channel for a group of characters that relays deaths
ChannelConfig groupChannel;
groupChannel.name = "Deaths";
groupChannel.id = "YourGroupChannelHere";
groupChannel.prefix = "[${Me.Name}]";
groupChannel.allowed.emplace_back("You have been slain by");
groupChannel.allow_commands = false;
groupChannel.show_command_response = 0;
GroupConfig group;
group.name = "YourGroup";
group.characters.emplace_back(ParseMacroDataString("${EverQuest.Server}_${Me.Name}"));
group.characters.emplace_back("server_OtherCharInGroup");
group.characters.emplace_back("server_OneMore");
group.channels.emplace_back(groupChannel);
config.groups.emplace_back(group);
// A channel for all characters that relays tells.
ChannelConfig tellsChannel;
tellsChannel.name = "TellsChannel";
tellsChannel.id = "YourChannelForTellsHere";
tellsChannel.prefix = "[${EverQuest.Server}_${Me.Name}]";
tellsChannel.allowed.emplace_back("tells you");
tellsChannel.blocked.emplace_back("|${Me.Pet.DisplayName}| tells you#*#");
tellsChannel.allow_commands = false;
tellsChannel.show_command_response = 0;
config.all.emplace_back(tellsChannel);
}
void WriteConfig(const DiscordConfig& config, const std::string& filename)
{
YAML::Node node;
node = config;
std::ofstream fout(filename);
fout << node;
}
DiscordConfig GetConfig()
{
// Load existing config if it exists
const std::filesystem::path configFile = std::filesystem::path(gPathConfig) / "MQ2Discord.yaml";
std::error_code ec_exists;
if (std::filesystem::exists(configFile, ec_exists))
return YAML::LoadFile(configFile.string()).as<DiscordConfig>();
// If old .json configs exist, convert them
std::map<std::string, YAML::Node> jsonConfigs;
for (const auto & file : std::filesystem::directory_iterator(gPathConfig))
{
const auto filename = file.path().filename().string();
const std::regex re("MQ2Discord_(.*)\\.json");
std::smatch matches;
if (std::regex_match(filename, matches, re))
jsonConfigs[matches[1]] = YAML::LoadFile(file.path().string());
}
if (!jsonConfigs.empty())
{
auto imported = false;
DiscordConfig config;
for (const auto & kvp : jsonConfigs)
{
// Skip if there's no channel id
if (kvp.second["channel"].as<std::string>().empty())
continue;
imported = true;
// Create a channel and add it to characters
ChannelConfig channel;
channel.name = kvp.first;
channel.id = kvp.second["channel"].as<std::string>();
channel.allow_commands = true;
channel.show_command_response = 2000;
for (const auto & filter : kvp.second["allow"].as<std::vector<std::string>>())
channel.allowed.push_back(filter);
for (const auto & filter : kvp.second["block"].as<std::vector<std::string>>())
channel.blocked.push_back(filter);
config.characters[kvp.first].push_back(channel);
// Set the token & add the user id to the allowed list
config.token = kvp.second["token"].as<std::string>();
if (std::find(config.user_ids.begin(), config.user_ids.end(), kvp.second["user"].as<std::string>()) != config.user_ids.end())
config.user_ids.push_back(kvp.second["user"].as<std::string>());
OutputNormal("Imported config for %s", kvp.first.c_str());
}
if (imported)
{
WriteConfig(config, configFile.string());
return config;
}
}
// Otherwise, create a default config
DiscordConfig config;
SetDefaults(config);
WriteConfig(config, configFile.string());
OutputNormal("Created a default configuration. Edit this, then do \ag/discord reload");
return config;
}
void Reload()
{
if (client)
client.reset();
DiscordConfig config;
try
{
config = GetConfig();
}
catch (std::exception& e)
{
OutputError("Failed to load config, %s", e.what());
return;
}
for (const auto& warning : config.warnings())
OutputWarning(warning.c_str());
auto errors = config.errors();
for (const auto& error : errors)
OutputError(error.c_str());
if (!errors.empty())
{
OutputNormal("Config not loaded due to errors, please fix them and \ag/discord reload");
return;
}
//const std::string server = EQADDR_SERVERNAME;
//const std::string server_character = server + "_" + GetCharInfo()->Name;
//const std::string classShortName = pEverQuest->GetClassThreeLetterCode(((PSPAWNINFO)pCharSpawn)->mActorClient.Class);
const std::string server = ParseMacroDataString("${EverQuest.Server}");
const std::string server_character = server + "_" + ParseMacroDataString("${Me.Name}");
const std::string classShortName = ParseMacroDataString("${Me.Class.ShortName}");
std::vector<ChannelConfig> channels;
// Character's own channels
if (config.characters.find(server_character) != config.characters.end())
channels.insert(channels.end(), config.characters[server_character].begin(), config.characters[server_character].end());
// Server channels
if (config.servers.find(GetServerShortName()) != config.servers.end())
channels.insert(channels.end(), config.servers[GetServerShortName()].begin(), config.servers[GetServerShortName()].end());
// Class channels
if (config.classes.find(classShortName) != config.classes.end())
channels.insert(channels.end(), config.classes[classShortName].begin(), config.classes[classShortName].end());
// Groups
for (const auto& group : config.groups)
if (std::find(group.characters.begin(), group.characters.end(), server_character) != group.characters.end())
channels.insert(channels.end(), group.channels.begin(), group.channels.end());
// Global
channels.insert(channels.end(), config.all.begin(), config.all.end());
if (channels.empty())
{
OutputWarning("No channels configured for this character");
return;
}
for (auto &channel : channels)
{
if (!channel.prefix.empty())
{
// Make prefixes end with a space if they don't already
if (channel.prefix.back() != ' ')
channel.prefix.append(" ");
channel.prefix = ParseMacroDataString(channel.prefix);
}
// FIXME: A lot of allocations happening here.
// Add #*# at the start/end of any filters that don't have it already
for (auto& filter : channel.allowed)
if (filter.substr(0, 3) != "#*#" && filter.substr(filter.length() - 3, 3) != "#*#")
filter = "#*#" + filter + "#*#";
for (auto& filter : channel.blocked)
if (filter.substr(0, 3) != "#*#" && filter.substr(filter.length() - 3, 3) != "#*#")
filter = "#*#" + filter + "#*#";
for (auto& filter : channel.notify)
if (filter.substr(0, 3) != "#*#" && filter.substr(filter.length() - 3, 3) != "#*#")
filter = "#*#" + filter + "#*#";
}
client = std::make_unique<MQ2Discord::DiscordClient>(config.token, config.user_ids, channels, OnCommand, ParseMacroDataString, OutputError, OutputWarning, OutputNormal, OutputDebug);
}
void DiscordCmd(PSPAWNINFO pChar, PCHAR szLine)
{
char buffer[MAX_STRING] = { 0 };
GetArg(buffer, szLine, 1);
if (!_stricmp(buffer, "reload"))
{
Reload();
}
else if (!_stricmp(buffer, "stop"))
{
client->Stop();
}
else if (!_stricmp(buffer, "debug"))
{
debug = !debug;
OutputNormal("Debug is now: %s\ax", debug ? "\agON" : "\arOFF");
}
else if (!_stricmp(buffer, "process"))
{
std::string strLine = szLine;
const std::string strBuffer = buffer;
if (strLine.length() > strBuffer.length())
{
strLine = strLine.substr((strBuffer).length() + 1);
OutputDebug("Processing: %s", strLine.c_str());
ProcessMessage(strLine);
}
else
{
OutputError("Process is used to process a specific line, you must enter something after the word 'process' ex: /discord process This is a test");
}
}
else
{
OutputWarning("Invalid command. Valid commands are process, reload, and for debugging: debug, stop.");
}
}
PLUGIN_API void InitializePlugin()
{
mainThreadId = GetCurrentThreadId();
AddCommand("/discord", DiscordCmd);
}
PLUGIN_API void ShutdownPlugin()
{
if (client)
{
client->Stop();
while(!client->IsStopped())
{
// Wait until client is stopped.
}
client.reset();
}
RemoveCommand("/discord");
}
PLUGIN_API void OnPulse()
{
// Execute any queued commands
while (true)
{
std::lock_guard<std::mutex> lock(commandsMutex);
if (commands.empty())
break;
OutputDebug("OnPulse: %s", commands.front().c_str());
EzCommand(commands.front().c_str());
commands.pop();
}
// Output any queued messages
while (true)
{
std::lock_guard<std::mutex> lock(messagesMutex);
if (messages.empty())
break;
disabled = true;
WriteChatf(messages.front().c_str());
disabled = false;
messages.pop();
}
}
PLUGIN_API void OnWriteChatColor(const char* Line, int Color, int Filter)
{
ProcessMessage(Line);
}
PLUGIN_API bool OnIncomingChat(const char* Line, DWORD Color)
{
ProcessMessage(Line);
return false;
}
PLUGIN_API void SetGameState(int GameState)
{
if (GameState == GAMESTATE_INGAME)
{
Reload();
}
else
{
if (client)
{
std::string message = "Disconnecting, no longer in game. GameState is ";
switch(GameState)
{
case GAMESTATE_PRECHARSELECT:
message += "PRECHARSELECT";
break;
case GAMESTATE_CHARSELECT:
message += "CHARSELECT";
break;
case GAMESTATE_CHARCREATE:
message += "CHARCREATE";
break;
case GAMESTATE_POSTCHARSELECT:
message += "POSTCHARSELECT";
break;
case GAMESTATE_SOMETHING:
message += "SOMETHING";
break;
case GAMESTATE_INGAME:
message += "INGAME";
break;
case GAMESTATE_LOGGINGIN:
message += "LOGGINGIN";
break;
case GAMESTATE_UNLOADING:
message += "UNLOADING";
break;
default:
message += std::to_string(GameState);
}
client->enqueueAll(message);
client.reset();
}
}
}