Merge pull request #1626 from cracyc/luaengine_sol2

Luaengine sol2
This commit is contained in:
Miodrag Milanović 2016-11-03 10:59:46 +01:00 committed by GitHub
commit 3ee5564b78
15 changed files with 1718 additions and 2796 deletions

View File

@ -52,7 +52,7 @@ function cheatfind.startplugin()
-- save data block
function cheat.save(space, start, size)
local data = { block = "", start = start, size = size, space = space }
if space.shortname then
if getmetatable(space).__name:match("device_t") then
if space:shortname() == "ram" then
data.block = emu.item(space.items["0/m_pointer"]):read_block(start, size)
if not data.block then

View File

@ -654,13 +654,15 @@ function dat.check(set, softlist)
local newbytes = {}
if skip == "odd" then
for i = 1, #bytes, 2 do
val = bytes[i]:byte(1)
newbytes[(i+1)/2] = string.char(((val & 0x0f) << 4) | (val & 0x0f))
val1 = bytes[i]:byte(1)
val2 = bytes[i+1]:byte(1)
newbytes[(i+1)/2] = string.char(((val1 & 0x0f) << 4) | (val2 & 0x0f))
end
elseif skip == "even" then
for i = 1, #bytes, 2 do
val = bytes[i]:byte(1)
newbytes[(i+1)/2] = string.char((val & 0xf0) | ((val & 0xf0) >> 4))
val1 = bytes[i]:byte(1)
val2 = bytes[i+1]:byte(1)
newbytes[(i+1)/2] = string.char((val1 & 0xf0) | ((val2 & 0xf0) >> 4))
end
end
return newbytes

View File

@ -7,11 +7,16 @@ function dat.check(set, softlist)
if softlist or not datread then
return nil
end
local status
local status, drvinfo
status, info = pcall(datread, "mame", "info", set)
if not status or not info then
return nil
end
local sourcefile = emu.driver_find(set).source_file:match("[^/\\]*$")
status, drvinfo = pcall(datread, "drv", "info", sourcefile)
if drvinfo then
info = info .. "\n\n--- DRIVER INFO ---\nDriver: " .. sourcefile .. "\n\n" .. drvinfo
end
info = info:gsub("\n\n", "\n")
return "Mameinfo"
end

View File

