Commit dabf2e3b authored by Alynel's avatar Alynel

Add config option to control if the "Connected" message is sent or not

Improved handling of colour codes
Import existing .json configuration on first run if .yaml config doesn't exist
parent 6246be24
......@@ -8,7 +8,7 @@
struct ChannelConfig
{
ChannelConfig() { }
ChannelConfig() : allow_commands(false), send_connected(true), show_command_response(2000) { }
std::string name;
std::string id;
......@@ -16,6 +16,7 @@ struct ChannelConfig
std::vector<std::string> blocked;
std::vector<std::string> notify;
std::string prefix;
bool send_connected;
bool allow_commands;
uint32_t show_command_response;
};
......@@ -129,6 +130,7 @@ namespace YAML {
node["blocked"] = rhs.blocked;
node["notify"] = rhs.notify;
node["prefix"] = rhs.prefix;
node["send_connected"] = rhs.send_connected;
node["allow_commands"] = rhs.allow_commands;
node["show_command_response"] = rhs.show_command_response;
return node;
......@@ -146,6 +148,8 @@ namespace YAML {
rhs.notify = node["notify"].as<std::vector<std::string>>();
if (node["prefix"])
rhs.prefix = node["prefix"].as<std::string>();
if (node["send_connected"])
rhs.send_connected = node["send_connected"].as<bool>();
if (node["allow_commands"])
rhs.allow_commands = node["allow_commands"].as<bool>();
if (node["show_command_response"])
......
......@@ -52,7 +52,10 @@ namespace MQ2Discord
// Create background thread, this starts it too
_thread = std::thread{ &DiscordClient::threadStart, this };
enqueueAll("Connected");
for (const auto &channel : _channels)
if (channel.send_connected)
enqueue(channel.id, "Connected");
}
~DiscordClient()
......@@ -77,10 +80,10 @@ namespace MQ2Discord
_filterMatches.clear();
for (auto channelIt = _channels.begin(); channelIt != _channels.end(); ++channelIt)
_filterMatches[&(*channelIt)] = FilterMatch::None;
// Feed the message through the parser
;
// Feed the message through the parser. Colour codes are removed beforehand.
char buffer[2048] = { 0 };
strcpy_s(buffer, message.c_str());
strcpy_s(buffer, std::regex_replace(message, std::regex("\a\\-?."), "").c_str());
_blech.Feed(buffer);
// Send to any channels that matched
......@@ -393,9 +396,12 @@ namespace MQ2Discord
o << "\\";
o << c;
}
// Clean up colour codes
// Clean up colour codes. First try to put anything between a colour code and a cancel in backticks e.g. \awStuff\ax -> `Stuff`
auto result = std::regex_replace(o.str(), std::regex("\a([^\\-x]|\\-[^x])([^\a]+)\ax"), "`$2`");
// Then remove any codes left over
result = std::regex_replace(result, std::regex("\a."), "");
// Then make sure there's no consecutive backticks as it makes things look funny
result = std::regex_replace(result, std::regex("``"), "` `");
return result;
}
......
......@@ -13,6 +13,7 @@
#pragma pack(pop)
PreSetup("MQ2Discord");
PLUGIN_VERSION(1.0);
// MQ2Main isn't nice enough to export this
unsigned int __stdcall MQ2DataVariableLookup(char * VarName, char * Value, size_t ValueLen)
......@@ -214,47 +215,93 @@ void SetDefaults(DiscordConfig& config)
config.all.emplace_back(tellsChannel);
}
void Reload()
void WriteConfig(const DiscordConfig& config, const std::string& filename)
{
if (client)
client.reset();
DiscordConfig config;
YAML::Node node;
node = config;
std::ofstream fout(filename);
fout << node;
}
DiscordConfig GetConfig()
{
// Load existing config if it exists
std::string configFile = std::string(gszINIPath) + "\\MQ2Discord.yaml";
const auto configFile = std::string(gszINIPath) + "\\MQ2Discord.yaml";
if (std::experimental::filesystem::exists(configFile))
return YAML::LoadFile(configFile).as<DiscordConfig>();
// If old .json configs exist, convert them
std::map<std::string, YAML::Node> jsonConfigs;
for (const auto & file : std::experimental::filesystem::directory_iterator(gszINIPath))
{
try
{
config = YAML::LoadFile(std::string(gszINIPath) + "\\MQ2Discord.yaml").as<DiscordConfig>();
}
catch (std::exception& e)
{
OutputError("Failed to load config, %s", e.what());
return;
}
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());
}
else
if (!jsonConfigs.empty())
{
// Otherwise, create a default config
try
auto imported = false;
DiscordConfig config;
for (const auto & kvp : jsonConfigs)
{
SetDefaults(config);
YAML::Node node;
node = config;
{
std::ofstream fout(configFile);
fout << node;
}
OutputNormal("Created a default configuration. Edit this, then do \ag/discord reload");
return;
// Skip if there's no channel id
if (kvp.second["channel"].as<std::string>() == "")
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());
}
catch (std::exception& e)
if (imported)
{
OutputError("Failed to create default config, %s", e.what());
WriteConfig(config, configFile);
return config;
}
}
// Otherwise, create a default config
DiscordConfig config;
SetDefaults(config);
WriteConfig(config, configFile);
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;
}
......
MQ2DotNet @ 9ecf80df
Subproject commit 9ecf80dfca534055836ad05b8629c83ae62e627e
......@@ -97,7 +97,7 @@ extern CRITICAL_SECTION gPluginCS;
//we default to LIVE though...
#else
//define LIVE, TEST, EQBETA, ROF2EMU or UFEMU here depending on which eqgame you are building for. -eqmule sep 27 2014
#define LIVE
#define TEST
#endif
#if defined(LIVE)
#include "eqgame.h"
......
......@@ -19,7 +19,7 @@ DETOUR_TRAMPOLINE_EMPTY(VOID __cdecl Call_T(PSPAWNINFO pChar, PCHAR szLine));
DETOUR_TRAMPOLINE_EMPTY(VOID __cdecl Return_T(PSPAWNINFO pChar, PCHAR szLine));
DETOUR_TRAMPOLINE_EMPTY(VOID __cdecl EndMacro_T(PSPAWNINFO pChar, PCHAR szLine));
DETOUR_TRAMPOLINE_EMPTY(VOID __cdecl DoEvents_T(PSPAWNINFO pChar, PCHAR szLine));
DETOUR_TRAMPOLINE_EMPTY(BOOL __cdecl DoNextCommand_T(PMACROBLOCK pMacroBlock));
DETOUR_TRAMPOLINE_EMPTY(BOOL __cdecl DoNextCommand_T(PMACROBLOCK pMacroBlock) noexcept);
VOID __cdecl Call_D(PSPAWNINFO pChar, PCHAR szLine);
VOID __cdecl Return_D(PSPAWNINFO pChar, PCHAR szLine);
......@@ -399,7 +399,9 @@ VOID __cdecl DoEvents_D(PSPAWNINFO pChar, PCHAR szLine)
BOOL __cdecl DoNextCommand_D(PMACROBLOCK pMacroBlock)
{
if (DoNextCommand_T(pMacroBlock))
BOOL (* __cdecl trampoline)(PMACROBLOCK) = DoNextCommand_T;
if (trampoline(pMacroBlock))
{
commandCount++;
return TRUE;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment