Enable tracking variable usage

This commit is contained in:
WarmUpTill
2026-10-19 18:09:14 +02:00
committed by WarmUpTill
parent 392aebff5c
commit 561f8224fd
30 changed files with 103 additions and 98 deletions

View File

@@ -546,10 +546,8 @@ bool MacroActionVariable::PerformAction()
bool MacroActionVariable::Save(obs_data_t *obj) const
{
MacroAction::Save(obj);
obs_data_set_string(obj, "variableName",
GetWeakVariableName(_variable).c_str());
obs_data_set_string(obj, "variable2Name",
GetWeakVariableName(_variable2).c_str());
_variable.Save(obj, "variableName");
_variable2.Save(obj, "variable2Name");
_strValue.Save(obj, "strValue");
_numValue.Save(obj, "numValue");
obs_data_set_int(obj, "condition", static_cast<int>(_action));
@@ -595,10 +593,8 @@ bool MacroActionVariable::Save(obs_data_t *obj) const
bool MacroActionVariable::Load(obs_data_t *obj)
{
MacroAction::Load(obj);
_variable =
GetWeakVariableByName(obs_data_get_string(obj, "variableName"));
_variable2 = GetWeakVariableByName(
obs_data_get_string(obj, "variable2Name"));
_variable.Load(obj, "variableName");
_variable2.Load(obj, "variable2Name");
_strValue.Load(obj, "strValue");
_action = static_cast<Action>(obs_data_get_int(obj, "condition"));
_segmentIdxLoadValue = obs_data_get_int(obj, "segmentIdx");

View File

@@ -9,6 +9,7 @@
#include "string-list.hpp"
#include "value-input.hpp"
#include "variable-line-edit.hpp"
#include "variable-ref.hpp"
#include "variable-text-edit.hpp"
#include "variable-spinbox.hpp"
@@ -63,8 +64,8 @@ public:
};
Action _action = Action::SET_VALUE;
std::weak_ptr<Variable> _variable;
std::weak_ptr<Variable> _variable2;
VariableRef _variable;
VariableRef _variable2;
StringVariable _strValue = "";
DoubleVariable _numValue = 0;
IntVariable _subStringStart = 0;

View File

@@ -164,8 +164,7 @@ bool MacroConditionTempVar::Save(obs_data_t *obj) const
{
MacroCondition::Save(obj);
_tempVar.Save(obj, GetMacro());
obs_data_set_string(obj, "variableName",
GetWeakVariableName(_variable2).c_str());
_variable2.Save(obj, "variableName");
_strValue.Save(obj, "strValue");
obs_data_set_double(obj, "numValue", _numValue);
obs_data_set_int(obj, "condition", static_cast<int>(_type));
@@ -177,8 +176,7 @@ bool MacroConditionTempVar::Load(obs_data_t *obj)
{
MacroCondition::Load(obj);
_tempVar.Load(obj, GetMacro());
_variable2 =
GetWeakVariableByName(obs_data_get_string(obj, "variableName"));
_variable2.Load(obj, "variableName");
_strValue.Load(obj, "strValue");
_numValue = obs_data_get_double(obj, "numValue");
_type = static_cast<Condition>(obs_data_get_int(obj, "condition"));

View File

@@ -1,6 +1,7 @@
#pragma once
#include "macro-condition-edit.hpp"
#include "resizing-text-edit.hpp"
#include "variable-ref.hpp"
#include "variable.hpp"
#include "regex-config.hpp"
#include "variable-text-edit.hpp"
@@ -40,7 +41,7 @@ public:
Condition _type = Condition::EQUALS;
TempVariableRef _tempVar;
std::weak_ptr<Variable> _variable2;
VariableRef _variable2;
StringVariable _strValue = "";
double _numValue = 0;
RegexConfig _regex;

View File

@@ -129,10 +129,8 @@ bool MacroConditionVariable::CheckCondition()
bool MacroConditionVariable::Save(obs_data_t *obj) const
{
MacroCondition::Save(obj);
obs_data_set_string(obj, "variableName",
GetWeakVariableName(_variable).c_str());
obs_data_set_string(obj, "variable2Name",
GetWeakVariableName(_variable2).c_str());
_variable.Save(obj, "variableName");
_variable2.Save(obj, "variable2Name");
_strValue.Save(obj, "strValue");
obs_data_set_double(obj, "numValue", _numValue);
obs_data_set_int(obj, "condition", static_cast<int>(_type));
@@ -143,10 +141,8 @@ bool MacroConditionVariable::Save(obs_data_t *obj) const
bool MacroConditionVariable::Load(obs_data_t *obj)
{
MacroCondition::Load(obj);
_variable =
GetWeakVariableByName(obs_data_get_string(obj, "variableName"));
_variable2 = GetWeakVariableByName(
obs_data_get_string(obj, "variable2Name"));
_variable.Load(obj, "variableName");
_variable2.Load(obj, "variable2Name");
_strValue.Load(obj, "strValue");
_numValue = obs_data_get_double(obj, "numValue");
_type = static_cast<Condition>(obs_data_get_int(obj, "condition"));

View File

@@ -1,6 +1,7 @@
#pragma once
#include "macro-condition-edit.hpp"
#include "resizing-text-edit.hpp"
#include "variable-ref.hpp"
#include "variable.hpp"
#include "regex-config.hpp"
#include "variable-text-edit.hpp"
@@ -38,8 +39,8 @@ public:
};
Condition _type = Condition::EQUALS;
std::weak_ptr<Variable> _variable;
std::weak_ptr<Variable> _variable2;
VariableRef _variable;
VariableRef _variable2;
StringVariable _strValue = "";
double _numValue = 0;
RegexConfig _regex;

View File

@@ -14,12 +14,10 @@ void MacroInputVariables::Save(obs_data_t *obj) const
OBSDataArrayAutoRelease array = obs_data_array_create();
for (auto &var : _inputVariables) {
OBSDataAutoRelease arrayObj = obs_data_create();
auto variable = var.lock();
if (variable) {
obs_data_set_string(arrayObj, "variableName",
variable->Name().c_str());
} else {
if (var.expired()) {
obs_data_set_bool(arrayObj, "invalidVariable", true);
} else {
var.Save(arrayObj, "variableName");
}
obs_data_array_push_back(array, arrayObj);
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "list-editor.hpp"
#include "string-list.hpp"
#include "variable-ref.hpp"
#include "variable.hpp"
#include <QGridLayout>
@@ -16,7 +17,7 @@ public:
void SetValues(const StringList &values);
private:
std::vector<std::weak_ptr<Variable>> _inputVariables;
std::vector<VariableRef> _inputVariables;
friend class MacroInputSelection;
friend class MacroInputEdit;
};

View File

@@ -107,9 +107,7 @@ bool MacroSegment::Save(obs_data_t *obj) const
for (const auto &mapping : _varMappings) {
OBSDataAutoRelease item = obs_data_create();
mapping.tempVar.Save(item, GetMacro(), "tempVar");
obs_data_set_string(
item, "variable",
GetWeakVariableName(mapping.variable).c_str());
mapping.variable.Save(item, "variable");
obs_data_array_push_back(mappingsArray, item);
}
obs_data_set_array(data, "varMappings", mappingsArray);
@@ -147,8 +145,7 @@ bool MacroSegment::Load(obs_data_t *obj)
for (size_t i = 0; i < count; i++) {
OBSDataAutoRelease item = obs_data_array_item(mappingsArray, i);
VarMapping mapping;
mapping.variable = GetWeakVariableByName(
obs_data_get_string(item, "variable"));
mapping.variable.Load(item, "variable");
_varMappings.push_back(std::move(mapping));
_varMappings.back().tempVar.Load(item, GetMacro(), "tempVar");
}

View File

@@ -6,6 +6,7 @@
#include "obs-module-helper.hpp"
#include "sync-helpers.hpp"
#include "temp-variable.hpp"
#include "variable-ref.hpp"
#include <obs-data.h>
@@ -26,7 +27,7 @@ class Variable;
struct VarMapping {
TempVariableRef tempVar;
std::weak_ptr<Variable> variable;
VariableRef variable;
};
class EXPORT MacroSegment : public Lockable {

View File

@@ -32,15 +32,9 @@ void SceneSelection::Save(obs_data_t *obj) const
obs_data_set_string(data, nameSaveName.data(),
_group->name.c_str());
break;
case Type::VARIABLE: {
auto var = _variable.lock();
if (!var) {
break;
}
obs_data_set_string(data, nameSaveName.data(),
var->Name().c_str());
case Type::VARIABLE:
_variable.Save(data, nameSaveName.data());
break;
}
default:
break;
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include "canvas-helpers.hpp"
#include "variable-ref.hpp"
namespace advss {
@@ -32,7 +33,7 @@ private:
OBSWeakSource _scene;
OBSWeakCanvas _canvas = GetMainCanvas();
SceneGroup *_group = nullptr;
std::weak_ptr<Variable> _variable;
VariableRef _variable;
Type _type = Type::SCENE;
friend class SceneSelectionWidget;
};

View File

@@ -19,15 +19,9 @@ void SourceSelection::Save(obs_data_t *obj, const char *name) const
obs_data_set_string(data, nameSaveName.data(),
GetWeakSourceName(_source).c_str());
break;
case Type::VARIABLE: {
auto var = _variable.lock();
if (!var) {
break;
}
obs_data_set_string(data, nameSaveName.data(),
var->Name().c_str());
case Type::VARIABLE:
_variable.Save(data, nameSaveName.data());
break;
}
default:
break;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "filter-combo-box.hpp"
#include "obs.hpp"
#include "variable-ref.hpp"
namespace advss {
@@ -30,7 +31,7 @@ private:
void LoadFallback(obs_data_t *obj, const char *name);
OBSWeakSource _source;
std::weak_ptr<Variable> _variable;
VariableRef _variable;
Type _type = Type::SOURCE;
friend class SourceSelectionWidget;
};

View File

@@ -11,9 +11,8 @@ void ColorVariable::Save(obs_data_t *obj, const char *name) const
OBSDataAutoRelease data = obs_data_create();
obs_data_set_int(data, "version", 1);
obs_data_set_int(data, "type", static_cast<int>(_type));
auto var = _variable.lock();
if (var) {
obs_data_set_string(data, "variable", var->Name().c_str());
if (_type == Type::VARIABLE) {
_variable.Save(data, "variable");
}
obs_data_set_int(data, "red", _value.red());
obs_data_set_int(data, "green", _value.green());
@@ -32,8 +31,7 @@ void ColorVariable::Load(obs_data_t *obj, const char *name)
_type = Type::FIXED_VALUE;
return;
}
auto variableName = obs_data_get_string(data, "variable");
_variable = GetWeakVariableByName(variableName);
_variable.Load(data, "variable");
_type = static_cast<Type>(obs_data_get_int(data, "type"));
}

View File

@@ -1,4 +1,5 @@
#pragma once
#include "variable-ref.hpp"
#include "variable.hpp"
#include <obs-data.h>
@@ -27,7 +28,7 @@ public:
private:
Type _type = Type::FIXED_VALUE;
QColor _value = Qt::black;
std::weak_ptr<Variable> _variable;
VariableRef _variable;
friend class VariableColorButton;
};

View File

@@ -1,4 +1,5 @@
#pragma once
#include "variable-ref.hpp"
#include "variable.hpp"
namespace advss {
@@ -27,7 +28,7 @@ public:
private:
Type _type = Type::FIXED_VALUE;
T _value = {};
std::weak_ptr<Variable> _variable;
VariableRef _variable;
friend class GenericVariableSpinbox;
friend class VariableSpinBox;

View File

@@ -18,9 +18,8 @@ void NumberVariable<T>::Save(obs_data_t *obj, const char *name) const
} else {
assert(false);
}
auto var = _variable.lock();
if (var) {
obs_data_set_string(data, "variable", var->Name().c_str());
if (_type == Type::VARIABLE) {
_variable.Save(data, "variable");
}
obs_data_set_int(data, "type", static_cast<int>(_type));
obs_data_set_obj(obj, name, data);
@@ -38,8 +37,7 @@ void NumberVariable<T>::Load(obs_data_t *obj, const char *name)
} else {
assert(false);
}
auto variableName = obs_data_get_string(data, "variable");
_variable = GetWeakVariableByName(variableName);
_variable.Load(data, "variable");
_type = static_cast<Type>(obs_data_get_int(data, "type"));
obs_data_release(data);
}

View File

@@ -1,5 +1,6 @@
#include "variable-string.hpp"
#include "utility.hpp"
#include "variable-ref.hpp"
namespace advss {
@@ -50,6 +51,7 @@ void StringVariable::Load(obs_data_t *obj, const char *name)
void StringVariable::Save(obs_data_t *obj, const char *name) const
{
obs_data_set_string(obj, name, _value.c_str());
ReportVariableRefsInText(_value);
}
void StringVariable::ResolveVariables()

View File

@@ -116,8 +116,7 @@ bool MacroActionScreenshot::Save(obs_data_t *obj) const
obs_data_set_int(obj, "saveType", static_cast<int>(_saveType));
obs_data_set_int(obj, "targetType", static_cast<int>(_targetType));
_path.Save(obj, "savePath");
obs_data_set_string(obj, "variable",
GetWeakVariableName(_variable).c_str());
_variable.Save(obj, "variable");
obs_data_set_int(obj, "version", 1);
return true;
}
@@ -131,7 +130,7 @@ bool MacroActionScreenshot::Load(obs_data_t *obj)
_targetType =
static_cast<TargetType>(obs_data_get_int(obj, "targetType"));
_path.Load(obj, "savePath");
_variable = GetWeakVariableByName(obs_data_get_string(obj, "variable"));
_variable.Load(obj, "variable");
// TODO: Remove fallback for older versions
if (!obs_data_has_user_value(obj, "version")) {

View File

@@ -4,6 +4,7 @@
#include "scene-selection.hpp"
#include "screenshot-helper.hpp"
#include "source-selection.hpp"
#include "variable-ref.hpp"
#include "variable.hpp"
#include <QComboBox>
@@ -31,7 +32,7 @@ public:
SceneSelection _scene;
SourceSelection _source;
StringVariable _path = obs_module_text("AdvSceneSwitcher.enterPath");
std::weak_ptr<Variable> _variable;
VariableRef _variable;
private:
void FrontendScreenshot(OBSWeakSource &) const;

View File

@@ -176,8 +176,7 @@ bool MacroActionWait::Save(obs_data_t *obj) const
obs_data_set_int(obj, "version", 1);
_duration.Save(obj);
_duration2.Save(obj, "duration2");
obs_data_set_string(obj, "variableName",
GetWeakVariableName(_variable).c_str());
_variable.Save(obj, "variableName");
obs_data_set_int(obj, "condition", static_cast<int>(_condition));
_strValue.Save(obj, "strValue");
_numValue.Save(obj, "numValue");
@@ -200,8 +199,7 @@ bool MacroActionWait::Load(obs_data_t *obj)
obs_data_get_int(obj, "displayUnit2")));
}
_duration.Load(obj);
_variable =
GetWeakVariableByName(obs_data_get_string(obj, "variableName"));
_variable.Load(obj, "variableName");
_condition = static_cast<Condition>(obs_data_get_int(obj, "condition"));
_strValue.Load(obj, "strValue");
_numValue.Load(obj, "numValue");

View File

@@ -2,6 +2,7 @@
#include "macro-action-edit.hpp"
#include "duration-control.hpp"
#include "regex-config.hpp"
#include "variable-ref.hpp"
#include "variable-spinbox.hpp"
#include "variable-text-edit.hpp"
#include "variable.hpp"
@@ -44,7 +45,7 @@ public:
LESS_THAN,
GREATER_THAN,
};
std::weak_ptr<Variable> _variable;
VariableRef _variable;
Condition _condition = Condition::EQUALS;
StringVariable _strValue = "";
DoubleVariable _numValue = 0.0;