@ -8,11 +8,16 @@ function dat.check(set, softlist)
if softlist or not datread then
return nil
end
local status
local status, drvinfo
status, info = pcall(datread, "mame", "info", set)
if not status or not info then
return nil
end
local sourcefile = emu.driver_find(set).source_file:match("[^/\\]*$")
status, drvinfo = pcall(datread, "drv", "info", sourcefile)
if drvinfo then
info = info .. "\n\n--- DRIVER INFO ---\nDriver: " .. sourcefile .. "\n\n" .. drvinfo
end
info = info:gsub("\n\n", "\n")
return "Messinfo"
end

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,7 @@
#define __LUA_ENGINE_H__
#include <map>
#include "sol2/sol.hpp"
// None is typedef'd already in SDL/X11 libs
#ifdef None
@ -47,50 +48,79 @@ public:
void serve_lua();
void periodic_check();
bool frame_hook();
void execute_function(const char *id);
struct menu_item {
std::string text;
std::string subtext;
std::string flags;
};
void menu_populate(std::string &menu, std::vector<menu_item> &menu_list);
bool menu_callback(std::string &menu, int index, std::string event);
void menu_populate(const std::string &menu, std::vector<std::tuple<std::string, std::string, std::string>> &menu_list);
bool menu_callback(const std::string &menu, int index, const std::string &event);
void resume(lua_State *L, int nparam = 0, lua_State *root = nullptr);
void set_machine(running_machine *machine) { m_machine = machine; update_machine(); }
void set_machine(running_machine *machine) { m_machine = machine; }
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, const 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(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 can 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 table = false)
{
bool ret = false;
sol::object outobj = call_plugin(name, sol::make_object(sol(), in));
if(outobj.is<T>() && !table)
ret = true;
else if(outobj.is<sol::table>() && 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;
}
template<typename T>
void call_plugin_set(const std::string &name, const T in)
{
call_plugin(name, sol::make_object(sol(), in));
}
private:
struct hook {
lua_State *L;
int cb;
hook();
void set(lua_State *L, int idx);
lua_State *precall();
void call(lua_engine *engine, lua_State *T, int nparam);
bool active() const { return L != nullptr; }
};
static const char *const tname_ioport;
// internal state
lua_State *m_lua_state;
running_machine * m_machine;
lua_State *m_lua_state;
sol::state_view *m_sol_state;
sol::state_view &sol() const { return *m_sol_state; }
running_machine *m_machine;
std::vector<std::string> m_menu;
hook hook_output_cb;
bool output_notifier_set;
hook hook_frame_cb;
static lua_engine* luaThis;
std::map<lua_State *, std::pair<lua_State *, int> > thread_registry;
running_machine &machine() const { return *m_machine; }
@ -104,154 +134,42 @@ private:
void on_machine_resume();
void on_machine_frame();
void output_notifier(const char *outname, int32_t value);
static void s_output_notifier(const char *outname, int32_t value, void *param);
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);
void emu_after_done(void *_h, int32_t param);
int emu_after(lua_State *L);
int emu_wait(lua_State *L);
void emu_hook_output(lua_State *L);
void emu_set_hook(lua_State *L);
static int l_ioport_write(lua_State *L);
static int l_emu_after(lua_State *L);
static int l_emu_app_name(lua_State *L);
static int l_emu_app_version(lua_State *L);
static int l_emu_wait(lua_State *L);
static int l_emu_time(lua_State *L);
static int l_emu_gamename(lua_State *L);
static int l_emu_romname(lua_State *L);
static int l_emu_softname(lua_State *L);
static int l_emu_keypost(lua_State *L);
static int l_emu_hook_output(lua_State *L);
static int l_emu_exit(lua_State *L);
static int l_emu_start(lua_State *L);
static int l_emu_pause(lua_State *L);
static int l_emu_unpause(lua_State *L);
static int l_emu_set_hook(lua_State *L);
static int l_emu_register_prestart(lua_State *L);
static int l_emu_register_start(lua_State *L);
static int l_emu_register_stop(lua_State *L);
static int l_emu_register_pause(lua_State *L);
static int l_emu_register_resume(lua_State *L);
static int l_emu_register_frame(lua_State *L);
static int l_emu_register_frame_done(lua_State *L);
static int l_emu_register_menu(lua_State *L);
static int l_emu_register_callback(lua_State *L);
static std::string get_print_buffer(lua_State *L);
static int l_osd_printf_verbose(lua_State *L);
static int l_osd_printf_error(lua_State *L);
static int l_osd_printf_info(lua_State *L);
static int l_osd_printf_debug(lua_State *L);
static int l_driver_find(lua_State *L);
static int register_function(lua_State *L, const char *id);
// "emu.machine" namespace
static luabridge::LuaRef l_machine_get_devices(const running_machine *r);
static luabridge::LuaRef l_machine_get_images(const running_machine *r);
static luabridge::LuaRef l_ioport_get_ports(const ioport_manager *i);
static luabridge::LuaRef l_render_get_targets(const render_manager *r);
static luabridge::LuaRef l_ioports_port_get_fields(const ioport_port *i);
static luabridge::LuaRef devtree_dfs(device_t *root, luabridge::LuaRef dev_table);
static luabridge::LuaRef l_dev_get_states(const device_t *d);
static uint64_t l_state_get_value(const device_state_entry *d);
static void l_state_set_value(device_state_entry *d, uint64_t v);
static luabridge::LuaRef l_dev_get_memspaces(const device_t *d);
struct lua_machine {
int l_popmessage(lua_State *L);
int l_logerror(lua_State *L);
};
struct lua_addr_space {
lua_addr_space(address_space *space, device_memory_interface *dev) :
space(*space), dev(dev) {}
template<typename T> int l_mem_read(lua_State *L);
template<typename T> int l_mem_write(lua_State *L);
template<typename T> int l_log_mem_read(lua_State *L);
template<typename T> int l_log_mem_write(lua_State *L);
template<typename T> int l_direct_mem_read(lua_State *L);
template<typename T> int l_direct_mem_write(lua_State *L);
struct addr_space {
addr_space(address_space &space, device_memory_interface &dev) :
space(space), dev(dev) {}
template<typename T> T mem_read(offs_t address, sol::object shift);
template<typename T> void mem_write(offs_t address, T val, sol::object shift);
template<typename T> T log_mem_read(offs_t address);
template<typename T> void log_mem_write(offs_t address, T val);
template<typename T> T direct_mem_read(offs_t address);
template<typename T> void direct_mem_write(offs_t address, T val);
const char *name() const { return space.name(); }
address_space &space;
device_memory_interface *dev;
};
static luabridge::LuaRef l_addr_space_map(const lua_addr_space *sp);
static luabridge::LuaRef l_machine_get_screens(const running_machine *r);
struct lua_screen {
int l_height(lua_State *L);
int l_width(lua_State *L);
int l_orientation(lua_State *L);
int l_refresh(lua_State *L);
int l_type(lua_State *L);
int l_snapshot(lua_State *L);
int l_draw_box(lua_State *L);
int l_draw_line(lua_State *L);
int l_draw_text(lua_State *L);
};
static luabridge::LuaRef l_dev_get_items(const device_t *d);
struct lua_video {
int l_begin_recording(lua_State *L);
int l_end_recording(lua_State *L);
device_memory_interface &dev;
};
static luabridge::LuaRef l_cheat_get_entries(const cheat_manager *c);
struct lua_cheat_entry {
int l_get_state(lua_State *L);
template<typename T> static T share_read(memory_share &share, offs_t address);
template<typename T> static void share_write(memory_share &share, offs_t address, T val);
template<typename T> static T region_read(memory_region &region, offs_t address);
template<typename T> static void region_write(memory_region &region, offs_t address, T val);
struct save_item {
void *base;
unsigned int size;
unsigned int count;
};
template<typename T> static luabridge::LuaRef l_options_get_entries(const T *o);
struct lua_options_entry {
int l_entry_value(lua_State *L);
};
static luabridge::LuaRef l_memory_get_banks(const memory_manager *m);
static luabridge::LuaRef l_memory_get_shares(const memory_manager *m);
struct lua_memory_share {
template<typename T> int l_share_read(lua_State *L);
template<typename T> int l_share_write(lua_State *L);
};
static luabridge::LuaRef l_memory_get_regions(const memory_manager *m);
struct lua_memory_region {
template<typename T> int l_region_read(lua_State *L);
template<typename T> int l_region_write(lua_State *L);
};
struct lua_ui_input {
int l_ui_input_find_mouse(lua_State *L);
};
struct lua_render_target {
int l_render_view_bounds(lua_State *L);
};
struct lua_emu_file {
int l_emu_file_read(lua_State *L);
};
struct lua_item {
lua_item(int index);
void *l_item_base;
unsigned int l_item_size;
unsigned int l_item_count;
int l_item_read(lua_State *L);
int l_item_read_block(lua_State *L);
int l_item_write(lua_State *L);
};
void resume(void *L, int32_t param);
void start();
static int luaopen_ioport(lua_State *L);
void close();
static void *checkparam(lua_State *L, int idx, const char *tname);
static void *getparam(lua_State *L, int idx, const char *tname);
static void push(lua_State *L, void *p, const char *tname);
int report(int status);
int docall(int narg, int nres);
int incomplete(int status) ;
int docall(int narg, int nres);
void run(sol::load_result res);
};
#endif /* __LUA_ENGINE_H__ */

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,17 +45,16 @@ 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::vector<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)
for(std::string &item : lua_list)
{
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 version;
mame_machine_manager::instance()->lua()->call_plugin("data_version", count, version);
m_items_list.emplace_back(_(item.c_str()), count, std::move(version));
count++;
token = strtok(nullptr, ",");
}
}
}
@ -78,17 +77,16 @@ 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::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))
{
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
int count = 1;
while(token)
for(std::string &item : lua_list)
{
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 version;
mame_machine_manager::instance()->lua()->call_plugin("data_version", count - 1, version);
m_items_list.emplace_back(_(item.c_str()), count, std::move(version));
count++;
token = strtok(nullptr, ",");
}
}
}
@ -390,13 +388,16 @@ 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", 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);
for (int x = 0; x < lines; ++x)
{
std::string tempbuf(buffer.substr(xstart[x], xend[x] - xstart[x]));
if((tempbuf[0] == '#') && !x)
continue;
item_append(tempbuf, "", (FLAG_UI_DATS | FLAG_DISABLE), (void *)(uintptr_t)(x + 1));
}
}
@ -409,7 +410,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", 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);
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", "", true))
item_append(_("External DAT View"), "", 0, (void *)EXTERNAL_DATS);
item_append(menu_item_type::SEPARATOR);

