From bacced3c81c010fb096f1df5cace1452a8132585 Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Mon, 15 Feb 2016 23:51:18 -0600 Subject: [PATCH] lua api: cleanup options handling and fix cheat state return value (nw) --- docs/luaengine.md | 25 +++++++++--- src/emu/luaengine.cpp | 88 +++++++++++++++++-------------------------- src/emu/luaengine.h | 3 +- 3 files changed, 55 insertions(+), 61 deletions(-) diff --git a/docs/luaengine.md b/docs/luaengine.md index ff79f46c21c..223a421cc27 100644 --- a/docs/luaengine.md +++ b/docs/luaengine.md @@ -156,10 +156,12 @@ program 41 ``` -manager:machine().options[] +manager:options() +manager:machine():options() +manager:machine():ui():options() ``` -> opts = manager:machine().options -> for k, entry in pairs(opts) do print(string.format("%10s: %s\n%11s %s", k, entry:value(), "", entry:description())) end +> opts = manager:machine():options() +> for k, entry in pairs(opts.entries) do print(string.format("%10s: %s\n%11s %s", k, entry:value(), "", entry:description())) end diff_directory: diff directory to save hard drive image differeVnce files joystick_contradictory: false @@ -169,9 +171,20 @@ joystick_contradictory: false oslog: false output error.log data to the system debugger [...] -> print(opts["sleep"]:value()) +> print(opts.entries["sleep"]:value()) true -> print(opts["sleep"]:value("invalid")) -Illegal boolean value for sleep: "invalid"; reverting to 0 +> print(opts.entries["sleep"]:value("invalid")) +Illegal boolean value for sleep: "invalid"; reverting to 1 +true +> print(opts.entries["sleep"]:value(false)) false ``` + +individual screen snapshots +``` +> local screen = manager:machine().screens[":screen"] +> screen:snapshot() +saved snap/gridlee/0000.png +> screen:snapshot('%g.png') +saved snap/gridlee.png +``` diff --git a/src/emu/luaengine.cpp b/src/emu/luaengine.cpp index bf89139f310..990f417eac4 100644 --- a/src/emu/luaengine.cpp +++ b/src/emu/luaengine.cpp @@ -375,31 +375,35 @@ void lua_engine::emu_set_hook(lua_State *L) } //------------------------------------------------- -// machine_options - return table of options -// -> manager:machine().options[] +// options_entry - return table of option entries +// -> manager:options().entries +// -> manager:machine():options().entries +// -> manager:machine():ui():options().entries //------------------------------------------------- -luabridge::LuaRef lua_engine::l_machine_get_options(const running_machine *r) +template +luabridge::LuaRef lua_engine::l_options_get_entries(const T *o) { + T *options = const_cast(o); lua_State *L = luaThis->m_lua_state; - luabridge::LuaRef options_table = luabridge::LuaRef::newTable(L); + luabridge::LuaRef entries_table = luabridge::LuaRef::newTable(L); int unadorned_index = 0; - for (core_options::entry *curentry = r->options().first(); curentry != nullptr; curentry = curentry->next()) + for (typename T::entry *curentry = options->first(); curentry != nullptr; curentry = curentry->next()) { const char *name = curentry->name(); bool is_unadorned = false; // check if it's unadorned - if (name && strlen(name) && !strcmp(name, core_options::unadorned(unadorned_index))) + if (name && strlen(name) && !strcmp(name, options->unadorned(unadorned_index))) { unadorned_index++; is_unadorned = true; } if (!curentry->is_header() && !curentry->is_command() && !curentry->is_internal() && !is_unadorned) - options_table[name] = curentry; + entries_table[name] = curentry; } - return options_table; + return entries_table; } //------------------------------------------------- @@ -469,16 +473,11 @@ int lua_engine::lua_cheat_entry::l_get_state(lua_State *L) switch (ce->state()) { - case SCRIPT_STATE_ON: - lua_pushliteral(L, "on"); - case SCRIPT_STATE_RUN: - lua_pushliteral(L, "run"); - case SCRIPT_STATE_CHANGE: - lua_pushliteral(L, "change"); - case SCRIPT_STATE_COUNT: - lua_pushliteral(L, "count"); - default: - lua_pushliteral(L, "off"); + case SCRIPT_STATE_ON: lua_pushliteral(L, "on"); break; + case SCRIPT_STATE_RUN: lua_pushliteral(L, "run"); break; + case SCRIPT_STATE_CHANGE: lua_pushliteral(L, "change"); break; + case SCRIPT_STATE_COUNT: lua_pushliteral(L, "count"); break; + default: lua_pushliteral(L, "off"); break; } return 1; @@ -722,35 +721,6 @@ int lua_engine::lua_addr_space::l_mem_write(lua_State *L) return 0; } -//------------------------------------------------- -// ui_options - return table of options -// -> manager:machine():ui().options[] -//------------------------------------------------- - -luabridge::LuaRef lua_engine::l_ui_get_options(const ui_manager *u) -{ - ui_manager *ui = const_cast(u); - lua_State *L = luaThis->m_lua_state; - luabridge::LuaRef options_table = luabridge::LuaRef::newTable(L); - - int unadorned_index = 0; - for (core_options::entry *curentry = ui->options().first(); curentry != nullptr; curentry = curentry->next()) - { - const char *name = curentry->name(); - bool is_unadorned = false; - // check if it's unadorned - if (name && strlen(name) && !strcmp(name, core_options::unadorned(unadorned_index))) - { - unadorned_index++; - is_unadorned = true; - } - if (!curentry->is_header() && !curentry->is_command() && !curentry->is_internal() && !is_unadorned) - options_table[name] = curentry; - } - - return options_table; -} - int lua_engine::lua_options_entry::l_entry_value(lua_State *L) { core_options::entry *e = luabridge::Stack::get(L, 1); @@ -763,13 +733,15 @@ int lua_engine::lua_options_entry::l_entry_value(lua_State *L) if (!lua_isnone(L, 2)) { std::string error; + // FIXME: not working with ui_options::entry + // TODO: optional arg for priority luaThis->machine().options().set_value(e->name(), lua_isboolean(L, 2) ? (lua_toboolean(L, 2) ? "1" : "0") : lua_tostring(L, 2), OPTION_PRIORITY_CMDLINE, error); if (!error.empty()) { - lua_writestringerror("%s", error.c_str()); + luaL_error(L, "%s", error.c_str()); } } @@ -833,7 +805,7 @@ int lua_engine::lua_video::l_end_recording(lua_State *L) } if (!vm->is_recording()) { - lua_writestringerror("%s", "Error, no active recording to stop"); + lua_writestringerror("%s", "No active recording to stop"); return 0; } @@ -907,7 +879,7 @@ int lua_engine::lua_screen::l_snapshot(lua_State *L) emu_file file(sc->machine().options().snapshot_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); file_error filerr; - if (!lua_isnone(L, 5)) { + if (!lua_isnone(L, 2)) { const char *filename = lua_tostring(L, 2); std::string snapstr(filename); strreplace(snapstr, "/", PATH_SEPARATOR); @@ -921,11 +893,12 @@ int lua_engine::lua_screen::l_snapshot(lua_State *L) if (filerr != FILERR_NONE) { - lua_writestringerror("Error creating snapshot, file_error=%d", filerr); + luaL_error(L, "file_error=%d", filerr); return 0; } sc->machine().video().save_snapshot(sc, file); + lua_writestringerror("saved %s", file.fullpath()); file.close(); return 1; } @@ -1421,9 +1394,9 @@ void lua_engine::initialize() .addFunction ("ioport", &running_machine::ioport) .addFunction ("parameters", &running_machine::parameters) .addFunction ("cheat", &running_machine::cheat) + .addFunction ("options", &running_machine::options) .addProperty ("devices", &lua_engine::l_machine_get_devices) .addProperty ("screens", &lua_engine::l_machine_get_screens) - .addProperty ("options", &lua_engine::l_machine_get_options) .endClass () .beginClass ("game_driver") .addData ("source_file", &game_driver::source_file) @@ -1512,6 +1485,11 @@ void lua_engine::initialize() .addProperty ("crosshair_scale", &ioport_field::crosshair_scale, &ioport_field::set_crosshair_scale) .addProperty ("crosshair_offset", &ioport_field::crosshair_offset, &ioport_field::set_crosshair_offset) .endClass() + .beginClass ("core_options") + .addFunction ("help", &core_options::output_help) + .addFunction ("command", &core_options::command) + .addProperty ("entries", &lua_engine::l_options_get_entries) + .endClass() .beginClass ("lua_options_entry") .addCFunction ("value", &lua_options_entry::l_entry_value) .endClass() @@ -1522,6 +1500,10 @@ void lua_engine::initialize() .addFunction ("maximum", &core_options::entry::maximum) .addFunction ("has_range", &core_options::entry::has_range) .endClass() + .deriveClass ("emu_options") + .endClass() + .deriveClass ("ui_options") + .endClass() .beginClass ("parameters") .addFunction ("add", ¶meters_manager::add) .addFunction ("lookup", ¶meters_manager::lookup) @@ -1594,10 +1576,10 @@ void lua_engine::initialize() .endClass() .beginClass ("ui") .addFunction ("is_menu_active", &ui_manager::is_menu_active) + .addFunction ("options", &ui_manager::options) .addProperty ("show_fps", &ui_manager::show_fps, &ui_manager::set_show_fps) .addProperty ("show_profiler", &ui_manager::show_profiler, &ui_manager::set_show_profiler) .addProperty ("single_step", &ui_manager::single_step, &ui_manager::set_single_step) - .addProperty ("options", &lua_engine::l_ui_get_options) .endClass() .beginClass ("lua_screen_dev") .addCFunction ("draw_box", &lua_screen::l_draw_box) diff --git a/src/emu/luaengine.h b/src/emu/luaengine.h index 94b90981819..fca3a564618 100644 --- a/src/emu/luaengine.h +++ b/src/emu/luaengine.h @@ -120,7 +120,6 @@ private: static int register_function(lua_State *L, const char *id); // "emu.machine" namespace - static luabridge::LuaRef l_machine_get_options(const running_machine *r); static luabridge::LuaRef l_machine_get_devices(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); @@ -156,7 +155,7 @@ private: int l_get_state(lua_State *L); }; - static luabridge::LuaRef l_ui_get_options(const ui_manager *ui); + template static luabridge::LuaRef l_options_get_entries(const T *o); struct lua_options_entry { int l_entry_value(lua_State *L); };