View File

@@ -20,15 +20,9 @@ void FilterSelection::Save(obs_data_t *obj, const char *name) const
_filter ? GetWeakSourceName(_filter).c_str()
: _filterName.c_str());
break;
case Type::VARIABLE: {
auto var = _variable.lock();
if (!var) {
break;
}
obs_data_set_string(data, nameSaveName.data(),
var->Name().c_str());
case Type::VARIABLE:
_variable.Save(data, nameSaveName.data());
break;
}
default:
break;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "source-selection.hpp"
#include "filter-combo-box.hpp"
#include "variable-ref.hpp"
namespace advss {
@@ -32,7 +33,7 @@ private:
// Storing the name separately as depending on the source selection
// the filter source might not be available at the moment.
std::string _filterName = "";
std::weak_ptr<Variable> _variable;
VariableRef _variable;
Type _type = Type::SOURCE;
friend class FilterSelectionWidget;
};

View File

@@ -205,14 +205,9 @@ void SceneItemSelection::Save(obs_data_t *obj, const char *name) const
obs_data_set_string(data, itemSaveName.data(),
GetWeakSourceName(_source).c_str());
break;
case SceneItemSelection::Type::VARIABLE_NAME: {
auto var = _variable.lock();
if (var) {
obs_data_set_string(data, itemSaveName.data(),
var->Name().c_str());
}
case SceneItemSelection::Type::VARIABLE_NAME:
_variable.Save(data, itemSaveName.data());
break;
}
case SceneItemSelection::Type::SOURCE_NAME_PATTERN:
_pattern.Save(data, patternSaveName.data());
_regex.Save(data);

