luaengine: use templated accessor for call_plugin (nw)

This commit is contained in:
cracyc 2016-10-31 21:57:40 -05:00
parent e5bb85da5b
commit 0aa29030da
8 changed files with 89 additions and 59 deletions

View File

@ -724,32 +724,22 @@ lua_engine::~lua_engine()
close();
}
const char *lua_engine::call_plugin(const char *data, const char *name)
sol::object lua_engine::call_plugin(const std::string &name, sol::object in)
{
std::string field("cb_");
const char *ret = nullptr;
field += name;
lua_settop(m_lua_state, 0);
lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str());
if(!lua_isfunction(m_lua_state, -1))
std::string field = "cb_" + name;
sol::object obj = sol().registry()[field];
if(obj.is<sol::protected_function>())
{
lua_pop(m_lua_state, 1);
return nullptr;
auto res = (obj.as<sol::protected_function>())(in);
if(!res.valid())
{
sol::error err = res;
osd_printf_error("[LUA ERROR] in call_plugin: %s\n", err.what());
}
else
return res.get<sol::object>();
}
lua_pushstring(m_lua_state, data);
int error;
if((error = lua_pcall(m_lua_state, 1, 1, 0)) != LUA_OK)
{
if(error == LUA_ERRRUN)
printf("%s\n", lua_tostring(m_lua_state, -1));
lua_pop(m_lua_state, 1);
return nullptr;
}
if(lua_isstring(m_lua_state, -1))
ret = lua_tostring(m_lua_state, -1);
lua_pop(m_lua_state, 1);
return ret;
return sol::make_object(sol(), sol::nil);
}
void lua_engine::menu_populate(const std::string &menu, std::vector<std::tuple<std::string, std::string, std::string>> &menu_list)
@ -772,7 +762,7 @@ void lua_engine::menu_populate(const std::string &menu, std::vector<std::tuple<s
if(entry.second.is<sol::table>())
{
sol::table enttable = entry.second.as<sol::table>();
menu_list.emplace_back(std::make_tuple(enttable.get<const char*>(1), enttable.get<const char*>(2), enttable.get<const char*>(3)));
menu_list.emplace_back(enttable.get<std::string, std::string, std::string>(1, 2, 3));
}
}
}
@ -1188,14 +1178,14 @@ void lua_engine::initialize()
*/
sol().new_usertype<game_driver>("game_driver", "new", sol::no_constructor,
"source_file", &game_driver::source_file,
"parent", &game_driver::parent,
"name", &game_driver::name,
"description", &game_driver::description,
"year", &game_driver::year,
"manufacturer", &game_driver::manufacturer,
"compatible_with", &game_driver::compatible_with,
"default_layout", &game_driver::default_layout);
"source_file", sol::readonly(&game_driver::source_file),
"parent", sol::readonly(&game_driver::parent),
"name", sol::readonly(&game_driver::name),
"description", sol::readonly(&game_driver::description),
"year", sol::readonly(&game_driver::year),
"manufacturer", sol::readonly(&game_driver::manufacturer),
"compatible_with", sol::readonly(&game_driver::compatible_with),
"default_layout", sol::readonly(&game_driver::default_layout));
/* machine.devices[device_tag]
* device:name() - device long name

View File

@ -56,7 +56,35 @@ public:
std::vector<std::string> &get_menu() { return m_menu; }
void attach_notifiers();
void on_frame_done();
const char *call_plugin(const char *data, const char *name);
template<typename T, typename U>
bool call_plugin(const std::string &name, T in, U& out)
{
bool ret = false;
sol::object outobj = call_plugin(name, sol::make_object(sol(), in));
if(outobj.is<U>())
{
out = outobj.as<U>();
ret = true;
}
return ret;
}
template<typename T, typename U>
bool call_plugin_check(const std::string &name, U in)
{
bool ret = false;
sol::object outobj = call_plugin(name, sol::make_object(sol(), in));
if(outobj.is<T>())
ret = true;
return ret;
}
template<typename T>
void call_plugin_set(const std::string &name, T in)
{
call_plugin(name, sol::make_object(sol(), in));
}
private:
// internal state
@ -82,6 +110,7 @@ private:
void register_function(sol::function func, const char *id);
bool execute_function(const char *id);
sol::object call_plugin(const std::string &name, sol::object in);
struct addr_space {
addr_space(address_space &space, device_memory_interface &dev) :

View File

@ -341,7 +341,7 @@ void emulator_info::layout_file_cb(xml_data_node &layout)
{
xml_data_node *script = xml_get_sibling(mamelayout->child, "script");
if(script)
mame_machine_manager::instance()->lua()->call_plugin(script->value, "layout");
mame_machine_manager::instance()->lua()->call_plugin_set("layout", script->value);
}
}

View File

@ -45,15 +45,17 @@ menu_dats_view::menu_dats_view(mame_ui_manager &mui, render_container &container
m_parent = image.software_entry()->parentname();
}
}
const char *lua_list = mame_machine_manager::instance()->lua()->call_plugin(driver ? driver->name : "", "data_list");
if(lua_list)
std::string 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;
while(token)
{
m_items_list.emplace_back(_(token), count, mame_machine_manager::instance()->lua()->call_plugin(util::string_format("%d", count).c_str(), "data_version"));
std::string item;
mame_machine_manager::instance()->lua()->call_plugin("data_version", util::string_format("%d", count).c_str(), item);
m_items_list.emplace_back(_(token), count, item);
count++;
token = strtok(nullptr, ",");
}
@ -78,15 +80,17 @@ menu_dats_view::menu_dats_view(mame_ui_manager &mui, render_container &container
{
if (swinfo != nullptr && !swinfo->usage.empty())
m_items_list.emplace_back(_("Software Usage"), 0, "");
const char *lua_list = mame_machine_manager::instance()->lua()->call_plugin(std::string(m_short).append(1, ',').append(m_list).c_str(), "data_list");
if(lua_list)
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))
{
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
int count = 1;
while(token)
{
m_items_list.emplace_back(_(token), count, mame_machine_manager::instance()->lua()->call_plugin(util::string_format("%d", count - 1).c_str(), "data_version"));
std::string item;
mame_machine_manager::instance()->lua()->call_plugin("data_version", util::string_format("%d", count - 1).c_str(), item);
m_items_list.emplace_back(_(token), count, item);
count++;
token = strtok(nullptr, ",");
}
@ -390,7 +394,8 @@ void menu_dats_view::custom_render(void *selectedref, float top, float bottom, f
void menu_dats_view::get_data()
{
std::vector<int> xstart, xend;
std::string buffer(mame_machine_manager::instance()->lua()->call_plugin(util::string_format("%d", m_items_list[m_actual].option).c_str(), "data"));
std::string buffer;
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("%d", m_items_list[m_actual].option).c_str(), buffer);
auto lines = ui().wrap_text(container(), buffer.c_str(), 0.0f, 0.0f, 1.0f - (4.0f * UI_BOX_LR_BORDER), xstart, xend);
@ -409,7 +414,7 @@ void menu_dats_view::get_data_sw()
if (m_items_list[m_actual].option == 0)
buffer = m_swinfo->usage;
else
buffer = mame_machine_manager::instance()->lua()->call_plugin(util::string_format("%d", m_items_list[m_actual].option - 1).c_str(), "data");
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("data", "%d", m_items_list[m_actual].option - 1).c_str(), buffer);
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)

View File

@ -130,7 +130,7 @@ void menu_main::populate()
item_append(_("Plugin Options"), "", 0, (void *)PLUGINS);
// add dats menu
if (mame_machine_manager::instance()->lua()->call_plugin("", "data_list"))
if (mame_machine_manager::instance()->lua()->call_plugin_check<const char *>("data_list", ""))
item_append(_("External DAT View"), "", 0, (void *)EXTERNAL_DATS);
item_append(menu_item_type::SEPARATOR);

View File

@ -347,7 +347,7 @@ void menu_select_game::handle()
if (!isfavorite())
{
const game_driver *driver = (const game_driver *)menu_event->itemref;
if ((uintptr_t)driver > skip_main_items && mame_machine_manager::instance()->lua()->call_plugin(driver->name, "data_list"))
if ((uintptr_t)driver > skip_main_items && mame_machine_manager::instance()->lua()->call_plugin_check<const char *>("data_list", driver->name))
menu::stack_push<menu_dats_view>(ui(), container(), driver);
}
else
@ -356,10 +356,10 @@ void menu_select_game::handle()
if ((uintptr_t)ui_swinfo > skip_main_items)
{
if (ui_swinfo->startempty == 1 && mame_machine_manager::instance()->lua()->call_plugin(ui_swinfo->driver->name, "data_list"))
if (ui_swinfo->startempty == 1 && mame_machine_manager::instance()->lua()->call_plugin_check<const char *>("data_list", ui_swinfo->driver->name))
menu::stack_push<menu_dats_view>(ui(), container(), ui_swinfo->driver);
else if (mame_machine_manager::instance()->lua()->call_plugin(std::string(ui_swinfo->shortname).append(1, ',').append(ui_swinfo->listname).c_str(), "data_list") || !ui_swinfo->usage.empty())
menu::stack_push<menu_dats_view>(ui(), container(), ui_swinfo);
else if (mame_machine_manager::instance()->lua()->call_plugin_check<const char *>("data_list", std::string(ui_swinfo->shortname).append(1, ',').append(ui_swinfo->listname).c_str()) || !ui_swinfo->usage.empty())
menu::stack_push<menu_dats_view>(ui(), container(), ui_swinfo);
}
}
}

View File

@ -1913,12 +1913,12 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
ui_globals::cur_sw_dats_view = 0;
ui_globals::cur_sw_dats_total = 1;
const char *lua_list = mame_machine_manager::instance()->lua()->call_plugin(std::string(software->shortname).append(1, ',').append(software->listname).c_str(), "data_list");
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();
if(lua_list)
if(lua_list.length())
{
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
char *token = strtok((char *)lua_list.c_str(), ",");
while(token)
{
ui_globals::cur_sw_dats_total++;
@ -1931,7 +1931,10 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
if (m_info_view == 0)
m_info_buffer = software->usage;
else
m_info_buffer = mame_machine_manager::instance()->lua()->call_plugin(util::string_format("%d", m_info_view - 1).c_str(), "data");
{
m_info_buffer = "";
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("%d", m_info_view - 1).c_str(), m_info_buffer);
}
}
total = ui_globals::cur_sw_dats_total;
}
@ -1954,12 +1957,12 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
ui_globals::curdats_view = 0;
ui_globals::curdats_total = 1;
const char *lua_list = mame_machine_manager::instance()->lua()->call_plugin(driver->name, "data_list");
std::string lua_list;
mame_machine_manager::instance()->lua()->call_plugin("data_list", driver->name, lua_list);
m_items_list.clear();
if(lua_list)
if(lua_list.length())
{
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
char *token = strtok((char *)lua_list.c_str(), ",");
while(token)
{
ui_globals::curdats_total++;
@ -1972,7 +1975,10 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
if (m_info_view == 0)
general_info(driver, m_info_buffer);
else
m_info_buffer = mame_machine_manager::instance()->lua()->call_plugin(util::string_format("%d", m_info_view - 1).c_str(), "data");
{
m_info_buffer = "";
mame_machine_manager::instance()->lua()->call_plugin("data", util::string_format("%d", m_info_view - 1).c_str(), m_info_buffer);
}
}
total = ui_globals::curdats_total;
}

View File

@ -247,9 +247,9 @@ void menu_select_software::handle()
// handle UI_DATS
ui_software_info *ui_swinfo = (ui_software_info *)menu_event->itemref;
if (ui_swinfo->startempty == 1 && mame_machine_manager::instance()->lua()->call_plugin(ui_swinfo->driver->name, "data_list"))
if (ui_swinfo->startempty == 1 && mame_machine_manager::instance()->lua()->call_plugin_check<const char *>("data_list", ui_swinfo->driver->name))
menu::stack_push<menu_dats_view>(ui(), container(), ui_swinfo->driver);
else if (mame_machine_manager::instance()->lua()->call_plugin(std::string(ui_swinfo->shortname).append(1, ',').append(ui_swinfo->listname).c_str(), "data_list") || !ui_swinfo->usage.empty())
else if (mame_machine_manager::instance()->lua()->call_plugin_check<const char *>("data_list", std::string(ui_swinfo->shortname).append(1, ',').append(ui_swinfo->listname).c_str()) || !ui_swinfo->usage.empty())
menu::stack_push<menu_dats_view>(ui(), container(), ui_swinfo);
}
else if (menu_event->iptkey == IPT_UI_LEFT_PANEL)