satdump/src-interface/viewer2/dsp/flowgraph/variable_manager.cpp

63 lines
1.9 KiB
C++
Raw Normal View History

2025-03-09 10:16:40 +01:00
#include "variable_manager.h"
#include "imgui/imgui.h"
#include "imgui/imgui_stdlib.h"
#include "common/widgets/json_editor.h"
#include "libs/sol2/sol.hpp"
#include "logger.h"
2025-03-22 16:24:29 +01:00
#include "core/exception.h"
#include <typeinfo>
2025-03-09 10:16:40 +01:00
namespace satdump
{
namespace ndsp
{
void LuaVariableManager::drawVariables()
{
widgets::JSONTableEditor(variables, "Variables");
}
2025-03-22 16:24:29 +01:00
nlohmann::json LuaVariableManager::getVariable(std::string var_str)
2025-03-09 10:16:40 +01:00
{
sol::state lua;
lua.open_libraries(sol::lib::string);
lua.open_libraries(sol::lib::math);
for (auto &j : variables.items())
{
2025-03-22 16:24:29 +01:00
if (j.value().is_number())
{
if (j.value().is_number_integer())
lua[j.key()] = j.value().get<int64_t>();
else
lua[j.key()] = j.value().get<double>();
}
else if (j.value().is_string())
lua[j.key()] = j.value().get<std::string>();
2025-03-09 10:16:40 +01:00
else
logger->error("Type unsupported for Lua! : " + j.key());
}
lua.script("output = " + var_str);
2025-03-22 16:24:29 +01:00
printf("%s\n", sol::type_name(lua, lua["output"].get_type()).c_str());
sol::object o = lua["output"];
2025-03-09 10:16:40 +01:00
2025-03-22 16:24:29 +01:00
if (o.is<bool>())
return o.as<bool>();
2025-03-24 17:59:58 +01:00
// else if (o.is<int64_t>())
// TODOREWORK return o.as<int64_t>();
2025-03-22 16:24:29 +01:00
else if (o.is<std::string>())
return o.as<std::string>();
else if (o.is<double>())
return o.as<double>();
else
{
lua.open_libraries(sol::lib::base);
lua.script("print(output)");
throw satdump_exception("Could not convert type from Lua to JSON");
}
}
2025-03-09 10:16:40 +01:00
}
}