Switch to RegexConfig

This commit is contained in:
WarmUpTill
2022-09-11 00:35:44 +02:00
committed by WarmUpTill
parent a243e026c8
commit 7ddfdcf5be
13 changed files with 152 additions and 119 deletions

View File

@@ -12,21 +12,22 @@ bool MacroConditionWebsocket::_registered = MacroConditionFactory::Register(
{MacroConditionWebsocket::Create, MacroConditionWebsocketEdit::Create,
"AdvSceneSwitcher.condition.websocket"});
static bool matchRegex(const std::string &msg, const std::string &expr)
static bool matchRegex(const RegexConfig &conf, const std::string &msg,
const std::string &expr)
{
try {
std::regex regExpr(expr);
return std::regex_match(msg, regExpr);
} catch (const std::regex_error &) {
auto regex = conf.GetRegularExpression(expr);
if (!regex.isValid()) {
return false;
}
return false;
auto match = regex.match(QString::fromStdString(msg));
return match.hasMatch();
}
bool MacroConditionWebsocket::CheckCondition()
{
for (const auto &msg : switcher->websocketMessages) {
if (_useRegex) {
if (matchRegex(msg, _message)) {
if (_regex.Enabled()) {
if (matchRegex(_regex, msg, _message)) {
return true;
}
} else {
@@ -42,7 +43,7 @@ bool MacroConditionWebsocket::Save(obs_data_t *obj)
{
MacroCondition::Save(obj);
obs_data_set_string(obj, "message", _message.c_str());
obs_data_set_bool(obj, "useRegex", _useRegex);
_regex.Save(obj);
return true;
}
@@ -50,7 +51,12 @@ bool MacroConditionWebsocket::Load(obs_data_t *obj)
{
MacroCondition::Load(obj);
_message = obs_data_get_string(obj, "message");
_useRegex = obs_data_get_bool(obj, "useRegex");
_regex.Load(obj);
// TOOD: remove in future version
if (obs_data_has_user_value(obj, "useRegex")) {
_regex.CreateBackwardsCompatibleRegex(
obs_data_get_bool(obj, "useRegex"), false);
}
return true;
}
@@ -63,20 +69,19 @@ MacroConditionWebsocketEdit::MacroConditionWebsocketEdit(
QWidget *parent, std::shared_ptr<MacroConditionWebsocket> entryData)
: QWidget(parent),
_message(new ResizingPlainTextEdit(this)),
_useRegex(new QCheckBox(obs_module_text(
"AdvSceneSwitcher.condition.websocket.useRegex")))
_regex(new RegexConfigWidget(parent))
{
QWidget::connect(_message, SIGNAL(textChanged()), this,
SLOT(MessageChanged()));
QWidget::connect(_useRegex, SIGNAL(stateChanged(int)), this,
SLOT(UseRegexChanged(int)));
QWidget::connect(_regex, SIGNAL(RegexConfigChanged(RegexConfig)), this,
SLOT(RegexChanged(RegexConfig)));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(new QLabel(
obs_module_text("AdvSceneSwitcher.condition.websocket.entry")));
mainLayout->addWidget(_message);
mainLayout->addWidget(_useRegex);
mainLayout->addWidget(_regex);
setLayout(mainLayout);
_entryData = entryData;
@@ -91,7 +96,7 @@ void MacroConditionWebsocketEdit::UpdateEntryData()
}
_message->setPlainText(QString::fromStdString(_entryData->_message));
_useRegex->setChecked(_entryData->_useRegex);
_regex->SetRegexConfig(_entryData->_regex);
adjustSize();
updateGeometry();
@@ -110,12 +115,15 @@ void MacroConditionWebsocketEdit::MessageChanged()
updateGeometry();
}
void MacroConditionWebsocketEdit::UseRegexChanged(int state)
void MacroConditionWebsocketEdit::RegexChanged(RegexConfig conf)
{
if (_loading || !_entryData) {
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
_entryData->_useRegex = state;
_entryData->_regex = conf;
adjustSize();
updateGeometry();
}