View File

@@ -3,6 +3,7 @@
#include "filter-combo-box.hpp"
#include "variable-spinbox.hpp"
#include "variable-line-edit.hpp"
#include "variable-ref.hpp"
#include "variable-string.hpp"
#include "regex-config.hpp"
@@ -77,7 +78,7 @@ private:
Type _type = Type::SOURCE_NAME;
OBSWeakSource _source;
std::weak_ptr<Variable> _variable;
VariableRef _variable;
IntVariable _index = 1;
IntVariable _indexEnd = 1;
NameConflictSelection _nameConflictSelectionType =

View File

@@ -1292,8 +1292,7 @@ bool MacroActionTwitch::Save(obs_data_t *obj) const
obs_data_set_int(obj, "pollEndStatus",
static_cast<int>(_pollEndStatus));
_pointsReward.Save(obj);
obs_data_set_string(obj, "rewardVariable",
GetWeakVariableName(_rewardVariable).c_str());
_rewardVariable.Save(obj, "rewardVariable");
obs_data_set_bool(obj, "useVariableForRewardSelection",
_useVariableForRewardSelection);
@@ -1334,8 +1333,7 @@ bool MacroActionTwitch::Load(obs_data_t *obj)
_pollEndStatus = static_cast<PollEndStatus>(
obs_data_get_int(obj, "pollEndStatus"));
_pointsReward.Load(obj);
_rewardVariable = GetWeakVariableByName(
obs_data_get_string(obj, "rewardVariable"));
_rewardVariable.Load(obj, "rewardVariable");
_useVariableForRewardSelection =
obs_data_get_bool(obj, "useVariableForRewardSelection");

