mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
Move JSON temp var heper functions
This commit is contained in:
parent
7b0f985f18
commit
d670b6d07e
|
|
@ -257,38 +257,6 @@ void MacroConditionTwitch::ResetChatConnection()
|
|||
_chatConnection.reset();
|
||||
}
|
||||
|
||||
static void
|
||||
setTempVarsHelper(const std::string &jsonStr,
|
||||
std::function<void(const char *, const char *)> setVar)
|
||||
{
|
||||
try {
|
||||
auto json = nlohmann::json::parse(jsonStr);
|
||||
for (auto it = json.begin(); it != json.end(); ++it) {
|
||||
if (it->is_string()) {
|
||||
setVar(it.key().c_str(),
|
||||
it->get<std::string>().c_str());
|
||||
continue;
|
||||
}
|
||||
setVar(it.key().c_str(), it->dump().c_str());
|
||||
}
|
||||
} catch (const nlohmann::json::exception &e) {
|
||||
vblog(LOG_INFO, "%s", jsonStr.c_str());
|
||||
vblog(LOG_INFO, "%s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
setTempVarsHelper(obs_data_t *data,
|
||||
std::function<void(const char *, const char *)> setVar)
|
||||
{
|
||||
auto jsonStr = obs_data_get_json(data);
|
||||
if (!jsonStr) {
|
||||
return;
|
||||
}
|
||||
|
||||
setTempVarsHelper(jsonStr, setVar);
|
||||
}
|
||||
|
||||
bool MacroConditionTwitch::CheckChannelGenericEvents()
|
||||
{
|
||||
if (!_eventBuffer) {
|
||||
|
|
@ -304,10 +272,10 @@ bool MacroConditionTwitch::CheckChannelGenericEvents()
|
|||
continue;
|
||||
}
|
||||
SetVariableValue(event->ToString());
|
||||
setTempVarsHelper(event->data,
|
||||
[this](const char *id, const char *value) {
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
SetJsonTempVars(event->data,
|
||||
[this](const char *id, const char *value) {
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
|
||||
if (_clearBufferOnMatch) {
|
||||
_eventBuffer->Clear();
|
||||
|
|
@ -345,10 +313,10 @@ bool MacroConditionTwitch::CheckChannelLiveEvents()
|
|||
}
|
||||
|
||||
SetVariableValue(event->ToString());
|
||||
setTempVarsHelper(event->data,
|
||||
[this](const char *id, const char *value) {
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
SetJsonTempVars(event->data,
|
||||
[this](const char *id, const char *value) {
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
|
||||
if (_clearBufferOnMatch) {
|
||||
_eventBuffer->Clear();
|
||||
|
|
@ -374,14 +342,14 @@ bool MacroConditionTwitch::CheckChannelRewardRedemptionEvents()
|
|||
continue;
|
||||
}
|
||||
SetVariableValue(event->ToString());
|
||||
setTempVarsHelper(event->data,
|
||||
[this](const char *id, const char *value) {
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
SetJsonTempVars(event->data,
|
||||
[this](const char *id, const char *value) {
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
OBSDataAutoRelease rewardInfo =
|
||||
obs_data_get_obj(event->data, "reward");
|
||||
setTempVarsHelper(rewardInfo, [this](const char *nestedId,
|
||||
const char *value) {
|
||||
SetJsonTempVars(rewardInfo, [this](const char *nestedId,
|
||||
const char *value) {
|
||||
std::string id = "reward." + std::string(nestedId);
|
||||
SetTempVarValue(id, value);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "token.hpp"
|
||||
|
||||
#include <log-helper.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace advss {
|
||||
|
||||
|
|
@ -330,4 +331,34 @@ RequestResult SendDeleteRequest(const TwitchToken &token,
|
|||
return processResult(response, __func__);
|
||||
}
|
||||
|
||||
void SetJsonTempVars(const std::string &jsonStr,
|
||||
std::function<void(const char *, const char *)> setVarFunc)
|
||||
{
|
||||
try {
|
||||
auto json = nlohmann::json::parse(jsonStr);
|
||||
for (auto it = json.begin(); it != json.end(); ++it) {
|
||||
if (it->is_string()) {
|
||||
setVarFunc(it.key().c_str(),
|
||||
it->get<std::string>().c_str());
|
||||
continue;
|
||||
}
|
||||
setVarFunc(it.key().c_str(), it->dump().c_str());
|
||||
}
|
||||
} catch (const nlohmann::json::exception &e) {
|
||||
vblog(LOG_INFO, "%s", jsonStr.c_str());
|
||||
vblog(LOG_INFO, "%s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void SetJsonTempVars(obs_data_t *data,
|
||||
std::function<void(const char *, const char *)> setVarFunc)
|
||||
{
|
||||
auto jsonStr = obs_data_get_json(data);
|
||||
if (!jsonStr) {
|
||||
return;
|
||||
}
|
||||
|
||||
SetJsonTempVars(jsonStr, setVarFunc);
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
|
|
|
|||
|
|
@ -53,4 +53,10 @@ RequestResult SendPatchRequest(const TwitchToken &token, const std::string &uri,
|
|||
const httplib::Params ¶ms,
|
||||
const OBSData &data, bool useCache);
|
||||
|
||||
// Helper functions to set temp var values
|
||||
void SetJsonTempVars(const std::string &jsonStr,
|
||||
std::function<void(const char *, const char *)> setVarFunc);
|
||||
void SetJsonTempVars(obs_data_t *data,
|
||||
std::function<void(const char *, const char *)> setVarFunc);
|
||||
|
||||
} // namespace advss
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user