luaengine: add basic table return support (nw)

This commit is contained in:
cracyc 2016-11-01 15:27:21 -05:00
parent 0aa29030da
commit 06363cf65a
4 changed files with 49 additions and 55 deletions

View File

@ -35,7 +35,7 @@ function data.startplugin()
end end
end) end)
emu.register_callback(function(set) emu.register_callback(function(set)
local ret local ret = {}
if set == "" then if set == "" then
set = emu.romname() set = emu.romname()
end end
@ -54,11 +54,7 @@ function data.startplugin()
end end
local name = scr.check(setname, softname) local name = scr.check(setname, softname)
if name then if name then
if not ret then ret[#ret + 1] = name
ret = name
else
ret = ret .. "," .. name
end
valid_lst[#valid_lst + 1] = scr valid_lst[#valid_lst + 1] = scr
end end
end end

View File

@ -58,7 +58,7 @@ public:
void on_frame_done(); void on_frame_done();
template<typename T, typename U> template<typename T, typename U>
bool call_plugin(const std::string &name, T in, U& out) bool call_plugin(const std::string &name, const T in, U &out)
{ {
bool ret = false; bool ret = false;
sol::object outobj = call_plugin(name, sol::make_object(sol(), in)); sol::object outobj = call_plugin(name, sol::make_object(sol(), in));
@ -71,17 +71,43 @@ public:
} }
template<typename T, typename U> template<typename T, typename U>
bool call_plugin_check(const std::string &name, U in) bool call_plugin(const std::string &name, const T in, std::vector<U> &out)
{
bool ret = false;
sol::object outobj = call_plugin(name, sol::make_object(sol(), in));
if(outobj.is<sol::table>())
{
for(auto &entry : outobj.as<sol::table>())
{
if(entry.second.template is<U>())
{
out.push_back(entry.second.template as<U>());
ret = true;
}
}
}
return ret;
}
// this will also check if a returned table contains type T
template<typename T, typename U>
bool call_plugin_check(const std::string &name, const U in)
{ {
bool ret = false; bool ret = false;
sol::object outobj = call_plugin(name, sol::make_object(sol(), in)); sol::object outobj = call_plugin(name, sol::make_object(sol(), in));
if(outobj.is<T>()) if(outobj.is<T>())
ret = true; ret = true;
else if(outobj.is<sol::table>())
{
// check just one entry, checking the whole thing shouldn't be necessary as this only supports homogeneous tables
if(outobj.as<sol::table>().begin().operator*().second.template is<T>())
ret = true;
}
return ret; return ret;
} }
template<typename T> template<typename T>
void call_plugin_set(const std::string &name, T in) void call_plugin_set(const std::string &name, const T in)
{ {
call_plugin(name, sol::make_object(sol(), in)); call_plugin(name, sol::make_object(sol(), in));
} }

View File

@ -45,19 +45,16 @@ menu_dats_view::menu_dats_view(mame_ui_manager &mui, render_container &container
m_parent = image.software_entry()->parentname(); m_parent = image.software_entry()->parentname();
} }
} }
std::string lua_list; std::vector<std::string> lua_list;
if(mame_machine_manager::instance()->lua()->call_plugin("data_list", driver ? driver->name : "", lua_list)) if(mame_machine_manager::instance()->lua()->call_plugin("data_list", driver ? driver->name : "", lua_list))
{ {
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
int count = 0; int count = 0;
while(token) for(std::string &item : lua_list)
{ {
std::string item; std::string version;
mame_machine_manager::instance()->lua()->call_plugin("data_version", util::string_format("%d", count).c_str(), item); mame_machine_manager::instance()->lua()->call_plugin("data_version", count, version);
m_items_list.emplace_back(_(token), count, item); m_items_list.emplace_back(_(item.c_str()), count, std::move(version));
count++; count++;
token = strtok(nullptr, ",");
} }
} }
} }
@ -80,19 +77,16 @@ menu_dats_view::menu_dats_view(mame_ui_manager &mui, render_container &container
{ {
if (swinfo != nullptr && !swinfo->usage.empty()) if (swinfo != nullptr && !swinfo->usage.empty())
m_items_list.emplace_back(_("Software Usage"), 0, ""); m_items_list.emplace_back(_("Software Usage"), 0, "");
std::string lua_list; std::vector<std::string> lua_list;
if(mame_machine_manager::instance()->lua()->call_plugin("data_list", std::string(m_short).append(1, ',').append(m_list).c_str(), lua_list)) if(mame_machine_manager::instance()->lua()->call_plugin("data_list", std::string(m_short).append(1, ',').append(m_list).c_str(), lua_list))
{ {
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
int count = 1; int count = 1;
while(token) for(std::string &item : lua_list)
{ {
std::string item; std::string version;
mame_machine_manager::instance()->lua()->call_plugin("data_version", util::string_format("%d", count - 1).c_str(), item); mame_machine_manager::instance()->lua()->call_plugin("data_version", count - 1, version);
m_items_list.emplace_back(_(token), count, item); m_items_list.emplace_back(_(item.c_str()), count, std::move(version));
count++; count++;
token = strtok(nullptr, ",");
} }
} }
} }
@ -395,7 +389,7 @@ void menu_dats_view::get_data()
{ {
std::vector<int> xstart, xend; std::vector<int> xstart, xend;
std::string buffer; std::string buffer;
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("%d", m_items_list[m_actual].option).c_str(), buffer); mame_machine_manager::instance()->lua()->call_plugin("data", m_items_list[m_actual].option, buffer);
auto lines = ui().wrap_text(container(), buffer.c_str(), 0.0f, 0.0f, 1.0f - (4.0f * UI_BOX_LR_BORDER), xstart, xend); auto lines = ui().wrap_text(container(), buffer.c_str(), 0.0f, 0.0f, 1.0f - (4.0f * UI_BOX_LR_BORDER), xstart, xend);
@ -414,7 +408,7 @@ void menu_dats_view::get_data_sw()
if (m_items_list[m_actual].option == 0) if (m_items_list[m_actual].option == 0)
buffer = m_swinfo->usage; buffer = m_swinfo->usage;
else else
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("data", "%d", m_items_list[m_actual].option - 1).c_str(), buffer); mame_machine_manager::instance()->lua()->call_plugin("data", m_items_list[m_actual].option - 1, buffer);
auto lines = ui().wrap_text(container(), buffer.c_str(), 0.0f, 0.0f, 1.0f - (4.0f * UI_BOX_LR_BORDER), xstart, xend); auto lines = ui().wrap_text(container(), buffer.c_str(), 0.0f, 0.0f, 1.0f - (4.0f * UI_BOX_LR_BORDER), xstart, xend);
for (int x = 0; x < lines; ++x) for (int x = 0; x < lines; ++x)

View File

@ -1911,21 +1911,10 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
m_info_view = 0; m_info_view = 0;
m_info_software = software; m_info_software = software;
ui_globals::cur_sw_dats_view = 0; ui_globals::cur_sw_dats_view = 0;
ui_globals::cur_sw_dats_total = 1;
std::string lua_list;
mame_machine_manager::instance()->lua()->call_plugin("data_list", std::string(software->shortname).append(1, ',').append(software->listname).c_str(), lua_list);
m_items_list.clear(); m_items_list.clear();
if(lua_list.length()) mame_machine_manager::instance()->lua()->call_plugin("data_list", std::string(software->shortname).append(1, ',').append(software->listname).c_str(), m_items_list);
{ ui_globals::cur_sw_dats_total = m_items_list.size() + 1;
char *token = strtok((char *)lua_list.c_str(), ",");
while(token)
{
ui_globals::cur_sw_dats_total++;
m_items_list.emplace_back(token);
token = strtok(nullptr, ",");
}
}
} }
if (m_info_view == 0) if (m_info_view == 0)
@ -1933,7 +1922,7 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
else else
{ {
m_info_buffer = ""; m_info_buffer = "";
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("%d", m_info_view - 1).c_str(), m_info_buffer); mame_machine_manager::instance()->lua()->call_plugin("data", m_info_view - 1, m_info_buffer);
} }
} }
total = ui_globals::cur_sw_dats_total; total = ui_globals::cur_sw_dats_total;
@ -1955,21 +1944,10 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
m_info_driver = driver; m_info_driver = driver;
m_info_view = 0; m_info_view = 0;
ui_globals::curdats_view = 0; ui_globals::curdats_view = 0;
ui_globals::curdats_total = 1;
std::string lua_list;
mame_machine_manager::instance()->lua()->call_plugin("data_list", driver->name, lua_list);
m_items_list.clear(); m_items_list.clear();
if(lua_list.length()) mame_machine_manager::instance()->lua()->call_plugin("data_list", driver->name, m_items_list);
{ ui_globals::curdats_total = m_items_list.size() + 1;
char *token = strtok((char *)lua_list.c_str(), ",");
while(token)
{
ui_globals::curdats_total++;
m_items_list.emplace_back(token);
token = strtok(nullptr, ",");
}
}
} }
if (m_info_view == 0) if (m_info_view == 0)
@ -1977,7 +1955,7 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
else else
{ {
m_info_buffer = ""; m_info_buffer = "";
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("%d", m_info_view - 1).c_str(), m_info_buffer); mame_machine_manager::instance()->lua()->call_plugin("data", m_info_view - 1, m_info_buffer);
} }
} }
total = ui_globals::curdats_total; total = ui_globals::curdats_total;