View File

@@ -10,6 +10,7 @@
#include "tag-selection.hpp"
#include <variable-line-edit.hpp>
#include <variable-ref.hpp>
#include <variable-text-edit.hpp>
#include <duration-control.hpp>
#include <duration.hpp>
@@ -197,7 +198,7 @@ public:
DoubleVariable _userId = 0;
StringVariable _banReason = "";
TwitchPointsReward _pointsReward;
std::weak_ptr<Variable> _rewardVariable;
VariableRef _rewardVariable;
bool _useVariableForRewardSelection = false;
StringVariable _pollTitle = "Poll question";
StringVariable _pollChoices = "Choice 1\nChoice 2";

View File

@@ -1,6 +1,8 @@
#include "catch.hpp"
#include <variable-number.hpp>
#include <variable-ref.hpp>
#include <variable-string.hpp>
#include <variable.hpp>
#include <obs.hpp>
@@ -118,5 +120,39 @@ TEST_CASE("VariableRef", "[variable]")
REQUIRE(outer.Counts().at("var") == 1);
}
SECTION("Number variable reports only when using a variable")
{
OBSDataAutoRelease data = obs_data_create();
OBSDataAutoRelease numberData = obs_data_create();
obs_data_set_string(numberData, "variable", "var");
obs_data_set_int(numberData, "type", 1);
obs_data_set_obj(data, "number", numberData);
advss::IntVariable number;
number.Load(data, "number");
{
advss::VariableRefCollector collector;
number.Save(data, "number");
REQUIRE(collector.Counts().at("var") == 1);
}
obs_data_set_int(numberData, "type", 0);
obs_data_set_obj(data, "number", numberData);
number.Load(data, "number");
advss::VariableRefCollector collector;
number.Save(data, "number");
REQUIRE(collector.Counts().empty());
}
SECTION("String variable reports text references")
{
advss::StringVariable text = "${var} and ${var2}";
advss::VariableRefCollector collector;
OBSDataAutoRelease data = obs_data_create();
text.Save(data, "text");
REQUIRE(collector.Counts().at("var") == 1);
REQUIRE(collector.Counts().at("var2") == 1);
}
advss::GetVariables().clear();
}