From c85e55692426e3860b6c5c24881bc12eb2e7c616 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sat, 9 Dec 2023 20:07:56 +0100 Subject: [PATCH] Add support for "random()" expression Generates a random number between [0,1) --- src/utils/math-helpers.cpp | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/utils/math-helpers.cpp b/src/utils/math-helpers.cpp index b8177c31..f7d5acd2 100644 --- a/src/utils/math-helpers.cpp +++ b/src/utils/math-helpers.cpp @@ -1,32 +1,35 @@ #include "math-helpers.hpp" +#include "obs-module-helper.hpp" + #include - -#ifdef UNIT_TEST -const char *obs_module_text(const char *text) -{ - return text; -} -#else -#include -#endif - -typedef exprtk::expression expression_t; -typedef exprtk::parser parser_t; +#include namespace advss { std::variant EvalMathExpression(const std::string &expr) { - expression_t expression; + static bool setupDone = false; + static exprtk::symbol_table symbolTable; + static std::random_device rd; + static std::mt19937 gen(rd()); + static std::uniform_real_distribution dis(0.0, 1.0); + static auto randomFunc = []() { return dis(gen); }; - parser_t parser; + if (!setupDone) { + symbolTable.add_function("random", randomFunc); + setupDone = true; + } + + exprtk::expression expression; + expression.register_symbol_table(symbolTable); + exprtk::parser parser; if (parser.compile(expr, expression)) { return expression.value(); } return std::string(obs_module_text( "AdvSceneSwitcher.math.expressionFail")) + - " \"" + expr; + " \"" + expr + "\""; } bool IsValidNumber(const std::string &str)