View File

@ -91,28 +91,32 @@ void menu_plugin_opt::handle()
void menu_plugin_opt::populate()
{
std::vector<lua_engine::menu_item> menu_list;
std::vector<std::tuple<std::string, std::string, std::string>> menu_list;
mame_machine_manager::instance()->lua()->menu_populate(m_menu, menu_list);
uintptr_t i = 1;
for(auto &item : menu_list)
{
const std::string &text = std::get<0>(item);
const std::string &subtext = std::get<1>(item);
const std::string &tflags = std::get<2>(item);
uint32_t flags = 0;
if(item.flags == "off")
if(tflags == "off")
flags = FLAG_DISABLE;
else if(item.flags == "l")
else if(tflags == "l")
flags = FLAG_LEFT_ARROW;
else if(item.flags == "r")
else if(tflags == "r")
flags = FLAG_RIGHT_ARROW;
else if(item.flags == "lr")
else if(tflags == "lr")
flags = FLAG_RIGHT_ARROW | FLAG_LEFT_ARROW;
if(item.text == "---")
if(text == "---")
{
item_append(menu_item_type::SEPARATOR);
i++;
}
else
item_append(item.text, item.subtext, flags, (void *)i++);
item_append(text, subtext, flags, (void *)i++);
}
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, true))
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, true))
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

