diff options
| author | 2026-01-13 01:59:02 +1100 | |
|---|---|---|
| committer | 2026-01-13 01:59:02 +1100 | |
| commit | e25ef95c4628fcf2c712e82d7ba4475a4a6faa89 (patch) | |
| tree | 5f968d58bd0e41eeb9bd2822097fa13bf102404a /src/frontend | |
| parent | 27b3c718c2fc1cab319316799ff8a0b19ba5af5d (diff) | |
frontend/mame/luaengine.cpp: Better handling of options:
* Use "classic" locale for numeric conversions.
* Don't use capturing lambdas.
* Put integer overload of value() before float overload.
Diffstat (limited to 'src/frontend')
| -rw-r--r-- | src/frontend/mame/luaengine.cpp | 93 |
1 files changed, 62 insertions, 31 deletions
diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index b8cd3d82963..5b6bed871f8 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -34,7 +34,9 @@ #include <algorithm> #include <condition_variable> #include <cstring> +#include <locale> #include <mutex> +#include <sstream> #include <thread> @@ -1351,43 +1353,72 @@ void lua_engine::initialize() auto core_options_entry_type = sol().registry().new_usertype<core_options::entry>("core_options_entry", "new", sol::no_constructor); core_options_entry_type.set("value", sol::overload( - [this](core_options::entry &e, bool val) { - if(e.type() != core_options::option_type::BOOLEAN) - luaL_error(m_lua_state, "Cannot set option to wrong type"); + [] (core_options::entry &e, sol::this_state s, bool val) + { + if (e.type() != core_options::option_type::BOOLEAN) + luaL_error(s, "Cannot set option to wrong type"); else e.set_value(val ? "1" : "0", OPTION_PRIORITY_CMDLINE); }, - [this](core_options::entry &e, float val) { - if(e.type() != core_options::option_type::FLOAT) - luaL_error(m_lua_state, "Cannot set option to wrong type"); + [] (core_options::entry &e, sol::this_state s, int val) + { + if (e.type() != core_options::option_type::INTEGER) + luaL_error(s, "Cannot set option to wrong type"); else - e.set_value(string_format("%f", val), OPTION_PRIORITY_CMDLINE); + e.set_value(string_format(std::locale::classic(), "%d", val), OPTION_PRIORITY_CMDLINE); }, - [this](core_options::entry &e, int val) { - if(e.type() != core_options::option_type::INTEGER) - luaL_error(m_lua_state, "Cannot set option to wrong type"); + [] (core_options::entry &e, sol::this_state s, float val) + { + if (e.type() != core_options::option_type::FLOAT) + luaL_error(s, "Cannot set option to wrong type"); else - e.set_value(string_format("%d", val), OPTION_PRIORITY_CMDLINE); + e.set_value(string_format(std::locale::classic(), "%f", val), OPTION_PRIORITY_CMDLINE); }, - [this](core_options::entry &e, const char *val) { - if(e.type() != core_options::option_type::STRING && e.type() != core_options::option_type::PATH && e.type() != core_options::option_type::MULTIPATH) - luaL_error(m_lua_state, "Cannot set option to wrong type"); + [] (core_options::entry &e, sol::this_state s, const char *val) + { + if (e.type() != core_options::option_type::STRING && e.type() != core_options::option_type::PATH && e.type() != core_options::option_type::MULTIPATH) + luaL_error(s, "Cannot set option to wrong type"); else e.set_value(val, OPTION_PRIORITY_CMDLINE); }, - [this](core_options::entry &e) -> sol::object { + [] (core_options::entry &e, sol::this_state s) -> sol::object + { if (e.type() == core_options::option_type::INVALID) return sol::lua_nil; - switch(e.type()) + switch (e.type()) { case core_options::option_type::BOOLEAN: - return sol::make_object(sol(), atoi(e.value()) != 0); + { + std::istringstream str(e.value()); + str.imbue(std::locale::classic()); + int ival; + if (str >> ival) + return sol::make_object(s, ival != 0); + else + return sol::make_object(s, false); + } case core_options::option_type::INTEGER: - return sol::make_object(sol(), atoi(e.value())); + { + std::istringstream str(e.value()); + str.imbue(std::locale::classic()); + int ival; + if (str >> ival) + return sol::make_object(s, ival); + else + return sol::make_object(s, 0); + } case core_options::option_type::FLOAT: - return sol::make_object(sol(), atof(e.value())); + { + std::istringstream str(e.value()); + str.imbue(std::locale::classic()); + float fval; + if (str >> fval) + return sol::make_object(s, fval); + else + return sol::make_object(s, 0.0F); + } default: - return sol::make_object(sol(), e.value()); + return sol::make_object(s, e.value()); } })); core_options_entry_type.set("description", &core_options::entry::description); @@ -1730,10 +1761,10 @@ void lua_engine::initialize() { float const sc_width(sdev.visible_area().width()); float const sc_height(sdev.visible_area().height()); - x1 = std::clamp(x1, 0.0f, sc_width) / sc_width; - y1 = std::clamp(y1, 0.0f, sc_height) / sc_height; - x2 = std::clamp(x2, 0.0f, sc_width) / sc_width; - y2 = std::clamp(y2, 0.0f, sc_height) / sc_height; + x1 = std::clamp(x1, 0.0F, sc_width) / sc_width; + y1 = std::clamp(y1, 0.0F, sc_height) / sc_height; + x2 = std::clamp(x2, 0.0F, sc_width) / sc_width; + y2 = std::clamp(y2, 0.0F, sc_height) / sc_height; mame_ui_manager &ui(mame_machine_manager::instance()->ui()); if (!fgcolor) fgcolor = ui.colors().text_color(); @@ -1747,10 +1778,10 @@ void lua_engine::initialize() { float const sc_width(sdev.visible_area().width()); float const sc_height(sdev.visible_area().height()); - x1 = std::clamp(x1, 0.0f, sc_width) / sc_width; - y1 = std::clamp(y1, 0.0f, sc_height) / sc_height; - x2 = std::clamp(x2, 0.0f, sc_width) / sc_width; - y2 = std::clamp(y2, 0.0f, sc_height) / sc_height; + x1 = std::clamp(x1, 0.0F, sc_width) / sc_width; + y1 = std::clamp(y1, 0.0F, sc_height) / sc_height; + x2 = std::clamp(x2, 0.0F, sc_width) / sc_width; + y2 = std::clamp(y2, 0.0F, sc_height) / sc_height; if (!color) color = mame_machine_manager::instance()->ui().colors().text_color(); sdev.container().add_line(x1, y1, x2, y2, UI_LINE_WIDTH, rgb_t(*color), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA)); @@ -1765,7 +1796,7 @@ void lua_engine::initialize() float x = 0; if (xobj.is<float>()) { - x = std::clamp(xobj.as<float>(), 0.0f, sc_width) / sc_width; + x = std::clamp(xobj.as<float>(), 0.0F, sc_width) / sc_width; } else if (xobj.is<char const *>()) { @@ -1782,7 +1813,7 @@ void lua_engine::initialize() luaL_error(m_lua_state, "Error in param 1 to draw_text"); return; } - y = std::clamp(y, 0.0f, sc_height) / sc_height; + y = std::clamp(y, 0.0F, sc_height) / sc_height; mame_ui_manager &ui(mame_machine_manager::instance()->ui()); if (!fgcolor) fgcolor = ui.colors().text_color(); @@ -1791,7 +1822,7 @@ void lua_engine::initialize() ui.draw_text_full( sdev.container(), msg, - x, y, (1.0f - x), + x, y, (1.0F - x), justify, ui::text_layout::word_wrapping::WORD, mame_ui_manager::OPAQUE_, *fgcolor, *bgcolor); }); |
