mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-06-21 20:12:00 -05:00
Add Matches() helper function
This commit is contained in:
parent
2defa06ba4
commit
2cc8a15118
|
|
@ -52,12 +52,7 @@ bool MacroConditionFile::MatchFileContent(QString &filedata)
|
|||
}
|
||||
|
||||
if (_regex.Enabled()) {
|
||||
auto expr = _regex.GetRegularExpression(_text);
|
||||
if (!expr.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = expr.match(filedata);
|
||||
return match.hasMatch();
|
||||
return _regex.Matches(filedata, QString::fromStdString(_text));
|
||||
}
|
||||
|
||||
QString text = QString::fromStdString(_text);
|
||||
|
|
|
|||
|
|
@ -68,12 +68,7 @@ bool MacroConditionTempVar::Compare(const TempVariable &var) const
|
|||
}
|
||||
|
||||
if (_regex.Enabled()) {
|
||||
auto expr = _regex.GetRegularExpression(_strValue);
|
||||
if (!expr.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = expr.match(QString::fromStdString(*value));
|
||||
return match.hasMatch();
|
||||
return _regex.Matches(*value, _strValue);
|
||||
}
|
||||
|
||||
return std::string(_strValue) == *value;
|
||||
|
|
|
|||
|
|
@ -52,14 +52,8 @@ static bool compareNumber(const Variable &var, double value, bool less)
|
|||
bool MacroConditionVariable::Compare(const Variable &var) const
|
||||
{
|
||||
if (_regex.Enabled()) {
|
||||
auto expr = _regex.GetRegularExpression(_strValue);
|
||||
if (!expr.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = expr.match(QString::fromStdString(var.Value()));
|
||||
return match.hasMatch();
|
||||
return _regex.Matches(var.Value(), _strValue);
|
||||
}
|
||||
|
||||
return std::string(_strValue) == var.Value();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,17 +20,6 @@ const static std::map<MacroConditionWebsocket::Type, std::string>
|
|||
"AdvSceneSwitcher.condition.websocket.type.event"},
|
||||
};
|
||||
|
||||
static bool matchRegex(const RegexConfig &conf, const std::string &msg,
|
||||
const std::string &expr)
|
||||
{
|
||||
auto regex = conf.GetRegularExpression(expr);
|
||||
if (!regex.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = regex.match(QString::fromStdString(msg));
|
||||
return match.hasMatch();
|
||||
}
|
||||
|
||||
bool MacroConditionWebsocket::CheckCondition()
|
||||
{
|
||||
std::vector<std::string> messages;
|
||||
|
|
@ -52,7 +41,7 @@ bool MacroConditionWebsocket::CheckCondition()
|
|||
|
||||
for (const auto &msg : messages) {
|
||||
if (_regex.Enabled()) {
|
||||
if (matchRegex(_regex, msg, _message)) {
|
||||
if (_regex.Matches(msg, _message)) {
|
||||
SetVariableValue(msg);
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,28 +14,17 @@ bool MacroConditionWindow::_registered = MacroConditionFactory::Register(
|
|||
{MacroConditionWindow::Create, MacroConditionWindowEdit::Create,
|
||||
"AdvSceneSwitcher.condition.window"});
|
||||
|
||||
static bool matchRegex(const RegexConfig &conf, const std::string &msg,
|
||||
const std::string &expr)
|
||||
{
|
||||
auto regex = conf.GetRegularExpression(expr);
|
||||
if (!regex.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = regex.match(QString::fromStdString(msg));
|
||||
return match.hasMatch();
|
||||
}
|
||||
|
||||
static bool windowContainsText(const std::string &window,
|
||||
const std::string &matchText,
|
||||
const RegexConfig &conf)
|
||||
const RegexConfig ®ex)
|
||||
{
|
||||
auto text = GetTextInWindow(window);
|
||||
if (!text.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conf.Enabled()) {
|
||||
return matchRegex(conf, *text, matchText);
|
||||
if (regex.Enabled()) {
|
||||
return regex.Matches(*text, matchText);
|
||||
}
|
||||
return text == matchText;
|
||||
}
|
||||
|
|
@ -84,7 +73,7 @@ bool MacroConditionWindow::WindowRegexMatches(
|
|||
// enabled in the backend and use the regular expression ".*".
|
||||
|
||||
for (const auto &window : windowList) {
|
||||
if (matchRegex(_windowRegex, window, _window) &&
|
||||
if (_windowRegex.Matches(window, _window) &&
|
||||
WindowMatchesRequirements(window)) {
|
||||
SetVariableValueBasedOnMatch(window);
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -339,19 +339,14 @@ bool MacroConditionTwitch::CheckChannelLiveEvents(TwitchToken &token)
|
|||
return false;
|
||||
}
|
||||
|
||||
static bool stringMatches(const RegexConfig &conf, const std::string &string,
|
||||
static bool stringMatches(const RegexConfig ®ex, const std::string &string,
|
||||
const std::string &expr)
|
||||
{
|
||||
if (!conf.Enabled()) {
|
||||
if (!regex.Enabled()) {
|
||||
return string == expr;
|
||||
}
|
||||
|
||||
auto regex = conf.GetRegularExpression(expr);
|
||||
if (!regex.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = regex.match(QString::fromStdString(string));
|
||||
return match.hasMatch();
|
||||
return regex.Matches(string, expr);
|
||||
}
|
||||
|
||||
bool MacroConditionTwitch::CheckChatMessages(TwitchToken &token)
|
||||
|
|
|
|||
|
|
@ -341,13 +341,7 @@ bool MacroConditionVideo::CheckOCR()
|
|||
if (!_ocrParameters.regex.Enabled()) {
|
||||
return text == std::string(_ocrParameters.text);
|
||||
}
|
||||
auto expr =
|
||||
_ocrParameters.regex.GetRegularExpression(_ocrParameters.text);
|
||||
if (!expr.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = expr.match(QString::fromStdString(text));
|
||||
return match.hasMatch();
|
||||
return _ocrParameters.regex.Matches(text, _ocrParameters.text);
|
||||
}
|
||||
|
||||
bool MacroConditionVideo::CheckColor()
|
||||
|
|
|
|||
|
|
@ -51,6 +51,23 @@ RegexConfig::GetRegularExpression(const std::string &expr) const
|
|||
return GetRegularExpression(QString::fromStdString(expr));
|
||||
}
|
||||
|
||||
bool RegexConfig::Matches(const QString &text, const QString &expression) const
|
||||
{
|
||||
auto regex = GetRegularExpression(expression);
|
||||
if (!regex.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = regex.match(text);
|
||||
return match.hasMatch();
|
||||
}
|
||||
|
||||
bool RegexConfig::Matches(const std::string &text,
|
||||
const std::string &expression) const
|
||||
{
|
||||
return Matches(QString::fromStdString(text),
|
||||
QString::fromStdString(expression));
|
||||
}
|
||||
|
||||
RegexConfig RegexConfig::PartialMatchRegexConfig()
|
||||
{
|
||||
RegexConfig conf;
|
||||
|
|
|
|||
|
|
@ -29,6 +29,9 @@ public:
|
|||
};
|
||||
QRegularExpression GetRegularExpression(const QString &) const;
|
||||
QRegularExpression GetRegularExpression(const std::string &) const;
|
||||
bool Matches(const QString &text, const QString &expression) const;
|
||||
bool Matches(const std::string &text,
|
||||
const std::string &expression) const;
|
||||
|
||||
static RegexConfig PartialMatchRegexConfig();
|
||||
|
||||
|
|
|
|||
|
|
@ -62,27 +62,16 @@ static std::vector<OBSSceneItem> getSceneItemsWithName(obs_scene_t *scene,
|
|||
|
||||
struct NamePatternMatchData {
|
||||
std::string pattern;
|
||||
const RegexConfig &conf;
|
||||
const RegexConfig ®ex;
|
||||
std::vector<OBSSceneItem> items = {};
|
||||
};
|
||||
|
||||
static bool matchesRegex(const RegexConfig &conf, const std::string &msg,
|
||||
const std::string &expr)
|
||||
{
|
||||
auto regex = conf.GetRegularExpression(expr);
|
||||
if (!regex.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = regex.match(QString::fromStdString(msg));
|
||||
return match.hasMatch();
|
||||
}
|
||||
|
||||
static bool getSceneItemsByPatternHelper(obs_scene_t *, obs_sceneitem_t *item,
|
||||
void *ptr)
|
||||
{
|
||||
auto data = reinterpret_cast<NamePatternMatchData *>(ptr);
|
||||
auto sourceName = obs_source_get_name(obs_sceneitem_get_source(item));
|
||||
if (matchesRegex(data->conf, sourceName, data->pattern)) {
|
||||
if (data->regex.Matches(sourceName, data->pattern)) {
|
||||
data->items.emplace_back(item);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -413,12 +413,7 @@ bool MatchJson(const std::string &json1, const std::string &json2,
|
|||
}
|
||||
|
||||
if (regex.Enabled()) {
|
||||
auto expr = regex.GetRegularExpression(j2);
|
||||
if (!expr.isValid()) {
|
||||
return false;
|
||||
}
|
||||
auto match = expr.match(QString::fromStdString(j1));
|
||||
return match.hasMatch();
|
||||
return regex.Matches(j1, j2);
|
||||
}
|
||||
return j1 == j2;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user