@ -1911,27 +1911,19 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
m_info_view = 0;
m_info_software = software;
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");
m_items_list.clear();
if(lua_list)
{
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
while(token)
{
ui_globals::cur_sw_dats_total++;
m_items_list.emplace_back(token);
token = strtok(nullptr, ",");
}
}
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;
}
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", m_info_view - 1, m_info_buffer);
}
}
total = ui_globals::cur_sw_dats_total;
}
@ -1952,27 +1944,19 @@ void menu_select_launch::infos_render(float origx1, float origy1, float origx2,
m_info_driver = driver;
m_info_view = 0;
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");
m_items_list.clear();
if(lua_list)
{
std::string list(lua_list);
char *token = strtok((char *)list.c_str(), ",");
while(token)
{
ui_globals::curdats_total++;
m_items_list.emplace_back(token);
token = strtok(nullptr, ",");
}
}
mame_machine_manager::instance()->lua()->call_plugin("data_list", driver->name, m_items_list);
ui_globals::curdats_total = m_items_list.size() + 1;
}
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", m_info_view - 1, 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, true))
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)

View File

@ -407,10 +407,7 @@ void mame_ui_manager::update_and_render(render_container &container)
// render any cheat stuff at the bottom
if (machine().phase() >= MACHINE_PHASE_RESET)
{
mame_machine_manager::instance()->lua()->on_frame_done();
mame_machine_manager::instance()->cheat().render_text(*this, container);
}
// call the current UI handler
m_handler_param = m_handler_callback(container);